blob: c7e23b20ae49e0b308e8476c08ecf2459f0a4fb7 [file] [log] [blame]
Joshua Hesketh4db99cb2014-05-23 11:28:08 +10001// jquery plugin for Zuul status page
Timo Tijhof51516cd2013-04-09 01:32:29 +02002//
Tristan Cacqueray48ddd9e2017-05-21 10:19:02 +00003// @licstart The following is the entire license notice for the
4// JavaScript code in this page.
5//
Timo Tijhof51516cd2013-04-09 01:32:29 +02006// Copyright 2012 OpenStack Foundation
7// Copyright 2013 Timo Tijhof
8// Copyright 2013 Wikimedia Foundation
Joshua Hesketh6b1a2182014-03-21 14:40:04 +11009// Copyright 2014 Rackspace Australia
Timo Tijhof51516cd2013-04-09 01:32:29 +020010//
11// Licensed under the Apache License, Version 2.0 (the "License"); you may
12// not use this file except in compliance with the License. You may obtain
13// a copy of the License at
14//
15// http://www.apache.org/licenses/LICENSE-2.0
16//
17// Unless required by applicable law or agreed to in writing, software
18// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
20// License for the specific language governing permissions and limitations
21// under the License.
Tristan Cacqueray48ddd9e2017-05-21 10:19:02 +000022//
23// @licend The above is the entire license notice
24// for the JavaScript code in this page.
Timo Tijhof51516cd2013-04-09 01:32:29 +020025
26(function ($) {
Timo Tijhof4be9f742015-04-02 01:13:19 +010027 'use strict';
28
Joshua Heskethace48892014-03-22 17:18:31 +110029 function set_cookie(name, value) {
Monty Taylor860bb0a2014-03-22 09:41:25 -070030 document.cookie = name + '=' + value + '; path=/';
Joshua Heskethace48892014-03-22 17:18:31 +110031 }
32
33 function read_cookie(name, default_value) {
Monty Taylor860bb0a2014-03-22 09:41:25 -070034 var nameEQ = name + '=';
Joshua Heskethace48892014-03-22 17:18:31 +110035 var ca = document.cookie.split(';');
36 for(var i=0;i < ca.length;i++) {
37 var c = ca[i];
Monty Taylor860bb0a2014-03-22 09:41:25 -070038 while (c.charAt(0) === ' ') {
39 c = c.substring(1, c.length);
40 }
41 if (c.indexOf(nameEQ) === 0) {
Joshua Heskethace48892014-03-22 17:18:31 +110042 return c.substring(nameEQ.length, c.length);
43 }
44 }
45 return default_value;
46 }
47
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +100048 $.zuul = function(options) {
Timo Tijhof4be9f742015-04-02 01:13:19 +010049 options = $.extend({
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +100050 'enabled': true,
51 'graphite_url': '',
52 'source': 'status.json',
53 'msg_id': '#zuul_msg',
54 'pipelines_id': '#zuul_pipelines',
55 'queue_events_num': '#zuul_queue_events_num',
56 'queue_results_num': '#zuul_queue_results_num',
57 }, options);
Joshua Heskethace48892014-03-22 17:18:31 +110058
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +100059 var collapsed_exceptions = [];
Joshua Hesketh6f94dd12014-08-20 16:14:53 +100060 var current_filter = read_cookie('zuul_filter_string', '');
Jan Hrubanddeb95a2017-01-03 15:12:41 +010061 var change_set_in_url = window.location.href.split('#')[1];
62 if (change_set_in_url) {
63 current_filter = change_set_in_url;
64 }
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +100065 var $jq;
Timo Tijhof51516cd2013-04-09 01:32:29 +020066
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +100067 var xhr,
68 zuul_graph_update_count = 0,
69 zuul_sparkline_urls = {};
Joshua Hesketh9d013542014-04-03 13:08:04 +110070
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +100071 function get_sparkline_url(pipeline_name) {
72 if (options.graphite_url !== '') {
73 if (!(pipeline_name in zuul_sparkline_urls)) {
74 zuul_sparkline_urls[pipeline_name] = $.fn.graphite
75 .geturl({
76 url: options.graphite_url,
77 from: "-8hours",
78 width: 100,
79 height: 26,
80 margin: 0,
81 hideLegend: true,
82 hideAxes: true,
83 hideGrid: true,
84 target: [
85 "color(stats.gauges.zuul.pipeline." + pipeline_name
Timo Tijhof4be9f742015-04-02 01:13:19 +010086 + ".current_changes, '6b8182')"
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +100087 ]
Joshua Hesketh298c4912014-03-20 16:06:25 +110088 });
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +100089 }
90 return zuul_sparkline_urls[pipeline_name];
91 }
92 return false;
93 }
Joshua Hesketh298c4912014-03-20 16:06:25 +110094
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +100095 var format = {
Joshua Hesketh6b1a2182014-03-21 14:40:04 +110096 job: function(job) {
Joshua Hesketh9e6ce532014-04-01 13:11:53 +110097 var $job_line = $('<span />');
98
Joshua Hesketh6b1a2182014-03-21 14:40:04 +110099 if (job.url !== null) {
Joshua Hesketh9e6ce532014-04-01 13:11:53 +1100100 $job_line.append(
101 $('<a />')
102 .addClass('zuul-job-name')
103 .attr('href', job.url)
104 .text(job.name)
105 );
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100106 }
Joshua Hesketh9e6ce532014-04-01 13:11:53 +1100107 else {
108 $job_line.append(
109 $('<span />')
110 .addClass('zuul-job-name')
111 .text(job.name)
112 );
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100113 }
Joshua Hesketh9e6ce532014-04-01 13:11:53 +1100114
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000115 $job_line.append(this.job_status(job));
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100116
117 if (job.voting === false) {
118 $job_line.append(
Joshua Hesketh9e6ce532014-04-01 13:11:53 +1100119 $(' <small />')
120 .addClass('zuul-non-voting-desc')
121 .text(' (non-voting)')
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100122 );
123 }
Joshua Hesketh9e6ce532014-04-01 13:11:53 +1100124
Joshua Hesketha95fd552014-08-21 11:21:05 +1000125 $job_line.append($('<div style="clear: both"></div>'));
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100126 return $job_line;
127 },
128
129 job_status: function(job) {
130 var result = job.result ? job.result.toLowerCase() : null;
131 if (result === null) {
132 result = job.url ? 'in progress' : 'queued';
133 }
134
Monty Taylor860bb0a2014-03-22 09:41:25 -0700135 if (result === 'in progress') {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000136 return this.job_progress_bar(job.elapsed_time,
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100137 job.remaining_time);
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100138 }
139 else {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000140 return this.status_label(result);
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100141 }
142 },
143
144 status_label: function(result) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700145 var $status = $('<span />');
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100146 $status.addClass('zuul-job-result label');
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100147
148 switch (result) {
149 case 'success':
150 $status.addClass('label-success');
151 break;
152 case 'failure':
153 $status.addClass('label-danger');
154 break;
155 case 'unstable':
156 $status.addClass('label-warning');
157 break;
Alexander Evseev3103a842015-11-17 10:12:45 +0300158 case 'skipped':
159 $status.addClass('label-info');
160 break;
Antoine Musso4c2053d2016-02-04 23:35:23 +0100161 // 'in progress' 'queued' 'lost' 'aborted' ...
162 default:
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100163 $status.addClass('label-default');
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100164 }
165 $status.text(result);
166 return $status;
167 },
168
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100169 job_progress_bar: function(elapsed_time, remaining_time) {
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100170 var progress_percent = 100 * (elapsed_time / (elapsed_time +
171 remaining_time));
172 var $bar_inner = $('<div />')
173 .addClass('progress-bar')
174 .attr('role', 'progressbar')
175 .attr('aria-valuenow', 'progressbar')
176 .attr('aria-valuemin', progress_percent)
177 .attr('aria-valuemin', '0')
178 .attr('aria-valuemax', '100')
179 .css('width', progress_percent + '%');
180
181 var $bar_outter = $('<div />')
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100182 .addClass('progress zuul-job-result')
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100183 .append($bar_inner);
184
185 return $bar_outter;
186 },
187
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100188 enqueue_time: function(ms) {
189 // Special format case for enqueue time to add style
190 var hours = 60 * 60 * 1000;
191 var now = Date.now();
192 var delta = now - ms;
Monty Taylor860bb0a2014-03-22 09:41:25 -0700193 var status = 'text-success';
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000194 var text = this.time(delta, true);
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100195 if (delta > (4 * hours)) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700196 status = 'text-danger';
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100197 } else if (delta > (2 * hours)) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700198 status = 'text-warning';
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100199 }
200 return '<span class="' + status + '">' + text + '</span>';
201 },
202
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100203 time: function(ms, words) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700204 if (typeof(words) === 'undefined') {
205 words = false;
206 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100207 var seconds = (+ms)/1000;
208 var minutes = Math.floor(seconds/60);
209 var hours = Math.floor(minutes/60);
210 seconds = Math.floor(seconds % 60);
211 minutes = Math.floor(minutes % 60);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700212 var r = '';
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100213 if (words) {
214 if (hours) {
215 r += hours;
216 r += ' hr ';
217 }
218 r += minutes + ' min';
219 } else {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700220 if (hours < 10) {
221 r += '0';
222 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100223 r += hours + ':';
Monty Taylor860bb0a2014-03-22 09:41:25 -0700224 if (minutes < 10) {
225 r += '0';
226 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100227 r += minutes + ':';
Monty Taylor860bb0a2014-03-22 09:41:25 -0700228 if (seconds < 10) {
229 r += '0';
230 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100231 r += seconds;
232 }
233 return r;
234 },
235
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100236 change_total_progress_bar: function(change) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700237 var job_percent = Math.floor(100 / change.jobs.length);
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100238 var $bar_outter = $('<div />')
239 .addClass('progress zuul-change-total-result');
240
241 $.each(change.jobs, function (i, job) {
242 var result = job.result ? job.result.toLowerCase() : null;
243 if (result === null) {
244 result = job.url ? 'in progress' : 'queued';
245 }
246
Monty Taylor860bb0a2014-03-22 09:41:25 -0700247 if (result !== 'queued') {
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100248 var $bar_inner = $('<div />')
249 .addClass('progress-bar');
250
251 switch (result) {
252 case 'success':
253 $bar_inner.addClass('progress-bar-success');
254 break;
255 case 'lost':
256 case 'failure':
257 $bar_inner.addClass('progress-bar-danger');
258 break;
259 case 'unstable':
260 $bar_inner.addClass('progress-bar-warning');
261 break;
262 case 'in progress':
263 case 'queued':
264 break;
265 }
266 $bar_inner.attr('title', job.name)
267 .css('width', job_percent + '%');
268 $bar_outter.append($bar_inner);
269 }
270 });
271 return $bar_outter;
272 },
273
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100274 change_header: function(change) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700275 var change_id = change.id || 'NA';
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100276 if (change_id.length === 40) {
277 change_id = change_id.substr(0, 7);
Joshua Hesketh298c4912014-03-20 16:06:25 +1100278 }
Timo Tijhof51516cd2013-04-09 01:32:29 +0200279
Monty Taylor860bb0a2014-03-22 09:41:25 -0700280 var $change_link = $('<small />');
Joshua Hesketh298c4912014-03-20 16:06:25 +1100281 if (change.url !== null) {
Joshua Heskethcd7a0772014-08-21 11:32:44 +1000282 if (/^[0-9a-f]{40}$/.test(change.id)) {
283 var change_id_short = change.id.slice(0, 7);
284 $change_link.append(
285 $('<a />').attr('href', change.url).append(
286 $('<abbr />')
287 .attr('title', change.id)
288 .text(change_id_short)
289 )
290 );
291 }
292 else {
293 $change_link.append(
294 $('<a />').attr('href', change.url).text(change.id)
295 );
296 }
Timo Tijhof51516cd2013-04-09 01:32:29 +0200297 }
Joshua Hesketh298c4912014-03-20 16:06:25 +1100298 else {
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100299 $change_link.text(change_id);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200300 }
Timo Tijhof51516cd2013-04-09 01:32:29 +0200301
Monty Taylor860bb0a2014-03-22 09:41:25 -0700302 var $change_progress_row_left = $('<div />')
Timo Tijhofa62e8492015-04-02 03:05:04 +0100303 .addClass('col-xs-4')
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100304 .append($change_link);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700305 var $change_progress_row_right = $('<div />')
Timo Tijhofa62e8492015-04-02 03:05:04 +0100306 .addClass('col-xs-8')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000307 .append(this.change_total_progress_bar(change));
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100308
Monty Taylor860bb0a2014-03-22 09:41:25 -0700309 var $change_progress_row = $('<div />')
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100310 .addClass('row')
311 .append($change_progress_row_left)
Monty Taylor860bb0a2014-03-22 09:41:25 -0700312 .append($change_progress_row_right);
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100313
Monty Taylor860bb0a2014-03-22 09:41:25 -0700314 var $project_span = $('<span />')
Joshua Heskethace48892014-03-22 17:18:31 +1100315 .addClass('change_project')
316 .text(change.project);
317
Monty Taylor860bb0a2014-03-22 09:41:25 -0700318 var $left = $('<div />')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100319 .addClass('col-xs-8')
Joshua Hesketh1751e4f2014-04-01 13:24:51 +1100320 .append($project_span, $change_progress_row);
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100321
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000322 var remaining_time = this.time(
Monty Taylor860bb0a2014-03-22 09:41:25 -0700323 change.remaining_time, true);
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000324 var enqueue_time = this.enqueue_time(
Monty Taylor860bb0a2014-03-22 09:41:25 -0700325 change.enqueue_time);
326 var $remaining_time = $('<small />').addClass('time')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100327 .attr('title', 'Remaining Time').html(remaining_time);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700328 var $enqueue_time = $('<small />').addClass('time')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100329 .attr('title', 'Elapsed Time').html(enqueue_time);
330
James E. Blair6dc954b2015-02-11 18:32:19 -0800331 var $right = $('<div />');
332 if (change.live === true) {
333 $right.addClass('col-xs-4 text-right')
334 .append($remaining_time, $('<br />'), $enqueue_time);
335 }
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100336
Monty Taylor860bb0a2014-03-22 09:41:25 -0700337 var $header = $('<div />')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100338 .addClass('row')
339 .append($left, $right);
340 return $header;
341 },
342
343 change_list: function(jobs) {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000344 var format = this;
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100345 var $list = $('<ul />')
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100346 .addClass('list-group zuul-patchset-body');
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100347
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100348 $.each(jobs, function (i, job) {
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100349 var $item = $('<li />')
350 .addClass('list-group-item')
351 .addClass('zuul-change-job')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000352 .append(format.job(job));
Joshua Hesketh298c4912014-03-20 16:06:25 +1100353 $list.append($item);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200354 });
355
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100356 return $list;
357 },
358
359 change_panel: function (change) {
360 var $header = $('<div />')
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100361 .addClass('panel-heading zuul-patchset-header')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000362 .append(this.change_header(change));
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100363
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100364 var panel_id = change.id ? change.id.replace(',', '_')
365 : change.project.replace('/', '_') +
Monty Taylor860bb0a2014-03-22 09:41:25 -0700366 '-' + change.enqueue_time;
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100367 var $panel = $('<div />')
Monty Taylor860bb0a2014-03-22 09:41:25 -0700368 .attr('id', panel_id)
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100369 .addClass('panel panel-default zuul-change')
370 .append($header)
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000371 .append(this.change_list(change.jobs));
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100372
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000373 $header.click(this.toggle_patchset);
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100374 return $panel;
Timo Tijhof51516cd2013-04-09 01:32:29 +0200375 },
376
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100377 change_status_icon: function(change) {
378 var icon_name = 'green.png';
379 var icon_title = 'Succeeding';
380
381 if (change.active !== true) {
382 // Grey icon
383 icon_name = 'grey.png';
384 icon_title = 'Waiting until closer to head of queue to' +
385 ' start jobs';
386 }
James E. Blair107c3852015-02-07 08:23:10 -0800387 else if (change.live !== true) {
388 // Grey icon
389 icon_name = 'grey.png';
Joshua Hesketh6bc619d2015-02-11 09:30:21 +1100390 icon_title = 'Dependent change required for testing';
James E. Blair107c3852015-02-07 08:23:10 -0800391 }
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100392 else if (change.failing_reasons &&
393 change.failing_reasons.length > 0) {
394 var reason = change.failing_reasons.join(', ');
395 icon_title = 'Failing because ' + reason;
396 if (reason.match(/merge conflict/)) {
397 // Black icon
398 icon_name = 'black.png';
399 }
400 else {
401 // Red icon
402 icon_name = 'red.png';
403 }
404 }
405
406 var $icon = $('<img />')
407 .attr('src', 'images/' + icon_name)
408 .attr('title', icon_title)
409 .css('margin-top', '-6px');
410
411 return $icon;
412 },
413
414 change_with_status_tree: function(change, change_queue) {
415 var $change_row = $('<tr />');
416
417 for (var i = 0; i < change_queue._tree_columns; i++) {
418 var $tree_cell = $('<td />')
419 .css('height', '100%')
420 .css('padding', '0 0 10px 0')
421 .css('margin', '0')
422 .css('width', '16px')
423 .css('min-width', '16px')
424 .css('overflow', 'hidden')
425 .css('vertical-align', 'top');
426
427 if (i < change._tree.length && change._tree[i] !== null) {
428 $tree_cell.css('background-image',
429 'url(\'images/line.png\')')
430 .css('background-repeat', 'repeat-y');
431 }
432
433 if (i === change._tree_index) {
434 $tree_cell.append(
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000435 this.change_status_icon(change));
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100436 }
437 if (change._tree_branches.indexOf(i) !== -1) {
438 var $image = $('<img />')
439 .css('vertical-align', 'baseline');
440 if (change._tree_branches.indexOf(i) ===
441 change._tree_branches.length - 1) {
442 // Angle line
443 $image.attr('src', 'images/line-angle.png');
444 }
445 else {
446 // T line
447 $image.attr('src', 'images/line-t.png');
448 }
449 $tree_cell.append($image);
450 }
451 $change_row.append($tree_cell);
452 }
453
454 var change_width = 360 - 16*change_queue._tree_columns;
455 var $change_column = $('<td />')
456 .css('width', change_width + 'px')
457 .addClass('zuul-change-cell')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000458 .append(this.change_panel(change));
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100459
460 $change_row.append($change_column);
461
462 var $change_table = $('<table />')
463 .addClass('zuul-change-box')
464 .css('-moz-box-sizing', 'content-box')
465 .css('box-sizing', 'content-box')
466 .append($change_row);
467
468 return $change_table;
469 },
470
Joshua Hesketh9d013542014-04-03 13:08:04 +1100471 pipeline_sparkline: function(pipeline_name) {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000472 if (options.graphite_url !== '') {
Joshua Hesketh9d013542014-04-03 13:08:04 +1100473 var $sparkline = $('<img />')
474 .addClass('pull-right')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000475 .attr('src', get_sparkline_url(pipeline_name));
Joshua Hesketh9d013542014-04-03 13:08:04 +1100476 return $sparkline;
477 }
478 return false;
479 },
480
481 pipeline_header: function(pipeline, count) {
482 // Format the pipeline name, sparkline and description
483 var $header_div = $('<div />')
484 .addClass('zuul-pipeline-header');
485
486 var $heading = $('<h3 />')
487 .css('vertical-align', 'middle')
488 .text(pipeline.name)
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100489 .append(
Joshua Hesketh9d013542014-04-03 13:08:04 +1100490 $('<span />')
491 .addClass('badge pull-right')
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100492 .css('vertical-align', 'middle')
Joshua Hesketh9d013542014-04-03 13:08:04 +1100493 .css('margin-top', '0.5em')
494 .text(count)
495 )
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000496 .append(this.pipeline_sparkline(pipeline.name));
Joshua Hesketh9d013542014-04-03 13:08:04 +1100497
498 $header_div.append($heading);
Joshua Hesketh298c4912014-03-20 16:06:25 +1100499
Timo Tijhof51516cd2013-04-09 01:32:29 +0200500 if (typeof pipeline.description === 'string') {
Alexander Evseev057aed12015-10-28 17:17:04 +0300501 var descr = $('<small />')
502 $.each( pipeline.description.split(/\r?\n\r?\n/), function(index, descr_part){
503 descr.append($('<p />').text(descr_part));
504 });
Joshua Hesketh9d013542014-04-03 13:08:04 +1100505 $header_div.append(
Alexander Evseev057aed12015-10-28 17:17:04 +0300506 $('<p />').append(descr)
Joshua Hesketh298c4912014-03-20 16:06:25 +1100507 );
Timo Tijhof51516cd2013-04-09 01:32:29 +0200508 }
Joshua Hesketh9d013542014-04-03 13:08:04 +1100509 return $header_div;
510 },
511
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000512 pipeline: function (pipeline, count) {
513 var format = this;
Joshua Hesketh9d013542014-04-03 13:08:04 +1100514 var $html = $('<div />')
515 .addClass('zuul-pipeline col-md-4')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000516 .append(this.pipeline_header(pipeline, count));
Timo Tijhof51516cd2013-04-09 01:32:29 +0200517
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100518 $.each(pipeline.change_queues,
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100519 function (queue_i, change_queue) {
520 $.each(change_queue.heads, function (head_i, changes) {
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100521 if (pipeline.change_queues.length > 1 &&
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100522 head_i === 0) {
523 var name = change_queue.name;
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100524 var short_name = name;
525 if (short_name.length > 32) {
526 short_name = short_name.substr(0, 32) + '...';
Timo Tijhof51516cd2013-04-09 01:32:29 +0200527 }
Joshua Hesketh298c4912014-03-20 16:06:25 +1100528 $html.append(
529 $('<p />')
530 .text('Queue: ')
531 .append(
532 $('<abbr />')
533 .attr('title', name)
534 .text(short_name)
535 )
536 );
Timo Tijhof51516cd2013-04-09 01:32:29 +0200537 }
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100538
539 $.each(changes, function (change_i, change) {
540 var $change_box =
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000541 format.change_with_status_tree(
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100542 change, change_queue);
543 $html.append($change_box);
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000544 format.display_patchset($change_box);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200545 });
546 });
547 });
Joshua Hesketh298c4912014-03-20 16:06:25 +1100548 return $html;
Joshua Heskethace48892014-03-22 17:18:31 +1100549 },
550
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000551 toggle_patchset: function(e) {
552 // Toggle showing/hiding the patchset when the header is
553 // clicked.
554
555 // Grab the patchset panel
556 var $panel = $(e.target).parents('.zuul-change');
557 var $body = $panel.children('.zuul-patchset-body');
558 $body.toggle(200);
559 var collapsed_index = collapsed_exceptions.indexOf(
560 $panel.attr('id'));
561 if (collapsed_index === -1 ) {
562 // Currently not an exception, add it to list
563 collapsed_exceptions.push($panel.attr('id'));
564 }
565 else {
566 // Currently an except, remove from exceptions
567 collapsed_exceptions.splice(collapsed_index, 1);
568 }
569 },
570
571 display_patchset: function($change_box, animate) {
572 // Determine if to show or hide the patchset and/or the results
573 // when loaded
574
575 // See if we should hide the body/results
576 var $panel = $change_box.find('.zuul-change');
577 var panel_change = $panel.attr('id');
578 var $body = $panel.children('.zuul-patchset-body');
579 var expand_by_default = $('#expand_by_default')
580 .prop('checked');
581
582 var collapsed_index = collapsed_exceptions
583 .indexOf(panel_change);
584
585 if (expand_by_default && collapsed_index === -1 ||
586 !expand_by_default && collapsed_index !== -1) {
587 // Expand by default, or is an exception
588 $body.show(animate);
589 }
590 else {
591 $body.hide(animate);
592 }
593
594 // Check if we should hide the whole panel
595 var panel_project = $panel.find('.change_project').text()
596 .toLowerCase();
597
598
599 var panel_pipeline = $change_box
600 .parents('.zuul-pipeline')
601 .find('.zuul-pipeline-header > h3')
602 .html()
603 .toLowerCase();
604
605 if (current_filter !== '') {
606 var show_panel = false;
607 var filter = current_filter.trim().split(/[\s,]+/);
608 $.each(filter, function(index, f_val) {
609 if (f_val !== '') {
610 f_val = f_val.toLowerCase();
611 if (panel_project.indexOf(f_val) !== -1 ||
612 panel_pipeline.indexOf(f_val) !== -1 ||
613 panel_change.indexOf(f_val) !== -1) {
614 show_panel = true;
615 }
616 }
617 });
618 if (show_panel === true) {
619 $change_box.show(animate);
620 }
621 else {
622 $change_box.hide(animate);
623 }
624 }
625 else {
626 $change_box.show(animate);
627 }
628 },
629 };
630
631 var app = {
632 schedule: function (app) {
Timo Tijhof4be9f742015-04-02 01:13:19 +0100633 app = app || this;
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000634 if (!options.enabled) {
635 setTimeout(function() {app.schedule(app);}, 5000);
636 return;
637 }
638 app.update().complete(function () {
639 setTimeout(function() {app.schedule(app);}, 5000);
640 });
641
642 /* Only update graphs every minute */
643 if (zuul_graph_update_count > 11) {
644 zuul_graph_update_count = 0;
645 zuul.update_sparklines();
646 }
647 },
648
649 /** @return {jQuery.Promise} */
650 update: function () {
651 // Cancel the previous update if it hasn't completed yet.
652 if (xhr) {
653 xhr.abort();
654 }
655
656 this.emit('update-start');
657 var app = this;
658
Timo Tijhof4be9f742015-04-02 01:13:19 +0100659 var $msg = $(options.msg_id);
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000660 xhr = $.getJSON(options.source)
661 .done(function (data) {
662 if ('message' in data) {
663 $msg.removeClass('alert-danger')
664 .addClass('alert-info')
665 .text(data.message)
666 .show();
667 } else {
668 $msg.empty()
669 .hide();
670 }
671
672 if ('zuul_version' in data) {
673 $('#zuul-version-span').text(data.zuul_version);
674 }
675 if ('last_reconfigured' in data) {
676 var last_reconfigured =
677 new Date(data.last_reconfigured);
678 $('#last-reconfigured-span').text(
679 last_reconfigured.toString());
680 }
681
682 var $pipelines = $(options.pipelines_id);
683 $pipelines.html('');
684 $.each(data.pipelines, function (i, pipeline) {
685 var count = app.create_tree(pipeline);
686 $pipelines.append(
687 format.pipeline(pipeline, count));
688 });
689
690 $(options.queue_events_num).text(
691 data.trigger_event_queue ?
692 data.trigger_event_queue.length : '0'
693 );
694 $(options.queue_results_num).text(
695 data.result_event_queue ?
696 data.result_event_queue.length : '0'
697 );
698 })
Timo Tijhof67f4ac12015-04-10 00:33:50 +0100699 .fail(function (jqXHR, statusText, errMsg) {
700 if (statusText === 'abort') {
701 return;
702 }
Joshua Heskethd50eb582014-04-30 19:51:22 +1000703 $msg.text(options.source + ': ' + errMsg)
704 .addClass('alert-danger')
705 .removeClass('zuul-msg-wrap-off')
706 .show();
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000707 })
708 .complete(function () {
709 xhr = undefined;
710 app.emit('update-end');
711 });
712
713 return xhr;
714 },
715
716 update_sparklines: function() {
717 $.each(zuul_sparkline_urls, function(name, url) {
718 var newimg = new Image();
719 var parts = url.split('#');
720 newimg.src = parts[0] + '#' + new Date().getTime();
Timo Tijhof4be9f742015-04-02 01:13:19 +0100721 $(newimg).load(function () {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000722 zuul_sparkline_urls[name] = newimg.src;
723 });
724 });
725 },
726
727 emit: function () {
728 $jq.trigger.apply($jq, arguments);
729 return this;
730 },
731 on: function () {
732 $jq.on.apply($jq, arguments);
733 return this;
734 },
735 one: function () {
736 $jq.one.apply($jq, arguments);
737 return this;
738 },
739
740 control_form: function() {
741 // Build the filter form filling anything from cookies
742
743 var $control_form = $('<form />')
744 .attr('role', 'form')
745 .addClass('form-inline')
746 .submit(this.handle_filter_change);
747
748 $control_form
749 .append(this.filter_form_group())
750 .append(this.expand_form_group());
751
752 return $control_form;
753 },
754
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100755 filter_form_group: function() {
Joshua Heskethace48892014-03-22 17:18:31 +1100756 // Update the filter form with a clear button if required
757
758 var $label = $('<label />')
759 .addClass('control-label')
760 .attr('for', 'filter_string')
761 .text('Filters')
762 .css('padding-right', '0.5em');
763
764 var $input = $('<input />')
765 .attr('type', 'text')
766 .attr('id', 'filter_string')
767 .addClass('form-control')
768 .attr('title',
769 'project(s), pipeline(s) or review(s) comma ' +
770 'separated')
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100771 .attr('value', current_filter);
Joshua Heskethace48892014-03-22 17:18:31 +1100772
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000773 $input.change(this.handle_filter_change);
Joshua Heskethace48892014-03-22 17:18:31 +1100774
775 var $clear_icon = $('<span />')
776 .addClass('form-control-feedback')
777 .addClass('glyphicon glyphicon-remove-circle')
778 .attr('id', 'filter_form_clear_box')
779 .attr('title', 'clear filter')
780 .css('cursor', 'pointer');
781
782 $clear_icon.click(function() {
783 $('#filter_string').val('').change();
784 });
785
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100786 if (current_filter === '') {
Joshua Heskethace48892014-03-22 17:18:31 +1100787 $clear_icon.hide();
788 }
789
790 var $form_group = $('<div />')
791 .addClass('form-group has-feedback')
792 .append($label, $input, $clear_icon);
793 return $form_group;
794 },
795
Joshua Heskethae230f62014-03-22 22:14:44 +1100796 expand_form_group: function() {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700797 var expand_by_default = (
Joshua Heskethae230f62014-03-22 22:14:44 +1100798 read_cookie('zuul_expand_by_default', false) === 'true');
799
Monty Taylor860bb0a2014-03-22 09:41:25 -0700800 var $checkbox = $('<input />')
Joshua Heskethae230f62014-03-22 22:14:44 +1100801 .attr('type', 'checkbox')
802 .attr('id', 'expand_by_default')
803 .prop('checked', expand_by_default)
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000804 .change(this.handle_expand_by_default);
Joshua Heskethae230f62014-03-22 22:14:44 +1100805
Monty Taylor860bb0a2014-03-22 09:41:25 -0700806 var $label = $('<label />')
Joshua Heskethae230f62014-03-22 22:14:44 +1100807 .css('padding-left', '1em')
808 .html('Expand by default: ')
809 .append($checkbox);
810
811 var $form_group = $('<div />')
812 .addClass('checkbox')
813 .append($label);
814 return $form_group;
815 },
816
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000817 handle_filter_change: function() {
818 // Update the filter and save it to a cookie
819 current_filter = $('#filter_string').val();
820 set_cookie('zuul_filter_string', current_filter);
821 if (current_filter === '') {
822 $('#filter_form_clear_box').hide();
Joshua Heskethace48892014-03-22 17:18:31 +1100823 }
824 else {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000825 $('#filter_form_clear_box').show();
Joshua Heskethace48892014-03-22 17:18:31 +1100826 }
Joshua Heskethace48892014-03-22 17:18:31 +1100827
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000828 $('.zuul-change-box').each(function(index, obj) {
829 var $change_box = $(obj);
830 format.display_patchset($change_box, 200);
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100831 });
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000832 return false;
Timo Tijhof51516cd2013-04-09 01:32:29 +0200833 },
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000834
835 handle_expand_by_default: function(e) {
836 // Handle toggling expand by default
837 set_cookie('zuul_expand_by_default', e.target.checked);
838 collapsed_exceptions = [];
839 $('.zuul-change-box').each(function(index, obj) {
840 var $change_box = $(obj);
841 format.display_patchset($change_box, 200);
842 });
843 },
844
845 create_tree: function(pipeline) {
846 var count = 0;
847 var pipeline_max_tree_columns = 1;
848 $.each(pipeline.change_queues, function(change_queue_i,
849 change_queue) {
850 var tree = [];
851 var max_tree_columns = 1;
852 var changes = [];
853 var last_tree_length = 0;
854 $.each(change_queue.heads, function(head_i, head) {
855 $.each(head, function(change_i, change) {
856 changes[change.id] = change;
857 change._tree_position = change_i;
858 });
859 });
860 $.each(change_queue.heads, function(head_i, head) {
861 $.each(head, function(change_i, change) {
James E. Blaird1d3ce32015-02-11 17:56:45 -0800862 if (change.live === true) {
863 count += 1;
864 }
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000865 var idx = tree.indexOf(change.id);
866 if (idx > -1) {
867 change._tree_index = idx;
868 // remove...
869 tree[idx] = null;
870 while (tree[tree.length - 1] === null) {
871 tree.pop();
872 }
873 } else {
874 change._tree_index = 0;
875 }
876 change._tree_branches = [];
877 change._tree = [];
878 if (typeof(change.items_behind) === 'undefined') {
879 change.items_behind = [];
880 }
881 change.items_behind.sort(function(a, b) {
882 return (changes[b]._tree_position -
883 changes[a]._tree_position);
884 });
885 $.each(change.items_behind, function(i, id) {
886 tree.push(id);
887 if (tree.length>last_tree_length &&
888 last_tree_length > 0) {
889 change._tree_branches.push(
890 tree.length - 1);
891 }
892 });
893 if (tree.length > max_tree_columns) {
894 max_tree_columns = tree.length;
895 }
896 if (tree.length > pipeline_max_tree_columns) {
897 pipeline_max_tree_columns = tree.length;
898 }
899 change._tree = tree.slice(0); // make a copy
900 last_tree_length = tree.length;
901 });
902 });
903 change_queue._tree_columns = max_tree_columns;
904 });
905 pipeline._tree_columns = pipeline_max_tree_columns;
906 return count;
907 },
908 };
909
910 $jq = $(app);
911 return {
912 options: options,
913 format: format,
914 app: app,
915 jq: $jq
916 };
Timo Tijhof4be9f742015-04-02 01:13:19 +0100917 };
Timo Tijhof51516cd2013-04-09 01:32:29 +0200918}(jQuery));