This repository has been archived by the owner on Sep 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.go
192 lines (178 loc) · 4.41 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"os"
"os/exec"
"sync"
"time"
"golang.org/x/sync/errgroup"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/object"
)
type stats struct {
Total int
Last30d int
Last7d int
Last24h int
Lines int
Time time.Time
}
func (s stats) FormattedTime() string {
return s.Time.In(time.UTC).Format(time.RFC850)
}
type tokeiLanguageReport struct {
Blanks int `json:"blanks"`
Code int `json:"code"`
Comments int `json:"comments"`
Stats []struct {
Blanks int `json:"blanks"`
Code int `json:"code"`
Comments int `json:"comments"`
Lines int `json:"lines"`
Name string `json:"name"`
} `json:"stats"`
Lines int `json:"lines"`
}
type TokeiReport struct {
CSS tokeiLanguageReport `json:"Css"`
Dockerfile tokeiLanguageReport `json:"Dockerfile"`
Go tokeiLanguageReport `json:"Go"`
Hex tokeiLanguageReport `json:"Hex"`
HTML tokeiLanguageReport `json:"Html"`
Makefile tokeiLanguageReport `json:"Makefile"`
Markdown tokeiLanguageReport `json:"Markdown"`
Sh tokeiLanguageReport `json:"Sh"`
Text tokeiLanguageReport `json:"Text"`
Toml tokeiLanguageReport `json:"Toml"`
Yaml tokeiLanguageReport `json:"Yaml"`
}
func getTokeiReport(path string) (*TokeiReport, error) {
cmd := exec.Command("tokei", "-e", "vendor/", path, "-o", "json")
buf := new(bytes.Buffer)
cmd.Stdout = buf
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return nil, err
}
decoder := json.NewDecoder(buf)
r := new(TokeiReport)
if err := decoder.Decode(r); err != nil {
return nil, err
}
return r, nil
}
func getStats(fetch bool) (*stats, error) {
const isBare = false
var (
n = time.Now()
last30Days = n.AddDate(0, 0, -30)
last7Days = n.AddDate(0, 0, -7)
last24h = n.AddDate(0, 0, -1)
total, last30DaysTotal, last7DaysTotal, last24hTotal, lines int
mux sync.Mutex
)
g, ctx := errgroup.WithContext(context.Background())
for _, n := range []string{
"stun", "turn", "sdp", "web", "stund", "tech-status", "ice", "rtc", "gortcd",
"ansible-role-nginx", "ansible-go", "api", "docs", "dtls", "neo", "turnc",
} {
name := n
g.Go(func() error {
p := "/tmp/gortc-analyze/" + name
r, err := git.PlainCloneContext(ctx, p, isBare, &git.CloneOptions{
URL: "https://github.com/gortc/" + name,
})
if err == git.ErrRepositoryAlreadyExists {
r, err = git.PlainOpen(p)
if err != nil {
return err
}
w, err := r.Worktree()
if err != nil {
return err
}
if fetch {
err = w.Pull(&git.PullOptions{
Force: true,
RemoteName: "origin",
})
log.Println("pull", name, err)
if err == git.NoErrAlreadyUpToDate {
err = nil
}
}
}
if err != nil {
return err
}
rep, err := getTokeiReport(p)
if err != nil {
return err
}
if name == "dtls" {
// It's not fair to count vendored code.
// TODO: count delta.
rep.Go.Lines = 0
}
mux.Lock()
lines += rep.Go.Lines + rep.Yaml.Lines + rep.Dockerfile.Lines
mux.Unlock()
ref, err := r.Head()
if err != nil {
return err
}
fmt.Println(name, "head", ref)
b, err := r.Log(&git.LogOptions{
From: ref.Hash(),
})
var commits int
countAuthors := map[string]struct{}{
"[email protected]": {},
"[email protected]": {},
"[email protected]": {},
"[email protected]": {},
"[email protected]": {},
"[email protected]": {},
}
if err = b.ForEach(func(commit *object.Commit) error {
if _, ok := countAuthors[commit.Author.Email]; !ok {
return nil
}
mux.Lock()
commits++
if commit.Author.When.After(last30Days) {
last30DaysTotal++
}
if commit.Author.When.After(last7Days) {
last7DaysTotal++
}
if commit.Author.When.After(last24h) {
last24hTotal++
}
mux.Unlock()
return nil
}); err != nil {
return err
}
mux.Lock()
total += commits
mux.Unlock()
return nil
})
}
if err := g.Wait(); err != nil {
log.Println("failed to fetch stats:", err)
}
return &stats{
Time: time.Now(),
Total: total,
Last30d: last30DaysTotal,
Last7d: last7DaysTotal,
Last24h: last24hTotal,
Lines: lines,
}, nil
}