blob: aec7a465577cc7fa2b9eb262acd9a4f3ba867f7c [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) {
Jamie Lennoxa6b33ba2017-02-10 11:33:44 +1100282 var github_id = change.id.match(/^([0-9]+),([0-9a-f]{40})$/);
283 if (github_id) {
284 $change_link.append(
285 $('<a />').attr('href', change.url).append(
286 $('<abbr />')
287 .attr('title', change.id)
288 .text('#' + github_id[1])
289 )
290 );
291 } else if (/^[0-9a-f]{40}$/.test(change.id)) {
Joshua Heskethcd7a0772014-08-21 11:32:44 +1000292 var change_id_short = change.id.slice(0, 7);
293 $change_link.append(
294 $('<a />').attr('href', change.url).append(
295 $('<abbr />')
296 .attr('title', change.id)
297 .text(change_id_short)
298 )
299 );
300 }
301 else {
302 $change_link.append(
303 $('<a />').attr('href', change.url).text(change.id)
304 );
305 }
Timo Tijhof51516cd2013-04-09 01:32:29 +0200306 }
Joshua Hesketh298c4912014-03-20 16:06:25 +1100307 else {
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100308 $change_link.text(change_id);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200309 }
Timo Tijhof51516cd2013-04-09 01:32:29 +0200310
Monty Taylor860bb0a2014-03-22 09:41:25 -0700311 var $change_progress_row_left = $('<div />')
Timo Tijhofa62e8492015-04-02 03:05:04 +0100312 .addClass('col-xs-4')
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100313 .append($change_link);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700314 var $change_progress_row_right = $('<div />')
Timo Tijhofa62e8492015-04-02 03:05:04 +0100315 .addClass('col-xs-8')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000316 .append(this.change_total_progress_bar(change));
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100317
Monty Taylor860bb0a2014-03-22 09:41:25 -0700318 var $change_progress_row = $('<div />')
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100319 .addClass('row')
320 .append($change_progress_row_left)
Monty Taylor860bb0a2014-03-22 09:41:25 -0700321 .append($change_progress_row_right);
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100322
Monty Taylor860bb0a2014-03-22 09:41:25 -0700323 var $project_span = $('<span />')
Joshua Heskethace48892014-03-22 17:18:31 +1100324 .addClass('change_project')
325 .text(change.project);
326
Monty Taylor860bb0a2014-03-22 09:41:25 -0700327 var $left = $('<div />')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100328 .addClass('col-xs-8')
Joshua Hesketh1751e4f2014-04-01 13:24:51 +1100329 .append($project_span, $change_progress_row);
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100330
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000331 var remaining_time = this.time(
Monty Taylor860bb0a2014-03-22 09:41:25 -0700332 change.remaining_time, true);
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000333 var enqueue_time = this.enqueue_time(
Monty Taylor860bb0a2014-03-22 09:41:25 -0700334 change.enqueue_time);
335 var $remaining_time = $('<small />').addClass('time')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100336 .attr('title', 'Remaining Time').html(remaining_time);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700337 var $enqueue_time = $('<small />').addClass('time')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100338 .attr('title', 'Elapsed Time').html(enqueue_time);
339
James E. Blair6dc954b2015-02-11 18:32:19 -0800340 var $right = $('<div />');
341 if (change.live === true) {
342 $right.addClass('col-xs-4 text-right')
343 .append($remaining_time, $('<br />'), $enqueue_time);
344 }
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100345
Monty Taylor860bb0a2014-03-22 09:41:25 -0700346 var $header = $('<div />')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100347 .addClass('row')
348 .append($left, $right);
349 return $header;
350 },
351
352 change_list: function(jobs) {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000353 var format = this;
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100354 var $list = $('<ul />')
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100355 .addClass('list-group zuul-patchset-body');
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100356
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100357 $.each(jobs, function (i, job) {
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100358 var $item = $('<li />')
359 .addClass('list-group-item')
360 .addClass('zuul-change-job')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000361 .append(format.job(job));
Joshua Hesketh298c4912014-03-20 16:06:25 +1100362 $list.append($item);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200363 });
364
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100365 return $list;
366 },
367
368 change_panel: function (change) {
369 var $header = $('<div />')
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100370 .addClass('panel-heading zuul-patchset-header')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000371 .append(this.change_header(change));
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100372
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100373 var panel_id = change.id ? change.id.replace(',', '_')
374 : change.project.replace('/', '_') +
Monty Taylor860bb0a2014-03-22 09:41:25 -0700375 '-' + change.enqueue_time;
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100376 var $panel = $('<div />')
Monty Taylor860bb0a2014-03-22 09:41:25 -0700377 .attr('id', panel_id)
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100378 .addClass('panel panel-default zuul-change')
379 .append($header)
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000380 .append(this.change_list(change.jobs));
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100381
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000382 $header.click(this.toggle_patchset);
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100383 return $panel;
Timo Tijhof51516cd2013-04-09 01:32:29 +0200384 },
385
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100386 change_status_icon: function(change) {
387 var icon_name = 'green.png';
388 var icon_title = 'Succeeding';
389
390 if (change.active !== true) {
391 // Grey icon
392 icon_name = 'grey.png';
393 icon_title = 'Waiting until closer to head of queue to' +
394 ' start jobs';
395 }
James E. Blair107c3852015-02-07 08:23:10 -0800396 else if (change.live !== true) {
397 // Grey icon
398 icon_name = 'grey.png';
Joshua Hesketh6bc619d2015-02-11 09:30:21 +1100399 icon_title = 'Dependent change required for testing';
James E. Blair107c3852015-02-07 08:23:10 -0800400 }
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100401 else if (change.failing_reasons &&
402 change.failing_reasons.length > 0) {
403 var reason = change.failing_reasons.join(', ');
404 icon_title = 'Failing because ' + reason;
405 if (reason.match(/merge conflict/)) {
406 // Black icon
407 icon_name = 'black.png';
408 }
409 else {
410 // Red icon
411 icon_name = 'red.png';
412 }
413 }
414
415 var $icon = $('<img />')
416 .attr('src', 'images/' + icon_name)
417 .attr('title', icon_title)
418 .css('margin-top', '-6px');
419
420 return $icon;
421 },
422
423 change_with_status_tree: function(change, change_queue) {
424 var $change_row = $('<tr />');
425
426 for (var i = 0; i < change_queue._tree_columns; i++) {
427 var $tree_cell = $('<td />')
428 .css('height', '100%')
429 .css('padding', '0 0 10px 0')
430 .css('margin', '0')
431 .css('width', '16px')
432 .css('min-width', '16px')
433 .css('overflow', 'hidden')
434 .css('vertical-align', 'top');
435
436 if (i < change._tree.length && change._tree[i] !== null) {
437 $tree_cell.css('background-image',
438 'url(\'images/line.png\')')
439 .css('background-repeat', 'repeat-y');
440 }
441
442 if (i === change._tree_index) {
443 $tree_cell.append(
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000444 this.change_status_icon(change));
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100445 }
446 if (change._tree_branches.indexOf(i) !== -1) {
447 var $image = $('<img />')
448 .css('vertical-align', 'baseline');
449 if (change._tree_branches.indexOf(i) ===
450 change._tree_branches.length - 1) {
451 // Angle line
452 $image.attr('src', 'images/line-angle.png');
453 }
454 else {
455 // T line
456 $image.attr('src', 'images/line-t.png');
457 }
458 $tree_cell.append($image);
459 }
460 $change_row.append($tree_cell);
461 }
462
463 var change_width = 360 - 16*change_queue._tree_columns;
464 var $change_column = $('<td />')
465 .css('width', change_width + 'px')
466 .addClass('zuul-change-cell')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000467 .append(this.change_panel(change));
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100468
469 $change_row.append($change_column);
470
471 var $change_table = $('<table />')
472 .addClass('zuul-change-box')
473 .css('-moz-box-sizing', 'content-box')
474 .css('box-sizing', 'content-box')
475 .append($change_row);
476
477 return $change_table;
478 },
479
Joshua Hesketh9d013542014-04-03 13:08:04 +1100480 pipeline_sparkline: function(pipeline_name) {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000481 if (options.graphite_url !== '') {
Joshua Hesketh9d013542014-04-03 13:08:04 +1100482 var $sparkline = $('<img />')
483 .addClass('pull-right')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000484 .attr('src', get_sparkline_url(pipeline_name));
Joshua Hesketh9d013542014-04-03 13:08:04 +1100485 return $sparkline;
486 }
487 return false;
488 },
489
490 pipeline_header: function(pipeline, count) {
491 // Format the pipeline name, sparkline and description
492 var $header_div = $('<div />')
493 .addClass('zuul-pipeline-header');
494
495 var $heading = $('<h3 />')
496 .css('vertical-align', 'middle')
497 .text(pipeline.name)
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100498 .append(
Joshua Hesketh9d013542014-04-03 13:08:04 +1100499 $('<span />')
500 .addClass('badge pull-right')
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100501 .css('vertical-align', 'middle')
Joshua Hesketh9d013542014-04-03 13:08:04 +1100502 .css('margin-top', '0.5em')
503 .text(count)
504 )
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000505 .append(this.pipeline_sparkline(pipeline.name));
Joshua Hesketh9d013542014-04-03 13:08:04 +1100506
507 $header_div.append($heading);
Joshua Hesketh298c4912014-03-20 16:06:25 +1100508
Timo Tijhof51516cd2013-04-09 01:32:29 +0200509 if (typeof pipeline.description === 'string') {
Alexander Evseev057aed12015-10-28 17:17:04 +0300510 var descr = $('<small />')
511 $.each( pipeline.description.split(/\r?\n\r?\n/), function(index, descr_part){
512 descr.append($('<p />').text(descr_part));
513 });
Joshua Hesketh9d013542014-04-03 13:08:04 +1100514 $header_div.append(
Alexander Evseev057aed12015-10-28 17:17:04 +0300515 $('<p />').append(descr)
Joshua Hesketh298c4912014-03-20 16:06:25 +1100516 );
Timo Tijhof51516cd2013-04-09 01:32:29 +0200517 }
Joshua Hesketh9d013542014-04-03 13:08:04 +1100518 return $header_div;
519 },
520
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000521 pipeline: function (pipeline, count) {
522 var format = this;
Joshua Hesketh9d013542014-04-03 13:08:04 +1100523 var $html = $('<div />')
524 .addClass('zuul-pipeline col-md-4')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000525 .append(this.pipeline_header(pipeline, count));
Timo Tijhof51516cd2013-04-09 01:32:29 +0200526
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100527 $.each(pipeline.change_queues,
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100528 function (queue_i, change_queue) {
529 $.each(change_queue.heads, function (head_i, changes) {
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100530 if (pipeline.change_queues.length > 1 &&
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100531 head_i === 0) {
532 var name = change_queue.name;
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100533 var short_name = name;
534 if (short_name.length > 32) {
535 short_name = short_name.substr(0, 32) + '...';
Timo Tijhof51516cd2013-04-09 01:32:29 +0200536 }
Joshua Hesketh298c4912014-03-20 16:06:25 +1100537 $html.append(
538 $('<p />')
539 .text('Queue: ')
540 .append(
541 $('<abbr />')
542 .attr('title', name)
543 .text(short_name)
544 )
545 );
Timo Tijhof51516cd2013-04-09 01:32:29 +0200546 }
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100547
548 $.each(changes, function (change_i, change) {
549 var $change_box =
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000550 format.change_with_status_tree(
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100551 change, change_queue);
552 $html.append($change_box);
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000553 format.display_patchset($change_box);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200554 });
555 });
556 });
Joshua Hesketh298c4912014-03-20 16:06:25 +1100557 return $html;
Joshua Heskethace48892014-03-22 17:18:31 +1100558 },
559
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000560 toggle_patchset: function(e) {
561 // Toggle showing/hiding the patchset when the header is
562 // clicked.
563
564 // Grab the patchset panel
565 var $panel = $(e.target).parents('.zuul-change');
566 var $body = $panel.children('.zuul-patchset-body');
567 $body.toggle(200);
568 var collapsed_index = collapsed_exceptions.indexOf(
569 $panel.attr('id'));
570 if (collapsed_index === -1 ) {
571 // Currently not an exception, add it to list
572 collapsed_exceptions.push($panel.attr('id'));
573 }
574 else {
575 // Currently an except, remove from exceptions
576 collapsed_exceptions.splice(collapsed_index, 1);
577 }
578 },
579
580 display_patchset: function($change_box, animate) {
581 // Determine if to show or hide the patchset and/or the results
582 // when loaded
583
584 // See if we should hide the body/results
585 var $panel = $change_box.find('.zuul-change');
586 var panel_change = $panel.attr('id');
587 var $body = $panel.children('.zuul-patchset-body');
588 var expand_by_default = $('#expand_by_default')
589 .prop('checked');
590
591 var collapsed_index = collapsed_exceptions
592 .indexOf(panel_change);
593
594 if (expand_by_default && collapsed_index === -1 ||
595 !expand_by_default && collapsed_index !== -1) {
596 // Expand by default, or is an exception
597 $body.show(animate);
598 }
599 else {
600 $body.hide(animate);
601 }
602
603 // Check if we should hide the whole panel
604 var panel_project = $panel.find('.change_project').text()
605 .toLowerCase();
606
607
608 var panel_pipeline = $change_box
609 .parents('.zuul-pipeline')
610 .find('.zuul-pipeline-header > h3')
611 .html()
612 .toLowerCase();
613
614 if (current_filter !== '') {
615 var show_panel = false;
616 var filter = current_filter.trim().split(/[\s,]+/);
617 $.each(filter, function(index, f_val) {
618 if (f_val !== '') {
619 f_val = f_val.toLowerCase();
620 if (panel_project.indexOf(f_val) !== -1 ||
621 panel_pipeline.indexOf(f_val) !== -1 ||
622 panel_change.indexOf(f_val) !== -1) {
623 show_panel = true;
624 }
625 }
626 });
627 if (show_panel === true) {
628 $change_box.show(animate);
629 }
630 else {
631 $change_box.hide(animate);
632 }
633 }
634 else {
635 $change_box.show(animate);
636 }
637 },
638 };
639
640 var app = {
641 schedule: function (app) {
Timo Tijhof4be9f742015-04-02 01:13:19 +0100642 app = app || this;
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000643 if (!options.enabled) {
644 setTimeout(function() {app.schedule(app);}, 5000);
645 return;
646 }
647 app.update().complete(function () {
648 setTimeout(function() {app.schedule(app);}, 5000);
649 });
650
651 /* Only update graphs every minute */
652 if (zuul_graph_update_count > 11) {
653 zuul_graph_update_count = 0;
654 zuul.update_sparklines();
655 }
656 },
657
658 /** @return {jQuery.Promise} */
659 update: function () {
660 // Cancel the previous update if it hasn't completed yet.
661 if (xhr) {
662 xhr.abort();
663 }
664
665 this.emit('update-start');
666 var app = this;
667
Timo Tijhof4be9f742015-04-02 01:13:19 +0100668 var $msg = $(options.msg_id);
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000669 xhr = $.getJSON(options.source)
670 .done(function (data) {
671 if ('message' in data) {
672 $msg.removeClass('alert-danger')
673 .addClass('alert-info')
674 .text(data.message)
675 .show();
676 } else {
677 $msg.empty()
678 .hide();
679 }
680
681 if ('zuul_version' in data) {
682 $('#zuul-version-span').text(data.zuul_version);
683 }
684 if ('last_reconfigured' in data) {
685 var last_reconfigured =
686 new Date(data.last_reconfigured);
687 $('#last-reconfigured-span').text(
688 last_reconfigured.toString());
689 }
690
691 var $pipelines = $(options.pipelines_id);
692 $pipelines.html('');
693 $.each(data.pipelines, function (i, pipeline) {
694 var count = app.create_tree(pipeline);
695 $pipelines.append(
696 format.pipeline(pipeline, count));
697 });
698
699 $(options.queue_events_num).text(
700 data.trigger_event_queue ?
701 data.trigger_event_queue.length : '0'
702 );
703 $(options.queue_results_num).text(
704 data.result_event_queue ?
705 data.result_event_queue.length : '0'
706 );
707 })
Timo Tijhof67f4ac12015-04-10 00:33:50 +0100708 .fail(function (jqXHR, statusText, errMsg) {
709 if (statusText === 'abort') {
710 return;
711 }
Joshua Heskethd50eb582014-04-30 19:51:22 +1000712 $msg.text(options.source + ': ' + errMsg)
713 .addClass('alert-danger')
714 .removeClass('zuul-msg-wrap-off')
715 .show();
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000716 })
717 .complete(function () {
718 xhr = undefined;
719 app.emit('update-end');
720 });
721
722 return xhr;
723 },
724
725 update_sparklines: function() {
726 $.each(zuul_sparkline_urls, function(name, url) {
727 var newimg = new Image();
728 var parts = url.split('#');
729 newimg.src = parts[0] + '#' + new Date().getTime();
Timo Tijhof4be9f742015-04-02 01:13:19 +0100730 $(newimg).load(function () {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000731 zuul_sparkline_urls[name] = newimg.src;
732 });
733 });
734 },
735
736 emit: function () {
737 $jq.trigger.apply($jq, arguments);
738 return this;
739 },
740 on: function () {
741 $jq.on.apply($jq, arguments);
742 return this;
743 },
744 one: function () {
745 $jq.one.apply($jq, arguments);
746 return this;
747 },
748
749 control_form: function() {
750 // Build the filter form filling anything from cookies
751
752 var $control_form = $('<form />')
753 .attr('role', 'form')
754 .addClass('form-inline')
755 .submit(this.handle_filter_change);
756
757 $control_form
758 .append(this.filter_form_group())
759 .append(this.expand_form_group());
760
761 return $control_form;
762 },
763
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100764 filter_form_group: function() {
Joshua Heskethace48892014-03-22 17:18:31 +1100765 // Update the filter form with a clear button if required
766
767 var $label = $('<label />')
768 .addClass('control-label')
769 .attr('for', 'filter_string')
770 .text('Filters')
771 .css('padding-right', '0.5em');
772
773 var $input = $('<input />')
774 .attr('type', 'text')
775 .attr('id', 'filter_string')
776 .addClass('form-control')
777 .attr('title',
778 'project(s), pipeline(s) or review(s) comma ' +
779 'separated')
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100780 .attr('value', current_filter);
Joshua Heskethace48892014-03-22 17:18:31 +1100781
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000782 $input.change(this.handle_filter_change);
Joshua Heskethace48892014-03-22 17:18:31 +1100783
784 var $clear_icon = $('<span />')
785 .addClass('form-control-feedback')
786 .addClass('glyphicon glyphicon-remove-circle')
787 .attr('id', 'filter_form_clear_box')
788 .attr('title', 'clear filter')
789 .css('cursor', 'pointer');
790
791 $clear_icon.click(function() {
792 $('#filter_string').val('').change();
793 });
794
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100795 if (current_filter === '') {
Joshua Heskethace48892014-03-22 17:18:31 +1100796 $clear_icon.hide();
797 }
798
799 var $form_group = $('<div />')
800 .addClass('form-group has-feedback')
801 .append($label, $input, $clear_icon);
802 return $form_group;
803 },
804
Joshua Heskethae230f62014-03-22 22:14:44 +1100805 expand_form_group: function() {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700806 var expand_by_default = (
Joshua Heskethae230f62014-03-22 22:14:44 +1100807 read_cookie('zuul_expand_by_default', false) === 'true');
808
Monty Taylor860bb0a2014-03-22 09:41:25 -0700809 var $checkbox = $('<input />')
Joshua Heskethae230f62014-03-22 22:14:44 +1100810 .attr('type', 'checkbox')
811 .attr('id', 'expand_by_default')
812 .prop('checked', expand_by_default)
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000813 .change(this.handle_expand_by_default);
Joshua Heskethae230f62014-03-22 22:14:44 +1100814
Monty Taylor860bb0a2014-03-22 09:41:25 -0700815 var $label = $('<label />')
Joshua Heskethae230f62014-03-22 22:14:44 +1100816 .css('padding-left', '1em')
817 .html('Expand by default: ')
818 .append($checkbox);
819
820 var $form_group = $('<div />')
821 .addClass('checkbox')
822 .append($label);
823 return $form_group;
824 },
825
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000826 handle_filter_change: function() {
827 // Update the filter and save it to a cookie
828 current_filter = $('#filter_string').val();
829 set_cookie('zuul_filter_string', current_filter);
830 if (current_filter === '') {
831 $('#filter_form_clear_box').hide();
Joshua Heskethace48892014-03-22 17:18:31 +1100832 }
833 else {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000834 $('#filter_form_clear_box').show();
Joshua Heskethace48892014-03-22 17:18:31 +1100835 }
Joshua Heskethace48892014-03-22 17:18:31 +1100836
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000837 $('.zuul-change-box').each(function(index, obj) {
838 var $change_box = $(obj);
839 format.display_patchset($change_box, 200);
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100840 });
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000841 return false;
Timo Tijhof51516cd2013-04-09 01:32:29 +0200842 },
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000843
844 handle_expand_by_default: function(e) {
845 // Handle toggling expand by default
846 set_cookie('zuul_expand_by_default', e.target.checked);
847 collapsed_exceptions = [];
848 $('.zuul-change-box').each(function(index, obj) {
849 var $change_box = $(obj);
850 format.display_patchset($change_box, 200);
851 });
852 },
853
854 create_tree: function(pipeline) {
855 var count = 0;
856 var pipeline_max_tree_columns = 1;
857 $.each(pipeline.change_queues, function(change_queue_i,
858 change_queue) {
859 var tree = [];
860 var max_tree_columns = 1;
861 var changes = [];
862 var last_tree_length = 0;
863 $.each(change_queue.heads, function(head_i, head) {
864 $.each(head, function(change_i, change) {
865 changes[change.id] = change;
866 change._tree_position = change_i;
867 });
868 });
869 $.each(change_queue.heads, function(head_i, head) {
870 $.each(head, function(change_i, change) {
James E. Blaird1d3ce32015-02-11 17:56:45 -0800871 if (change.live === true) {
872 count += 1;
873 }
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000874 var idx = tree.indexOf(change.id);
875 if (idx > -1) {
876 change._tree_index = idx;
877 // remove...
878 tree[idx] = null;
879 while (tree[tree.length - 1] === null) {
880 tree.pop();
881 }
882 } else {
883 change._tree_index = 0;
884 }
885 change._tree_branches = [];
886 change._tree = [];
887 if (typeof(change.items_behind) === 'undefined') {
888 change.items_behind = [];
889 }
890 change.items_behind.sort(function(a, b) {
891 return (changes[b]._tree_position -
892 changes[a]._tree_position);
893 });
894 $.each(change.items_behind, function(i, id) {
895 tree.push(id);
896 if (tree.length>last_tree_length &&
897 last_tree_length > 0) {
898 change._tree_branches.push(
899 tree.length - 1);
900 }
901 });
902 if (tree.length > max_tree_columns) {
903 max_tree_columns = tree.length;
904 }
905 if (tree.length > pipeline_max_tree_columns) {
906 pipeline_max_tree_columns = tree.length;
907 }
908 change._tree = tree.slice(0); // make a copy
909 last_tree_length = tree.length;
910 });
911 });
912 change_queue._tree_columns = max_tree_columns;
913 });
914 pipeline._tree_columns = pipeline_max_tree_columns;
915 return count;
916 },
917 };
918
919 $jq = $(app);
920 return {
921 options: options,
922 format: format,
923 app: app,
924 jq: $jq
925 };
Timo Tijhof4be9f742015-04-02 01:13:19 +0100926 };
Timo Tijhof51516cd2013-04-09 01:32:29 +0200927}(jQuery));