empact/db/stat.go

204 lines
5.0 KiB
Go
Raw Normal View History

2015-03-06 23:29:26 +07:00
package db
import (
2015-03-08 18:17:56 +07:00
"fmt"
2015-03-06 23:29:26 +07:00
"time"
)
type (
StatItem struct {
2015-03-08 18:17:56 +07:00
Item string `json:"item"`
Commits int `json:"commits"`
Delta int `json:"delta"`
2015-03-06 23:29:26 +07:00
}
StatPoint struct {
StatItem
2015-03-08 18:17:56 +07:00
Week uint64 `json:"week"`
2015-03-06 23:29:26 +07:00
}
)
2015-03-20 21:20:16 +07:00
const joinContribFT = `
join team_members tm on
c.user_id = tm.user_id and
c.org_id = tm.org_id
join orgs o on
c.org_id = o.id
2015-03-08 18:17:56 +07:00
join teams t on
2015-03-20 21:20:16 +07:00
tm.team_id = t.id
join users u on
c.user_id = u.id
join repos r on
c.repo_id = r.id`
2015-03-06 23:29:26 +07:00
2015-03-08 18:17:56 +07:00
func StatOrgTop(p map[string]interface{}) (res []StatItem) {
defer measure("StatOrgTop", time.Now())
2015-03-20 21:20:16 +07:00
mustSelectN(&res, fmt.Sprintf(`
select
%s as item,
sum(c.commits) as commits,
sum(c.additions) - sum(c.deletions) as delta
from contribs c
`+joinContribFT+`
where
o.login = :org and
c.week >= :from and
c.week <= :to
group by item
order by commits desc
`, p["item"]), p)
2015-03-06 23:29:26 +07:00
return
}
2015-03-08 18:17:56 +07:00
func StatOrgActivity(p map[string]interface{}) (res []StatPoint) {
defer measure("StatOrgActivity", time.Now())
2015-03-20 21:20:16 +07:00
mustSelectN(&res, fmt.Sprintf(`
select
%s as item,
sum(c.commits) as commits,
sum(c.additions) - sum(c.deletions) as delta,
c.week as week
from contribs c
`+joinContribFT+`
where
o.login = :org and
c.week >= :from and
c.week <= :to
group by item, week
order by week, commits desc
`, p["item"]), p)
2015-03-06 23:29:26 +07:00
return
}
2015-03-08 18:17:56 +07:00
func StatTeamTop(p map[string]interface{}) (res []StatItem) {
defer measure("StatTeamTop", time.Now())
2015-03-20 21:20:16 +07:00
mustSelectN(&res, fmt.Sprintf(`
select
%s as item,
sum(c.commits) as commits,
sum(c.additions) - sum(c.deletions) as delta
from contribs c
`+joinContribFT+`
where
o.login = :org and
t.name = :team and
c.week >= :from and
c.week <= :to
group by item
order by commits desc
`, p["item"]), p)
2015-03-06 23:29:26 +07:00
return
}
2015-03-08 18:17:56 +07:00
func StatTeamActivity(p map[string]interface{}) (res []StatPoint) {
defer measure("StatTeamActivity", time.Now())
2015-03-20 21:20:16 +07:00
mustSelectN(&res, fmt.Sprintf(`
select
%s as item,
sum(c.commits) as commits,
sum(c.additions) - sum(c.deletions) as delta,
c.week as week
from contribs c
`+joinContribFT+`
where
o.login = :org and
t.name = :team and
c.week >= :from and
c.week <= :to
group by item, week
order by week, commits desc
`, p["item"]), p)
2015-03-06 23:29:26 +07:00
return
}
2015-03-08 21:35:25 +07:00
2015-03-09 22:26:38 +07:00
func StatUserTop(p map[string]interface{}) (res []StatItem) {
2015-03-08 21:35:25 +07:00
defer measure("StatUserTop", time.Now())
2015-03-20 21:20:16 +07:00
mustSelectN(&res, `
select
r.name as item,
sum(c.commits) as commits,
sum(c.additions) - sum(c.deletions) as delta
from contribs c
join orgs o on
c.org_id = o.id
join users u on
c.user_id = u.id
join repos r on
c.repo_id = r.id
where
o.login = :org and
u.login = :user and
c.week >= :from and
c.week <= :to
group by item
order by commits desc
`, p)
2015-03-08 21:35:25 +07:00
return
}
2015-03-09 22:26:38 +07:00
func StatUserActivity(p map[string]interface{}) (res []StatPoint) {
2015-03-08 21:35:25 +07:00
defer measure("StatUserActivity", time.Now())
2015-03-20 21:20:16 +07:00
mustSelectN(&res, `
select
c.week as week,
r.name as item,
sum(c.commits) as commits,
sum(c.additions) - sum(c.deletions) as delta
from contribs c
join orgs o on
c.org_id = o.id
join users u on
c.user_id = u.id
join repos r on
c.repo_id = r.id
where
o.login = :org and
u.login = :user and
c.week >= :from and
c.week <= :to
group by item
order by week, commits desc
`, p)
2015-03-08 21:35:25 +07:00
return
}
2015-03-09 22:26:38 +07:00
func StatRepoTop(p map[string]interface{}) (res []StatItem) {
2015-03-08 21:35:25 +07:00
defer measure("StatRepoTop", time.Now())
2015-03-20 21:20:16 +07:00
mustSelectN(&res, fmt.Sprintf(`
select
%s as item,
sum(c.commits) as commits,
sum(c.additions) - sum(c.deletions) as delta
from contribs c
`+joinContribFT+`
where
o.login = :org and
r.name = :repo and
c.week >= :from and
c.week <= :to
group by item
order by commits desc
`, p["item"]), p)
2015-03-08 21:35:25 +07:00
return
}
2015-03-09 22:26:38 +07:00
func StatRepoActivity(p map[string]interface{}) (res []StatPoint) {
2015-03-08 21:35:25 +07:00
defer measure("StatRepoActivity", time.Now())
2015-03-20 21:20:16 +07:00
mustSelectN(&res, fmt.Sprintf(`
select
c.week as week,
%s as item,
sum(c.commits) as commits,
sum(c.additions) - sum(c.deletions) as delta
from contribs c
`+joinContribFT+`
where
o.login = :org and
r.name = :repo and
c.week >= :from and
c.week <= :to
group by week, item
order by commits desc
`, p["item"]), p)
2015-03-08 21:35:25 +07:00
return
}