-
Notifications
You must be signed in to change notification settings - Fork 124
/
models.go
204 lines (177 loc) · 6 KB
/
models.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
package service
import (
"context"
"html/template"
"time"
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/evergreen/apimodels"
"github.com/evergreen-ci/evergreen/model"
"github.com/evergreen-ci/evergreen/model/build"
"github.com/evergreen-ci/evergreen/model/distro"
"github.com/evergreen-ci/evergreen/model/host"
"github.com/evergreen-ci/evergreen/model/patch"
"github.com/evergreen-ci/evergreen/model/task"
"github.com/evergreen-ci/evergreen/plugin"
"github.com/mongodb/grip"
"github.com/pkg/errors"
)
type hostsData struct {
Hosts []uiHost
}
type pluginData struct {
Includes []template.HTML
Panels plugin.PanelLayout
Data map[string]interface{}
}
type uiVersion struct {
Version model.Version
Builds []uiBuild
PatchInfo *uiPatch `json:",omitempty"`
ActiveTasks int
RepoOwner string `json:"repo_owner"`
Repo string `json:"repo_name"`
UpstreamData *uiUpstreamData `json:"upstream,omitempty"`
TimeTaken time.Duration `json:"time_taken"`
Makespan time.Duration `json:"makespan"`
}
type uiUpstreamData struct {
Owner string `json:"owner"`
Repo string `json:"repo"`
Revision string `json:"revision"`
ProjectName string `json:"project_name"`
TriggerID string `json:"trigger_id"`
TriggerType string `json:"trigger_type"`
}
type uiPatch struct {
Patch patch.Patch
StatusDiffs interface{}
// only used on task pages
BaseTimeTaken time.Duration `json:"base_time_taken"`
// for linking to other pages
BaseVersionId string
BaseBuildId string
BaseTaskId string
}
type uiHost struct {
Host host.Host
RunningTask *task.Task
IdleTime float64 // idle time in seconds
}
type uiBuild struct {
Build build.Build
Version model.Version
PatchInfo *uiPatch `json:",omitempty"`
Tasks []uiTask
Elapsed time.Duration
TimeTaken time.Duration `json:"time_taken"`
Makespan time.Duration `json:"makespan"`
CurrentTime int64
RepoOwner string `json:"repo_owner"`
Repo string `json:"repo_name"`
TaskStatusCount task.TaskStatusCount `json:"taskStatusCount"`
UpstreamData *uiUpstreamData `json:"upstream_data,omitempty"`
}
type uiTask struct {
Task task.Task
Gitspec string
BuildDisplay string
TaskLog []apimodels.LogMessage
NextTasks []task.Task
PreviousTasks []task.Task
Elapsed time.Duration
StartTime int64
FailedTestNames []string `json:"failed_test_names"`
ExpectedDuration time.Duration `json:"expected_duration"`
}
///////////////////////////////////////////////////////////////////////////
//// Functions to create and populate the models
///////////////////////////////////////////////////////////////////////////
// getBuildVariantHistory returns a slice of builds that surround a given build.
// As many as 'before' builds (less recent builds) plus as many as 'after' builds
// (more recent builds) are returned.
func getBuildVariantHistory(buildId string, before int, after int) ([]build.Build, error) {
b, err := build.FindOne(build.ById(buildId))
if err != nil {
return nil, errors.WithStack(err)
}
if b == nil {
return nil, errors.Errorf("no build with id %v", buildId)
}
lessRecentBuilds, err := build.Find(
build.ByBeforeRevision(b.Project, b.BuildVariant, b.RevisionOrderNumber).
WithFields(build.IdKey, build.TasksKey, build.StatusKey, build.VersionKey, build.ActivatedKey).
Limit(before))
if err != nil {
return nil, errors.WithStack(err)
}
moreRecentBuilds, err := build.Find(
build.ByAfterRevision(b.Project, b.BuildVariant, b.RevisionOrderNumber).
WithFields(build.IdKey, build.TasksKey, build.StatusKey, build.VersionKey, build.ActivatedKey).
Limit(after))
if err != nil {
return nil, errors.WithStack(err)
}
builds := make([]build.Build, 0, len(lessRecentBuilds)+len(moreRecentBuilds))
for i := len(moreRecentBuilds); i > 0; i-- {
builds = append(builds, moreRecentBuilds[i-1])
}
builds = append(builds, lessRecentBuilds...)
return builds, nil
}
// Given build id, get last successful build before this one
func getBuildVariantHistoryLastSuccess(buildId string) (*build.Build, error) {
b, err := build.FindOne(build.ById(buildId))
if err != nil {
return nil, errors.WithStack(err)
}
if b.Status == evergreen.BuildSucceeded {
return b, nil
}
b, err = b.PreviousSuccessful()
return b, errors.WithStack(err)
}
func getHostsData(ctx context.Context, includeSpawnedHosts bool) (*hostsData, error) {
dbHosts, err := host.FindRunningHosts(ctx, includeSpawnedHosts)
if err != nil {
return nil, errors.Wrap(err, "problem finding hosts")
}
data := &hostsData{}
// convert the hosts to the ui models
uiHosts := make([]uiHost, len(dbHosts))
for idx, dbHost := range dbHosts {
// we only need the distro id for the hosts page
dbHost.Distro = distro.Distro{Id: dbHost.Distro.Id}
host := uiHost{
Host: dbHost,
RunningTask: nil,
}
uiHosts[idx] = host
// get the task running on this host
if dbHost.RunningTaskFull != nil {
uiHosts[idx].RunningTask = dbHost.RunningTaskFull
}
uiHosts[idx].IdleTime = host.Host.IdleTime().Seconds()
}
data.Hosts = uiHosts
return data, nil
}
// getPluginDataAndHTML returns all data needed to properly render plugins
// for a page. It logs errors but does not return them, as plugin errors
// cannot stop the rendering of the rest of the page
func getPluginDataAndHTML(pluginManager plugin.PanelManager, page plugin.PageScope, ctx plugin.UIContext) pluginData {
includes, err := pluginManager.Includes(page)
if err != nil {
grip.Errorf("error getting include html from plugin manager on %v page: %v",
page, err)
}
panels, err := pluginManager.Panels(page)
if err != nil {
grip.Errorf("error getting panel html from plugin manager on %v page: %v",
page, err)
}
data, err := pluginManager.UIData(ctx, page)
if err != nil {
grip.Errorf("error getting plugin data on %v page: %+v", page, err)
}
return pluginData{includes, panels, data}
}