forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_history.go
215 lines (191 loc) · 6.75 KB
/
project_history.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package reports
import (
"fmt"
"io/ioutil"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"gopkg.in/yaml.v2"
)
type ProjectHistory struct {
LastReportDate string `yaml:"lastReportDate,omitempty"`
Reports []*ProjectReport `yaml:"reports,omitempty"`
Contributors []string `yaml:"contributors,omitempty"`
Committers []string `yaml:"committers,omitempty"`
}
type CountMetrics struct {
Count int `yaml:"count,omitempty"`
Total int `yaml:"total,omitempty"`
}
type ProjectReport struct {
ReportDate string `yaml:"reportDate,omitempty"`
StarsMetrics CountMetrics `yaml:"starsMetrics,omitempty"`
DownloadMetrics CountMetrics `yaml:"downloadMetrics,omitempty"`
IssueMetrics CountMetrics `yaml:"issueMetrics,omitempty"`
PullRequestMetrics CountMetrics `yaml:"pullRequestMetrics,omitempty"`
CommitMetrics CountMetrics `yaml:"commitMetrics,omitempty"`
NewCommitterMetrics CountMetrics `yaml:"newCommitterMetrics,omitempty"`
NewContributorMetrics CountMetrics `yaml:"newContributorMetrics,omitempty"`
DeveloperChatMetrics CountMetrics `yaml:"developerChatMetrics,omitempty"`
UserChatMetrics CountMetrics `yaml:"userChatMetrics,omitempty"`
}
func (h *ProjectHistory) GetOrCreateReport(reportDate string) *ProjectReport {
report := h.FindReport(reportDate)
if report == nil {
report = &ProjectReport{
ReportDate: reportDate,
}
h.Reports = append(h.Reports, report)
h.LastReportDate = reportDate
}
return report
}
func (h *ProjectHistory) FindReport(reportDate string) *ProjectReport {
for _, report := range h.Reports {
if report.ReportDate == reportDate {
return report
}
}
return nil
}
func (h *ProjectHistory) FindPreviousReport(reportDate string) *ProjectReport {
// TODO we should really do a date comparison but for noe we do posts in order anyway
var previous = &ProjectReport{}
for _, report := range h.Reports {
if report.ReportDate == reportDate {
return previous
} else {
previous = report
}
}
return previous
}
func (h *ProjectHistory) DownloadMetrics(reportDate string, total int) *ProjectReport {
report := h.GetOrCreateReport(reportDate)
previous := h.FindPreviousReport(reportDate)
updateMetricTotal(&report.DownloadMetrics, &previous.DownloadMetrics, total)
return report
}
func (h *ProjectHistory) IssueMetrics(reportDate string, total int) *ProjectReport {
report := h.GetOrCreateReport(reportDate)
previous := h.FindPreviousReport(reportDate)
addMetricCount(&report.IssueMetrics, &previous.IssueMetrics, total)
return report
}
func (h *ProjectHistory) PullRequestMetrics(reportDate string, total int) *ProjectReport {
report := h.GetOrCreateReport(reportDate)
previous := h.FindPreviousReport(reportDate)
addMetricCount(&report.PullRequestMetrics, &previous.PullRequestMetrics, total)
return report
}
func (h *ProjectHistory) CommitMetrics(reportDate string, total int) *ProjectReport {
report := h.GetOrCreateReport(reportDate)
previous := h.FindPreviousReport(reportDate)
addMetricCount(&report.CommitMetrics, &previous.CommitMetrics, total)
return report
}
func (h *ProjectHistory) NewCommitterMetrics(reportDate string, total int) *ProjectReport {
report := h.GetOrCreateReport(reportDate)
previous := h.FindPreviousReport(reportDate)
addMetricCount(&report.NewCommitterMetrics, &previous.NewCommitterMetrics, total)
return report
}
func (h *ProjectHistory) NewContributorMetrics(reportDate string, total int) *ProjectReport {
report := h.GetOrCreateReport(reportDate)
previous := h.FindPreviousReport(reportDate)
addMetricCount(&report.NewContributorMetrics, &previous.NewContributorMetrics, total)
return report
}
func (h *ProjectHistory) StarsMetrics(reportDate string, total int) *ProjectReport {
report := h.GetOrCreateReport(reportDate)
previous := h.FindPreviousReport(reportDate)
updateMetricTotal(&report.StarsMetrics, &previous.StarsMetrics, total)
return report
}
func (h *ProjectHistory) DeveloperChatMetrics(reportDate string, total int) *ProjectReport {
report := h.GetOrCreateReport(reportDate)
previous := h.FindPreviousReport(reportDate)
updateMetricTotal(&report.DeveloperChatMetrics, &previous.DeveloperChatMetrics, total)
return report
}
func (h *ProjectHistory) UserChatMetrics(reportDate string, total int) *ProjectReport {
report := h.GetOrCreateReport(reportDate)
previous := h.FindPreviousReport(reportDate)
updateMetricTotal(&report.UserChatMetrics, &previous.UserChatMetrics, total)
return report
}
// addMetricCount adds a new metric value, such as number of commits in a release
func addMetricCount(current *CountMetrics, previous *CountMetrics, total int) {
current.Count = total
previousTotal := 0
if previous != nil {
previousTotal = previous.Total
}
current.Total = total + previousTotal
}
// updateMetricTotal takes the current total value and works out the incremental change
// since the last report. e.g. for updating the new number of stars or users in a chat room
func updateMetricTotal(current *CountMetrics, previous *CountMetrics, total int) {
current.Total = total
previousTotal := 0
if previous != nil {
previousTotal = previous.Total
}
count := total - previousTotal
current.Count = count
}
type ProjectHistoryService struct {
FileName string
history *ProjectHistory
}
func NewProjectHistoryService(fileName string) (*ProjectHistoryService, *ProjectHistory, error) {
svc := &ProjectHistoryService{
FileName: fileName,
history: &ProjectHistory{},
}
history, err := svc.LoadHistory()
return svc, history, err
}
// LoadHistory loads the project history from disk if it exists
func (s *ProjectHistoryService) LoadHistory() (*ProjectHistory, error) {
history := s.History()
fileName := s.FileName
if fileName != "" {
exists, err := util.FileExists(fileName)
if err != nil {
return history, fmt.Errorf("Could not check if file exists %s due to %s", fileName, err)
}
if exists {
data, err := ioutil.ReadFile(fileName)
if err != nil {
return history, fmt.Errorf("Failed to load file %s due to %s", fileName, err)
}
err = yaml.Unmarshal(data, history)
if err != nil {
return history, fmt.Errorf("Failed to unmarshal YAML file %s due to %s", fileName, err)
}
}
}
return history, nil
}
func (s *ProjectHistoryService) History() *ProjectHistory {
if s.history == nil {
s.history = &ProjectHistory{}
}
return s.history
}
// SaveHistory saves the history to disk
func (s *ProjectHistoryService) SaveHistory() error {
fileName := s.FileName
if fileName == "" {
return fmt.Errorf("No filename defined!")
}
data, err := yaml.Marshal(s.history)
if err != nil {
return err
}
err = ioutil.WriteFile(fileName, data, util.DefaultWritePermissions)
if err == nil {
log.Infof("Wrote Project History file %s\n", util.ColorInfo(fileName))
}
return err
}