blob: 5c69bd198722ee3f3835719a3779a306a8d15591 [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', '');
Jan Hrubanddeb95a2017-01-03 15:12:41 +010055 var change_set_in_url = window.location.href.split('#')[1];
56 if (change_set_in_url) {
57 current_filter = change_set_in_url;
58 }
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +100059 var $jq;
Timo Tijhof51516cd2013-04-09 01:32:29 +020060
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +100061 var xhr,
62 zuul_graph_update_count = 0,
63 zuul_sparkline_urls = {};
Joshua Hesketh9d013542014-04-03 13:08:04 +110064
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +100065 function get_sparkline_url(pipeline_name) {
66 if (options.graphite_url !== '') {
67 if (!(pipeline_name in zuul_sparkline_urls)) {
68 zuul_sparkline_urls[pipeline_name] = $.fn.graphite
69 .geturl({
70 url: options.graphite_url,
71 from: "-8hours",
72 width: 100,
73 height: 26,
74 margin: 0,
75 hideLegend: true,
76 hideAxes: true,
77 hideGrid: true,
78 target: [
79 "color(stats.gauges.zuul.pipeline." + pipeline_name
Timo Tijhof4be9f742015-04-02 01:13:19 +010080 + ".current_changes, '6b8182')"
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +100081 ]
Joshua Hesketh298c4912014-03-20 16:06:25 +110082 });
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +100083 }
84 return zuul_sparkline_urls[pipeline_name];
85 }
86 return false;
87 }
Joshua Hesketh298c4912014-03-20 16:06:25 +110088
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +100089 var format = {
Joshua Hesketh6b1a2182014-03-21 14:40:04 +110090 job: function(job) {
Joshua Hesketh9e6ce532014-04-01 13:11:53 +110091 var $job_line = $('<span />');
92
Joshua Hesketh6b1a2182014-03-21 14:40:04 +110093 if (job.url !== null) {
Joshua Hesketh9e6ce532014-04-01 13:11:53 +110094 $job_line.append(
95 $('<a />')
96 .addClass('zuul-job-name')
97 .attr('href', job.url)
98 .text(job.name)
99 );
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100100 }
Joshua Hesketh9e6ce532014-04-01 13:11:53 +1100101 else {
102 $job_line.append(
103 $('<span />')
104 .addClass('zuul-job-name')
105 .text(job.name)
106 );
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100107 }
Joshua Hesketh9e6ce532014-04-01 13:11:53 +1100108
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000109 $job_line.append(this.job_status(job));
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100110
111 if (job.voting === false) {
112 $job_line.append(
Joshua Hesketh9e6ce532014-04-01 13:11:53 +1100113 $(' <small />')
114 .addClass('zuul-non-voting-desc')
115 .text(' (non-voting)')
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100116 );
117 }
Joshua Hesketh9e6ce532014-04-01 13:11:53 +1100118
Joshua Hesketha95fd552014-08-21 11:21:05 +1000119 $job_line.append($('<div style="clear: both"></div>'));
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100120 return $job_line;
121 },
122
123 job_status: function(job) {
124 var result = job.result ? job.result.toLowerCase() : null;
125 if (result === null) {
126 result = job.url ? 'in progress' : 'queued';
127 }
128
Monty Taylor860bb0a2014-03-22 09:41:25 -0700129 if (result === 'in progress') {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000130 return this.job_progress_bar(job.elapsed_time,
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100131 job.remaining_time);
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100132 }
133 else {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000134 return this.status_label(result);
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100135 }
136 },
137
138 status_label: function(result) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700139 var $status = $('<span />');
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100140 $status.addClass('zuul-job-result label');
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100141
142 switch (result) {
143 case 'success':
144 $status.addClass('label-success');
145 break;
146 case 'failure':
147 $status.addClass('label-danger');
148 break;
149 case 'unstable':
150 $status.addClass('label-warning');
151 break;
Alexander Evseev3103a842015-11-17 10:12:45 +0300152 case 'skipped':
153 $status.addClass('label-info');
154 break;
Antoine Musso4c2053d2016-02-04 23:35:23 +0100155 // 'in progress' 'queued' 'lost' 'aborted' ...
156 default:
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100157 $status.addClass('label-default');
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100158 }
159 $status.text(result);
160 return $status;
161 },
162
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100163 job_progress_bar: function(elapsed_time, remaining_time) {
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100164 var progress_percent = 100 * (elapsed_time / (elapsed_time +
165 remaining_time));
166 var $bar_inner = $('<div />')
167 .addClass('progress-bar')
168 .attr('role', 'progressbar')
169 .attr('aria-valuenow', 'progressbar')
170 .attr('aria-valuemin', progress_percent)
171 .attr('aria-valuemin', '0')
172 .attr('aria-valuemax', '100')
173 .css('width', progress_percent + '%');
174
175 var $bar_outter = $('<div />')
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100176 .addClass('progress zuul-job-result')
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100177 .append($bar_inner);
178
179 return $bar_outter;
180 },
181
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100182 enqueue_time: function(ms) {
183 // Special format case for enqueue time to add style
184 var hours = 60 * 60 * 1000;
185 var now = Date.now();
186 var delta = now - ms;
Monty Taylor860bb0a2014-03-22 09:41:25 -0700187 var status = 'text-success';
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000188 var text = this.time(delta, true);
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100189 if (delta > (4 * hours)) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700190 status = 'text-danger';
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100191 } else if (delta > (2 * hours)) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700192 status = 'text-warning';
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100193 }
194 return '<span class="' + status + '">' + text + '</span>';
195 },
196
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100197 time: function(ms, words) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700198 if (typeof(words) === 'undefined') {
199 words = false;
200 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100201 var seconds = (+ms)/1000;
202 var minutes = Math.floor(seconds/60);
203 var hours = Math.floor(minutes/60);
204 seconds = Math.floor(seconds % 60);
205 minutes = Math.floor(minutes % 60);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700206 var r = '';
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100207 if (words) {
208 if (hours) {
209 r += hours;
210 r += ' hr ';
211 }
212 r += minutes + ' min';
213 } else {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700214 if (hours < 10) {
215 r += '0';
216 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100217 r += hours + ':';
Monty Taylor860bb0a2014-03-22 09:41:25 -0700218 if (minutes < 10) {
219 r += '0';
220 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100221 r += minutes + ':';
Monty Taylor860bb0a2014-03-22 09:41:25 -0700222 if (seconds < 10) {
223 r += '0';
224 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100225 r += seconds;
226 }
227 return r;
228 },
229
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100230 change_total_progress_bar: function(change) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700231 var job_percent = Math.floor(100 / change.jobs.length);
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100232 var $bar_outter = $('<div />')
233 .addClass('progress zuul-change-total-result');
234
235 $.each(change.jobs, function (i, job) {
236 var result = job.result ? job.result.toLowerCase() : null;
237 if (result === null) {
238 result = job.url ? 'in progress' : 'queued';
239 }
240
Monty Taylor860bb0a2014-03-22 09:41:25 -0700241 if (result !== 'queued') {
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100242 var $bar_inner = $('<div />')
243 .addClass('progress-bar');
244
245 switch (result) {
246 case 'success':
247 $bar_inner.addClass('progress-bar-success');
248 break;
249 case 'lost':
250 case 'failure':
251 $bar_inner.addClass('progress-bar-danger');
252 break;
253 case 'unstable':
254 $bar_inner.addClass('progress-bar-warning');
255 break;
256 case 'in progress':
257 case 'queued':
258 break;
259 }
260 $bar_inner.attr('title', job.name)
261 .css('width', job_percent + '%');
262 $bar_outter.append($bar_inner);
263 }
264 });
265 return $bar_outter;
266 },
267
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100268 change_header: function(change) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700269 var change_id = change.id || 'NA';
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100270 if (change_id.length === 40) {
271 change_id = change_id.substr(0, 7);
Joshua Hesketh298c4912014-03-20 16:06:25 +1100272 }
Timo Tijhof51516cd2013-04-09 01:32:29 +0200273
Monty Taylor860bb0a2014-03-22 09:41:25 -0700274 var $change_link = $('<small />');
Joshua Hesketh298c4912014-03-20 16:06:25 +1100275 if (change.url !== null) {
Joshua Heskethcd7a0772014-08-21 11:32:44 +1000276 if (/^[0-9a-f]{40}$/.test(change.id)) {
277 var change_id_short = change.id.slice(0, 7);
278 $change_link.append(
279 $('<a />').attr('href', change.url).append(
280 $('<abbr />')
281 .attr('title', change.id)
282 .text(change_id_short)
283 )
284 );
285 }
286 else {
287 $change_link.append(
288 $('<a />').attr('href', change.url).text(change.id)
289 );
290 }
Timo Tijhof51516cd2013-04-09 01:32:29 +0200291 }
Joshua Hesketh298c4912014-03-20 16:06:25 +1100292 else {
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100293 $change_link.text(change_id);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200294 }
Timo Tijhof51516cd2013-04-09 01:32:29 +0200295
Monty Taylor860bb0a2014-03-22 09:41:25 -0700296 var $change_progress_row_left = $('<div />')
Timo Tijhofa62e8492015-04-02 03:05:04 +0100297 .addClass('col-xs-4')
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100298 .append($change_link);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700299 var $change_progress_row_right = $('<div />')
Timo Tijhofa62e8492015-04-02 03:05:04 +0100300 .addClass('col-xs-8')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000301 .append(this.change_total_progress_bar(change));
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100302
Monty Taylor860bb0a2014-03-22 09:41:25 -0700303 var $change_progress_row = $('<div />')
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100304 .addClass('row')
305 .append($change_progress_row_left)
Monty Taylor860bb0a2014-03-22 09:41:25 -0700306 .append($change_progress_row_right);
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100307
Monty Taylor860bb0a2014-03-22 09:41:25 -0700308 var $project_span = $('<span />')
Joshua Heskethace48892014-03-22 17:18:31 +1100309 .addClass('change_project')
310 .text(change.project);
311
Monty Taylor860bb0a2014-03-22 09:41:25 -0700312 var $left = $('<div />')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100313 .addClass('col-xs-8')
Joshua Hesketh1751e4f2014-04-01 13:24:51 +1100314 .append($project_span, $change_progress_row);
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100315
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000316 var remaining_time = this.time(
Monty Taylor860bb0a2014-03-22 09:41:25 -0700317 change.remaining_time, true);
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000318 var enqueue_time = this.enqueue_time(
Monty Taylor860bb0a2014-03-22 09:41:25 -0700319 change.enqueue_time);
320 var $remaining_time = $('<small />').addClass('time')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100321 .attr('title', 'Remaining Time').html(remaining_time);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700322 var $enqueue_time = $('<small />').addClass('time')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100323 .attr('title', 'Elapsed Time').html(enqueue_time);
324
James E. Blair6dc954b2015-02-11 18:32:19 -0800325 var $right = $('<div />');
326 if (change.live === true) {
327 $right.addClass('col-xs-4 text-right')
328 .append($remaining_time, $('<br />'), $enqueue_time);
329 }
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100330
Monty Taylor860bb0a2014-03-22 09:41:25 -0700331 var $header = $('<div />')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100332 .addClass('row')
333 .append($left, $right);
334 return $header;
335 },
336
337 change_list: function(jobs) {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000338 var format = this;
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100339 var $list = $('<ul />')
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100340 .addClass('list-group zuul-patchset-body');
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100341
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100342 $.each(jobs, function (i, job) {
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100343 var $item = $('<li />')
344 .addClass('list-group-item')
345 .addClass('zuul-change-job')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000346 .append(format.job(job));
Joshua Hesketh298c4912014-03-20 16:06:25 +1100347 $list.append($item);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200348 });
349
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100350 return $list;
351 },
352
353 change_panel: function (change) {
354 var $header = $('<div />')
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100355 .addClass('panel-heading zuul-patchset-header')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000356 .append(this.change_header(change));
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100357
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100358 var panel_id = change.id ? change.id.replace(',', '_')
359 : change.project.replace('/', '_') +
Monty Taylor860bb0a2014-03-22 09:41:25 -0700360 '-' + change.enqueue_time;
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100361 var $panel = $('<div />')
Monty Taylor860bb0a2014-03-22 09:41:25 -0700362 .attr('id', panel_id)
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100363 .addClass('panel panel-default zuul-change')
364 .append($header)
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000365 .append(this.change_list(change.jobs));
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100366
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000367 $header.click(this.toggle_patchset);
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100368 return $panel;
Timo Tijhof51516cd2013-04-09 01:32:29 +0200369 },
370
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100371 change_status_icon: function(change) {
372 var icon_name = 'green.png';
373 var icon_title = 'Succeeding';
374
375 if (change.active !== true) {
376 // Grey icon
377 icon_name = 'grey.png';
378 icon_title = 'Waiting until closer to head of queue to' +
379 ' start jobs';
380 }
James E. Blair107c3852015-02-07 08:23:10 -0800381 else if (change.live !== true) {
382 // Grey icon
383 icon_name = 'grey.png';
Joshua Hesketh6bc619d2015-02-11 09:30:21 +1100384 icon_title = 'Dependent change required for testing';
James E. Blair107c3852015-02-07 08:23:10 -0800385 }
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100386 else if (change.failing_reasons &&
387 change.failing_reasons.length > 0) {
388 var reason = change.failing_reasons.join(', ');
389 icon_title = 'Failing because ' + reason;
390 if (reason.match(/merge conflict/)) {
391 // Black icon
392 icon_name = 'black.png';
393 }
394 else {
395 // Red icon
396 icon_name = 'red.png';
397 }
398 }
399
400 var $icon = $('<img />')
401 .attr('src', 'images/' + icon_name)
402 .attr('title', icon_title)
403 .css('margin-top', '-6px');
404
405 return $icon;
406 },
407
408 change_with_status_tree: function(change, change_queue) {
409 var $change_row = $('<tr />');
410
411 for (var i = 0; i < change_queue._tree_columns; i++) {
412 var $tree_cell = $('<td />')
413 .css('height', '100%')
414 .css('padding', '0 0 10px 0')
415 .css('margin', '0')
416 .css('width', '16px')
417 .css('min-width', '16px')
418 .css('overflow', 'hidden')
419 .css('vertical-align', 'top');
420
421 if (i < change._tree.length && change._tree[i] !== null) {
422 $tree_cell.css('background-image',
423 'url(\'images/line.png\')')
424 .css('background-repeat', 'repeat-y');
425 }
426
427 if (i === change._tree_index) {
428 $tree_cell.append(
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000429 this.change_status_icon(change));
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100430 }
431 if (change._tree_branches.indexOf(i) !== -1) {
432 var $image = $('<img />')
433 .css('vertical-align', 'baseline');
434 if (change._tree_branches.indexOf(i) ===
435 change._tree_branches.length - 1) {
436 // Angle line
437 $image.attr('src', 'images/line-angle.png');
438 }
439 else {
440 // T line
441 $image.attr('src', 'images/line-t.png');
442 }
443 $tree_cell.append($image);
444 }
445 $change_row.append($tree_cell);
446 }
447
448 var change_width = 360 - 16*change_queue._tree_columns;
449 var $change_column = $('<td />')
450 .css('width', change_width + 'px')
451 .addClass('zuul-change-cell')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000452 .append(this.change_panel(change));
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100453
454 $change_row.append($change_column);
455
456 var $change_table = $('<table />')
457 .addClass('zuul-change-box')
458 .css('-moz-box-sizing', 'content-box')
459 .css('box-sizing', 'content-box')
460 .append($change_row);
461
462 return $change_table;
463 },
464
Joshua Hesketh9d013542014-04-03 13:08:04 +1100465 pipeline_sparkline: function(pipeline_name) {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000466 if (options.graphite_url !== '') {
Joshua Hesketh9d013542014-04-03 13:08:04 +1100467 var $sparkline = $('<img />')
468 .addClass('pull-right')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000469 .attr('src', get_sparkline_url(pipeline_name));
Joshua Hesketh9d013542014-04-03 13:08:04 +1100470 return $sparkline;
471 }
472 return false;
473 },
474
475 pipeline_header: function(pipeline, count) {
476 // Format the pipeline name, sparkline and description
477 var $header_div = $('<div />')
478 .addClass('zuul-pipeline-header');
479
480 var $heading = $('<h3 />')
481 .css('vertical-align', 'middle')
482 .text(pipeline.name)
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100483 .append(
Joshua Hesketh9d013542014-04-03 13:08:04 +1100484 $('<span />')
485 .addClass('badge pull-right')
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100486 .css('vertical-align', 'middle')
Joshua Hesketh9d013542014-04-03 13:08:04 +1100487 .css('margin-top', '0.5em')
488 .text(count)
489 )
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000490 .append(this.pipeline_sparkline(pipeline.name));
Joshua Hesketh9d013542014-04-03 13:08:04 +1100491
492 $header_div.append($heading);
Joshua Hesketh298c4912014-03-20 16:06:25 +1100493
Timo Tijhof51516cd2013-04-09 01:32:29 +0200494 if (typeof pipeline.description === 'string') {
Alexander Evseev057aed12015-10-28 17:17:04 +0300495 var descr = $('<small />')
496 $.each( pipeline.description.split(/\r?\n\r?\n/), function(index, descr_part){
497 descr.append($('<p />').text(descr_part));
498 });
Joshua Hesketh9d013542014-04-03 13:08:04 +1100499 $header_div.append(
Alexander Evseev057aed12015-10-28 17:17:04 +0300500 $('<p />').append(descr)
Joshua Hesketh298c4912014-03-20 16:06:25 +1100501 );
Timo Tijhof51516cd2013-04-09 01:32:29 +0200502 }
Joshua Hesketh9d013542014-04-03 13:08:04 +1100503 return $header_div;
504 },
505
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000506 pipeline: function (pipeline, count) {
507 var format = this;
Joshua Hesketh9d013542014-04-03 13:08:04 +1100508 var $html = $('<div />')
509 .addClass('zuul-pipeline col-md-4')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000510 .append(this.pipeline_header(pipeline, count));
Timo Tijhof51516cd2013-04-09 01:32:29 +0200511
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100512 $.each(pipeline.change_queues,
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100513 function (queue_i, change_queue) {
514 $.each(change_queue.heads, function (head_i, changes) {
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100515 if (pipeline.change_queues.length > 1 &&
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100516 head_i === 0) {
517 var name = change_queue.name;
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100518 var short_name = name;
519 if (short_name.length > 32) {
520 short_name = short_name.substr(0, 32) + '...';
Timo Tijhof51516cd2013-04-09 01:32:29 +0200521 }
Joshua Hesketh298c4912014-03-20 16:06:25 +1100522 $html.append(
523 $('<p />')
524 .text('Queue: ')
525 .append(
526 $('<abbr />')
527 .attr('title', name)
528 .text(short_name)
529 )
530 );
Timo Tijhof51516cd2013-04-09 01:32:29 +0200531 }
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100532
533 $.each(changes, function (change_i, change) {
534 var $change_box =
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000535 format.change_with_status_tree(
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100536 change, change_queue);
537 $html.append($change_box);
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000538 format.display_patchset($change_box);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200539 });
540 });
541 });
Joshua Hesketh298c4912014-03-20 16:06:25 +1100542 return $html;
Joshua Heskethace48892014-03-22 17:18:31 +1100543 },
544
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000545 toggle_patchset: function(e) {
546 // Toggle showing/hiding the patchset when the header is
547 // clicked.
548
549 // Grab the patchset panel
550 var $panel = $(e.target).parents('.zuul-change');
551 var $body = $panel.children('.zuul-patchset-body');
552 $body.toggle(200);
553 var collapsed_index = collapsed_exceptions.indexOf(
554 $panel.attr('id'));
555 if (collapsed_index === -1 ) {
556 // Currently not an exception, add it to list
557 collapsed_exceptions.push($panel.attr('id'));
558 }
559 else {
560 // Currently an except, remove from exceptions
561 collapsed_exceptions.splice(collapsed_index, 1);
562 }
563 },
564
565 display_patchset: function($change_box, animate) {
566 // Determine if to show or hide the patchset and/or the results
567 // when loaded
568
569 // See if we should hide the body/results
570 var $panel = $change_box.find('.zuul-change');
571 var panel_change = $panel.attr('id');
572 var $body = $panel.children('.zuul-patchset-body');
573 var expand_by_default = $('#expand_by_default')
574 .prop('checked');
575
576 var collapsed_index = collapsed_exceptions
577 .indexOf(panel_change);
578
579 if (expand_by_default && collapsed_index === -1 ||
580 !expand_by_default && collapsed_index !== -1) {
581 // Expand by default, or is an exception
582 $body.show(animate);
583 }
584 else {
585 $body.hide(animate);
586 }
587
588 // Check if we should hide the whole panel
589 var panel_project = $panel.find('.change_project').text()
590 .toLowerCase();
591
592
593 var panel_pipeline = $change_box
594 .parents('.zuul-pipeline')
595 .find('.zuul-pipeline-header > h3')
596 .html()
597 .toLowerCase();
598
599 if (current_filter !== '') {
600 var show_panel = false;
601 var filter = current_filter.trim().split(/[\s,]+/);
602 $.each(filter, function(index, f_val) {
603 if (f_val !== '') {
604 f_val = f_val.toLowerCase();
605 if (panel_project.indexOf(f_val) !== -1 ||
606 panel_pipeline.indexOf(f_val) !== -1 ||
607 panel_change.indexOf(f_val) !== -1) {
608 show_panel = true;
609 }
610 }
611 });
612 if (show_panel === true) {
613 $change_box.show(animate);
614 }
615 else {
616 $change_box.hide(animate);
617 }
618 }
619 else {
620 $change_box.show(animate);
621 }
622 },
623 };
624
625 var app = {
626 schedule: function (app) {
Timo Tijhof4be9f742015-04-02 01:13:19 +0100627 app = app || this;
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000628 if (!options.enabled) {
629 setTimeout(function() {app.schedule(app);}, 5000);
630 return;
631 }
632 app.update().complete(function () {
633 setTimeout(function() {app.schedule(app);}, 5000);
634 });
635
636 /* Only update graphs every minute */
637 if (zuul_graph_update_count > 11) {
638 zuul_graph_update_count = 0;
639 zuul.update_sparklines();
640 }
641 },
642
643 /** @return {jQuery.Promise} */
644 update: function () {
645 // Cancel the previous update if it hasn't completed yet.
646 if (xhr) {
647 xhr.abort();
648 }
649
650 this.emit('update-start');
651 var app = this;
652
Timo Tijhof4be9f742015-04-02 01:13:19 +0100653 var $msg = $(options.msg_id);
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000654 xhr = $.getJSON(options.source)
655 .done(function (data) {
656 if ('message' in data) {
657 $msg.removeClass('alert-danger')
658 .addClass('alert-info')
659 .text(data.message)
660 .show();
661 } else {
662 $msg.empty()
663 .hide();
664 }
665
666 if ('zuul_version' in data) {
667 $('#zuul-version-span').text(data.zuul_version);
668 }
669 if ('last_reconfigured' in data) {
670 var last_reconfigured =
671 new Date(data.last_reconfigured);
672 $('#last-reconfigured-span').text(
673 last_reconfigured.toString());
674 }
675
676 var $pipelines = $(options.pipelines_id);
677 $pipelines.html('');
678 $.each(data.pipelines, function (i, pipeline) {
679 var count = app.create_tree(pipeline);
680 $pipelines.append(
681 format.pipeline(pipeline, count));
682 });
683
684 $(options.queue_events_num).text(
685 data.trigger_event_queue ?
686 data.trigger_event_queue.length : '0'
687 );
688 $(options.queue_results_num).text(
689 data.result_event_queue ?
690 data.result_event_queue.length : '0'
691 );
692 })
Timo Tijhof67f4ac12015-04-10 00:33:50 +0100693 .fail(function (jqXHR, statusText, errMsg) {
694 if (statusText === 'abort') {
695 return;
696 }
Joshua Heskethd50eb582014-04-30 19:51:22 +1000697 $msg.text(options.source + ': ' + errMsg)
698 .addClass('alert-danger')
699 .removeClass('zuul-msg-wrap-off')
700 .show();
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000701 })
702 .complete(function () {
703 xhr = undefined;
704 app.emit('update-end');
705 });
706
707 return xhr;
708 },
709
710 update_sparklines: function() {
711 $.each(zuul_sparkline_urls, function(name, url) {
712 var newimg = new Image();
713 var parts = url.split('#');
714 newimg.src = parts[0] + '#' + new Date().getTime();
Timo Tijhof4be9f742015-04-02 01:13:19 +0100715 $(newimg).load(function () {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000716 zuul_sparkline_urls[name] = newimg.src;
717 });
718 });
719 },
720
721 emit: function () {
722 $jq.trigger.apply($jq, arguments);
723 return this;
724 },
725 on: function () {
726 $jq.on.apply($jq, arguments);
727 return this;
728 },
729 one: function () {
730 $jq.one.apply($jq, arguments);
731 return this;
732 },
733
734 control_form: function() {
735 // Build the filter form filling anything from cookies
736
737 var $control_form = $('<form />')
738 .attr('role', 'form')
739 .addClass('form-inline')
740 .submit(this.handle_filter_change);
741
742 $control_form
743 .append(this.filter_form_group())
744 .append(this.expand_form_group());
745
746 return $control_form;
747 },
748
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100749 filter_form_group: function() {
Joshua Heskethace48892014-03-22 17:18:31 +1100750 // Update the filter form with a clear button if required
751
752 var $label = $('<label />')
753 .addClass('control-label')
754 .attr('for', 'filter_string')
755 .text('Filters')
756 .css('padding-right', '0.5em');
757
758 var $input = $('<input />')
759 .attr('type', 'text')
760 .attr('id', 'filter_string')
761 .addClass('form-control')
762 .attr('title',
763 'project(s), pipeline(s) or review(s) comma ' +
764 'separated')
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100765 .attr('value', current_filter);
Joshua Heskethace48892014-03-22 17:18:31 +1100766
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000767 $input.change(this.handle_filter_change);
Joshua Heskethace48892014-03-22 17:18:31 +1100768
769 var $clear_icon = $('<span />')
770 .addClass('form-control-feedback')
771 .addClass('glyphicon glyphicon-remove-circle')
772 .attr('id', 'filter_form_clear_box')
773 .attr('title', 'clear filter')
774 .css('cursor', 'pointer');
775
776 $clear_icon.click(function() {
777 $('#filter_string').val('').change();
778 });
779
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100780 if (current_filter === '') {
Joshua Heskethace48892014-03-22 17:18:31 +1100781 $clear_icon.hide();
782 }
783
784 var $form_group = $('<div />')
785 .addClass('form-group has-feedback')
786 .append($label, $input, $clear_icon);
787 return $form_group;
788 },
789
Joshua Heskethae230f62014-03-22 22:14:44 +1100790 expand_form_group: function() {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700791 var expand_by_default = (
Joshua Heskethae230f62014-03-22 22:14:44 +1100792 read_cookie('zuul_expand_by_default', false) === 'true');
793
Monty Taylor860bb0a2014-03-22 09:41:25 -0700794 var $checkbox = $('<input />')
Joshua Heskethae230f62014-03-22 22:14:44 +1100795 .attr('type', 'checkbox')
796 .attr('id', 'expand_by_default')
797 .prop('checked', expand_by_default)
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000798 .change(this.handle_expand_by_default);
Joshua Heskethae230f62014-03-22 22:14:44 +1100799
Monty Taylor860bb0a2014-03-22 09:41:25 -0700800 var $label = $('<label />')
Joshua Heskethae230f62014-03-22 22:14:44 +1100801 .css('padding-left', '1em')
802 .html('Expand by default: ')
803 .append($checkbox);
804
805 var $form_group = $('<div />')
806 .addClass('checkbox')
807 .append($label);
808 return $form_group;
809 },
810
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000811 handle_filter_change: function() {
812 // Update the filter and save it to a cookie
813 current_filter = $('#filter_string').val();
814 set_cookie('zuul_filter_string', current_filter);
815 if (current_filter === '') {
816 $('#filter_form_clear_box').hide();
Joshua Heskethace48892014-03-22 17:18:31 +1100817 }
818 else {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000819 $('#filter_form_clear_box').show();
Joshua Heskethace48892014-03-22 17:18:31 +1100820 }
Joshua Heskethace48892014-03-22 17:18:31 +1100821
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000822 $('.zuul-change-box').each(function(index, obj) {
823 var $change_box = $(obj);
824 format.display_patchset($change_box, 200);
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100825 });
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000826 return false;
Timo Tijhof51516cd2013-04-09 01:32:29 +0200827 },
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000828
829 handle_expand_by_default: function(e) {
830 // Handle toggling expand by default
831 set_cookie('zuul_expand_by_default', e.target.checked);
832 collapsed_exceptions = [];
833 $('.zuul-change-box').each(function(index, obj) {
834 var $change_box = $(obj);
835 format.display_patchset($change_box, 200);
836 });
837 },
838
839 create_tree: function(pipeline) {
840 var count = 0;
841 var pipeline_max_tree_columns = 1;
842 $.each(pipeline.change_queues, function(change_queue_i,
843 change_queue) {
844 var tree = [];
845 var max_tree_columns = 1;
846 var changes = [];
847 var last_tree_length = 0;
848 $.each(change_queue.heads, function(head_i, head) {
849 $.each(head, function(change_i, change) {
850 changes[change.id] = change;
851 change._tree_position = change_i;
852 });
853 });
854 $.each(change_queue.heads, function(head_i, head) {
855 $.each(head, function(change_i, change) {
James E. Blaird1d3ce32015-02-11 17:56:45 -0800856 if (change.live === true) {
857 count += 1;
858 }
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000859 var idx = tree.indexOf(change.id);
860 if (idx > -1) {
861 change._tree_index = idx;
862 // remove...
863 tree[idx] = null;
864 while (tree[tree.length - 1] === null) {
865 tree.pop();
866 }
867 } else {
868 change._tree_index = 0;
869 }
870 change._tree_branches = [];
871 change._tree = [];
872 if (typeof(change.items_behind) === 'undefined') {
873 change.items_behind = [];
874 }
875 change.items_behind.sort(function(a, b) {
876 return (changes[b]._tree_position -
877 changes[a]._tree_position);
878 });
879 $.each(change.items_behind, function(i, id) {
880 tree.push(id);
881 if (tree.length>last_tree_length &&
882 last_tree_length > 0) {
883 change._tree_branches.push(
884 tree.length - 1);
885 }
886 });
887 if (tree.length > max_tree_columns) {
888 max_tree_columns = tree.length;
889 }
890 if (tree.length > pipeline_max_tree_columns) {
891 pipeline_max_tree_columns = tree.length;
892 }
893 change._tree = tree.slice(0); // make a copy
894 last_tree_length = tree.length;
895 });
896 });
897 change_queue._tree_columns = max_tree_columns;
898 });
899 pipeline._tree_columns = pipeline_max_tree_columns;
900 return count;
901 },
902 };
903
904 $jq = $(app);
905 return {
906 options: options,
907 format: format,
908 app: app,
909 jq: $jq
910 };
Timo Tijhof4be9f742015-04-02 01:13:19 +0100911 };
Timo Tijhof51516cd2013-04-09 01:32:29 +0200912}(jQuery));