forked from kubernetes/perf-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
359 lines (331 loc) · 12.2 KB
/
script.js
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var app = angular.module('PerfDashApp', ['ngMaterial', 'ngRoute', 'chart.js']);
var PerfDashApp = function(http, scope, route) {
this.http = http;
this.scope = scope;
this.route = route;
this.jobNames = [];
this.metricCategoryNames = [];
this.metricNames = [];
this.selectedLabels = {};
this.onClick = this.onClickInternal_.bind(this);
this.cap = 0;
this.currentCall = 0;
this.scope.$on('$routeChangeSuccess', this.routeChanged.bind(this));
this.lastCall = {jobname: "", metriccategoryname: "", metricname: "", time: Date.now()};
};
PerfDashApp.prototype.onClickInternal_ = function(data, evt, chart) {
console.log(this, data, evt, chart);
if (evt.ctrlKey) {
this.cap = (chart.scale.min + chart.scale.max) / 2;
this.labelChanged();
return;
}
this.setURLParameters();
this.http.get("config")
.success(function(config) {
window.open(config["storageURL"] + "/" +
config["logsBucket"] + "/" +
config["logsPath"] + "/" +
this.job + "/" +
data[0].label + "/",
"_blank");
}.bind(this))
.error(function(config) {
console.log("error fetching result");
console.log(config);
});
};
// Fetch data from the server and update the data to display
PerfDashApp.prototype.refresh = function() {
this.http.get("jobnames")
.success(function(data) {
this.jobNames = data;
//init jobName only if needed
if (this.jobName == undefined || this.jobNames.indexOf(this.jobName) == -1) {
this.jobName = this.jobNames[0];
}
this.jobNameChanged();
}.bind(this))
.error(function(data) {
console.log("error fetching result");
console.log(data);
});
};
// Update the select drop-downs based on the query params.
PerfDashApp.prototype.routeChanged = function(event, data) {
var app = this;
angular.forEach(this.route.current.params, function(value, name) {
switch (name) {
case "jobname":
if (app.jobName !== value) {
app.jobName = value;
app.jobNameChanged();
}
break;
case "metriccategoryname":
if (app.metricCategoryName !== value) {
app.metricCategoryName = value;
app.metricCategoryNameChanged();
}
break;
case "metricname":
if (app.metricName !== value) {
app.metricName = value;
app.metricNameChanged();
}
break;
default:
app.selectedLabels[name] = value;
}
});
this.labelChanged();
}
// Update the data to graph, using the selected jobName
PerfDashApp.prototype.jobNameChanged = function() {
this.setURLParameters();
this.http.get("metriccategorynames", {params: {jobname: this.jobName}})
.success(function(data) {
this.metricCategoryNames = data;
if (this.metricCategoryName == undefined || this.metricCategoryNames.indexOf(this.metricCategoryName) == -1) {
this.metricCategoryName = this.metricCategoryNames[0]
}
this.metricCategoryNameChanged();
}.bind(this))
.error(function(data) {
console.log("error fetching result");
console.log(data);
});
};
// Update the data to graph, using the selected jobName
PerfDashApp.prototype.metricCategoryNameChanged = function() {
this.setURLParameters();
this.http.get("metricnames", {params: {jobname: this.jobName, metriccategoryname: this.metricCategoryName}})
.success(function(data) {
this.metricNames = data;
if (this.metricName == undefined || this.metricNames.indexOf(this.metricName) == -1) {
this.metricName = this.metricNames[0]
}
this.metricNameChanged();
}.bind(this))
.error(function(data) {
console.log("error fetching result");
console.log(data);
});
};
// Update the data to graph, using the selected metricName
PerfDashApp.prototype.metricNameChanged = function() {
this.setURLParameters();
if (
this.lastCall.jobname == this.jobName &&
this.lastCall.metriccategoryname == this.metricCategoryName &&
this.lastCall.metricname == this.metricName &&
Date.now() < this.lastCall.time
) {
// Preventing initialization calls spamming.
return;
}
var callId = ++this.currentCall;
this.lastCall = {jobname: this.jobName, metriccategoryname: this.metricCategoryName, metricname: this.metricName, time: Date.now() + 1000};
this.http.get("buildsdata", {params: {jobname: this.jobName, metriccategoryname: this.metricCategoryName, metricname: this.metricName}})
.success(function(data) {
if (this.currentCall != callId) {
return;
}
this.data = data.builds;
this.job = data.job;
this.builds = this.getBuilds();
this.labels = this.getLabels();
this.labelChanged();
}.bind(this))
.error(function(data) {
console.log("error fetching result");
console.log(data);
});
};
// Update the data to graph, using selected labels
PerfDashApp.prototype.labelChanged = function() {
this.setURLParameters();
this.seriesData = [];
this.series = [];
result = this.getData(this.selectedLabels);
this.options = null;
var seriesLabels = new Array();
var a = 0;
for (; a < result.length; a++) {
if ("unit" in result[a] && "data" in result[a] && result[a].data != {}) {
// All the unit should be the same
this.options = {scaleLabel: "<%=value%> "+result[a].unit, animation: false};
// Start with higher percentiles, since their values are usually strictly higher
// than lower percentiles, which avoids obscuring graph data. It also orders data
// in the onHover labels more naturally.
seriesLabels = seriesLabels.concat(Object.keys(result[a].data));
}
}
seriesLabels = [...new Set(seriesLabels)]
seriesLabels.sort();
seriesLabels.reverse();
if(this.options == null) {
return;
}
angular.forEach(seriesLabels, function(name) {
this.seriesData.push(this.getStream(result, name));
this.series.push(name);
}, this);
this.cap = 0;
};
// Overwrite the URL query params with the current drop-down selections.
PerfDashApp.prototype.setURLParameters = function() {
var newParams = {};
// By setting all existing params to null in newParams, we will delete label
// parameters that do not apply to the current selection.
angular.forEach(this.route.current.params, function(ignore, name) {
newParams[name] = null;
});
newParams["jobname"] = this.jobName;
newParams["metriccategoryname"] = this.metricCategoryName;
newParams["metricname"] = this.metricName;
angular.forEach(this.selectedLabels, function(value, name) {
newParams[name] = value;
});
this.route.updateParams(newParams);
}
// Get all of the builds for the data set (e.g. build numbers)
PerfDashApp.prototype.getBuilds = function() {
return Object.keys(this.data)
};
// Verify if selected labels are in label set.
function verifySelectedLabels(selectedLabels, allLabels) {
if (selectedLabels == undefined || allLabels == undefined) {
return false;
}
if (Object.keys(selectedLabels).length != Object.keys(allLabels).length) {
return false;
}
var result = true;
angular.forEach(selectedLabels, function(value, key) {
if (!(key in allLabels) || !(value in allLabels[key])) {
result = false;
}
});
return result;
}
function isEmptySet(obj) {
var count = 0;
for (k in obj) {
if (obj.hasOwnProperty(k)) {
count++;
}
}
if (count == 0) {
return true
}
if (count == 1) {
if (obj.hasOwnProperty("")) {
return true
}
}
return false
}
// Get the set of all labels (e.g. 'resources', 'verbs') in the data set
PerfDashApp.prototype.getLabels = function() {
// Set is a map of every label name to a set of every label value.
var set = {};
angular.forEach(this.data, function(items, build) {
angular.forEach(items, function(item) {
angular.forEach(item.labels, function(label, name) {
if (set[name] == undefined) {
set[name] = {}
}
set[name][label] = true
});
});
});
angular.forEach(set, function(labels, name) {
if (isEmptySet(labels)) {
delete set[name]
}
});
if (!verifySelectedLabels(this.selectedLabels, set)) {
this.selectedLabels = {}
}
var labels = {};
angular.forEach(set, function(items, name) {
labels[name] = [];
angular.forEach(items, function(ignore, item) {
if (this.selectedLabels[name] == undefined) {
this.selectedLabels[name] = item;
}
labels[name].push(item)
}, this);
labels[name].sort()
}, this);
return labels;
};
// Extract a time series of data for specific labels
PerfDashApp.prototype.getData = function(labels) {
var result = [];
angular.forEach(this.data, function(items, build) {
var hasAnyResult = false;
angular.forEach(items, function(item) {
var match = true;
angular.forEach(labels, function(label, name) {
if (item.labels == undefined || item.labels[name] != label) {
match = false;
}
});
if (match) {
result.push(item);
hasAnyResult = true;
}
});
if (!hasAnyResult) {
// We need to add empty object so result series will still correspond to build series
result.push({});
}
});
return result;
};
// Given a slice of data, turn it into a time series of numbers
// 'data' is an array of APICallLatency objects
// 'stream' is a selector for latency data, (e.g. 'Perc50')
PerfDashApp.prototype.getStream = function(data, stream) {
var result = [];
angular.forEach(data, function(value) {
var x = undefined
if ("data" in value) {
x = value.data[stream];
}
//This is a handling for undefined values which cause chart.js to not display plots
//TODO(krzysied): Check whether new version of chart.js has support for this case
if (x == undefined) {
x = 0;
}
if (this.cap != 0 && x > this.cap) {
x = this.cap;
}
result.push(x);
}, this);
return result;
};
app.controller('AppCtrl', ['$scope', '$http', '$interval', '$route', function($scope, $http, $interval, $route) {
$scope.controller = new PerfDashApp($http, $scope, $route);
$scope.controller.refresh();
// Refresh every 10 min. The data only refreshes every 10 minutes on the server
$interval($scope.controller.refresh.bind($scope.controller), 600000)
}]);
// Add a dummy route so that we can manipulate URL params.
app.config(function($routeProvider) {
$routeProvider.when('/', {})
});