forked from golang/build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.go
181 lines (161 loc) · 4.49 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
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"html/template"
"io"
"log"
"math"
"net/http"
"strings"
"time"
"golang.org/x/build/maintner"
)
// handleStats serves dev.golang.org/stats.
func (s *server) handleStats(t *template.Template, w http.ResponseWriter, r *http.Request) {
s.cMu.RLock()
dirty := s.data.stats.dirty
s.cMu.RUnlock()
if dirty {
s.updateStatsData()
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
var buf bytes.Buffer
s.cMu.RLock()
defer s.cMu.RUnlock()
data := struct {
DataJSON interface{}
}{
DataJSON: s.data.stats,
}
if err := t.Execute(&buf, data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if _, err := io.Copy(w, &buf); err != nil {
log.Printf("io.Copy(w, %+v) = %v", buf, err)
return
}
}
type statsData struct {
Charts []*chart
// dirty is set if this data needs to be updated due to a corpus change.
dirty bool
}
// A chart holds data used by the Google Charts JavaScript API to render
// an interactive visualization.
type chart struct {
Title string `json:"title"`
Columns []*chartColumn `json:"columns"`
Data [][]interface{} `json:"data"`
}
// A chartColumn is analogous to a Google Charts DataTable column.
type chartColumn struct {
// Type is the data type of the values of the column.
// Supported values are 'string', 'number', 'boolean',
// 'timeofday', 'date', and 'datetime'.
Type string `json:"type"`
// Label is an optional label for the column.
Label string `json:"label"`
}
func (s *server) updateStatsData() {
log.Println("Updating stats data ...")
s.cMu.Lock()
defer s.cMu.Unlock()
var (
windowStart = time.Now().Add(-1 * 365 * 24 * time.Hour)
intervals []*clInterval
)
s.corpus.Gerrit().ForeachProjectUnsorted(filterProjects(func(p *maintner.GerritProject) error {
p.ForeachCLUnsorted(withoutDeletedCLs(p, func(cl *maintner.GerritCL) error {
closed := cl.Status == "merged" || cl.Status == "abandoned"
// Discard CL if closed and last updated before windowStart.
if closed && cl.Meta.Commit.CommitTime.Before(windowStart) {
return nil
}
intervals = append(intervals, newIntervalFromCL(cl))
return nil
}))
return nil
}))
var chartData [][]interface{}
for t0, t1 := windowStart, windowStart.Add(24*time.Hour); t0.Before(time.Now()); t0, t1 = t0.Add(24*time.Hour), t1.Add(24*time.Hour) {
var (
open int
withIssues int
)
for _, i := range intervals {
if !i.intersects(t0, t1) {
continue
}
open++
if len(i.cl.GitHubIssueRefs) > 0 {
withIssues++
}
}
chartData = append(chartData, []interface{}{
t0, open, withIssues,
})
}
cols := []*chartColumn{
{Type: "date", Label: "date"},
{Type: "number", Label: "All CLs"},
{Type: "number", Label: "With issues"},
}
var charts []*chart
charts = append(charts, &chart{
Title: "Open CLs (1 Year)",
Columns: cols,
Data: chartData,
})
charts = append(charts, &chart{
Title: "Open CLs (30 Days)",
Columns: cols,
Data: chartData[len(chartData)-30:],
})
charts = append(charts, &chart{
Title: "Open CLs (7 Days)",
Columns: cols,
Data: chartData[len(chartData)-7:],
})
s.data.stats.Charts = charts
}
// A clInterval describes a time period during which a CL is open.
// points on the interval are seconds since the epoch.
type clInterval struct {
start, end int64 // seconds since epoch
cl *maintner.GerritCL
}
// returns true iff the interval contains any seconds
// in the timespan [t0,t1]. t0 must be before t1.
func (i *clInterval) intersects(t0, t1 time.Time) bool {
if t1.Before(t0) {
panic("t0 cannot be before t1")
}
return i.end >= t0.Unix() && i.start <= t1.Unix()
}
func newIntervalFromCL(cl *maintner.GerritCL) *clInterval {
interval := &clInterval{
start: cl.Created.Unix(),
end: math.MaxInt64,
cl: cl,
}
closed := cl.Status == "merged" || cl.Status == "abandoned"
if closed {
for i := len(cl.Metas) - 1; i >= 0; i-- {
if !strings.Contains(cl.Metas[i].Commit.Msg, "autogenerated:gerrit") {
continue
}
if strings.Contains(cl.Metas[i].Commit.Msg, "autogenerated:gerrit:merged") ||
strings.Contains(cl.Metas[i].Commit.Msg, "autogenerated:gerrit:abandon") {
interval.end = cl.Metas[i].Commit.CommitTime.Unix()
}
}
if interval.end == math.MaxInt64 {
log.Printf("Unable to determine close time of CL: %+v", cl)
}
}
return interval
}