-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreporting.go
More file actions
258 lines (216 loc) · 5.33 KB
/
reporting.go
File metadata and controls
258 lines (216 loc) · 5.33 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package main
import (
"errors"
"fmt"
"strings"
)
type action int
const (
cloneAction action = 0
syncAction action = 1
workerStartAction action = 2
workerFinishAction action = 3
)
type reportingCmd struct {
worker int
action action
repoName string
err error
}
type cloneStat struct {
total int
succeeded int
failed int
}
type cloneResult struct {
names []string
errors []error
}
type syncStat struct {
total int
succeeded int
failed int
}
type syncResult struct {
names []string
errors []error
}
type otherStat struct {
names []string
}
type workerStats struct {
started int
finished int
}
type reportingStats struct {
cloneStat cloneStat
cloneResult cloneResult
syncStat syncStat
syncResult syncResult
otherStat otherStat
workerStats workerStats
}
func stringSliceToMap(values []string) map[string]struct{} {
m := make(map[string]struct{}, len(values))
for i := 0; i < len(values); i++ {
m[values[i]] = struct{}{}
}
return m
}
type reporter struct {
verbose bool
fields []string
chReporting chan reportingCmd
chReportingDone chan struct{}
}
func newReporter(verbose bool, reportFields string) (*reporter, error) {
fields, err := validateReportFields(reportFields)
if err != nil {
return nil, err
}
return &reporter{
chReporting: make(chan reportingCmd, 1000), chReportingDone: make(chan struct{}),
verbose: verbose, fields: fields,
}, nil
}
func validateReportFields(value string) ([]string, error) {
if value == "" {
return nil, errors.New("fields are empty")
}
fieldMap := map[string]int{
"error": 0,
"cloned": 0,
"synced": 0,
"other": 0,
"all": 0,
}
fields := strings.Split(value, ",")
for _, field := range fields {
_, ok := fieldMap[field]
if !ok {
return nil, fmt.Errorf("report field %s is invalid", field)
}
fieldMap[field]++
}
for field, count := range fieldMap {
if count > 1 {
return nil, fmt.Errorf("report field %s exists multiple times", field)
}
}
return fields, nil
}
func (r *reporter) process(clone, sync int, others []string) {
stats := reportingStats{
cloneStat: cloneStat{
total: clone,
},
syncStat: syncStat{
total: sync,
},
otherStat: otherStat{
names: others,
},
}
for cmd := range r.chReporting {
switch cmd.action {
case cloneAction:
if cmd.err != nil {
stats.cloneStat.failed++
stats.cloneResult.errors = append(stats.cloneResult.errors, cmd.err)
} else {
stats.cloneStat.succeeded++
stats.cloneResult.names = append(stats.cloneResult.names, cmd.repoName)
}
case syncAction:
if cmd.err != nil {
stats.syncStat.failed++
stats.syncResult.errors = append(stats.syncResult.errors, cmd.err)
} else {
stats.syncStat.succeeded++
stats.syncResult.names = append(stats.syncResult.names, cmd.repoName)
}
case workerStartAction:
stats.workerStats.started++
if r.verbose {
fmt.Printf("worker %d started\n", cmd.worker)
}
case workerFinishAction:
stats.workerStats.finished++
if r.verbose {
fmt.Printf("worker %d finished\n", cmd.worker)
}
}
}
if r.verbose {
fmt.Printf("stats: workers: %+v, clone: %+v, sync: %+v, other: %d\n", stats.workerStats, stats.cloneStat,
stats.syncStat, len(stats.otherStat.names))
}
r.print(stats)
r.chReportingDone <- struct{}{}
}
func (r *reporter) print(stats reportingStats) {
for _, field := range r.fields {
switch field {
case "error":
r.printErrors(stats.cloneResult.errors, stats.syncResult.errors)
case "cloned":
printSection("Cloned", stats.cloneResult.names)
case "synced":
printSection("Synced", stats.cloneResult.names)
case "other":
printSection("Other", stats.cloneResult.names)
}
}
}
func (r *reporter) printErrors(cloneErrors, syncErrors []error) {
if len(cloneErrors) != 0 {
fmt.Println("Clone failures")
for _, err := range cloneErrors {
fmt.Println(err)
}
}
if len(syncErrors) == 0 {
return
}
fmt.Println("Sync failures")
for _, err := range syncErrors {
fmt.Println(err)
}
}
func (r *reporter) reportSyncSuccess(worker int, repo string) {
r.chReporting <- reportingCmd{action: syncAction, repoName: repo, worker: worker}
}
func (r *reporter) reportSyncFailure(worker int, repo string, err error) {
r.chReporting <- reportingCmd{action: syncAction, repoName: repo, err: err, worker: worker}
}
func (r *reporter) reportCloneSuccess(worker int, repo string) {
r.chReporting <- reportingCmd{action: cloneAction, repoName: repo, worker: worker}
}
func (r *reporter) reportCloneFailure(worker int, repo string, err error) {
r.chReporting <- reportingCmd{action: cloneAction, repoName: repo, err: err, worker: worker}
}
func (r *reporter) reportWorkerStarted(worker int) {
r.chReporting <- reportingCmd{action: workerStartAction, worker: worker}
}
func (r *reporter) reportWorkerFinished(worker int) {
r.chReporting <- reportingCmd{action: workerFinishAction, worker: worker}
}
func (r *reporter) wait() {
close(r.chReporting)
<-r.chReportingDone
}
func (r *reporter) dryRun(clone []string, sync []string, other []string) {
printSection("Clone", clone)
printSection("Sync", sync)
printSection("Other", other)
}
func printSection(section string, values []string) {
fmt.Println(section)
if len(values) == 0 {
fmt.Println("n/a")
return
}
for i, value := range values {
fmt.Printf("%5d %s\n", i+1, value)
}
}