This repository has been archived by the owner on Apr 6, 2019. It is now read-only.
/
stats.go
128 lines (92 loc) · 2.56 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"fmt"
c "github.com/dlapiduz/govcode.org/common"
"time"
)
func generateStats() {
// Commit Count per Org
fmt.Println("Generating commit count per org")
rows, err := c.DB.Table("repo_stats").Select(`
week, organization_id, sum(commits) as commit_count`).Joins(`
inner join repositories
on repositories.id = repo_stats.repository_id
`).Group("week, repository_id, organization_id").Rows()
c.PanicOn(err)
for rows.Next() {
var week time.Time
var organization_id, commit_count int64
rows.Scan(&week, &organization_id, &commit_count)
stat := c.CommitOrgStats{
Week: week,
OrganizationId: organization_id,
}
c.DB.Where(stat).FirstOrInit(&stat)
stat.CommitCount = commit_count
c.DB.Save(&stat)
}
rows.Close()
// Update users commit count and org list
fmt.Println("Generating commit count per user")
rows, err = c.DB.Table("repo_stats").Select(`
user_id, array_agg(distinct o.login) as organization_list, sum(commits) as commit_count
`).Joins(`
inner join repositories r
on r.id = repo_stats.repository_id
inner join organizations o
on o.id = r.organization_id
`).Group("user_id").Rows()
c.PanicOn(err)
for rows.Next() {
var user c.User
var user_id, commit_count int64
var org_list string
rows.Scan(&user_id, &org_list, &commit_count)
if user_id == 0 {
continue
}
c.DB.Where("id = ?", user_id).First(&user)
if commit_count != user.CommitCount {
user.CommitCount = commit_count
user.OrgList = org_list
c.DB.Save(&user)
}
}
rows.Close()
// Update LastCommit
fmt.Println("Updating LastCommit")
rows, err = c.DB.Table("repo_stats").Select(`
repository_id, max(week) as week
`).Group("repository_id").Rows()
c.PanicOn(err)
for rows.Next() {
var repository_id int64
var week time.Time
rows.Scan(&repository_id, &week)
var repo c.Repository
c.DB.Where("id = ?", repository_id).First(&repo)
if repo.LastCommit.Time != week {
c.DB.Model(&repo).Update("last_commit", week)
}
}
rows.Close()
// Update LastPull
fmt.Println("Updating LastPull")
rows, err = c.DB.Table("pulls").Select(`
repository_id, max(gh_created_at) as latest_pull
`).Group("repository_id").Rows()
c.PanicOn(err)
for rows.Next() {
var repository_id int64
var latest_pull time.Time
rows.Scan(&repository_id, &latest_pull)
var repo c.Repository
c.DB.Where("id = ?", repository_id).First(&repo)
if repo.LastPull.Time != latest_pull {
repo.LastPull.Time = latest_pull
repo.LastPull.Valid = true
c.DB.Save(&repo)
}
}
rows.Close()
}