forked from evergreen-ci/evergreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.go
262 lines (230 loc) · 8.02 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
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
package service
import (
"net/http"
"time"
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/evergreen/model/build"
"github.com/evergreen-ci/evergreen/model/patch"
"github.com/evergreen-ci/evergreen/model/task"
"github.com/evergreen-ci/evergreen/model/version"
"github.com/evergreen-ci/gimlet"
"github.com/pkg/errors"
)
// UIBuildVariant contains the name of the build variant and the tasks associated with that build variant.
type UIBuildVariant struct {
Name string `json:"name"`
TaskNames []string `json:"task_names"`
}
// UIProject has all the tasks that are in a project and all the BuildVariants.
type UIProject struct {
Name string `json:"name"`
BuildVariants []UIBuildVariant `json:"build_variants"`
TaskNames []string `json:"task_names"`
}
//UITask has the fields that are necessary to send over the wire for tasks
type UITask struct {
Id string `json:"id"`
CreateTime time.Time `json:"create_time"`
DispatchTime time.Time `json:"dispatch_time"`
ScheduledTime time.Time `json:"scheduled_time"`
StartTime time.Time `json:"start_time"`
FinishTime time.Time `json:"finish_time"`
Version string `json:"version"`
Status string `json:"status"`
Host string `json:"host"`
Distro string `json:"distro"`
}
//UIBuild has the fields that are necessary to send over the wire for builds
type UIBuild struct {
Id string `json:"id"`
CreateTime time.Time `json:"create_time"`
StartTime time.Time `json:"start_time"`
FinishTime time.Time `json:"finish_time"`
Version string `json:"version"`
Status string `json:"status"`
Tasks []build.TaskCache `json:"tasks"`
TimeTaken int64 `json:"time_taken"`
}
// UIStats is all of the data that the stats page might need.
type UIStats struct {
Tasks []*UITask `json:"tasks"`
Builds []*UIBuild `json:"builds"`
Versions []version.Version `json:"versions"`
Patches []patch.Patch `json:"patches"`
}
func (uis *UIServer) taskTimingPage(w http.ResponseWriter, r *http.Request) {
projCtx := MustHaveProjectContext(r)
project, err := projCtx.GetProject()
if err != nil || project == nil {
uis.ProjectNotFound(projCtx, w, r)
return
}
currentProject := UIProject{project.Identifier, []UIBuildVariant{}, []string{}}
// populate buildVariants by iterating over the build variants tasks
for _, bv := range project.BuildVariants {
newBv := UIBuildVariant{bv.Name, []string{}}
for _, task := range bv.Tasks {
newBv.TaskNames = append(newBv.TaskNames, task.Name)
}
currentProject.BuildVariants = append(currentProject.BuildVariants, newBv)
}
for _, task := range project.Tasks {
currentProject.TaskNames = append(currentProject.TaskNames, task.Name)
}
data := struct {
Project UIProject
ViewData
}{currentProject, uis.GetCommonViewData(w, r, false, true)}
uis.render.WriteResponse(w, http.StatusOK, data, "base", "task_timing.html", "base_angular.html", "menu.html")
}
// taskTimingJSON sends over the task data for a certain task of a certain build variant
func (uis *UIServer) taskTimingJSON(w http.ResponseWriter, r *http.Request) {
projCtx := MustHaveProjectContext(r)
beforeTaskId := r.FormValue("before")
onlySuccessful := r.FormValue("onlySuccessful")
limit, err := getIntValue(r, "limit", 50)
if err != nil {
uis.LoggedError(w, r, http.StatusBadRequest, err)
return
}
vars := gimlet.GetVars(r)
buildVariant := vars["build_variant"]
taskName := vars["task_name"]
request := vars["request"]
project, err := projCtx.GetProject()
if err != nil || project == nil {
uis.LoggedError(w, r, http.StatusNotFound, errors.New("not found"))
return
}
bv := project.FindBuildVariant(buildVariant)
if bv == nil {
uis.LoggedError(w, r, http.StatusNotFound, errors.Errorf("build variant %v not found", buildVariant))
return
}
var versionIds []string
data := UIStats{}
var statuses []string
if onlySuccessful == "true" {
statuses = []string{evergreen.TaskSucceeded}
} else {
statuses = evergreen.CompletedStatuses
}
// if its all tasks find the build
if taskName == "" || taskName == "All Tasks" {
// TODO: switch this to be a query on the builds TaskCache
var builds []build.Build
builds, err = build.Find(build.ByProjectAndVariant(project.Identifier, buildVariant, request, statuses).
WithFields(build.IdKey, build.CreateTimeKey, build.VersionKey,
build.TimeTakenKey, build.TasksKey, build.FinishTimeKey, build.StartTimeKey, build.StatusKey).
Sort([]string{"-" + build.RevisionOrderNumberKey}).
Limit(limit))
if err != nil {
uis.LoggedError(w, r, http.StatusBadRequest, err)
return
}
versionIds = make([]string, 0, len(builds))
uiBuilds := []*UIBuild{}
// get the versions for every single task that was returned
for _, build := range builds {
// create a UITask
b := &UIBuild{
Id: build.Id,
CreateTime: build.CreateTime,
StartTime: build.StartTime,
FinishTime: build.FinishTime,
Version: build.Version,
Status: build.Status,
TimeTaken: int64(build.TimeTaken),
Tasks: build.Tasks,
}
uiBuilds = append(uiBuilds, b)
versionIds = append(versionIds, b.Version)
}
data.Builds = uiBuilds
} else {
foundTask := false
for _, t := range bv.Tasks {
if t.Name == taskName {
foundTask = true
break
}
}
if !foundTask {
uis.LoggedError(w, r, http.StatusNotFound, errors.Errorf("no task named '%v'", taskName))
return
}
var tasks []task.Task
fields := []string{task.CreateTimeKey, task.DispatchTimeKey,
task.ScheduledTimeKey, task.StartTimeKey, task.FinishTimeKey,
task.VersionKey, task.HostIdKey, task.StatusKey, task.HostIdKey,
task.DistroIdKey}
if beforeTaskId != "" {
var t *task.Task
t, err = task.FindOne(task.ById(beforeTaskId))
if err != nil {
uis.LoggedError(w, r, http.StatusNotFound, err)
return
}
if t == nil {
uis.LoggedError(w, r, http.StatusNotFound, errors.Errorf("Task %v not found", beforeTaskId))
return
}
tasks, err = task.Find(task.ByBeforeRevisionWithStatusesAndRequester(t.RevisionOrderNumber, statuses,
buildVariant, taskName, project.Identifier, request).Limit(limit).WithFields(fields...))
if err != nil {
uis.LoggedError(w, r, http.StatusNotFound, err)
return
}
} else {
tasks, err = task.Find(task.ByStatuses(statuses,
buildVariant, taskName, project.Identifier, request).Limit(limit).WithFields(fields...).Sort([]string{"-" + task.RevisionOrderNumberKey}))
if err != nil {
uis.LoggedError(w, r, http.StatusNotFound, err)
return
}
}
uiTasks := []*UITask{}
versionIds = make([]string, 0, len(tasks))
// get the versions for every single task that was returned
for _, t := range tasks {
// create a UITask
uiTask := &UITask{
Id: t.Id,
CreateTime: t.CreateTime,
DispatchTime: t.DispatchTime,
ScheduledTime: t.ScheduledTime,
StartTime: t.StartTime,
FinishTime: t.FinishTime,
Version: t.Version,
Status: t.Status,
Host: t.HostId,
Distro: t.DistroId,
}
uiTasks = append(uiTasks, uiTask)
versionIds = append(versionIds, t.Version)
}
data.Tasks = uiTasks
}
// Populate the versions field if with commits, otherwise patches field
if request == evergreen.RepotrackerVersionRequester {
versions, err := version.Find(version.ByIds(versionIds).
WithFields(version.IdKey, version.CreateTimeKey, version.MessageKey,
version.AuthorKey, version.RevisionKey, version.RevisionOrderNumberKey).
Sort([]string{"-" + version.RevisionOrderNumberKey}))
if err != nil {
uis.LoggedError(w, r, http.StatusBadRequest, err)
return
}
data.Versions = versions
} else {
// patches
patches, err := patch.Find(patch.ByVersions(versionIds).
WithFields(patch.IdKey, patch.CreateTimeKey, patch.DescriptionKey, patch.AuthorKey))
if err != nil {
uis.LoggedError(w, r, http.StatusBadRequest, err)
return
}
data.Patches = patches
}
gimlet.WriteJSON(w, data)
}