blob: d973948be83c9555e9af8bac4990a1a6e6e43b1a [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//
3// Copyright 2012 OpenStack Foundation
4// Copyright 2013 Timo Tijhof
5// Copyright 2013 Wikimedia Foundation
Joshua Hesketh6b1a2182014-03-21 14:40:04 +11006// Copyright 2014 Rackspace Australia
Timo Tijhof51516cd2013-04-09 01:32:29 +02007//
8// Licensed under the Apache License, Version 2.0 (the "License"); you may
9// not use this file except in compliance with the License. You may obtain
10// a copy of the License at
11//
12// http://www.apache.org/licenses/LICENSE-2.0
13//
14// Unless required by applicable law or agreed to in writing, software
15// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17// License for the specific language governing permissions and limitations
18// under the License.
19
20(function ($) {
Timo Tijhof4be9f742015-04-02 01:13:19 +010021 'use strict';
22
Joshua Heskethace48892014-03-22 17:18:31 +110023 function set_cookie(name, value) {
Monty Taylor860bb0a2014-03-22 09:41:25 -070024 document.cookie = name + '=' + value + '; path=/';
Joshua Heskethace48892014-03-22 17:18:31 +110025 }
26
27 function read_cookie(name, default_value) {
Monty Taylor860bb0a2014-03-22 09:41:25 -070028 var nameEQ = name + '=';
Joshua Heskethace48892014-03-22 17:18:31 +110029 var ca = document.cookie.split(';');
30 for(var i=0;i < ca.length;i++) {
31 var c = ca[i];
Monty Taylor860bb0a2014-03-22 09:41:25 -070032 while (c.charAt(0) === ' ') {
33 c = c.substring(1, c.length);
34 }
35 if (c.indexOf(nameEQ) === 0) {
Joshua Heskethace48892014-03-22 17:18:31 +110036 return c.substring(nameEQ.length, c.length);
37 }
38 }
39 return default_value;
40 }
41
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +100042 $.zuul = function(options) {
Timo Tijhof4be9f742015-04-02 01:13:19 +010043 options = $.extend({
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +100044 'enabled': true,
45 'graphite_url': '',
46 'source': 'status.json',
47 'msg_id': '#zuul_msg',
48 'pipelines_id': '#zuul_pipelines',
49 'queue_events_num': '#zuul_queue_events_num',
50 'queue_results_num': '#zuul_queue_results_num',
51 }, options);
Joshua Heskethace48892014-03-22 17:18:31 +110052
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +100053 var collapsed_exceptions = [];
Joshua Hesketh6f94dd12014-08-20 16:14:53 +100054 var current_filter = read_cookie('zuul_filter_string', '');
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +100055 var $jq;
Timo Tijhof51516cd2013-04-09 01:32:29 +020056
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +100057 var xhr,
58 zuul_graph_update_count = 0,
59 zuul_sparkline_urls = {};
Joshua Hesketh9d013542014-04-03 13:08:04 +110060
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +100061 function get_sparkline_url(pipeline_name) {
62 if (options.graphite_url !== '') {
63 if (!(pipeline_name in zuul_sparkline_urls)) {
64 zuul_sparkline_urls[pipeline_name] = $.fn.graphite
65 .geturl({
66 url: options.graphite_url,
67 from: "-8hours",
68 width: 100,
69 height: 26,
70 margin: 0,
71 hideLegend: true,
72 hideAxes: true,
73 hideGrid: true,
74 target: [
75 "color(stats.gauges.zuul.pipeline." + pipeline_name
Timo Tijhof4be9f742015-04-02 01:13:19 +010076 + ".current_changes, '6b8182')"
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +100077 ]
Joshua Hesketh298c4912014-03-20 16:06:25 +110078 });
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +100079 }
80 return zuul_sparkline_urls[pipeline_name];
81 }
82 return false;
83 }
Joshua Hesketh298c4912014-03-20 16:06:25 +110084
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +100085 var format = {
Joshua Hesketh6b1a2182014-03-21 14:40:04 +110086 job: function(job) {
Joshua Hesketh9e6ce532014-04-01 13:11:53 +110087 var $job_line = $('<span />');
88
Joshua Hesketh6b1a2182014-03-21 14:40:04 +110089 if (job.url !== null) {
Joshua Hesketh9e6ce532014-04-01 13:11:53 +110090 $job_line.append(
91 $('<a />')
92 .addClass('zuul-job-name')
93 .attr('href', job.url)
94 .text(job.name)
95 );
Joshua Hesketh6b1a2182014-03-21 14:40:04 +110096 }
Joshua Hesketh9e6ce532014-04-01 13:11:53 +110097 else {
98 $job_line.append(
99 $('<span />')
100 .addClass('zuul-job-name')
101 .text(job.name)
102 );
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100103 }
Joshua Hesketh9e6ce532014-04-01 13:11:53 +1100104
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000105 $job_line.append(this.job_status(job));
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100106
107 if (job.voting === false) {
108 $job_line.append(
Joshua Hesketh9e6ce532014-04-01 13:11:53 +1100109 $(' <small />')
110 .addClass('zuul-non-voting-desc')
111 .text(' (non-voting)')
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100112 );
113 }
Joshua Hesketh9e6ce532014-04-01 13:11:53 +1100114
Joshua Hesketha95fd552014-08-21 11:21:05 +1000115 $job_line.append($('<div style="clear: both"></div>'));
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100116 return $job_line;
117 },
118
119 job_status: function(job) {
120 var result = job.result ? job.result.toLowerCase() : null;
121 if (result === null) {
122 result = job.url ? 'in progress' : 'queued';
123 }
124
Monty Taylor860bb0a2014-03-22 09:41:25 -0700125 if (result === 'in progress') {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000126 return this.job_progress_bar(job.elapsed_time,
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100127 job.remaining_time);
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100128 }
129 else {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000130 return this.status_label(result);
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100131 }
132 },
133
134 status_label: function(result) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700135 var $status = $('<span />');
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100136 $status.addClass('zuul-job-result label');
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100137
138 switch (result) {
139 case 'success':
140 $status.addClass('label-success');
141 break;
142 case 'failure':
143 $status.addClass('label-danger');
144 break;
145 case 'unstable':
146 $status.addClass('label-warning');
147 break;
Alexander Evseev3103a842015-11-17 10:12:45 +0300148 case 'skipped':
149 $status.addClass('label-info');
150 break;
Antoine Musso4c2053d2016-02-04 23:35:23 +0100151 // 'in progress' 'queued' 'lost' 'aborted' ...
152 default:
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100153 $status.addClass('label-default');
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100154 }
155 $status.text(result);
156 return $status;
157 },
158
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100159 job_progress_bar: function(elapsed_time, remaining_time) {
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100160 var progress_percent = 100 * (elapsed_time / (elapsed_time +
161 remaining_time));
162 var $bar_inner = $('<div />')
163 .addClass('progress-bar')
164 .attr('role', 'progressbar')
165 .attr('aria-valuenow', 'progressbar')
166 .attr('aria-valuemin', progress_percent)
167 .attr('aria-valuemin', '0')
168 .attr('aria-valuemax', '100')
169 .css('width', progress_percent + '%');
170
171 var $bar_outter = $('<div />')
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100172 .addClass('progress zuul-job-result')
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100173 .append($bar_inner);
174
175 return $bar_outter;
176 },
177
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100178 enqueue_time: function(ms) {
179 // Special format case for enqueue time to add style
180 var hours = 60 * 60 * 1000;
181 var now = Date.now();
182 var delta = now - ms;
Monty Taylor860bb0a2014-03-22 09:41:25 -0700183 var status = 'text-success';
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000184 var text = this.time(delta, true);
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100185 if (delta > (4 * hours)) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700186 status = 'text-danger';
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100187 } else if (delta > (2 * hours)) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700188 status = 'text-warning';
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100189 }
190 return '<span class="' + status + '">' + text + '</span>';
191 },
192
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100193 time: function(ms, words) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700194 if (typeof(words) === 'undefined') {
195 words = false;
196 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100197 var seconds = (+ms)/1000;
198 var minutes = Math.floor(seconds/60);
199 var hours = Math.floor(minutes/60);
200 seconds = Math.floor(seconds % 60);
201 minutes = Math.floor(minutes % 60);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700202 var r = '';
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100203 if (words) {
204 if (hours) {
205 r += hours;
206 r += ' hr ';
207 }
208 r += minutes + ' min';
209 } else {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700210 if (hours < 10) {
211 r += '0';
212 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100213 r += hours + ':';
Monty Taylor860bb0a2014-03-22 09:41:25 -0700214 if (minutes < 10) {
215 r += '0';
216 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100217 r += minutes + ':';
Monty Taylor860bb0a2014-03-22 09:41:25 -0700218 if (seconds < 10) {
219 r += '0';
220 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100221 r += seconds;
222 }
223 return r;
224 },
225
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100226 change_total_progress_bar: function(change) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700227 var job_percent = Math.floor(100 / change.jobs.length);
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100228 var $bar_outter = $('<div />')
229 .addClass('progress zuul-change-total-result');
230
231 $.each(change.jobs, function (i, job) {
232 var result = job.result ? job.result.toLowerCase() : null;
233 if (result === null) {
234 result = job.url ? 'in progress' : 'queued';
235 }
236
Monty Taylor860bb0a2014-03-22 09:41:25 -0700237 if (result !== 'queued') {
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100238 var $bar_inner = $('<div />')
239 .addClass('progress-bar');
240
241 switch (result) {
242 case 'success':
243 $bar_inner.addClass('progress-bar-success');
244 break;
245 case 'lost':
246 case 'failure':
247 $bar_inner.addClass('progress-bar-danger');
248 break;
249 case 'unstable':
250 $bar_inner.addClass('progress-bar-warning');
251 break;
252 case 'in progress':
253 case 'queued':
254 break;
255 }
256 $bar_inner.attr('title', job.name)
257 .css('width', job_percent + '%');
258 $bar_outter.append($bar_inner);
259 }
260 });
261 return $bar_outter;
262 },
263
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100264 change_header: function(change) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700265 var change_id = change.id || 'NA';
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100266 if (change_id.length === 40) {
267 change_id = change_id.substr(0, 7);
Joshua Hesketh298c4912014-03-20 16:06:25 +1100268 }
Timo Tijhof51516cd2013-04-09 01:32:29 +0200269
Monty Taylor860bb0a2014-03-22 09:41:25 -0700270 var $change_link = $('<small />');
Joshua Hesketh298c4912014-03-20 16:06:25 +1100271 if (change.url !== null) {
Joshua Heskethcd7a0772014-08-21 11:32:44 +1000272 if (/^[0-9a-f]{40}$/.test(change.id)) {
273 var change_id_short = change.id.slice(0, 7);
274 $change_link.append(
275 $('<a />').attr('href', change.url).append(
276 $('<abbr />')
277 .attr('title', change.id)
278 .text(change_id_short)
279 )
280 );
281 }
282 else {
283 $change_link.append(
284 $('<a />').attr('href', change.url).text(change.id)
285 );
286 }
Timo Tijhof51516cd2013-04-09 01:32:29 +0200287 }
Joshua Hesketh298c4912014-03-20 16:06:25 +1100288 else {
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100289 $change_link.text(change_id);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200290 }
Timo Tijhof51516cd2013-04-09 01:32:29 +0200291
Monty Taylor860bb0a2014-03-22 09:41:25 -0700292 var $change_progress_row_left = $('<div />')
Timo Tijhofa62e8492015-04-02 03:05:04 +0100293 .addClass('col-xs-4')
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100294 .append($change_link);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700295 var $change_progress_row_right = $('<div />')
Timo Tijhofa62e8492015-04-02 03:05:04 +0100296 .addClass('col-xs-8')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000297 .append(this.change_total_progress_bar(change));
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100298
Monty Taylor860bb0a2014-03-22 09:41:25 -0700299 var $change_progress_row = $('<div />')
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100300 .addClass('row')
301 .append($change_progress_row_left)
Monty Taylor860bb0a2014-03-22 09:41:25 -0700302 .append($change_progress_row_right);
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100303
Monty Taylor860bb0a2014-03-22 09:41:25 -0700304 var $project_span = $('<span />')
Joshua Heskethace48892014-03-22 17:18:31 +1100305 .addClass('change_project')
306 .text(change.project);
307
Monty Taylor860bb0a2014-03-22 09:41:25 -0700308 var $left = $('<div />')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100309 .addClass('col-xs-8')
Joshua Hesketh1751e4f2014-04-01 13:24:51 +1100310 .append($project_span, $change_progress_row);
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100311
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000312 var remaining_time = this.time(
Monty Taylor860bb0a2014-03-22 09:41:25 -0700313 change.remaining_time, true);
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000314 var enqueue_time = this.enqueue_time(
Monty Taylor860bb0a2014-03-22 09:41:25 -0700315 change.enqueue_time);
316 var $remaining_time = $('<small />').addClass('time')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100317 .attr('title', 'Remaining Time').html(remaining_time);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700318 var $enqueue_time = $('<small />').addClass('time')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100319 .attr('title', 'Elapsed Time').html(enqueue_time);
320
James E. Blair6dc954b2015-02-11 18:32:19 -0800321 var $right = $('<div />');
322 if (change.live === true) {
323 $right.addClass('col-xs-4 text-right')
324 .append($remaining_time, $('<br />'), $enqueue_time);
325 }
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100326
Monty Taylor860bb0a2014-03-22 09:41:25 -0700327 var $header = $('<div />')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100328 .addClass('row')
329 .append($left, $right);
330 return $header;
331 },
332
333 change_list: function(jobs) {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000334 var format = this;
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100335 var $list = $('<ul />')
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100336 .addClass('list-group zuul-patchset-body');
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100337
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100338 $.each(jobs, function (i, job) {
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100339 var $item = $('<li />')
340 .addClass('list-group-item')
341 .addClass('zuul-change-job')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000342 .append(format.job(job));
Joshua Hesketh298c4912014-03-20 16:06:25 +1100343 $list.append($item);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200344 });
345
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100346 return $list;
347 },
348
349 change_panel: function (change) {
350 var $header = $('<div />')
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100351 .addClass('panel-heading zuul-patchset-header')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000352 .append(this.change_header(change));
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100353
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100354 var panel_id = change.id ? change.id.replace(',', '_')
355 : change.project.replace('/', '_') +
Monty Taylor860bb0a2014-03-22 09:41:25 -0700356 '-' + change.enqueue_time;
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100357 var $panel = $('<div />')
Monty Taylor860bb0a2014-03-22 09:41:25 -0700358 .attr('id', panel_id)
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100359 .addClass('panel panel-default zuul-change')
360 .append($header)
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000361 .append(this.change_list(change.jobs));
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100362
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000363 $header.click(this.toggle_patchset);
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100364 return $panel;
Timo Tijhof51516cd2013-04-09 01:32:29 +0200365 },
366
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100367 change_status_icon: function(change) {
368 var icon_name = 'green.png';
369 var icon_title = 'Succeeding';
370
371 if (change.active !== true) {
372 // Grey icon
373 icon_name = 'grey.png';
374 icon_title = 'Waiting until closer to head of queue to' +
375 ' start jobs';
376 }
James E. Blair107c3852015-02-07 08:23:10 -0800377 else if (change.live !== true) {
378 // Grey icon
379 icon_name = 'grey.png';
Joshua Hesketh6bc619d2015-02-11 09:30:21 +1100380 icon_title = 'Dependent change required for testing';
James E. Blair107c3852015-02-07 08:23:10 -0800381 }
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100382 else if (change.failing_reasons &&
383 change.failing_reasons.length > 0) {
384 var reason = change.failing_reasons.join(', ');
385 icon_title = 'Failing because ' + reason;
386 if (reason.match(/merge conflict/)) {
387 // Black icon
388 icon_name = 'black.png';
389 }
390 else {
391 // Red icon
392 icon_name = 'red.png';
393 }
394 }
395
396 var $icon = $('<img />')
397 .attr('src', 'images/' + icon_name)
398 .attr('title', icon_title)
399 .css('margin-top', '-6px');
400
401 return $icon;
402 },
403
404 change_with_status_tree: function(change, change_queue) {
405 var $change_row = $('<tr />');
406
407 for (var i = 0; i < change_queue._tree_columns; i++) {
408 var $tree_cell = $('<td />')
409 .css('height', '100%')
410 .css('padding', '0 0 10px 0')
411 .css('margin', '0')
412 .css('width', '16px')
413 .css('min-width', '16px')
414 .css('overflow', 'hidden')
415 .css('vertical-align', 'top');
416
417 if (i < change._tree.length && change._tree[i] !== null) {
418 $tree_cell.css('background-image',
419 'url(\'images/line.png\')')
420 .css('background-repeat', 'repeat-y');
421 }
422
423 if (i === change._tree_index) {
424 $tree_cell.append(
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000425 this.change_status_icon(change));
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100426 }
427 if (change._tree_branches.indexOf(i) !== -1) {
428 var $image = $('<img />')
429 .css('vertical-align', 'baseline');
430 if (change._tree_branches.indexOf(i) ===
431 change._tree_branches.length - 1) {
432 // Angle line
433 $image.attr('src', 'images/line-angle.png');
434 }
435 else {
436 // T line
437 $image.attr('src', 'images/line-t.png');
438 }
439 $tree_cell.append($image);
440 }
441 $change_row.append($tree_cell);
442 }
443
444 var change_width = 360 - 16*change_queue._tree_columns;
445 var $change_column = $('<td />')
446 .css('width', change_width + 'px')
447 .addClass('zuul-change-cell')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000448 .append(this.change_panel(change));
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100449
450 $change_row.append($change_column);
451
452 var $change_table = $('<table />')
453 .addClass('zuul-change-box')
454 .css('-moz-box-sizing', 'content-box')
455 .css('box-sizing', 'content-box')
456 .append($change_row);
457
458 return $change_table;
459 },
460
Joshua Hesketh9d013542014-04-03 13:08:04 +1100461 pipeline_sparkline: function(pipeline_name) {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000462 if (options.graphite_url !== '') {
Joshua Hesketh9d013542014-04-03 13:08:04 +1100463 var $sparkline = $('<img />')
464 .addClass('pull-right')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000465 .attr('src', get_sparkline_url(pipeline_name));
Joshua Hesketh9d013542014-04-03 13:08:04 +1100466 return $sparkline;
467 }
468 return false;
469 },
470
471 pipeline_header: function(pipeline, count) {
472 // Format the pipeline name, sparkline and description
473 var $header_div = $('<div />')
474 .addClass('zuul-pipeline-header');
475
476 var $heading = $('<h3 />')
477 .css('vertical-align', 'middle')
478 .text(pipeline.name)
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100479 .append(
Joshua Hesketh9d013542014-04-03 13:08:04 +1100480 $('<span />')
481 .addClass('badge pull-right')
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100482 .css('vertical-align', 'middle')
Joshua Hesketh9d013542014-04-03 13:08:04 +1100483 .css('margin-top', '0.5em')
484 .text(count)
485 )
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000486 .append(this.pipeline_sparkline(pipeline.name));
Joshua Hesketh9d013542014-04-03 13:08:04 +1100487
488 $header_div.append($heading);
Joshua Hesketh298c4912014-03-20 16:06:25 +1100489
Timo Tijhof51516cd2013-04-09 01:32:29 +0200490 if (typeof pipeline.description === 'string') {
Alexander Evseev057aed12015-10-28 17:17:04 +0300491 var descr = $('<small />')
492 $.each( pipeline.description.split(/\r?\n\r?\n/), function(index, descr_part){
493 descr.append($('<p />').text(descr_part));
494 });
Joshua Hesketh9d013542014-04-03 13:08:04 +1100495 $header_div.append(
Alexander Evseev057aed12015-10-28 17:17:04 +0300496 $('<p />').append(descr)
Joshua Hesketh298c4912014-03-20 16:06:25 +1100497 );
Timo Tijhof51516cd2013-04-09 01:32:29 +0200498 }
Joshua Hesketh9d013542014-04-03 13:08:04 +1100499 return $header_div;
500 },
501
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000502 pipeline: function (pipeline, count) {
503 var format = this;
Joshua Hesketh9d013542014-04-03 13:08:04 +1100504 var $html = $('<div />')
505 .addClass('zuul-pipeline col-md-4')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000506 .append(this.pipeline_header(pipeline, count));
Timo Tijhof51516cd2013-04-09 01:32:29 +0200507
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100508 $.each(pipeline.change_queues,
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100509 function (queue_i, change_queue) {
510 $.each(change_queue.heads, function (head_i, changes) {
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100511 if (pipeline.change_queues.length > 1 &&
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100512 head_i === 0) {
513 var name = change_queue.name;
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100514 var short_name = name;
515 if (short_name.length > 32) {
516 short_name = short_name.substr(0, 32) + '...';
Timo Tijhof51516cd2013-04-09 01:32:29 +0200517 }
Joshua Hesketh298c4912014-03-20 16:06:25 +1100518 $html.append(
519 $('<p />')
520 .text('Queue: ')
521 .append(
522 $('<abbr />')
523 .attr('title', name)
524 .text(short_name)
525 )
526 );
Timo Tijhof51516cd2013-04-09 01:32:29 +0200527 }
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100528
529 $.each(changes, function (change_i, change) {
530 var $change_box =
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000531 format.change_with_status_tree(
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100532 change, change_queue);
533 $html.append($change_box);
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000534 format.display_patchset($change_box);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200535 });
536 });
537 });
Joshua Hesketh298c4912014-03-20 16:06:25 +1100538 return $html;
Joshua Heskethace48892014-03-22 17:18:31 +1100539 },
540
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000541 toggle_patchset: function(e) {
542 // Toggle showing/hiding the patchset when the header is
543 // clicked.
544
545 // Grab the patchset panel
546 var $panel = $(e.target).parents('.zuul-change');
547 var $body = $panel.children('.zuul-patchset-body');
548 $body.toggle(200);
549 var collapsed_index = collapsed_exceptions.indexOf(
550 $panel.attr('id'));
551 if (collapsed_index === -1 ) {
552 // Currently not an exception, add it to list
553 collapsed_exceptions.push($panel.attr('id'));
554 }
555 else {
556 // Currently an except, remove from exceptions
557 collapsed_exceptions.splice(collapsed_index, 1);
558 }
559 },
560
561 display_patchset: function($change_box, animate) {
562 // Determine if to show or hide the patchset and/or the results
563 // when loaded
564
565 // See if we should hide the body/results
566 var $panel = $change_box.find('.zuul-change');
567 var panel_change = $panel.attr('id');
568 var $body = $panel.children('.zuul-patchset-body');
569 var expand_by_default = $('#expand_by_default')
570 .prop('checked');
571
572 var collapsed_index = collapsed_exceptions
573 .indexOf(panel_change);
574
575 if (expand_by_default && collapsed_index === -1 ||
576 !expand_by_default && collapsed_index !== -1) {
577 // Expand by default, or is an exception
578 $body.show(animate);
579 }
580 else {
581 $body.hide(animate);
582 }
583
584 // Check if we should hide the whole panel
585 var panel_project = $panel.find('.change_project').text()
586 .toLowerCase();
587
588
589 var panel_pipeline = $change_box
590 .parents('.zuul-pipeline')
591 .find('.zuul-pipeline-header > h3')
592 .html()
593 .toLowerCase();
594
595 if (current_filter !== '') {
596 var show_panel = false;
597 var filter = current_filter.trim().split(/[\s,]+/);
598 $.each(filter, function(index, f_val) {
599 if (f_val !== '') {
600 f_val = f_val.toLowerCase();
601 if (panel_project.indexOf(f_val) !== -1 ||
602 panel_pipeline.indexOf(f_val) !== -1 ||
603 panel_change.indexOf(f_val) !== -1) {
604 show_panel = true;
605 }
606 }
607 });
608 if (show_panel === true) {
609 $change_box.show(animate);
610 }
611 else {
612 $change_box.hide(animate);
613 }
614 }
615 else {
616 $change_box.show(animate);
617 }
618 },
619 };
620
621 var app = {
622 schedule: function (app) {
Timo Tijhof4be9f742015-04-02 01:13:19 +0100623 app = app || this;
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000624 if (!options.enabled) {
625 setTimeout(function() {app.schedule(app);}, 5000);
626 return;
627 }
628 app.update().complete(function () {
629 setTimeout(function() {app.schedule(app);}, 5000);
630 });
631
632 /* Only update graphs every minute */
633 if (zuul_graph_update_count > 11) {
634 zuul_graph_update_count = 0;
635 zuul.update_sparklines();
636 }
637 },
638
639 /** @return {jQuery.Promise} */
640 update: function () {
641 // Cancel the previous update if it hasn't completed yet.
642 if (xhr) {
643 xhr.abort();
644 }
645
646 this.emit('update-start');
647 var app = this;
648
Timo Tijhof4be9f742015-04-02 01:13:19 +0100649 var $msg = $(options.msg_id);
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000650 xhr = $.getJSON(options.source)
651 .done(function (data) {
652 if ('message' in data) {
653 $msg.removeClass('alert-danger')
654 .addClass('alert-info')
655 .text(data.message)
656 .show();
657 } else {
658 $msg.empty()
659 .hide();
660 }
661
662 if ('zuul_version' in data) {
663 $('#zuul-version-span').text(data.zuul_version);
664 }
665 if ('last_reconfigured' in data) {
666 var last_reconfigured =
667 new Date(data.last_reconfigured);
668 $('#last-reconfigured-span').text(
669 last_reconfigured.toString());
670 }
671
672 var $pipelines = $(options.pipelines_id);
673 $pipelines.html('');
674 $.each(data.pipelines, function (i, pipeline) {
675 var count = app.create_tree(pipeline);
676 $pipelines.append(
677 format.pipeline(pipeline, count));
678 });
679
680 $(options.queue_events_num).text(
681 data.trigger_event_queue ?
682 data.trigger_event_queue.length : '0'
683 );
684 $(options.queue_results_num).text(
685 data.result_event_queue ?
686 data.result_event_queue.length : '0'
687 );
688 })
Timo Tijhof67f4ac12015-04-10 00:33:50 +0100689 .fail(function (jqXHR, statusText, errMsg) {
690 if (statusText === 'abort') {
691 return;
692 }
Joshua Heskethd50eb582014-04-30 19:51:22 +1000693 $msg.text(options.source + ': ' + errMsg)
694 .addClass('alert-danger')
695 .removeClass('zuul-msg-wrap-off')
696 .show();
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000697 })
698 .complete(function () {
699 xhr = undefined;
700 app.emit('update-end');
701 });
702
703 return xhr;
704 },
705
706 update_sparklines: function() {
707 $.each(zuul_sparkline_urls, function(name, url) {
708 var newimg = new Image();
709 var parts = url.split('#');
710 newimg.src = parts[0] + '#' + new Date().getTime();
Timo Tijhof4be9f742015-04-02 01:13:19 +0100711 $(newimg).load(function () {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000712 zuul_sparkline_urls[name] = newimg.src;
713 });
714 });
715 },
716
717 emit: function () {
718 $jq.trigger.apply($jq, arguments);
719 return this;
720 },
721 on: function () {
722 $jq.on.apply($jq, arguments);
723 return this;
724 },
725 one: function () {
726 $jq.one.apply($jq, arguments);
727 return this;
728 },
729
730 control_form: function() {
731 // Build the filter form filling anything from cookies
732
733 var $control_form = $('<form />')
734 .attr('role', 'form')
735 .addClass('form-inline')
736 .submit(this.handle_filter_change);
737
738 $control_form
739 .append(this.filter_form_group())
740 .append(this.expand_form_group());
741
742 return $control_form;
743 },
744
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100745 filter_form_group: function() {
Joshua Heskethace48892014-03-22 17:18:31 +1100746 // Update the filter form with a clear button if required
747
748 var $label = $('<label />')
749 .addClass('control-label')
750 .attr('for', 'filter_string')
751 .text('Filters')
752 .css('padding-right', '0.5em');
753
754 var $input = $('<input />')
755 .attr('type', 'text')
756 .attr('id', 'filter_string')
757 .addClass('form-control')
758 .attr('title',
759 'project(s), pipeline(s) or review(s) comma ' +
760 'separated')
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100761 .attr('value', current_filter);
Joshua Heskethace48892014-03-22 17:18:31 +1100762
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000763 $input.change(this.handle_filter_change);
Joshua Heskethace48892014-03-22 17:18:31 +1100764
765 var $clear_icon = $('<span />')
766 .addClass('form-control-feedback')
767 .addClass('glyphicon glyphicon-remove-circle')
768 .attr('id', 'filter_form_clear_box')
769 .attr('title', 'clear filter')
770 .css('cursor', 'pointer');
771
772 $clear_icon.click(function() {
773 $('#filter_string').val('').change();
774 });
775
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100776 if (current_filter === '') {
Joshua Heskethace48892014-03-22 17:18:31 +1100777 $clear_icon.hide();
778 }
779
780 var $form_group = $('<div />')
781 .addClass('form-group has-feedback')
782 .append($label, $input, $clear_icon);
783 return $form_group;
784 },
785
Joshua Heskethae230f62014-03-22 22:14:44 +1100786 expand_form_group: function() {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700787 var expand_by_default = (
Joshua Heskethae230f62014-03-22 22:14:44 +1100788 read_cookie('zuul_expand_by_default', false) === 'true');
789
Monty Taylor860bb0a2014-03-22 09:41:25 -0700790 var $checkbox = $('<input />')
Joshua Heskethae230f62014-03-22 22:14:44 +1100791 .attr('type', 'checkbox')
792 .attr('id', 'expand_by_default')
793 .prop('checked', expand_by_default)
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000794 .change(this.handle_expand_by_default);
Joshua Heskethae230f62014-03-22 22:14:44 +1100795
Monty Taylor860bb0a2014-03-22 09:41:25 -0700796 var $label = $('<label />')
Joshua Heskethae230f62014-03-22 22:14:44 +1100797 .css('padding-left', '1em')
798 .html('Expand by default: ')
799 .append($checkbox);
800
801 var $form_group = $('<div />')
802 .addClass('checkbox')
803 .append($label);
804 return $form_group;
805 },
806
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000807 handle_filter_change: function() {
808 // Update the filter and save it to a cookie
809 current_filter = $('#filter_string').val();
810 set_cookie('zuul_filter_string', current_filter);
811 if (current_filter === '') {
812 $('#filter_form_clear_box').hide();
Joshua Heskethace48892014-03-22 17:18:31 +1100813 }
814 else {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000815 $('#filter_form_clear_box').show();
Joshua Heskethace48892014-03-22 17:18:31 +1100816 }
Joshua Heskethace48892014-03-22 17:18:31 +1100817
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000818 $('.zuul-change-box').each(function(index, obj) {
819 var $change_box = $(obj);
820 format.display_patchset($change_box, 200);
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100821 });
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000822 return false;
Timo Tijhof51516cd2013-04-09 01:32:29 +0200823 },
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000824
825 handle_expand_by_default: function(e) {
826 // Handle toggling expand by default
827 set_cookie('zuul_expand_by_default', e.target.checked);
828 collapsed_exceptions = [];
829 $('.zuul-change-box').each(function(index, obj) {
830 var $change_box = $(obj);
831 format.display_patchset($change_box, 200);
832 });
833 },
834
835 create_tree: function(pipeline) {
836 var count = 0;
837 var pipeline_max_tree_columns = 1;
838 $.each(pipeline.change_queues, function(change_queue_i,
839 change_queue) {
840 var tree = [];
841 var max_tree_columns = 1;
842 var changes = [];
843 var last_tree_length = 0;
844 $.each(change_queue.heads, function(head_i, head) {
845 $.each(head, function(change_i, change) {
846 changes[change.id] = change;
847 change._tree_position = change_i;
848 });
849 });
850 $.each(change_queue.heads, function(head_i, head) {
851 $.each(head, function(change_i, change) {
James E. Blaird1d3ce32015-02-11 17:56:45 -0800852 if (change.live === true) {
853 count += 1;
854 }
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000855 var idx = tree.indexOf(change.id);
856 if (idx > -1) {
857 change._tree_index = idx;
858 // remove...
859 tree[idx] = null;
860 while (tree[tree.length - 1] === null) {
861 tree.pop();
862 }
863 } else {
864 change._tree_index = 0;
865 }
866 change._tree_branches = [];
867 change._tree = [];
868 if (typeof(change.items_behind) === 'undefined') {
869 change.items_behind = [];
870 }
871 change.items_behind.sort(function(a, b) {
872 return (changes[b]._tree_position -
873 changes[a]._tree_position);
874 });
875 $.each(change.items_behind, function(i, id) {
876 tree.push(id);
877 if (tree.length>last_tree_length &&
878 last_tree_length > 0) {
879 change._tree_branches.push(
880 tree.length - 1);
881 }
882 });
883 if (tree.length > max_tree_columns) {
884 max_tree_columns = tree.length;
885 }
886 if (tree.length > pipeline_max_tree_columns) {
887 pipeline_max_tree_columns = tree.length;
888 }
889 change._tree = tree.slice(0); // make a copy
890 last_tree_length = tree.length;
891 });
892 });
893 change_queue._tree_columns = max_tree_columns;
894 });
895 pipeline._tree_columns = pipeline_max_tree_columns;
896 return count;
897 },
898 };
899
900 $jq = $(app);
901 return {
902 options: options,
903 format: format,
904 app: app,
905 jq: $jq
906 };
Timo Tijhof4be9f742015-04-02 01:13:19 +0100907 };
Timo Tijhof51516cd2013-04-09 01:32:29 +0200908}(jQuery));