forked from evergreen-ci/evergreen
/
task_history.go
280 lines (245 loc) · 8.43 KB
/
task_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
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
package model
import (
"fmt"
"time"
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/evergreen/apimodels"
"github.com/evergreen-ci/evergreen/db"
"github.com/evergreen-ci/evergreen/model/task"
"github.com/evergreen-ci/evergreen/model/version"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
type taskHistoryIterator struct {
TaskName string
BuildVariants []string
ProjectName string
}
type TaskHistoryChunk struct {
Tasks []bson.M
Versions []version.Version
FailedTests map[string][]task.TestResult
Exhausted ExhaustedIterator
}
type ExhaustedIterator struct {
Before, After bool
}
type TaskHistory struct {
Id string `bson:"_id" json:"_id"`
Order string `bson:"order" json:"order"`
Tasks []aggregatedTaskHistory `bson:"tasks" json:"tasks"`
}
type aggregatedTaskHistory struct {
Id string `bson:"_id" json:"_id"`
Status string `bson:"status" json:"status"`
Activated bool `bson:"activated" json:"activated"`
TimeTaken time.Duration `bson:"time_taken" json:"time_taken"`
BuildVariant string `bson:"build_variant" json:"build_variant"`
TestResults apimodels.TaskEndDetails `bson:"status_details" json:"status_details"`
}
type TaskHistoryIterator interface {
GetChunk(version *version.Version, numBefore, numAfter int, include bool) (TaskHistoryChunk, error)
GetDistinctTestNames(numCommits int) ([]string, error)
}
func NewTaskHistoryIterator(name string, buildVariants []string, projectName string) TaskHistoryIterator {
return TaskHistoryIterator(&taskHistoryIterator{TaskName: name, BuildVariants: buildVariants, ProjectName: projectName})
}
func (iter *taskHistoryIterator) findAllVersions(v *version.Version, numRevisions int, before, include bool) ([]version.Version, bool, error) {
versionQuery := bson.M{
version.RequesterKey: evergreen.RepotrackerVersionRequester,
version.IdentifierKey: iter.ProjectName,
}
// If including the specified version in the result, then should
// get an additional revision
if include {
numRevisions++
}
// Determine the comparator to use based on whether the revisions
// come before/after the specified version
compare, order := "$gt", version.RevisionOrderNumberKey
if before {
compare, order = "$lt", fmt.Sprintf("-%v", version.RevisionOrderNumberKey)
if include {
compare = "$lte"
}
} else if include {
compare = "$gte"
}
if v != nil {
versionQuery[version.RevisionOrderNumberKey] = bson.M{compare: v.RevisionOrderNumber}
}
// Get the next numRevisions, plus an additional one to check if have
// reached the beginning/end of history
versions, err := version.Find(
db.Query(versionQuery).WithFields(
version.IdKey,
version.RevisionOrderNumberKey,
version.RevisionKey,
version.MessageKey,
version.CreateTimeKey,
).Sort([]string{order}).Limit(numRevisions + 1))
// Check if there were fewer results returned by the query than what
// the limit was set as
exhausted := len(versions) <= numRevisions
if !exhausted {
// Exclude the last version because we actually only wanted
// `numRevisions` number of commits
versions = versions[:len(versions)-1]
}
// The iterator can only be exhausted if an actual version was specified
exhausted = exhausted || (v == nil && numRevisions == 0)
if !before {
// Reverse the order so that the most recent version is first
for i, j := 0, len(versions)-1; i < j; i, j = i+1, j-1 {
versions[i], versions[j] = versions[j], versions[i]
}
}
return versions, exhausted, err
}
// Returns tasks grouped by their versions, and sorted with the most
// recent first (i.e. descending commit order number).
func (iter *taskHistoryIterator) GetChunk(v *version.Version, numBefore, numAfter int, include bool) (TaskHistoryChunk, error) {
session, database, err := db.GetGlobalSessionFactory().GetSession()
defer session.Close()
chunk := TaskHistoryChunk{
Tasks: []bson.M{},
Versions: []version.Version{},
FailedTests: map[string][]task.TestResult{},
}
versionsBefore, exhausted, err := iter.findAllVersions(v, numBefore, true, include)
if err != nil {
return chunk, err
}
chunk.Exhausted.Before = exhausted
versionsAfter, exhausted, err := iter.findAllVersions(v, numAfter, false, false)
if err != nil {
return chunk, err
}
chunk.Exhausted.After = exhausted
versions := append(versionsAfter, versionsBefore...)
if len(versions) == 0 {
return chunk, nil
}
chunk.Versions = versions
// versionStartBoundary is the most recent version (i.e. newest) that
// should be included in the results.
//
// versionEndBoundary is the least recent version (i.e. oldest) that
// should be included in the results.
versionStartBoundary, versionEndBoundary := versions[0], versions[len(versions)-1]
pipeline := database.C(task.Collection).Pipe(
[]bson.M{
{"$match": bson.M{
task.RequesterKey: evergreen.RepotrackerVersionRequester,
task.ProjectKey: iter.ProjectName,
task.DisplayNameKey: iter.TaskName,
task.BuildVariantKey: bson.M{"$in": iter.BuildVariants},
task.RevisionOrderNumberKey: bson.M{
"$gte": versionEndBoundary.RevisionOrderNumber,
"$lte": versionStartBoundary.RevisionOrderNumber,
},
}},
{"$project": bson.M{
task.IdKey: 1,
task.StatusKey: 1,
task.DetailsKey: 1,
task.ActivatedKey: 1,
task.TimeTakenKey: 1,
task.BuildVariantKey: 1,
task.RevisionKey: 1,
task.RevisionOrderNumberKey: 1,
}},
{"$group": bson.M{
"_id": fmt.Sprintf("$%v", task.RevisionKey),
"order": bson.M{"$first": fmt.Sprintf("$%v", task.RevisionOrderNumberKey)},
"tasks": bson.M{
"$push": bson.M{
task.IdKey: fmt.Sprintf("$%v", task.IdKey),
task.StatusKey: fmt.Sprintf("$%v", task.StatusKey),
task.DetailsKey: fmt.Sprintf("$%v", task.DetailsKey),
task.ActivatedKey: fmt.Sprintf("$%v", task.ActivatedKey),
task.TimeTakenKey: fmt.Sprintf("$%v", task.TimeTakenKey),
task.BuildVariantKey: fmt.Sprintf("$%v", task.BuildVariantKey),
},
},
}},
{"$sort": bson.M{task.RevisionOrderNumberKey: -1}},
},
)
var aggregatedTasks []bson.M
if err := pipeline.All(&aggregatedTasks); err != nil {
return chunk, err
}
chunk.Tasks = aggregatedTasks
failedTests, err := iter.GetFailedTests(pipeline)
if err != nil {
return chunk, err
}
chunk.FailedTests = failedTests
return chunk, nil
}
func (self *taskHistoryIterator) GetDistinctTestNames(numCommits int) ([]string, error) {
session, db, err := db.GetGlobalSessionFactory().GetSession()
defer session.Close()
pipeline := db.C(task.Collection).Pipe(
[]bson.M{
{
"$match": bson.M{
task.BuildVariantKey: bson.M{"$in": self.BuildVariants},
task.DisplayNameKey: self.TaskName,
},
},
{"$sort": bson.D{{task.RevisionOrderNumberKey, -1}}},
{"$limit": numCommits},
{"$unwind": fmt.Sprintf("$%v", task.TestResultsKey)},
{"$group": bson.M{"_id": fmt.Sprintf("$%v.%v", task.TestResultsKey, task.TestResultTestFileKey)}},
},
)
var output []bson.M
err = pipeline.All(&output)
if err != nil {
return nil, err
}
names := make([]string, 0)
for _, doc := range output {
names = append(names, doc["_id"].(string))
}
return names, nil
}
// GetFailedTests returns a mapping of task id to a slice of failed tasks
// extracted from a pipeline of aggregated tasks
func (self *taskHistoryIterator) GetFailedTests(aggregatedTasks *mgo.Pipe) (map[string][]task.TestResult, error) {
// get the ids of the failed task
var failedTaskIds []string
var taskHistory TaskHistory
iter := aggregatedTasks.Iter()
for {
if iter.Next(&taskHistory) {
for _, task := range taskHistory.Tasks {
if task.Status == evergreen.TaskFailed {
failedTaskIds = append(failedTaskIds, task.Id)
}
}
} else {
break
}
}
if err := iter.Err(); err != nil {
return nil, err
}
// find all the relevant failed tests
failedTestsMap := make(map[string][]task.TestResult)
tasks, err := task.Find(task.ByIds(failedTaskIds).WithFields(task.IdKey, task.TestResultsKey))
if err != nil {
return nil, err
}
// create the mapping of the task id to the list of failed tasks
for _, task := range tasks {
for _, test := range task.TestResults {
if test.Status == evergreen.TestFailedStatus {
failedTestsMap[task.Id] = append(failedTestsMap[task.Id], test)
}
}
}
return failedTestsMap, nil
}