1
0
Fork 0
empact/db/stat.go

255 lines
6.8 KiB
Go
Raw Permalink Normal View History

2015-03-06 16:29:26 +00:00
package db
import (
2015-03-08 11:17:56 +00:00
"fmt"
2015-03-06 16:29:26 +00:00
"time"
)
type (
StatItem struct {
2015-03-08 11:17:56 +00:00
Item string `json:"item"`
Commits int `json:"commits"`
Delta int `json:"delta"`
2015-03-06 16:29:26 +00:00
}
StatPoint struct {
StatItem
2015-03-08 11:17:56 +00:00
Week uint64 `json:"week"`
2015-03-06 16:29:26 +00:00
}
)
func StatOrgTop(login string, p map[string]interface{}) (res []StatItem) {
2015-03-21 14:58:40 +00:00
defer measure(time.Now(), "StatOrgTop")
2015-03-20 14:20:16 +00: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
2015-03-22 14:01:09 +00:00
join orgs o
on o.id = c.org_id
`+joinContribFT(p["item"])+`
2015-03-20 14:20:16 +00:00
where
o.login = :org and
c.repo_id in `+reposScope(login)+` and
2015-03-20 14:20:16 +00:00
c.week >= :from and
c.week <= :to
group by item
order by commits desc
`, p["item"]), p)
2015-03-06 16:29:26 +00:00
return
}
func StatOrgActivity(login string, p map[string]interface{}) (res []StatPoint) {
2015-03-21 14:58:40 +00:00
defer measure(time.Now(), "StatOrgActivity")
2015-03-20 14:20:16 +00: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
2015-03-22 14:01:09 +00:00
join orgs o on
o.id = c.org_id
`+joinContribFT(p["item"])+`
2015-03-20 14:20:16 +00:00
where
o.login = :org and
c.repo_id in `+reposScope(login)+` and
2015-03-20 14:20:16 +00:00
c.week >= :from and
c.week <= :to
group by item, week
order by week, commits desc
`, p["item"]), p)
2015-03-06 16:29:26 +00:00
return
}
func StatTeamTop(login string, p map[string]interface{}) (res []StatItem) {
2015-03-21 14:58:40 +00:00
defer measure(time.Now(), "StatTeamTop")
2015-03-20 14:20:16 +00: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
2015-03-22 14:01:09 +00:00
join orgs o on
o.id = c.org_id
join team_members tm on
c.user_id = tm.user_id and
c.org_id = tm.org_id
join teams t on
t.id = tm.team_id
`+joinContribFT(p["item"])+`
2015-03-20 14:20:16 +00:00
where
o.login = :org and
c.repo_id in `+reposScope(login)+` and
2015-03-20 14:20:16 +00:00
t.name = :team and
c.week >= :from and
c.week <= :to
group by item
order by commits desc
`, p["item"]), p)
2015-03-06 16:29:26 +00:00
return
}
func StatTeamActivity(login string, p map[string]interface{}) (res []StatPoint) {
2015-03-21 14:58:40 +00:00
defer measure(time.Now(), "StatTeamActivity")
2015-03-20 14:20:16 +00: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
2015-03-22 14:01:09 +00:00
join orgs o on
o.id = c.org_id
join team_members tm on
c.user_id = tm.user_id and
c.org_id = tm.org_id
join teams t on
tm.team_id = t.id
`+joinContribFT(p["item"])+`
2015-03-20 14:20:16 +00:00
where
o.login = :org and
c.repo_id in `+reposScope(login)+` and
2015-03-20 14:20:16 +00:00
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 16:29:26 +00:00
return
}
2015-03-08 14:35:25 +00:00
func StatUserTop(login string, p map[string]interface{}) (res []StatItem) {
2015-03-21 14:58:40 +00:00
defer measure(time.Now(), "StatUserTop")
2015-03-20 14:20:16 +00: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
2015-03-22 14:01:09 +00:00
o.id = c.org_id
2015-03-20 14:20:16 +00:00
join users u on
c.user_id = u.id
join repos r on
c.repo_id = r.id
where
o.login = :org and
c.repo_id in `+reposScope(login)+` and
2015-03-20 14:20:16 +00:00
u.login = :user and
c.week >= :from and
c.week <= :to
group by item
order by commits desc
`, p)
2015-03-08 14:35:25 +00:00
return
}
func StatUserActivity(login string, p map[string]interface{}) (res []StatPoint) {
2015-03-21 14:58:40 +00:00
defer measure(time.Now(), "StatUserActivity")
2015-03-20 14:20:16 +00: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
2015-03-22 14:01:09 +00:00
o.id = c.org_id
2015-03-20 14:20:16 +00:00
join users u on
c.user_id = u.id
join repos r on
c.repo_id = r.id
where
o.login = :org and
c.repo_id in `+reposScope(login)+` and
2015-03-20 14:20:16 +00:00
u.login = :user and
c.week >= :from and
c.week <= :to
group by item
order by week, commits desc
`, p)
2015-03-08 14:35:25 +00:00
return
}
func StatRepoTop(login string, p map[string]interface{}) (res []StatItem) {
2015-03-21 14:58:40 +00:00
defer measure(time.Now(), "StatRepoTop")
2015-03-20 14:20:16 +00: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
2015-03-22 14:01:09 +00:00
join orgs o on
o.id = c.org_id
join repos r on
c.repo_id = r.id
`+joinContribFT(p["item"])+`
2015-03-20 14:20:16 +00:00
where
o.login = :org and
c.repo_id in `+reposScope(login)+` and
2015-03-20 14:20:16 +00:00
r.name = :repo and
c.week >= :from and
c.week <= :to
group by item
order by commits desc
`, p["item"]), p)
2015-03-08 14:35:25 +00:00
return
}
func StatRepoActivity(login string, p map[string]interface{}) (res []StatPoint) {
2015-03-21 14:58:40 +00:00
defer measure(time.Now(), "StatRepoActivity")
2015-03-20 14:20:16 +00: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
2015-03-22 14:01:09 +00:00
join orgs o on
o.id = c.org_id
join repos r on
c.repo_id = r.id
`+joinContribFT(p["item"])+`
2015-03-20 14:20:16 +00:00
where
o.login = :org and
c.repo_id in `+reposScope(login)+` and
2015-03-20 14:20:16 +00:00
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 14:35:25 +00:00
return
}
2015-03-22 14:01:09 +00:00
func joinContribFT(item interface{}) string {
switch item {
case "r.name":
return "join repos r on c.repo_id = r.id"
case "u.login":
return "join users u on c.user_id = u.id"
case "t.name":
return `
join team_members tm on
tm.user_id = c.user_id and
tm.org_id = c.org_id
join teams t on
t.id = tm.team_id`
default:
panic("unreachable")
}
}
func reposScope(login string) string {
return fmt.Sprintf(`(
2015-03-22 14:01:09 +00:00
select
distinct(repo_id)
from team_repos tr
2015-03-22 14:01:09 +00:00
join team_members tm on
tm.team_id = tr.team_id
join users u on
u.login = %q
)`, login)
}