-
Notifications
You must be signed in to change notification settings - Fork 0
/
perf_dashboard.go
220 lines (193 loc) · 6.35 KB
/
perf_dashboard.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
package perfdash
import (
"fmt"
"html/template"
"io/ioutil"
"net/http"
"path/filepath"
"github.com/evergreen-ci/evergreen/model"
"github.com/evergreen-ci/evergreen/model/version"
"github.com/evergreen-ci/evergreen/plugin"
"github.com/evergreen-ci/evergreen/util"
"github.com/gorilla/mux"
"github.com/mitchellh/mapstructure"
)
const (
perfDashboardPluginName = "dashboard"
)
func init() {
plugin.Publish(&PerfDashboardPlugin{})
}
var includes = []template.HTML{
`<script type="text/javascript" src="/plugin/dashboard/static/js/dashboard.js"></script>`,
`<link href="/plugin/dashboard/static/css/dashboard.css" rel="stylesheet"/>`,
}
// PerfDashboardPlugin displays performance statistics in the UI.
// Branches is a map of the branch name to the list of project names
// associated with that branch.
type PerfDashboardPlugin struct {
Branches map[string][]string `yaml:"branches"`
}
// DashboardAppData is the data that is returned from calling the app level data function
// Branches is a mapping from a branch name to the projects in that branch
// DefaultBranch is the branch that should show up if it exists.
// DefaultBaselines is a mapping from project to baseline that is default
type DashboardAppData struct {
Branches map[string][]string `json:"branches"`
DefaultBranch string `json:"default_branch"`
DefaultBaselines map[string]string `json:"default_baselines"`
}
// Name implements Plugin Interface.
func (pdp *PerfDashboardPlugin) Name() string {
return perfDashboardPluginName
}
func (pdp *PerfDashboardPlugin) GetUIHandler() http.Handler {
r := mux.NewRouter()
r.HandleFunc("/tasks/project/{project_id}/version/{version_id}", getTasksForVersion)
return r
}
func (pdp *PerfDashboardPlugin) GetAppPluginInfo() *plugin.UIPage {
data := func(context plugin.UIContext) (interface{}, error) {
defaultBranch := context.Request.FormValue("branch")
defaultBaselines := map[string]string{}
for _, projects := range pdp.Branches {
for _, projectName := range projects {
defaultBaselines[projectName] = context.Request.FormValue(projectName)
}
}
dashboardData := DashboardAppData{
DefaultBranch: defaultBranch,
DefaultBaselines: defaultBaselines,
Branches: pdp.Branches,
}
return dashboardData, nil
}
return &plugin.UIPage{"perf_dashboard.html", data}
}
func (pdp *PerfDashboardPlugin) Configure(params map[string]interface{}) error {
err := mapstructure.Decode(params, pdp)
if err != nil {
return fmt.Errorf("error decoding %v params: %v", pdp.Name(), err)
}
return nil
}
func (pdp *PerfDashboardPlugin) GetPanelConfig() (*plugin.PanelConfig, error) {
dashboardHTML, err := ioutil.ReadFile(filepath.Join(plugin.TemplateRoot(pdp.Name()), "version_perf_dashboard.html"))
if err != nil {
return nil, fmt.Errorf("Can't load version panel file html %v", err)
}
return &plugin.PanelConfig{
Panels: []plugin.UIPanel{
{
Includes: includes,
Page: plugin.VersionPage,
Position: plugin.PageCenter,
PanelHTML: template.HTML(dashboardHTML),
DataFunc: func(context plugin.UIContext) (interface{}, error) {
exists := false
for _, projects := range pdp.Branches {
if util.SliceContains(projects, context.ProjectRef.Identifier) {
exists = true
break
}
}
return struct {
Enabled bool `json:"enabled"`
}{exists}, nil
},
},
},
}, nil
}
func getTasksForVersion(w http.ResponseWriter, r *http.Request) {
projectId := mux.Vars(r)["project_id"]
versionId := mux.Vars(r)["version_id"]
if projectId == "" {
http.Error(w, "empty project id", http.StatusBadRequest)
return
}
if versionId == "" {
http.Error(w, "empty version id", http.StatusBadRequest)
return
}
projectRef, err := model.FindOneProjectRef(projectId)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if projectRef == nil {
http.Error(w, "empty project ref", http.StatusNotFound)
return
}
v, err := version.FindOne(version.ById(versionId).WithFields(version.RevisionKey))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if v == nil {
http.Error(w, "empty version", http.StatusNotFound)
return
}
project, err := model.FindProject(v.Revision, projectRef)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if project == nil {
http.Error(w, "empty project", http.StatusNotFound)
return
}
if len(project.Tasks) == 0 {
http.Error(w, fmt.Sprintf("no project tasks for project %v with revision %v", projectRef.Identifier, v.Revision),
http.StatusBadRequest)
return
}
taskMap := getVariantsWithCommand("json.send", project)
plugin.WriteJSON(w, http.StatusOK, taskMap)
}
// hasCommand returns true if the command name exists in the command
// or in the functions that may be in the command.
func hasCommand(commandName string, command model.PluginCommandConf, project *model.Project) bool {
exists := false
if command.Function != "" {
for _, c := range project.Functions[command.Function].List() {
exists = exists || hasCommand(commandName, c, project)
}
} else {
exists = (command.Command == commandName && command.Params["name"] == perfDashboardPluginName)
}
return exists
}
// createTaskCacheForCommand returns a map of tasks that have the command
func createTaskCacheForCommand(commandName string, project *model.Project) map[string]struct{} {
tasks := map[string]struct{}{}
for _, t := range project.Tasks {
for _, command := range t.Commands {
if hasCommand(commandName, command, project) {
tasks[t.Name] = struct{}{}
break
}
}
}
return tasks
}
// getVariantsWithCommand creates a cache of all tasks that have a command name
// and then iterates over all build variants to check if the task is in the cache,
// adds the bv name to a map which is returned as a mapping of the task name to the build variants.
func getVariantsWithCommand(commandName string, project *model.Project) map[string][]string {
taskCache := createTaskCacheForCommand(commandName, project)
buildVariants := map[string][]string{}
for _, bv := range project.BuildVariants {
for _, t := range bv.Tasks {
if _, ok := taskCache[t.Name]; ok {
variants, ok := buildVariants[t.Name]
if !ok {
buildVariants[t.Name] = []string{bv.Name}
} else {
buildVariants[t.Name] = append(variants, bv.Name)
}
}
}
}
return buildVariants
}