-
Notifications
You must be signed in to change notification settings - Fork 331
/
Copy pathchurn_analysis.go
300 lines (271 loc) · 8.1 KB
/
churn_analysis.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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package main
import (
"fmt"
"io"
"sort"
"strings"
"unicode/utf8"
"github.com/gogo/protobuf/proto"
"github.com/sergi/go-diff/diffmatchpatch"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"gopkg.in/src-d/go-git.v4/utils/merkletrie"
"gopkg.in/src-d/hercules.v10"
)
// ChurnAnalysis contains the intermediate state which is mutated by Consume(). It should implement
// hercules.LeafPipelineItem.
type ChurnAnalysis struct {
// No special merge logic is required
hercules.NoopMerger
// Process each merge only once
hercules.OneShotMergeProcessor
TrackPeople bool
global []editInfo
people map[int][]editInfo
// references IdentityDetector.ReversedPeopleDict
reversedPeopleDict []string
l hercules.Logger
}
type editInfo struct {
Tick int
Added int
Removed int
}
// ChurnAnalysisResult is returned by Finalize() and represents the analysis result.
type ChurnAnalysisResult struct {
Global Edits
People map[string]Edits
}
type Edits struct {
Ticks []int
Additions []int
Removals []int
}
const (
ConfigChurnTrackPeople = "Churn.TrackPeople"
)
// Analysis' name in the graph is usually the same as the type's name, however, does not have to.
func (churn *ChurnAnalysis) Name() string {
return "ChurnAnalysis"
}
// LeafPipelineItem-s normally do not act as intermediate nodes and thus we return an empty slice.
func (churn *ChurnAnalysis) Provides() []string {
return []string{}
}
// Requires returns the list of dependencies which must be supplied in Consume().
// file_diff - line diff for each commit change
// changes - list of changed files for each commit
// blob_cache - set of blobs affected by each commit
// tick - number of ticks since start for each commit
// author - author of the commit
func (churn *ChurnAnalysis) Requires() []string {
return []string{
hercules.DependencyFileDiff,
hercules.DependencyTreeChanges,
hercules.DependencyBlobCache,
hercules.DependencyTick,
hercules.DependencyAuthor}
}
// ListConfigurationOptions tells the engine which parameters can be changed through the command
// line.
func (churn *ChurnAnalysis) ListConfigurationOptions() []hercules.ConfigurationOption {
opts := [...]hercules.ConfigurationOption{{
Name: ConfigChurnTrackPeople,
Description: "Record detailed statistics per each developer.",
Flag: "churn-people",
Type: hercules.BoolConfigurationOption,
Default: false},
}
return opts[:]
}
// Flag returns the command line switch which activates the analysis.
func (churn *ChurnAnalysis) Flag() string {
return "churn"
}
// Description returns the text which explains what the analysis is doing.
func (churn *ChurnAnalysis) Description() string {
return "Collects the daily numbers of inserted and removed lines."
}
// Configure applies the parameters specified in the command line. Map keys correspond to "Name".
func (churn *ChurnAnalysis) Configure(facts map[string]interface{}) error {
if l, exists := facts[hercules.ConfigLogger].(hercules.Logger); exists {
churn.l = l
}
if val, exists := facts[ConfigChurnTrackPeople].(bool); exists {
churn.TrackPeople = val
}
if churn.TrackPeople {
churn.reversedPeopleDict = facts[hercules.FactIdentityDetectorReversedPeopleDict].([]string)
}
return nil
}
// Initialize resets the internal temporary data structures and prepares the object for Consume().
func (churn *ChurnAnalysis) Initialize(repository *git.Repository) error {
churn.l = hercules.NewLogger()
churn.global = []editInfo{}
churn.people = map[int][]editInfo{}
churn.OneShotMergeProcessor.Initialize()
return nil
}
func (churn *ChurnAnalysis) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
if !churn.ShouldConsumeCommit(deps) {
return nil, nil
}
fileDiffs := deps[hercules.DependencyFileDiff].(map[string]hercules.FileDiffData)
treeDiffs := deps[hercules.DependencyTreeChanges].(object.Changes)
cache := deps[hercules.DependencyBlobCache].(map[plumbing.Hash]*hercules.CachedBlob)
tick := deps[hercules.DependencyTick].(int)
author := deps[hercules.DependencyAuthor].(int)
for _, change := range treeDiffs {
action, err := change.Action()
if err != nil {
return nil, err
}
added := 0
removed := 0
switch action {
case merkletrie.Insert:
added, _ = cache[change.To.TreeEntry.Hash].CountLines()
case merkletrie.Delete:
removed, _ = cache[change.From.TreeEntry.Hash].CountLines()
case merkletrie.Modify:
diffs := fileDiffs[change.To.Name]
for _, edit := range diffs.Diffs {
length := utf8.RuneCountInString(edit.Text)
switch edit.Type {
case diffmatchpatch.DiffEqual:
continue
case diffmatchpatch.DiffInsert:
added += length
case diffmatchpatch.DiffDelete:
removed += length
}
}
}
if err != nil {
return nil, err
}
ei := editInfo{Tick: tick, Added: added, Removed: removed}
churn.global = append(churn.global, ei)
if churn.TrackPeople {
seq, exists := churn.people[author]
if !exists {
seq = []editInfo{}
}
seq = append(seq, ei)
churn.people[author] = seq
}
}
return nil, nil
}
// Fork clones the same item several times on branches.
func (churn *ChurnAnalysis) Fork(n int) []hercules.PipelineItem {
return hercules.ForkSamePipelineItem(churn, n)
}
func (churn *ChurnAnalysis) Finalize() interface{} {
result := ChurnAnalysisResult{
Global: editInfosToEdits(churn.global),
People: map[string]Edits{},
}
if churn.TrackPeople {
for key, val := range churn.people {
result.People[churn.reversedPeopleDict[key]] = editInfosToEdits(val)
}
}
return result
}
func (churn *ChurnAnalysis) Serialize(result interface{}, binary bool, writer io.Writer) error {
burndownResult := result.(ChurnAnalysisResult)
if binary {
return churn.serializeBinary(&burndownResult, writer)
}
churn.serializeText(&burndownResult, writer)
return nil
}
func (churn *ChurnAnalysis) serializeText(result *ChurnAnalysisResult, writer io.Writer) {
fmt.Fprintln(writer, " global:")
printEdits(result.Global, writer, 4)
for key, val := range result.People {
fmt.Fprintf(writer, " %s:\n", hercules.SafeYamlString(key))
printEdits(val, writer, 4)
}
}
func (churn *ChurnAnalysis) serializeBinary(result *ChurnAnalysisResult, writer io.Writer) error {
message := ChurnAnalysisResultMessage{
Global: editsToEditsMessage(result.Global),
People: map[string]*EditsMessage{},
}
for key, val := range result.People {
message.People[key] = editsToEditsMessage(val)
}
serialized, err := proto.Marshal(&message)
if err != nil {
return err
}
writer.Write(serialized)
return nil
}
func editInfosToEdits(eis []editInfo) Edits {
aux := map[int]*editInfo{}
for _, ei := range eis {
ptr := aux[ei.Tick]
if ptr == nil {
ptr = &editInfo{Tick: ei.Tick}
}
ptr.Added += ei.Added
ptr.Removed += ei.Removed
aux[ei.Tick] = ptr
}
seq := []int{}
for key := range aux {
seq = append(seq, key)
}
sort.Ints(seq)
edits := Edits{
Ticks: make([]int, len(seq)),
Additions: make([]int, len(seq)),
Removals: make([]int, len(seq)),
}
for i, tick := range seq {
edits.Ticks[i] = tick
edits.Additions[i] = aux[tick].Added
edits.Removals[i] = aux[tick].Removed
}
return edits
}
func printEdits(edits Edits, writer io.Writer, indent int) {
strIndent := strings.Repeat(" ", indent)
printArray := func(arr []int, name string) {
fmt.Fprintf(writer, "%s%s: [", strIndent, name)
for i, v := range arr {
if i < len(arr)-1 {
fmt.Fprintf(writer, "%d, ", v)
} else {
fmt.Fprintf(writer, "%d]\n", v)
}
}
}
printArray(edits.Ticks, "ticks")
printArray(edits.Additions, "additions")
printArray(edits.Removals, "removals")
}
func editsToEditsMessage(edits Edits) *EditsMessage {
message := &EditsMessage{
Ticks: make([]uint32, len(edits.Ticks)),
Additions: make([]uint32, len(edits.Additions)),
Removals: make([]uint32, len(edits.Removals)),
}
copyInts := func(arr []int, where []uint32) {
for i, v := range arr {
where[i] = uint32(v)
}
}
copyInts(edits.Ticks, message.Ticks)
copyInts(edits.Additions, message.Additions)
copyInts(edits.Removals, message.Removals)
return message
}
func init() {
hercules.Registry.Register(&ChurnAnalysis{})
}