blob: 1937cd597721f605d304d1c4f990c1f729a88f2d [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
Monty Taylore78211d2017-07-21 09:48:41 +090099 if (job.result !== null) {
100 $job_line.append(
101 $('<a />')
102 .addClass('zuul-job-name')
103 .attr('href', job.report_url)
104 .text(job.name)
105 );
106 }
107 else if (job.url !== null) {
Joshua Hesketh9e6ce532014-04-01 13:11:53 +1100108 $job_line.append(
109 $('<a />')
110 .addClass('zuul-job-name')
111 .attr('href', job.url)
112 .text(job.name)
113 );
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100114 }
Joshua Hesketh9e6ce532014-04-01 13:11:53 +1100115 else {
116 $job_line.append(
117 $('<span />')
118 .addClass('zuul-job-name')
119 .text(job.name)
120 );
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100121 }
Joshua Hesketh9e6ce532014-04-01 13:11:53 +1100122
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000123 $job_line.append(this.job_status(job));
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100124
125 if (job.voting === false) {
126 $job_line.append(
Joshua Hesketh9e6ce532014-04-01 13:11:53 +1100127 $(' <small />')
128 .addClass('zuul-non-voting-desc')
129 .text(' (non-voting)')
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100130 );
131 }
Joshua Hesketh9e6ce532014-04-01 13:11:53 +1100132
Joshua Hesketha95fd552014-08-21 11:21:05 +1000133 $job_line.append($('<div style="clear: both"></div>'));
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100134 return $job_line;
135 },
136
137 job_status: function(job) {
138 var result = job.result ? job.result.toLowerCase() : null;
139 if (result === null) {
140 result = job.url ? 'in progress' : 'queued';
141 }
142
Monty Taylor860bb0a2014-03-22 09:41:25 -0700143 if (result === 'in progress') {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000144 return this.job_progress_bar(job.elapsed_time,
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100145 job.remaining_time);
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100146 }
147 else {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000148 return this.status_label(result);
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100149 }
150 },
151
152 status_label: function(result) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700153 var $status = $('<span />');
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100154 $status.addClass('zuul-job-result label');
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100155
156 switch (result) {
157 case 'success':
158 $status.addClass('label-success');
159 break;
160 case 'failure':
161 $status.addClass('label-danger');
162 break;
163 case 'unstable':
164 $status.addClass('label-warning');
165 break;
Alexander Evseev3103a842015-11-17 10:12:45 +0300166 case 'skipped':
167 $status.addClass('label-info');
168 break;
Antoine Musso4c2053d2016-02-04 23:35:23 +0100169 // 'in progress' 'queued' 'lost' 'aborted' ...
170 default:
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100171 $status.addClass('label-default');
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100172 }
173 $status.text(result);
174 return $status;
175 },
176
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100177 job_progress_bar: function(elapsed_time, remaining_time) {
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100178 var progress_percent = 100 * (elapsed_time / (elapsed_time +
179 remaining_time));
180 var $bar_inner = $('<div />')
181 .addClass('progress-bar')
182 .attr('role', 'progressbar')
183 .attr('aria-valuenow', 'progressbar')
184 .attr('aria-valuemin', progress_percent)
185 .attr('aria-valuemin', '0')
186 .attr('aria-valuemax', '100')
187 .css('width', progress_percent + '%');
188
189 var $bar_outter = $('<div />')
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100190 .addClass('progress zuul-job-result')
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100191 .append($bar_inner);
192
193 return $bar_outter;
194 },
195
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100196 enqueue_time: function(ms) {
197 // Special format case for enqueue time to add style
198 var hours = 60 * 60 * 1000;
199 var now = Date.now();
200 var delta = now - ms;
Monty Taylor860bb0a2014-03-22 09:41:25 -0700201 var status = 'text-success';
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000202 var text = this.time(delta, true);
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100203 if (delta > (4 * hours)) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700204 status = 'text-danger';
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100205 } else if (delta > (2 * hours)) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700206 status = 'text-warning';
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100207 }
208 return '<span class="' + status + '">' + text + '</span>';
209 },
210
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100211 time: function(ms, words) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700212 if (typeof(words) === 'undefined') {
213 words = false;
214 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100215 var seconds = (+ms)/1000;
216 var minutes = Math.floor(seconds/60);
217 var hours = Math.floor(minutes/60);
218 seconds = Math.floor(seconds % 60);
219 minutes = Math.floor(minutes % 60);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700220 var r = '';
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100221 if (words) {
222 if (hours) {
223 r += hours;
224 r += ' hr ';
225 }
226 r += minutes + ' min';
227 } else {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700228 if (hours < 10) {
229 r += '0';
230 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100231 r += hours + ':';
Monty Taylor860bb0a2014-03-22 09:41:25 -0700232 if (minutes < 10) {
233 r += '0';
234 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100235 r += minutes + ':';
Monty Taylor860bb0a2014-03-22 09:41:25 -0700236 if (seconds < 10) {
237 r += '0';
238 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100239 r += seconds;
240 }
241 return r;
242 },
243
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100244 change_total_progress_bar: function(change) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700245 var job_percent = Math.floor(100 / change.jobs.length);
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100246 var $bar_outter = $('<div />')
247 .addClass('progress zuul-change-total-result');
248
249 $.each(change.jobs, function (i, job) {
250 var result = job.result ? job.result.toLowerCase() : null;
251 if (result === null) {
252 result = job.url ? 'in progress' : 'queued';
253 }
254
Monty Taylor860bb0a2014-03-22 09:41:25 -0700255 if (result !== 'queued') {
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100256 var $bar_inner = $('<div />')
257 .addClass('progress-bar');
258
259 switch (result) {
260 case 'success':
261 $bar_inner.addClass('progress-bar-success');
262 break;
263 case 'lost':
264 case 'failure':
265 $bar_inner.addClass('progress-bar-danger');
266 break;
267 case 'unstable':
268 $bar_inner.addClass('progress-bar-warning');
269 break;
270 case 'in progress':
271 case 'queued':
272 break;
273 }
274 $bar_inner.attr('title', job.name)
275 .css('width', job_percent + '%');
276 $bar_outter.append($bar_inner);
277 }
278 });
279 return $bar_outter;
280 },
281
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100282 change_header: function(change) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700283 var change_id = change.id || 'NA';
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100284 if (change_id.length === 40) {
285 change_id = change_id.substr(0, 7);
Joshua Hesketh298c4912014-03-20 16:06:25 +1100286 }
Timo Tijhof51516cd2013-04-09 01:32:29 +0200287
Monty Taylor860bb0a2014-03-22 09:41:25 -0700288 var $change_link = $('<small />');
Joshua Hesketh298c4912014-03-20 16:06:25 +1100289 if (change.url !== null) {
Jamie Lennoxa6b33ba2017-02-10 11:33:44 +1100290 var github_id = change.id.match(/^([0-9]+),([0-9a-f]{40})$/);
291 if (github_id) {
292 $change_link.append(
293 $('<a />').attr('href', change.url).append(
294 $('<abbr />')
295 .attr('title', change.id)
296 .text('#' + github_id[1])
297 )
298 );
299 } else if (/^[0-9a-f]{40}$/.test(change.id)) {
Joshua Heskethcd7a0772014-08-21 11:32:44 +1000300 var change_id_short = change.id.slice(0, 7);
301 $change_link.append(
302 $('<a />').attr('href', change.url).append(
303 $('<abbr />')
304 .attr('title', change.id)
305 .text(change_id_short)
306 )
307 );
308 }
309 else {
310 $change_link.append(
311 $('<a />').attr('href', change.url).text(change.id)
312 );
313 }
Timo Tijhof51516cd2013-04-09 01:32:29 +0200314 }
Joshua Hesketh298c4912014-03-20 16:06:25 +1100315 else {
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100316 $change_link.text(change_id);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200317 }
Timo Tijhof51516cd2013-04-09 01:32:29 +0200318
Monty Taylor860bb0a2014-03-22 09:41:25 -0700319 var $change_progress_row_left = $('<div />')
Timo Tijhofa62e8492015-04-02 03:05:04 +0100320 .addClass('col-xs-4')
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100321 .append($change_link);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700322 var $change_progress_row_right = $('<div />')
Timo Tijhofa62e8492015-04-02 03:05:04 +0100323 .addClass('col-xs-8')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000324 .append(this.change_total_progress_bar(change));
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100325
Monty Taylor860bb0a2014-03-22 09:41:25 -0700326 var $change_progress_row = $('<div />')
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100327 .addClass('row')
328 .append($change_progress_row_left)
Monty Taylor860bb0a2014-03-22 09:41:25 -0700329 .append($change_progress_row_right);
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100330
Monty Taylor860bb0a2014-03-22 09:41:25 -0700331 var $project_span = $('<span />')
Joshua Heskethace48892014-03-22 17:18:31 +1100332 .addClass('change_project')
333 .text(change.project);
334
Monty Taylor860bb0a2014-03-22 09:41:25 -0700335 var $left = $('<div />')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100336 .addClass('col-xs-8')
Joshua Hesketh1751e4f2014-04-01 13:24:51 +1100337 .append($project_span, $change_progress_row);
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100338
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000339 var remaining_time = this.time(
Monty Taylor860bb0a2014-03-22 09:41:25 -0700340 change.remaining_time, true);
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000341 var enqueue_time = this.enqueue_time(
Monty Taylor860bb0a2014-03-22 09:41:25 -0700342 change.enqueue_time);
343 var $remaining_time = $('<small />').addClass('time')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100344 .attr('title', 'Remaining Time').html(remaining_time);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700345 var $enqueue_time = $('<small />').addClass('time')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100346 .attr('title', 'Elapsed Time').html(enqueue_time);
347
James E. Blair6dc954b2015-02-11 18:32:19 -0800348 var $right = $('<div />');
349 if (change.live === true) {
350 $right.addClass('col-xs-4 text-right')
351 .append($remaining_time, $('<br />'), $enqueue_time);
352 }
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100353
Monty Taylor860bb0a2014-03-22 09:41:25 -0700354 var $header = $('<div />')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100355 .addClass('row')
356 .append($left, $right);
357 return $header;
358 },
359
360 change_list: function(jobs) {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000361 var format = this;
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100362 var $list = $('<ul />')
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100363 .addClass('list-group zuul-patchset-body');
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100364
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100365 $.each(jobs, function (i, job) {
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100366 var $item = $('<li />')
367 .addClass('list-group-item')
368 .addClass('zuul-change-job')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000369 .append(format.job(job));
Joshua Hesketh298c4912014-03-20 16:06:25 +1100370 $list.append($item);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200371 });
372
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100373 return $list;
374 },
375
376 change_panel: function (change) {
377 var $header = $('<div />')
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100378 .addClass('panel-heading zuul-patchset-header')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000379 .append(this.change_header(change));
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100380
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100381 var panel_id = change.id ? change.id.replace(',', '_')
382 : change.project.replace('/', '_') +
Monty Taylor860bb0a2014-03-22 09:41:25 -0700383 '-' + change.enqueue_time;
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100384 var $panel = $('<div />')
Monty Taylor860bb0a2014-03-22 09:41:25 -0700385 .attr('id', panel_id)
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100386 .addClass('panel panel-default zuul-change')
387 .append($header)
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000388 .append(this.change_list(change.jobs));
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100389
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000390 $header.click(this.toggle_patchset);
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100391 return $panel;
Timo Tijhof51516cd2013-04-09 01:32:29 +0200392 },
393
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100394 change_status_icon: function(change) {
395 var icon_name = 'green.png';
396 var icon_title = 'Succeeding';
397
398 if (change.active !== true) {
399 // Grey icon
400 icon_name = 'grey.png';
401 icon_title = 'Waiting until closer to head of queue to' +
402 ' start jobs';
403 }
James E. Blair107c3852015-02-07 08:23:10 -0800404 else if (change.live !== true) {
405 // Grey icon
406 icon_name = 'grey.png';
Joshua Hesketh6bc619d2015-02-11 09:30:21 +1100407 icon_title = 'Dependent change required for testing';
James E. Blair107c3852015-02-07 08:23:10 -0800408 }
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100409 else if (change.failing_reasons &&
410 change.failing_reasons.length > 0) {
411 var reason = change.failing_reasons.join(', ');
412 icon_title = 'Failing because ' + reason;
413 if (reason.match(/merge conflict/)) {
414 // Black icon
415 icon_name = 'black.png';
416 }
417 else {
418 // Red icon
419 icon_name = 'red.png';
420 }
421 }
422
423 var $icon = $('<img />')
424 .attr('src', 'images/' + icon_name)
425 .attr('title', icon_title)
426 .css('margin-top', '-6px');
427
428 return $icon;
429 },
430
431 change_with_status_tree: function(change, change_queue) {
432 var $change_row = $('<tr />');
433
434 for (var i = 0; i < change_queue._tree_columns; i++) {
435 var $tree_cell = $('<td />')
436 .css('height', '100%')
437 .css('padding', '0 0 10px 0')
438 .css('margin', '0')
439 .css('width', '16px')
440 .css('min-width', '16px')
441 .css('overflow', 'hidden')
442 .css('vertical-align', 'top');
443
444 if (i < change._tree.length && change._tree[i] !== null) {
445 $tree_cell.css('background-image',
446 'url(\'images/line.png\')')
447 .css('background-repeat', 'repeat-y');
448 }
449
450 if (i === change._tree_index) {
451 $tree_cell.append(
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000452 this.change_status_icon(change));
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100453 }
454 if (change._tree_branches.indexOf(i) !== -1) {
455 var $image = $('<img />')
456 .css('vertical-align', 'baseline');
457 if (change._tree_branches.indexOf(i) ===
458 change._tree_branches.length - 1) {
459 // Angle line
460 $image.attr('src', 'images/line-angle.png');
461 }
462 else {
463 // T line
464 $image.attr('src', 'images/line-t.png');
465 }
466 $tree_cell.append($image);
467 }
468 $change_row.append($tree_cell);
469 }
470
471 var change_width = 360 - 16*change_queue._tree_columns;
472 var $change_column = $('<td />')
473 .css('width', change_width + 'px')
474 .addClass('zuul-change-cell')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000475 .append(this.change_panel(change));
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100476
477 $change_row.append($change_column);
478
479 var $change_table = $('<table />')
480 .addClass('zuul-change-box')
481 .css('-moz-box-sizing', 'content-box')
482 .css('box-sizing', 'content-box')
483 .append($change_row);
484
485 return $change_table;
486 },
487
Joshua Hesketh9d013542014-04-03 13:08:04 +1100488 pipeline_sparkline: function(pipeline_name) {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000489 if (options.graphite_url !== '') {
Joshua Hesketh9d013542014-04-03 13:08:04 +1100490 var $sparkline = $('<img />')
491 .addClass('pull-right')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000492 .attr('src', get_sparkline_url(pipeline_name));
Joshua Hesketh9d013542014-04-03 13:08:04 +1100493 return $sparkline;
494 }
495 return false;
496 },
497
498 pipeline_header: function(pipeline, count) {
499 // Format the pipeline name, sparkline and description
500 var $header_div = $('<div />')
501 .addClass('zuul-pipeline-header');
502
503 var $heading = $('<h3 />')
504 .css('vertical-align', 'middle')
505 .text(pipeline.name)
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100506 .append(
Joshua Hesketh9d013542014-04-03 13:08:04 +1100507 $('<span />')
508 .addClass('badge pull-right')
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100509 .css('vertical-align', 'middle')
Joshua Hesketh9d013542014-04-03 13:08:04 +1100510 .css('margin-top', '0.5em')
511 .text(count)
512 )
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000513 .append(this.pipeline_sparkline(pipeline.name));
Joshua Hesketh9d013542014-04-03 13:08:04 +1100514
515 $header_div.append($heading);
Joshua Hesketh298c4912014-03-20 16:06:25 +1100516
Timo Tijhof51516cd2013-04-09 01:32:29 +0200517 if (typeof pipeline.description === 'string') {
Alexander Evseev057aed12015-10-28 17:17:04 +0300518 var descr = $('<small />')
519 $.each( pipeline.description.split(/\r?\n\r?\n/), function(index, descr_part){
520 descr.append($('<p />').text(descr_part));
521 });
Joshua Hesketh9d013542014-04-03 13:08:04 +1100522 $header_div.append(
Alexander Evseev057aed12015-10-28 17:17:04 +0300523 $('<p />').append(descr)
Joshua Hesketh298c4912014-03-20 16:06:25 +1100524 );
Timo Tijhof51516cd2013-04-09 01:32:29 +0200525 }
Joshua Hesketh9d013542014-04-03 13:08:04 +1100526 return $header_div;
527 },
528
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000529 pipeline: function (pipeline, count) {
530 var format = this;
Joshua Hesketh9d013542014-04-03 13:08:04 +1100531 var $html = $('<div />')
532 .addClass('zuul-pipeline col-md-4')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000533 .append(this.pipeline_header(pipeline, count));
Timo Tijhof51516cd2013-04-09 01:32:29 +0200534
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100535 $.each(pipeline.change_queues,
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100536 function (queue_i, change_queue) {
537 $.each(change_queue.heads, function (head_i, changes) {
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100538 if (pipeline.change_queues.length > 1 &&
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100539 head_i === 0) {
540 var name = change_queue.name;
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100541 var short_name = name;
542 if (short_name.length > 32) {
543 short_name = short_name.substr(0, 32) + '...';
Timo Tijhof51516cd2013-04-09 01:32:29 +0200544 }
Joshua Hesketh298c4912014-03-20 16:06:25 +1100545 $html.append(
546 $('<p />')
547 .text('Queue: ')
548 .append(
549 $('<abbr />')
550 .attr('title', name)
551 .text(short_name)
552 )
553 );
Timo Tijhof51516cd2013-04-09 01:32:29 +0200554 }
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100555
556 $.each(changes, function (change_i, change) {
557 var $change_box =
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000558 format.change_with_status_tree(
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100559 change, change_queue);
560 $html.append($change_box);
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000561 format.display_patchset($change_box);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200562 });
563 });
564 });
Joshua Hesketh298c4912014-03-20 16:06:25 +1100565 return $html;
Joshua Heskethace48892014-03-22 17:18:31 +1100566 },
567
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000568 toggle_patchset: function(e) {
569 // Toggle showing/hiding the patchset when the header is
570 // clicked.
571
572 // Grab the patchset panel
573 var $panel = $(e.target).parents('.zuul-change');
574 var $body = $panel.children('.zuul-patchset-body');
575 $body.toggle(200);
576 var collapsed_index = collapsed_exceptions.indexOf(
577 $panel.attr('id'));
578 if (collapsed_index === -1 ) {
579 // Currently not an exception, add it to list
580 collapsed_exceptions.push($panel.attr('id'));
581 }
582 else {
583 // Currently an except, remove from exceptions
584 collapsed_exceptions.splice(collapsed_index, 1);
585 }
586 },
587
588 display_patchset: function($change_box, animate) {
589 // Determine if to show or hide the patchset and/or the results
590 // when loaded
591
592 // See if we should hide the body/results
593 var $panel = $change_box.find('.zuul-change');
594 var panel_change = $panel.attr('id');
595 var $body = $panel.children('.zuul-patchset-body');
596 var expand_by_default = $('#expand_by_default')
597 .prop('checked');
598
599 var collapsed_index = collapsed_exceptions
600 .indexOf(panel_change);
601
602 if (expand_by_default && collapsed_index === -1 ||
603 !expand_by_default && collapsed_index !== -1) {
604 // Expand by default, or is an exception
605 $body.show(animate);
606 }
607 else {
608 $body.hide(animate);
609 }
610
611 // Check if we should hide the whole panel
612 var panel_project = $panel.find('.change_project').text()
613 .toLowerCase();
614
615
616 var panel_pipeline = $change_box
617 .parents('.zuul-pipeline')
618 .find('.zuul-pipeline-header > h3')
619 .html()
620 .toLowerCase();
621
622 if (current_filter !== '') {
623 var show_panel = false;
624 var filter = current_filter.trim().split(/[\s,]+/);
625 $.each(filter, function(index, f_val) {
626 if (f_val !== '') {
627 f_val = f_val.toLowerCase();
628 if (panel_project.indexOf(f_val) !== -1 ||
629 panel_pipeline.indexOf(f_val) !== -1 ||
630 panel_change.indexOf(f_val) !== -1) {
631 show_panel = true;
632 }
633 }
634 });
635 if (show_panel === true) {
636 $change_box.show(animate);
637 }
638 else {
639 $change_box.hide(animate);
640 }
641 }
642 else {
643 $change_box.show(animate);
644 }
645 },
646 };
647
648 var app = {
649 schedule: function (app) {
Timo Tijhof4be9f742015-04-02 01:13:19 +0100650 app = app || this;
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000651 if (!options.enabled) {
652 setTimeout(function() {app.schedule(app);}, 5000);
653 return;
654 }
655 app.update().complete(function () {
656 setTimeout(function() {app.schedule(app);}, 5000);
657 });
658
659 /* Only update graphs every minute */
660 if (zuul_graph_update_count > 11) {
661 zuul_graph_update_count = 0;
662 zuul.update_sparklines();
663 }
664 },
665
666 /** @return {jQuery.Promise} */
667 update: function () {
668 // Cancel the previous update if it hasn't completed yet.
669 if (xhr) {
670 xhr.abort();
671 }
672
673 this.emit('update-start');
674 var app = this;
675
Timo Tijhof4be9f742015-04-02 01:13:19 +0100676 var $msg = $(options.msg_id);
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000677 xhr = $.getJSON(options.source)
678 .done(function (data) {
679 if ('message' in data) {
680 $msg.removeClass('alert-danger')
681 .addClass('alert-info')
682 .text(data.message)
683 .show();
684 } else {
685 $msg.empty()
686 .hide();
687 }
688
689 if ('zuul_version' in data) {
690 $('#zuul-version-span').text(data.zuul_version);
691 }
692 if ('last_reconfigured' in data) {
693 var last_reconfigured =
694 new Date(data.last_reconfigured);
695 $('#last-reconfigured-span').text(
696 last_reconfigured.toString());
697 }
698
699 var $pipelines = $(options.pipelines_id);
700 $pipelines.html('');
701 $.each(data.pipelines, function (i, pipeline) {
702 var count = app.create_tree(pipeline);
703 $pipelines.append(
704 format.pipeline(pipeline, count));
705 });
706
707 $(options.queue_events_num).text(
708 data.trigger_event_queue ?
709 data.trigger_event_queue.length : '0'
710 );
711 $(options.queue_results_num).text(
712 data.result_event_queue ?
713 data.result_event_queue.length : '0'
714 );
715 })
Timo Tijhof67f4ac12015-04-10 00:33:50 +0100716 .fail(function (jqXHR, statusText, errMsg) {
717 if (statusText === 'abort') {
718 return;
719 }
Joshua Heskethd50eb582014-04-30 19:51:22 +1000720 $msg.text(options.source + ': ' + errMsg)
721 .addClass('alert-danger')
722 .removeClass('zuul-msg-wrap-off')
723 .show();
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000724 })
725 .complete(function () {
726 xhr = undefined;
727 app.emit('update-end');
728 });
729
730 return xhr;
731 },
732
733 update_sparklines: function() {
734 $.each(zuul_sparkline_urls, function(name, url) {
735 var newimg = new Image();
736 var parts = url.split('#');
737 newimg.src = parts[0] + '#' + new Date().getTime();
Timo Tijhof4be9f742015-04-02 01:13:19 +0100738 $(newimg).load(function () {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000739 zuul_sparkline_urls[name] = newimg.src;
740 });
741 });
742 },
743
744 emit: function () {
745 $jq.trigger.apply($jq, arguments);
746 return this;
747 },
748 on: function () {
749 $jq.on.apply($jq, arguments);
750 return this;
751 },
752 one: function () {
753 $jq.one.apply($jq, arguments);
754 return this;
755 },
756
757 control_form: function() {
758 // Build the filter form filling anything from cookies
759
760 var $control_form = $('<form />')
761 .attr('role', 'form')
762 .addClass('form-inline')
763 .submit(this.handle_filter_change);
764
765 $control_form
766 .append(this.filter_form_group())
767 .append(this.expand_form_group());
768
769 return $control_form;
770 },
771
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100772 filter_form_group: function() {
Joshua Heskethace48892014-03-22 17:18:31 +1100773 // Update the filter form with a clear button if required
774
775 var $label = $('<label />')
776 .addClass('control-label')
777 .attr('for', 'filter_string')
778 .text('Filters')
779 .css('padding-right', '0.5em');
780
781 var $input = $('<input />')
782 .attr('type', 'text')
783 .attr('id', 'filter_string')
784 .addClass('form-control')
785 .attr('title',
786 'project(s), pipeline(s) or review(s) comma ' +
787 'separated')
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100788 .attr('value', current_filter);
Joshua Heskethace48892014-03-22 17:18:31 +1100789
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000790 $input.change(this.handle_filter_change);
Joshua Heskethace48892014-03-22 17:18:31 +1100791
792 var $clear_icon = $('<span />')
793 .addClass('form-control-feedback')
794 .addClass('glyphicon glyphicon-remove-circle')
795 .attr('id', 'filter_form_clear_box')
796 .attr('title', 'clear filter')
797 .css('cursor', 'pointer');
798
799 $clear_icon.click(function() {
800 $('#filter_string').val('').change();
801 });
802
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100803 if (current_filter === '') {
Joshua Heskethace48892014-03-22 17:18:31 +1100804 $clear_icon.hide();
805 }
806
807 var $form_group = $('<div />')
808 .addClass('form-group has-feedback')
809 .append($label, $input, $clear_icon);
810 return $form_group;
811 },
812
Joshua Heskethae230f62014-03-22 22:14:44 +1100813 expand_form_group: function() {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700814 var expand_by_default = (
Joshua Heskethae230f62014-03-22 22:14:44 +1100815 read_cookie('zuul_expand_by_default', false) === 'true');
816
Monty Taylor860bb0a2014-03-22 09:41:25 -0700817 var $checkbox = $('<input />')
Joshua Heskethae230f62014-03-22 22:14:44 +1100818 .attr('type', 'checkbox')
819 .attr('id', 'expand_by_default')
820 .prop('checked', expand_by_default)
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000821 .change(this.handle_expand_by_default);
Joshua Heskethae230f62014-03-22 22:14:44 +1100822
Monty Taylor860bb0a2014-03-22 09:41:25 -0700823 var $label = $('<label />')
Joshua Heskethae230f62014-03-22 22:14:44 +1100824 .css('padding-left', '1em')
825 .html('Expand by default: ')
826 .append($checkbox);
827
828 var $form_group = $('<div />')
829 .addClass('checkbox')
830 .append($label);
831 return $form_group;
832 },
833
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000834 handle_filter_change: function() {
835 // Update the filter and save it to a cookie
836 current_filter = $('#filter_string').val();
837 set_cookie('zuul_filter_string', current_filter);
838 if (current_filter === '') {
839 $('#filter_form_clear_box').hide();
Joshua Heskethace48892014-03-22 17:18:31 +1100840 }
841 else {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000842 $('#filter_form_clear_box').show();
Joshua Heskethace48892014-03-22 17:18:31 +1100843 }
Joshua Heskethace48892014-03-22 17:18:31 +1100844
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000845 $('.zuul-change-box').each(function(index, obj) {
846 var $change_box = $(obj);
847 format.display_patchset($change_box, 200);
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100848 });
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000849 return false;
Timo Tijhof51516cd2013-04-09 01:32:29 +0200850 },
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000851
852 handle_expand_by_default: function(e) {
853 // Handle toggling expand by default
854 set_cookie('zuul_expand_by_default', e.target.checked);
855 collapsed_exceptions = [];
856 $('.zuul-change-box').each(function(index, obj) {
857 var $change_box = $(obj);
858 format.display_patchset($change_box, 200);
859 });
860 },
861
862 create_tree: function(pipeline) {
863 var count = 0;
864 var pipeline_max_tree_columns = 1;
865 $.each(pipeline.change_queues, function(change_queue_i,
866 change_queue) {
867 var tree = [];
868 var max_tree_columns = 1;
869 var changes = [];
870 var last_tree_length = 0;
871 $.each(change_queue.heads, function(head_i, head) {
872 $.each(head, function(change_i, change) {
873 changes[change.id] = change;
874 change._tree_position = change_i;
875 });
876 });
877 $.each(change_queue.heads, function(head_i, head) {
878 $.each(head, function(change_i, change) {
James E. Blaird1d3ce32015-02-11 17:56:45 -0800879 if (change.live === true) {
880 count += 1;
881 }
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000882 var idx = tree.indexOf(change.id);
883 if (idx > -1) {
884 change._tree_index = idx;
885 // remove...
886 tree[idx] = null;
887 while (tree[tree.length - 1] === null) {
888 tree.pop();
889 }
890 } else {
891 change._tree_index = 0;
892 }
893 change._tree_branches = [];
894 change._tree = [];
895 if (typeof(change.items_behind) === 'undefined') {
896 change.items_behind = [];
897 }
898 change.items_behind.sort(function(a, b) {
899 return (changes[b]._tree_position -
900 changes[a]._tree_position);
901 });
902 $.each(change.items_behind, function(i, id) {
903 tree.push(id);
904 if (tree.length>last_tree_length &&
905 last_tree_length > 0) {
906 change._tree_branches.push(
907 tree.length - 1);
908 }
909 });
910 if (tree.length > max_tree_columns) {
911 max_tree_columns = tree.length;
912 }
913 if (tree.length > pipeline_max_tree_columns) {
914 pipeline_max_tree_columns = tree.length;
915 }
916 change._tree = tree.slice(0); // make a copy
917 last_tree_length = tree.length;
918 });
919 });
920 change_queue._tree_columns = max_tree_columns;
921 });
922 pipeline._tree_columns = pipeline_max_tree_columns;
923 return count;
924 },
925 };
926
927 $jq = $(app);
928 return {
929 options: options,
930 format: format,
931 app: app,
932 jq: $jq
933 };
Timo Tijhof4be9f742015-04-02 01:13:19 +0100934 };
Timo Tijhof51516cd2013-04-09 01:32:29 +0200935}(jQuery));