forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildTrendsChart.js
306 lines (277 loc) · 9.25 KB
/
buildTrendsChart.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
"use strict";
angular.module('openshiftConsole')
.directive('buildTrendsChart', function($filter, $location, $rootScope, $timeout) {
return {
restrict: 'E',
scope: {
builds: '='
},
templateUrl: 'views/_build-trends-chart.html',
link: function($scope) {
var buildByNumber;
var completePhases = ['Complete', 'Failed', 'Cancelled', 'Error'];
// Minimum number of builds to show.
$scope.minBuilds = _.constant(4);
// Simple humanize function that returns an abbreviated duration like "1h 4m" or "2m 4s"
// suitable for use on the chart y-axis.
var humanize = function(value) {
var result = [];
var duration = moment.duration(value);
var hours = Math.floor(duration.asHours());
var minutes = duration.minutes();
var seconds = duration.seconds();
if (!hours && !minutes && !seconds) {
return '';
}
if (hours) {
result.push(hours + "h");
}
if (minutes) {
result.push(minutes + "m");
}
// Only show seconds if not duration doesn't include hours.
// Always show seconds otherwise (even 0s).
if (!hours) {
result.push(seconds + "s");
}
return result.join(" ");
};
var getStartTimestsamp = function(build) {
return build.status.startTimestamp || build.metadata.creationTimestamp;
};
// Chart configuration, see http://c3js.org/reference.html
$scope.chartID = _.uniqueId('build-trends-chart-');
var animationDuration = _.constant(350);
var config = {
bindto: '#' + $scope.chartID,
padding: {
right: 30,
// Prevent problems with the y-axis label getting cut off on updates.
left: 80
},
axis: {
x: {
fit: true,
label: {
text: 'Build Number',
position: 'outer-right'
},
tick: {
culling: true,
fit: true,
format: function(x) {
return '#' + x;
}
}
},
y: {
label: {
text: 'Duration',
position: 'outer-top'
},
min: 0,
padding: {
bottom: 0
},
tick: {
count: 5,
culling: true,
fit: true,
format: humanize
}
}
},
bar: {
width: {
max: 50
}
},
legend: {
item: {
// Disable clicking to filter groups
onclick: _.noop
}
},
size: {
height: 200
},
tooltip: {
format: {
title: function(x) {
var build = buildByNumber[x];
var startTimestamp = getStartTimestsamp(build);
return '#' + x + ' (' + moment(startTimestamp).fromNow() + ')';
}
}
},
transition: {
duration: animationDuration()
},
data: {
// https://www.patternfly.org/styles/color-palette/
colors: {
Cancelled: "#d1d1d1",
Complete: "#00b9e4",
Error: "#393f44",
Failed: "#cc0000"
},
empty: {
label: {
text: "No Completed Builds"
}
},
onclick: function(d) {
var build = buildByNumber[d.x];
var url = $filter('navigateResourceURL')(build);
if (url) {
$rootScope.$apply(function() {
$location.path(url);
});
}
},
selection: {
enabled: true
},
type: 'bar'
}
};
var updateCompleteBuilds = function() {
$scope.completeBuilds = [];
var isIncomplete = $filter('isIncompleteBuild');
angular.forEach($scope.builds, function(build) {
if (!isIncomplete(build)) {
$scope.completeBuilds.push(build);
}
});
};
var numCompleteBuilds = function() {
updateCompleteBuilds();
return $scope.completeBuilds.length;
};
var annotationFilter = $filter('annotation');
var getBuildNumber = function(build) {
var buildNumber = annotationFilter(build, 'buildNumber') || parseInt(build.metadata.name.match(/(\d+)$/), 10);
if (isNaN(buildNumber)) {
return null;
}
return buildNumber;
};
var getBuildDuration = function(build) {
var startTimestamp = getStartTimestsamp(build);
var endTimestamp = build.status.completionTimestamp;
if (!startTimestamp || !endTimestamp) {
return 0;
}
return moment(endTimestamp).diff(moment(startTimestamp));
};
var chart, averageDuration, showAverageLine = false;
var updateAvgLine = function() {
if (averageDuration && showAverageLine) {
chart.ygrids([{
value: averageDuration,
'class': 'build-trends-avg-line'
}]);
} else {
chart.ygrids.remove();
}
};
$scope.toggleAvgLine = function() {
showAverageLine = !showAverageLine;
updateAvgLine();
};
var update = function() {
// Keep a map of builds by number so we can find the build later when a data point is clicked.
buildByNumber = {};
var data = {
json: [],
keys: {
x: 'buildNumber'
}
};
var sum = 0, count = 0;
angular.forEach($scope.completeBuilds, function(build) {
var buildNumber = getBuildNumber(build);
if (!buildNumber) {
return;
}
var duration = getBuildDuration(build);
// Track the sum and count to calculate the average duration.
sum += duration;
count++;
var buildData = {
buildNumber: buildNumber,
phase: build.status.phase
};
buildData[build.status.phase] = duration;
data.json.push(buildData);
buildByNumber[buildNumber] = build;
});
// Show only the last 50 builds.
if (data.json.length > 50) {
data.json.sort(function(a, b) {
return a.buildNumber - b.buildNumber;
});
data.json = data.json.slice(data.json.length - 50);
}
// Check for found phases only after we've sliced the array.
var foundPhases = {};
angular.forEach(data.json, function(buildData) {
foundPhases[buildData.phase] = true;
});
// Calculate the average duration.
// TODO: Should we only show the average for the last 50 builds
// instead of all builds?
if (count) {
averageDuration = sum / count;
$scope.averageDurationText = humanize(averageDuration);
} else {
averageDuration = null;
$scope.averageDurationText = null;
}
var groups = [], unload = [];
angular.forEach(completePhases, function(phase) {
if (foundPhases[phase]) {
// Only show found phases in the chart legend.
groups.push(phase);
} else {
// Unload any groups not found to remove them from the chart.
// This can happen for filters, deleted builds, or if a build
// phase is no longer in the last 50.
unload.push(phase);
}
});
data.keys.value = groups;
data.groups = [groups];
if (!chart) {
config.data = angular.extend(data, config.data);
// Allow the rest of the page to load so c3 calculates the correct
// width for the chart based on the parent width.
$timeout(function() {
chart = c3.generate(config);
updateAvgLine();
});
} else {
data.unload = unload;
// Call flush to work around a c3.js bug where the y-axis label
// overlaps the tick values when the data changes.
data.done = function() {
// done() is called before the chart animation finishes.
// Wait until the animation is complete to call flush().
setTimeout(function() {
chart.flush();
}, animationDuration() + 25);
};
chart.load(data);
updateAvgLine();
}
};
$scope.$watch(numCompleteBuilds, update);
$scope.$on('destroy', function() {
if (chart) {
// http://c3js.org/reference.html#api-destroy
chart = chart.destroy();
}
});
}
};
});