This repository has been archived by the owner on Jan 25, 2022. It is now read-only.
/
ui.js
358 lines (297 loc) · 11.1 KB
/
ui.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
function refreshJobs(jenkins_view_url) {
var JOB_STAT_TYPE = {
status: 1,
build_stability: 2,
last_failed: 3,
duration: 4
};
jenkins_view_url += '/api/json?tree=jobs[name,color,url,lastFailedBuild[timestamp],lastSuccessfulBuild[timestamp,duration],healthReport[description,score]]&jsonp=?';
$.getJSON(jenkins_view_url, function(data) {
// Iterate through each Jenkins job
$.each(data.jobs, function(index, job_data) {
// Each Jenkins job is a div.content_box
var view_content_box = $('<div/>', {
class: 'content_box job',
});
// 27 is roughly the # of letters that can fit on one line
var tiny = (job_data.name.length > 25) ? 'tiny' : '';
// Render the title of the Jenkins job
var view_title = '<h2 class="' + tiny + '"><a href="' + job_data.url + '">' + job_data.name + '</a></h2>';
view_content_box.append(view_title);
var job_build_stability_pct = 0, job_status = mapColorToStatus(job_data.color);
$.each(job_data.healthReport, function(index, healthReport_data) {
// Parse out the build stability
var re = /Build stability/;
var match = re.exec(healthReport_data.description);
if (match && match.length > 0) {
job_build_stability_pct = parseInt(healthReport_data.score);
}
});
// Calculate time delta in sec, of last failed build
var last_failed_build_time_delta = job_data.lastFailedBuild ? getTimeDeltaToNow(job_data.lastFailedBuild.timestamp/1000) : null;
// Calculate duration
var duration = job_data.lastSuccessfulBuild ? job_data.lastSuccessfulBuild.duration : null;
// Add the job_info_boxes
buildJobInfoBox(JOB_STAT_TYPE.status, job_status).appendTo(view_content_box);
buildJobInfoBox(JOB_STAT_TYPE.build_stability, job_build_stability_pct).appendTo(view_content_box);
buildJobInfoBox(JOB_STAT_TYPE.last_failed, last_failed_build_time_delta).appendTo(view_content_box);
buildJobInfoBox(JOB_STAT_TYPE.duration, duration).appendTo(view_content_box);
view_content_box.appendTo('div.content_box_container');
});
});
function getAlertLevel(view_stat_type, value) {
if (!value) {
// The job has never failed, so no value is a good thing
if (view_stat_type == JOB_STAT_TYPE.last_failed)
return 'good';
// For all other job_stat_type's, no value is a bad thing
else
return 'bad';
}
// Handle the job status string
if (view_stat_type == JOB_STAT_TYPE.status) {
value = value.toLowerCase();
if (value == 'success') {
return 'good';
} else if ((value == 'unstable') || (value == 'disabled')) {
return 'warning';
} else {
return 'bad';
}
}
// Handle other numeric stat values
var thresholds = {};
thresholds[JOB_STAT_TYPE.build_stability] = {
'warning': 80,
'good': 90
};
thresholds[JOB_STAT_TYPE.last_failed] = {
'warning': 60 * 60 * 12,
'good': 60 * 60 * 24
};
thresholds[JOB_STAT_TYPE.duration] = {
'warning': 90,
'good': 1
};
if (value >= thresholds[view_stat_type]['good']) {
return 'good';
} else if ((value >= thresholds[view_stat_type]['warning']) && (value < thresholds[view_stat_type]['good'])) {
return 'warning';
} else if (value < thresholds[view_stat_type]['warning']) {
return 'bad';
}
}
function buildJobInfoBox(view_stat_type, value) {
var stat_label, stat_value, stat_alert = getAlertLevel(view_stat_type, value);
switch (view_stat_type) {
case JOB_STAT_TYPE.status:
stat_label = 'Status';
stat_value = value;
break;
case JOB_STAT_TYPE.build_stability:
stat_label = 'Build Stability';
stat_value = isNaN(value) ? '-' : value + ' %';
break;
case JOB_STAT_TYPE.last_failed:
stat_label = 'Last Failed';
stat_value = !value ? '-' : getTimeDeltaStr(value);
break;
case JOB_STAT_TYPE.duration:
stat_label = 'Duration';
stat_value = !value ? '-' : getTimeDeltaStr(value/1000);
break;
default:
stat_label = '?';
stat_value = '?';
break;
}
var job_info_box = $('<div/>', {
class: 'job_info_box ' + stat_alert,
});
var value = $('<p/>', {
text: stat_value,
class: 'job_stat_value ' + stat_alert,
}).appendTo(job_info_box);
var label = $('<p/>', {
text: stat_label,
class: 'job_stat_name',
}).appendTo(job_info_box);
return job_info_box;
}
}
function refreshViews() {
var VIEW_STAT_TYPE = {
passed: 1,
coverage: 2,
last_failed: 3,
duration: 4
};
var config = new Config();
var jenkins_root_url = config.JenkinsRootUrl();
jenkins_root_url += '/api/json?tree=views[name,url,jobs[name,color,url,lastFailedBuild[timestamp],healthReport[description,score]]]&jsonp=?';
$.getJSON(jenkins_root_url, function(data) {
// Iterate through each Jenkins tab (aka 'view')
$.each(data.views, function(index, view_data) {
// To avoid overloading the UI, skip any Jenkins view named 'All'
if (view_data.name.indexOf('All') >= 0) {
return true;
}
var view_jobs_coverage = [];
var view_jobs_num_failing_tests = 0;
var view_jobs_num_total_tests = 0;
var view_jobs_list = [];
var last_failed_builds = [];
// Each Jenkins view is a div.content_box
var view_content_box = $('<div/>', {
class: 'content_box',
});
// Render the title of the Jenkins view
var view_title = '<h2 class="jenkins_view_url"><a href="' + view_data.url + '">' + view_data.name + '</a></h2>';
view_content_box.append(view_title);
// Iterate through each job of the Jenkins view, and aggregate job info (ie. coverage, failing tests)
$.each(view_data.jobs, function(index, job_data) {
// Skip disabled jobs + jobs with specified ommitted prefixes
if ((job_data.name.indexOf('DELENG') < 0) && (mapColorToStatus(job_data.color) != 'Disabled')) {
if (job_data.lastFailedBuild) {
last_failed_builds.push(job_data.lastFailedBuild.timestamp / 1000);
}
view_jobs_list.push({
name: job_data.name,
status: mapColorToStatus(job_data.color),
url: job_data.url
});
$.each(job_data.healthReport, function(index, healthReport_data) {
// Parse out the code coverage
var re = /\((\S+)\)/;
var match = re.exec(healthReport_data.description);
if (match && match.length > 0) {
view_jobs_coverage.push(match[1]);
}
// Parse out the test results
var re = /Test Result: (\d+) tests* failing out of a total of (\d+) tests*/;
var match = re.exec(healthReport_data.description);
if (match && match.length > 1) {
view_jobs_num_failing_tests += parseInt(match[1]);
view_jobs_num_total_tests += parseInt(match[2]);
}
});
}
});
// Calculate time delta in sec, of last failed build string
var last_failed_build = (last_failed_builds.length > 0) ? _.max(last_failed_builds) : null;
var last_failed_build_time_delta = last_failed_build ? getTimeDeltaToNow(last_failed_build) : null;
// Calculate average coverage string
var coverage_sum = _.reduce(view_jobs_coverage, function(memo, num) {
return memo + parseFloat(num, 10);
}, 0);
var coverage_avg_pct = coverage_sum / view_jobs_coverage.length;
// Calculate test pass percentage
var passed_pct = ((view_jobs_num_total_tests - view_jobs_num_failing_tests) / view_jobs_num_total_tests * 100);
// Add the job_info_boxes (passed %, coverage %, last failed, duration)
buildJobInfoBox(VIEW_STAT_TYPE.passed, passed_pct).appendTo(view_content_box);
buildJobInfoBox(VIEW_STAT_TYPE.coverage, coverage_avg_pct).appendTo(view_content_box);
buildJobInfoBox(VIEW_STAT_TYPE.last_failed, last_failed_build_time_delta).appendTo(view_content_box);
buildJobInfoBox(VIEW_STAT_TYPE.duration, null).appendTo(view_content_box);
var job_boxes_container = $('<div/>', {
class: 'job_boxes_container clear',
});
// Add the job status boxes
$.each(view_jobs_list, function(index, view_job) {
$('<a/>', {
class: 'job_status',
title: view_job.name + ' - ' + view_job.status,
href: view_job.url,
target: '_blank'
}).addClass('status-' + view_job.status.toLowerCase()).appendTo(job_boxes_container);
});
job_boxes_container.appendTo(view_content_box);
view_content_box.appendTo('div.content_box_container');
view_content_box.on('click', function() {
refreshUI(view_data.url);
});
});
});
function getAlertLevel(view_stat_type, value) {
if (!value) {
return 'bad';
}
var thresholds = {};
thresholds[VIEW_STAT_TYPE.passed] = {
'warning': 90,
'good': 100
};
thresholds[VIEW_STAT_TYPE.coverage] = {
'warning': 60,
'good': 70
};
thresholds[VIEW_STAT_TYPE.last_failed] = {
'warning': 60 * 60 * 12,
'good': 60 * 60 * 24
};
thresholds[VIEW_STAT_TYPE.duration] = {
'warning': 90,
'good': 100
};
if (value >= thresholds[view_stat_type]['good']) {
return 'good';
} else if ((value >= thresholds[view_stat_type]['warning']) && (value < thresholds[view_stat_type]['good'])) {
return 'warning';
} else if (value < thresholds[view_stat_type]['warning']) {
return 'bad';
}
}
function buildJobInfoBox(view_stat_type, value) {
var stat_label, stat_value, stat_alert = getAlertLevel(view_stat_type, value);
switch (view_stat_type) {
case VIEW_STAT_TYPE.passed:
stat_label = 'Passed';
stat_value = isNaN(value) ? '-' : value.toFixed(1) + ' %';
break;
case VIEW_STAT_TYPE.coverage:
stat_label = 'Coverage';
stat_value = isNaN(value) ? '-' : value.toFixed(1) + ' %';
break;
case VIEW_STAT_TYPE.last_failed:
stat_label = 'Last Failed';
stat_value = !value ? '-' : getTimeDeltaStr(value);
break;
case VIEW_STAT_TYPE.duration:
stat_label = 'Duration';
stat_value = !value ? '-' : value;
break;
default:
stat_label = '?';
stat_value = '?';
break;
}
var job_info_box = $('<div/>', {
class: 'job_info_box ' + stat_alert,
});
var value = $('<p/>', {
text: stat_value,
class: 'job_stat_value ' + stat_alert,
}).appendTo(job_info_box);
var label = $('<p/>', {
text: stat_label,
class: 'job_stat_name',
}).appendTo(job_info_box);
return job_info_box;
}
}
function mapColorToStatus(color) {
// Map Jenkins status colors -> status
var MAP_COLOR_TO_STATUS = {
blue: 'Success',
blue_anime: 'Success',
yellow: 'Unstable',
yellow_anime: 'Unstable',
red: 'Failed',
red_anime: 'Failed',
disabled: 'Disabled',
grey: 'Disabled',
grey_anime: 'Disabled',
aborted: 'Disabled',
aborted_anime: 'Disabled',
};
return MAP_COLOR_TO_STATUS[color];
}