1
0
Fork 0
empact/db/stat.go

217 lines
4.6 KiB
Go
Raw 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
}
)
2015-03-08 11:17:56 +00:00
const orgTopQuery = `
2015-03-06 16:29:26 +00:00
select
2015-03-08 11:17:56 +00:00
%s as item,
sum(c.commits) as commits,
sum(c.additions) - sum(c.deletions) as delta
2015-03-06 16:29:26 +00:00
from contribs c
join members m on
c.author = m.user and
c.owner = m.org
2015-03-08 11:17:56 +00:00
join teams t on
m.team_id = t.id
2015-03-06 16:29:26 +00:00
where
m.id is not null and
2015-03-08 11:17:56 +00:00
c.owner = :org and
c.week >= :from and
c.week <= :to
2015-03-06 16:29:26 +00:00
group by item
2015-03-08 15:29:25 +00:00
order by commits desc`
2015-03-06 16:29:26 +00:00
2015-03-08 11:17:56 +00:00
const orgActivityQuery = `
2015-03-06 16:29:26 +00:00
select
2015-03-08 11:17:56 +00:00
%s as item,
sum(c.commits) as commits,
sum(c.additions) - sum(c.deletions) as delta,
c.week as week
2015-03-06 16:29:26 +00:00
from contribs c
join members m on
c.author = m.user and
c.owner = m.org
join teams t on
m.team_id = t.id
where
m.id is not null and
2015-03-08 11:17:56 +00:00
c.owner = :org and
c.week >= :from and
c.week <= :to
group by item, week
2015-03-11 12:10:11 +00:00
order by week, commits desc`
2015-03-06 16:29:26 +00:00
2015-03-08 11:17:56 +00:00
const teamTopQuery = `
2015-03-06 16:29:26 +00:00
select
2015-03-08 11:17:56 +00:00
%s as item,
sum(c.commits) as commits,
sum(c.additions) - sum(c.deletions) as delta
2015-03-06 16:29:26 +00:00
from contribs c
join members m on
c.author = m.user and
c.owner = m.org
join teams t on
2015-03-08 11:17:56 +00:00
m.team_id = t.id and
t.name = :team
2015-03-06 16:29:26 +00:00
where
m.id is not null and
2015-03-08 11:17:56 +00:00
c.owner = :org and
c.week >= :from and
c.week <= :to
group by item
2015-03-08 14:35:25 +00:00
order by commits desc`
2015-03-06 16:29:26 +00:00
2015-03-08 11:17:56 +00:00
const teamActivityQuery = `
2015-03-06 16:29:26 +00:00
select
2015-03-08 11:17:56 +00:00
%s as item,
sum(c.commits) as commits,
sum(c.additions) - sum(c.deletions) as delta,
c.week as week
2015-03-06 16:29:26 +00:00
from contribs c
join members m on
c.author = m.user and
c.owner = m.org
2015-03-08 11:17:56 +00:00
join teams t on
m.team_id = t.id and
t.name = :team
2015-03-06 16:29:26 +00:00
where
m.id is not null and
2015-03-08 11:17:56 +00:00
c.owner = :org and
c.week >= :from and
c.week <= :to
group by item, week
2015-03-08 14:35:25 +00:00
order by week, commits desc`
const userTopQuery = `
select
c.repo as item,
sum(c.commits) as commits,
sum(c.additions) - sum(c.deletions) as delta
from contribs c
where
c.owner = :org and
2015-03-09 13:31:51 +00:00
c.author = :user and
2015-03-08 14:35:25 +00:00
c.week >= :from and
c.week <= :to
group by item
order by commits desc`
const userActivityQuery = `
select
c.week as week,
c.repo as item,
sum(c.commits) as commits,
sum(c.additions) - sum(c.deletions) as delta
from contribs c
where
c.owner = :org and
2015-03-09 13:31:51 +00:00
c.author = :user and
2015-03-08 14:35:25 +00:00
c.week >= :from and
c.week <= :to
group by item
order by week, commits desc`
const repoTopQuery = `
select
2015-03-09 15:26:38 +00:00
%s as item,
2015-03-08 14:35:25 +00:00
sum(c.commits) as commits,
sum(c.additions) - sum(c.deletions) as delta
from contribs c
join members m on
c.author = m.user and
c.owner = m.org
join teams t on
m.team_id = t.id
where
m.id is not null and
c.owner = :org and
c.repo = :repo and
c.week >= :from and
c.week <= :to
group by item
order by commits desc`
const repoActivityQuery = `
select
c.week as week,
2015-03-08 14:35:25 +00:00
%s as item,
sum(c.commits) as commits,
sum(c.additions) - sum(c.deletions) as delta
from contribs c
join members m on
c.author = m.user and
c.owner = m.org
join teams t on
m.team_id = t.id
where
m.id is not null and
c.owner = :org and
c.repo = :repo and
c.week >= :from and
c.week <= :to
group by week, item
order by commits desc`
2015-03-06 16:29:26 +00:00
2015-03-08 11:17:56 +00:00
func StatOrgTop(p map[string]interface{}) (res []StatItem) {
defer measure("StatOrgTop", time.Now())
2015-03-08 15:29:25 +00:00
mustSelectN(&res, fmt.Sprintf(orgTopQuery, p["item"]), p)
2015-03-06 16:29:26 +00:00
return
}
2015-03-08 11:17:56 +00:00
func StatOrgActivity(p map[string]interface{}) (res []StatPoint) {
defer measure("StatOrgActivity", time.Now())
2015-03-08 15:29:25 +00:00
mustSelectN(&res, fmt.Sprintf(orgActivityQuery, p["item"]), p)
2015-03-06 16:29:26 +00:00
return
}
2015-03-08 11:17:56 +00:00
func StatTeamTop(p map[string]interface{}) (res []StatItem) {
defer measure("StatTeamTop", time.Now())
2015-03-08 15:16:27 +00:00
mustSelectN(&res, fmt.Sprintf(teamTopQuery, p["item"]), p)
2015-03-06 16:29:26 +00:00
return
}
2015-03-08 11:17:56 +00:00
func StatTeamActivity(p map[string]interface{}) (res []StatPoint) {
defer measure("StatTeamActivity", time.Now())
2015-03-08 15:29:25 +00:00
mustSelectN(&res, fmt.Sprintf(teamActivityQuery, p["item"]), p)
2015-03-06 16:29:26 +00:00
return
}
2015-03-08 14:35:25 +00:00
2015-03-09 15:26:38 +00:00
func StatUserTop(p map[string]interface{}) (res []StatItem) {
2015-03-08 14:35:25 +00:00
defer measure("StatUserTop", time.Now())
mustSelectN(&res, userTopQuery, p)
return
}
2015-03-09 15:26:38 +00:00
func StatUserActivity(p map[string]interface{}) (res []StatPoint) {
2015-03-08 14:35:25 +00:00
defer measure("StatUserActivity", time.Now())
mustSelectN(&res, userActivityQuery, p)
return
}
2015-03-09 15:26:38 +00:00
func StatRepoTop(p map[string]interface{}) (res []StatItem) {
2015-03-08 14:35:25 +00:00
defer measure("StatRepoTop", time.Now())
2015-03-09 15:26:38 +00:00
mustSelectN(&res, fmt.Sprintf(repoTopQuery, p["item"]), p)
2015-03-08 14:35:25 +00:00
return
}
2015-03-09 15:26:38 +00:00
func StatRepoActivity(p map[string]interface{}) (res []StatPoint) {
2015-03-08 14:35:25 +00:00
defer measure("StatRepoActivity", time.Now())
mustSelectN(&res, fmt.Sprintf(repoActivityQuery, p["item"]), p)
2015-03-08 14:35:25 +00:00
return
}