blob: 32c45d441f3ba23901ca9edfc959bc82d6a370db [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.
Monty Taylor860bb0a2014-03-22 09:41:25 -070019'use strict';
Timo Tijhof51516cd2013-04-09 01:32:29 +020020
21(function ($) {
Joshua Heskethace48892014-03-22 17:18:31 +110022 function set_cookie(name, value) {
Monty Taylor860bb0a2014-03-22 09:41:25 -070023 document.cookie = name + '=' + value + '; path=/';
Joshua Heskethace48892014-03-22 17:18:31 +110024 }
25
26 function read_cookie(name, default_value) {
Monty Taylor860bb0a2014-03-22 09:41:25 -070027 var nameEQ = name + '=';
Joshua Heskethace48892014-03-22 17:18:31 +110028 var ca = document.cookie.split(';');
29 for(var i=0;i < ca.length;i++) {
30 var c = ca[i];
Monty Taylor860bb0a2014-03-22 09:41:25 -070031 while (c.charAt(0) === ' ') {
32 c = c.substring(1, c.length);
33 }
34 if (c.indexOf(nameEQ) === 0) {
Joshua Heskethace48892014-03-22 17:18:31 +110035 return c.substring(nameEQ.length, c.length);
36 }
37 }
38 return default_value;
39 }
40
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +100041 $.zuul = function(options) {
42 var options = $.extend({
43 'enabled': true,
44 'graphite_url': '',
45 'source': 'status.json',
46 'msg_id': '#zuul_msg',
47 'pipelines_id': '#zuul_pipelines',
48 'queue_events_num': '#zuul_queue_events_num',
49 'queue_results_num': '#zuul_queue_results_num',
50 }, options);
Joshua Heskethace48892014-03-22 17:18:31 +110051
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +100052 var collapsed_exceptions = [];
Joshua Hesketh6f94dd12014-08-20 16:14:53 +100053 var current_filter = read_cookie('zuul_filter_string', '');
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +100054 var $jq;
Timo Tijhof51516cd2013-04-09 01:32:29 +020055
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +100056 var xhr,
57 zuul_graph_update_count = 0,
58 zuul_sparkline_urls = {};
Joshua Hesketh9d013542014-04-03 13:08:04 +110059
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +100060 function get_sparkline_url(pipeline_name) {
61 if (options.graphite_url !== '') {
62 if (!(pipeline_name in zuul_sparkline_urls)) {
63 zuul_sparkline_urls[pipeline_name] = $.fn.graphite
64 .geturl({
65 url: options.graphite_url,
66 from: "-8hours",
67 width: 100,
68 height: 26,
69 margin: 0,
70 hideLegend: true,
71 hideAxes: true,
72 hideGrid: true,
73 target: [
74 "color(stats.gauges.zuul.pipeline." + pipeline_name
75 + ".current_changes, '6b8182')"
76 ]
Joshua Hesketh298c4912014-03-20 16:06:25 +110077 });
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +100078 }
79 return zuul_sparkline_urls[pipeline_name];
80 }
81 return false;
82 }
Joshua Hesketh298c4912014-03-20 16:06:25 +110083
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +100084 var format = {
Joshua Hesketh6b1a2182014-03-21 14:40:04 +110085 job: function(job) {
Joshua Hesketh9e6ce532014-04-01 13:11:53 +110086 var $job_line = $('<span />');
87
Joshua Hesketh6b1a2182014-03-21 14:40:04 +110088 if (job.url !== null) {
Joshua Hesketh9e6ce532014-04-01 13:11:53 +110089 $job_line.append(
90 $('<a />')
91 .addClass('zuul-job-name')
92 .attr('href', job.url)
93 .text(job.name)
94 );
Joshua Hesketh6b1a2182014-03-21 14:40:04 +110095 }
Joshua Hesketh9e6ce532014-04-01 13:11:53 +110096 else {
97 $job_line.append(
98 $('<span />')
99 .addClass('zuul-job-name')
100 .text(job.name)
101 );
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100102 }
Joshua Hesketh9e6ce532014-04-01 13:11:53 +1100103
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000104 $job_line.append(this.job_status(job));
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100105
106 if (job.voting === false) {
107 $job_line.append(
Joshua Hesketh9e6ce532014-04-01 13:11:53 +1100108 $(' <small />')
109 .addClass('zuul-non-voting-desc')
110 .text(' (non-voting)')
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100111 );
112 }
Joshua Hesketh9e6ce532014-04-01 13:11:53 +1100113
Joshua Hesketha95fd552014-08-21 11:21:05 +1000114 $job_line.append($('<div style="clear: both"></div>'));
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100115 return $job_line;
116 },
117
118 job_status: function(job) {
119 var result = job.result ? job.result.toLowerCase() : null;
120 if (result === null) {
121 result = job.url ? 'in progress' : 'queued';
122 }
123
Monty Taylor860bb0a2014-03-22 09:41:25 -0700124 if (result === 'in progress') {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000125 return this.job_progress_bar(job.elapsed_time,
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100126 job.remaining_time);
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100127 }
128 else {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000129 return this.status_label(result);
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100130 }
131 },
132
133 status_label: function(result) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700134 var $status = $('<span />');
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100135 $status.addClass('zuul-job-result label');
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100136
137 switch (result) {
138 case 'success':
139 $status.addClass('label-success');
140 break;
141 case 'failure':
142 $status.addClass('label-danger');
143 break;
144 case 'unstable':
145 $status.addClass('label-warning');
146 break;
147 case 'in progress':
148 case 'queued':
149 case 'lost':
150 $status.addClass('label-default');
151 break;
152 }
153 $status.text(result);
154 return $status;
155 },
156
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100157 job_progress_bar: function(elapsed_time, remaining_time) {
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100158 var progress_percent = 100 * (elapsed_time / (elapsed_time +
159 remaining_time));
160 var $bar_inner = $('<div />')
161 .addClass('progress-bar')
162 .attr('role', 'progressbar')
163 .attr('aria-valuenow', 'progressbar')
164 .attr('aria-valuemin', progress_percent)
165 .attr('aria-valuemin', '0')
166 .attr('aria-valuemax', '100')
167 .css('width', progress_percent + '%');
168
169 var $bar_outter = $('<div />')
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100170 .addClass('progress zuul-job-result')
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100171 .append($bar_inner);
172
173 return $bar_outter;
174 },
175
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100176 enqueue_time: function(ms) {
177 // Special format case for enqueue time to add style
178 var hours = 60 * 60 * 1000;
179 var now = Date.now();
180 var delta = now - ms;
Monty Taylor860bb0a2014-03-22 09:41:25 -0700181 var status = 'text-success';
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000182 var text = this.time(delta, true);
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100183 if (delta > (4 * hours)) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700184 status = 'text-danger';
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100185 } else if (delta > (2 * hours)) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700186 status = 'text-warning';
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100187 }
188 return '<span class="' + status + '">' + text + '</span>';
189 },
190
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100191 time: function(ms, words) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700192 if (typeof(words) === 'undefined') {
193 words = false;
194 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100195 var seconds = (+ms)/1000;
196 var minutes = Math.floor(seconds/60);
197 var hours = Math.floor(minutes/60);
198 seconds = Math.floor(seconds % 60);
199 minutes = Math.floor(minutes % 60);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700200 var r = '';
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100201 if (words) {
202 if (hours) {
203 r += hours;
204 r += ' hr ';
205 }
206 r += minutes + ' min';
207 } else {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700208 if (hours < 10) {
209 r += '0';
210 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100211 r += hours + ':';
Monty Taylor860bb0a2014-03-22 09:41:25 -0700212 if (minutes < 10) {
213 r += '0';
214 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100215 r += minutes + ':';
Monty Taylor860bb0a2014-03-22 09:41:25 -0700216 if (seconds < 10) {
217 r += '0';
218 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100219 r += seconds;
220 }
221 return r;
222 },
223
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100224 change_total_progress_bar: function(change) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700225 var job_percent = Math.floor(100 / change.jobs.length);
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100226 var $bar_outter = $('<div />')
227 .addClass('progress zuul-change-total-result');
228
229 $.each(change.jobs, function (i, job) {
230 var result = job.result ? job.result.toLowerCase() : null;
231 if (result === null) {
232 result = job.url ? 'in progress' : 'queued';
233 }
234
Monty Taylor860bb0a2014-03-22 09:41:25 -0700235 if (result !== 'queued') {
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100236 var $bar_inner = $('<div />')
237 .addClass('progress-bar');
238
239 switch (result) {
240 case 'success':
241 $bar_inner.addClass('progress-bar-success');
242 break;
243 case 'lost':
244 case 'failure':
245 $bar_inner.addClass('progress-bar-danger');
246 break;
247 case 'unstable':
248 $bar_inner.addClass('progress-bar-warning');
249 break;
250 case 'in progress':
251 case 'queued':
252 break;
253 }
254 $bar_inner.attr('title', job.name)
255 .css('width', job_percent + '%');
256 $bar_outter.append($bar_inner);
257 }
258 });
259 return $bar_outter;
260 },
261
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100262 change_header: function(change) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700263 var change_id = change.id || 'NA';
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100264 if (change_id.length === 40) {
265 change_id = change_id.substr(0, 7);
Joshua Hesketh298c4912014-03-20 16:06:25 +1100266 }
Timo Tijhof51516cd2013-04-09 01:32:29 +0200267
Monty Taylor860bb0a2014-03-22 09:41:25 -0700268 var $change_link = $('<small />');
Joshua Hesketh298c4912014-03-20 16:06:25 +1100269 if (change.url !== null) {
Joshua Heskethcd7a0772014-08-21 11:32:44 +1000270 if (/^[0-9a-f]{40}$/.test(change.id)) {
271 var change_id_short = change.id.slice(0, 7);
272 $change_link.append(
273 $('<a />').attr('href', change.url).append(
274 $('<abbr />')
275 .attr('title', change.id)
276 .text(change_id_short)
277 )
278 );
279 }
280 else {
281 $change_link.append(
282 $('<a />').attr('href', change.url).text(change.id)
283 );
284 }
Timo Tijhof51516cd2013-04-09 01:32:29 +0200285 }
Joshua Hesketh298c4912014-03-20 16:06:25 +1100286 else {
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100287 $change_link.text(change_id);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200288 }
Timo Tijhof51516cd2013-04-09 01:32:29 +0200289
Monty Taylor860bb0a2014-03-22 09:41:25 -0700290 var $change_progress_row_left = $('<div />')
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100291 .addClass('col-xs-3')
292 .append($change_link);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700293 var $change_progress_row_right = $('<div />')
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100294 .addClass('col-xs-9')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000295 .append(this.change_total_progress_bar(change));
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100296
Monty Taylor860bb0a2014-03-22 09:41:25 -0700297 var $change_progress_row = $('<div />')
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100298 .addClass('row')
299 .append($change_progress_row_left)
Monty Taylor860bb0a2014-03-22 09:41:25 -0700300 .append($change_progress_row_right);
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100301
Monty Taylor860bb0a2014-03-22 09:41:25 -0700302 var $project_span = $('<span />')
Joshua Heskethace48892014-03-22 17:18:31 +1100303 .addClass('change_project')
304 .text(change.project);
305
Monty Taylor860bb0a2014-03-22 09:41:25 -0700306 var $left = $('<div />')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100307 .addClass('col-xs-8')
Joshua Hesketh1751e4f2014-04-01 13:24:51 +1100308 .append($project_span, $change_progress_row);
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100309
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000310 var remaining_time = this.time(
Monty Taylor860bb0a2014-03-22 09:41:25 -0700311 change.remaining_time, true);
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000312 var enqueue_time = this.enqueue_time(
Monty Taylor860bb0a2014-03-22 09:41:25 -0700313 change.enqueue_time);
314 var $remaining_time = $('<small />').addClass('time')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100315 .attr('title', 'Remaining Time').html(remaining_time);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700316 var $enqueue_time = $('<small />').addClass('time')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100317 .attr('title', 'Elapsed Time').html(enqueue_time);
318
Monty Taylor860bb0a2014-03-22 09:41:25 -0700319 var $right = $('<div />')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100320 .addClass('col-xs-4 text-right')
321 .append($remaining_time, $('<br />'), $enqueue_time);
322
Monty Taylor860bb0a2014-03-22 09:41:25 -0700323 var $header = $('<div />')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100324 .addClass('row')
325 .append($left, $right);
326 return $header;
327 },
328
329 change_list: function(jobs) {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000330 var format = this;
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100331 var $list = $('<ul />')
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100332 .addClass('list-group zuul-patchset-body');
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100333
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100334 $.each(jobs, function (i, job) {
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100335 var $item = $('<li />')
336 .addClass('list-group-item')
337 .addClass('zuul-change-job')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000338 .append(format.job(job));
Joshua Hesketh298c4912014-03-20 16:06:25 +1100339 $list.append($item);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200340 });
341
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100342 return $list;
343 },
344
345 change_panel: function (change) {
346 var $header = $('<div />')
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100347 .addClass('panel-heading zuul-patchset-header')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000348 .append(this.change_header(change));
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100349
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100350 var panel_id = change.id ? change.id.replace(',', '_')
351 : change.project.replace('/', '_') +
Monty Taylor860bb0a2014-03-22 09:41:25 -0700352 '-' + change.enqueue_time;
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100353 var $panel = $('<div />')
Monty Taylor860bb0a2014-03-22 09:41:25 -0700354 .attr('id', panel_id)
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100355 .addClass('panel panel-default zuul-change')
356 .append($header)
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000357 .append(this.change_list(change.jobs));
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100358
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000359 $header.click(this.toggle_patchset);
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100360 return $panel;
Timo Tijhof51516cd2013-04-09 01:32:29 +0200361 },
362
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100363 change_status_icon: function(change) {
364 var icon_name = 'green.png';
365 var icon_title = 'Succeeding';
366
367 if (change.active !== true) {
368 // Grey icon
369 icon_name = 'grey.png';
370 icon_title = 'Waiting until closer to head of queue to' +
371 ' start jobs';
372 }
James E. Blair107c3852015-02-07 08:23:10 -0800373 else if (change.live !== true) {
374 // Grey icon
375 icon_name = 'grey.png';
376 icon_title = 'Dependent change independently tested';
377 }
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100378 else if (change.failing_reasons &&
379 change.failing_reasons.length > 0) {
380 var reason = change.failing_reasons.join(', ');
381 icon_title = 'Failing because ' + reason;
382 if (reason.match(/merge conflict/)) {
383 // Black icon
384 icon_name = 'black.png';
385 }
386 else {
387 // Red icon
388 icon_name = 'red.png';
389 }
390 }
391
392 var $icon = $('<img />')
393 .attr('src', 'images/' + icon_name)
394 .attr('title', icon_title)
395 .css('margin-top', '-6px');
396
397 return $icon;
398 },
399
400 change_with_status_tree: function(change, change_queue) {
401 var $change_row = $('<tr />');
402
403 for (var i = 0; i < change_queue._tree_columns; i++) {
404 var $tree_cell = $('<td />')
405 .css('height', '100%')
406 .css('padding', '0 0 10px 0')
407 .css('margin', '0')
408 .css('width', '16px')
409 .css('min-width', '16px')
410 .css('overflow', 'hidden')
411 .css('vertical-align', 'top');
412
413 if (i < change._tree.length && change._tree[i] !== null) {
414 $tree_cell.css('background-image',
415 'url(\'images/line.png\')')
416 .css('background-repeat', 'repeat-y');
417 }
418
419 if (i === change._tree_index) {
420 $tree_cell.append(
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000421 this.change_status_icon(change));
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100422 }
423 if (change._tree_branches.indexOf(i) !== -1) {
424 var $image = $('<img />')
425 .css('vertical-align', 'baseline');
426 if (change._tree_branches.indexOf(i) ===
427 change._tree_branches.length - 1) {
428 // Angle line
429 $image.attr('src', 'images/line-angle.png');
430 }
431 else {
432 // T line
433 $image.attr('src', 'images/line-t.png');
434 }
435 $tree_cell.append($image);
436 }
437 $change_row.append($tree_cell);
438 }
439
440 var change_width = 360 - 16*change_queue._tree_columns;
441 var $change_column = $('<td />')
442 .css('width', change_width + 'px')
443 .addClass('zuul-change-cell')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000444 .append(this.change_panel(change));
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100445
446 $change_row.append($change_column);
447
448 var $change_table = $('<table />')
449 .addClass('zuul-change-box')
450 .css('-moz-box-sizing', 'content-box')
451 .css('box-sizing', 'content-box')
452 .append($change_row);
453
454 return $change_table;
455 },
456
Joshua Hesketh9d013542014-04-03 13:08:04 +1100457 pipeline_sparkline: function(pipeline_name) {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000458 if (options.graphite_url !== '') {
Joshua Hesketh9d013542014-04-03 13:08:04 +1100459 var $sparkline = $('<img />')
460 .addClass('pull-right')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000461 .attr('src', get_sparkline_url(pipeline_name));
Joshua Hesketh9d013542014-04-03 13:08:04 +1100462 return $sparkline;
463 }
464 return false;
465 },
466
467 pipeline_header: function(pipeline, count) {
468 // Format the pipeline name, sparkline and description
469 var $header_div = $('<div />')
470 .addClass('zuul-pipeline-header');
471
472 var $heading = $('<h3 />')
473 .css('vertical-align', 'middle')
474 .text(pipeline.name)
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100475 .append(
Joshua Hesketh9d013542014-04-03 13:08:04 +1100476 $('<span />')
477 .addClass('badge pull-right')
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100478 .css('vertical-align', 'middle')
Joshua Hesketh9d013542014-04-03 13:08:04 +1100479 .css('margin-top', '0.5em')
480 .text(count)
481 )
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000482 .append(this.pipeline_sparkline(pipeline.name));
Joshua Hesketh9d013542014-04-03 13:08:04 +1100483
484 $header_div.append($heading);
Joshua Hesketh298c4912014-03-20 16:06:25 +1100485
Timo Tijhof51516cd2013-04-09 01:32:29 +0200486 if (typeof pipeline.description === 'string') {
Joshua Hesketh9d013542014-04-03 13:08:04 +1100487 $header_div.append(
Joshua Hesketh298c4912014-03-20 16:06:25 +1100488 $('<p />').append(
489 $('<small />').text(pipeline.description)
490 )
491 );
Timo Tijhof51516cd2013-04-09 01:32:29 +0200492 }
Joshua Hesketh9d013542014-04-03 13:08:04 +1100493 return $header_div;
494 },
495
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000496 pipeline: function (pipeline, count) {
497 var format = this;
Joshua Hesketh9d013542014-04-03 13:08:04 +1100498 var $html = $('<div />')
499 .addClass('zuul-pipeline col-md-4')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000500 .append(this.pipeline_header(pipeline, count));
Timo Tijhof51516cd2013-04-09 01:32:29 +0200501
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100502 $.each(pipeline.change_queues,
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100503 function (queue_i, change_queue) {
504 $.each(change_queue.heads, function (head_i, changes) {
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100505 if (pipeline.change_queues.length > 1 &&
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100506 head_i === 0) {
507 var name = change_queue.name;
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100508 var short_name = name;
509 if (short_name.length > 32) {
510 short_name = short_name.substr(0, 32) + '...';
Timo Tijhof51516cd2013-04-09 01:32:29 +0200511 }
Joshua Hesketh298c4912014-03-20 16:06:25 +1100512 $html.append(
513 $('<p />')
514 .text('Queue: ')
515 .append(
516 $('<abbr />')
517 .attr('title', name)
518 .text(short_name)
519 )
520 );
Timo Tijhof51516cd2013-04-09 01:32:29 +0200521 }
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100522
523 $.each(changes, function (change_i, change) {
524 var $change_box =
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000525 format.change_with_status_tree(
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100526 change, change_queue);
527 $html.append($change_box);
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000528 format.display_patchset($change_box);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200529 });
530 });
531 });
Joshua Hesketh298c4912014-03-20 16:06:25 +1100532 return $html;
Joshua Heskethace48892014-03-22 17:18:31 +1100533 },
534
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000535 toggle_patchset: function(e) {
536 // Toggle showing/hiding the patchset when the header is
537 // clicked.
538
539 // Grab the patchset panel
540 var $panel = $(e.target).parents('.zuul-change');
541 var $body = $panel.children('.zuul-patchset-body');
542 $body.toggle(200);
543 var collapsed_index = collapsed_exceptions.indexOf(
544 $panel.attr('id'));
545 if (collapsed_index === -1 ) {
546 // Currently not an exception, add it to list
547 collapsed_exceptions.push($panel.attr('id'));
548 }
549 else {
550 // Currently an except, remove from exceptions
551 collapsed_exceptions.splice(collapsed_index, 1);
552 }
553 },
554
555 display_patchset: function($change_box, animate) {
556 // Determine if to show or hide the patchset and/or the results
557 // when loaded
558
559 // See if we should hide the body/results
560 var $panel = $change_box.find('.zuul-change');
561 var panel_change = $panel.attr('id');
562 var $body = $panel.children('.zuul-patchset-body');
563 var expand_by_default = $('#expand_by_default')
564 .prop('checked');
565
566 var collapsed_index = collapsed_exceptions
567 .indexOf(panel_change);
568
569 if (expand_by_default && collapsed_index === -1 ||
570 !expand_by_default && collapsed_index !== -1) {
571 // Expand by default, or is an exception
572 $body.show(animate);
573 }
574 else {
575 $body.hide(animate);
576 }
577
578 // Check if we should hide the whole panel
579 var panel_project = $panel.find('.change_project').text()
580 .toLowerCase();
581
582
583 var panel_pipeline = $change_box
584 .parents('.zuul-pipeline')
585 .find('.zuul-pipeline-header > h3')
586 .html()
587 .toLowerCase();
588
589 if (current_filter !== '') {
590 var show_panel = false;
591 var filter = current_filter.trim().split(/[\s,]+/);
592 $.each(filter, function(index, f_val) {
593 if (f_val !== '') {
594 f_val = f_val.toLowerCase();
595 if (panel_project.indexOf(f_val) !== -1 ||
596 panel_pipeline.indexOf(f_val) !== -1 ||
597 panel_change.indexOf(f_val) !== -1) {
598 show_panel = true;
599 }
600 }
601 });
602 if (show_panel === true) {
603 $change_box.show(animate);
604 }
605 else {
606 $change_box.hide(animate);
607 }
608 }
609 else {
610 $change_box.show(animate);
611 }
612 },
613 };
614
615 var app = {
616 schedule: function (app) {
617 var app = app || this;
618 if (!options.enabled) {
619 setTimeout(function() {app.schedule(app);}, 5000);
620 return;
621 }
622 app.update().complete(function () {
623 setTimeout(function() {app.schedule(app);}, 5000);
624 });
625
626 /* Only update graphs every minute */
627 if (zuul_graph_update_count > 11) {
628 zuul_graph_update_count = 0;
629 zuul.update_sparklines();
630 }
631 },
632
633 /** @return {jQuery.Promise} */
634 update: function () {
635 // Cancel the previous update if it hasn't completed yet.
636 if (xhr) {
637 xhr.abort();
638 }
639
640 this.emit('update-start');
641 var app = this;
642
643 var $msg = $(options.msg_id)
644 xhr = $.getJSON(options.source)
645 .done(function (data) {
646 if ('message' in data) {
647 $msg.removeClass('alert-danger')
648 .addClass('alert-info')
649 .text(data.message)
650 .show();
651 } else {
652 $msg.empty()
653 .hide();
654 }
655
656 if ('zuul_version' in data) {
657 $('#zuul-version-span').text(data.zuul_version);
658 }
659 if ('last_reconfigured' in data) {
660 var last_reconfigured =
661 new Date(data.last_reconfigured);
662 $('#last-reconfigured-span').text(
663 last_reconfigured.toString());
664 }
665
666 var $pipelines = $(options.pipelines_id);
667 $pipelines.html('');
668 $.each(data.pipelines, function (i, pipeline) {
669 var count = app.create_tree(pipeline);
670 $pipelines.append(
671 format.pipeline(pipeline, count));
672 });
673
674 $(options.queue_events_num).text(
675 data.trigger_event_queue ?
676 data.trigger_event_queue.length : '0'
677 );
678 $(options.queue_results_num).text(
679 data.result_event_queue ?
680 data.result_event_queue.length : '0'
681 );
682 })
683 .fail(function (err, jqXHR, errMsg) {
Joshua Heskethd50eb582014-04-30 19:51:22 +1000684 $msg.text(options.source + ': ' + errMsg)
685 .addClass('alert-danger')
686 .removeClass('zuul-msg-wrap-off')
687 .show();
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000688 })
689 .complete(function () {
690 xhr = undefined;
691 app.emit('update-end');
692 });
693
694 return xhr;
695 },
696
697 update_sparklines: function() {
698 $.each(zuul_sparkline_urls, function(name, url) {
699 var newimg = new Image();
700 var parts = url.split('#');
701 newimg.src = parts[0] + '#' + new Date().getTime();
702 $(newimg).load(function (x) {
703 zuul_sparkline_urls[name] = newimg.src;
704 });
705 });
706 },
707
708 emit: function () {
709 $jq.trigger.apply($jq, arguments);
710 return this;
711 },
712 on: function () {
713 $jq.on.apply($jq, arguments);
714 return this;
715 },
716 one: function () {
717 $jq.one.apply($jq, arguments);
718 return this;
719 },
720
721 control_form: function() {
722 // Build the filter form filling anything from cookies
723
724 var $control_form = $('<form />')
725 .attr('role', 'form')
726 .addClass('form-inline')
727 .submit(this.handle_filter_change);
728
729 $control_form
730 .append(this.filter_form_group())
731 .append(this.expand_form_group());
732
733 return $control_form;
734 },
735
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100736 filter_form_group: function() {
Joshua Heskethace48892014-03-22 17:18:31 +1100737 // Update the filter form with a clear button if required
738
739 var $label = $('<label />')
740 .addClass('control-label')
741 .attr('for', 'filter_string')
742 .text('Filters')
743 .css('padding-right', '0.5em');
744
745 var $input = $('<input />')
746 .attr('type', 'text')
747 .attr('id', 'filter_string')
748 .addClass('form-control')
749 .attr('title',
750 'project(s), pipeline(s) or review(s) comma ' +
751 'separated')
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100752 .attr('value', current_filter);
Joshua Heskethace48892014-03-22 17:18:31 +1100753
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000754 $input.change(this.handle_filter_change);
Joshua Heskethace48892014-03-22 17:18:31 +1100755
756 var $clear_icon = $('<span />')
757 .addClass('form-control-feedback')
758 .addClass('glyphicon glyphicon-remove-circle')
759 .attr('id', 'filter_form_clear_box')
760 .attr('title', 'clear filter')
761 .css('cursor', 'pointer');
762
763 $clear_icon.click(function() {
764 $('#filter_string').val('').change();
765 });
766
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100767 if (current_filter === '') {
Joshua Heskethace48892014-03-22 17:18:31 +1100768 $clear_icon.hide();
769 }
770
771 var $form_group = $('<div />')
772 .addClass('form-group has-feedback')
773 .append($label, $input, $clear_icon);
774 return $form_group;
775 },
776
Joshua Heskethae230f62014-03-22 22:14:44 +1100777 expand_form_group: function() {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700778 var expand_by_default = (
Joshua Heskethae230f62014-03-22 22:14:44 +1100779 read_cookie('zuul_expand_by_default', false) === 'true');
780
Monty Taylor860bb0a2014-03-22 09:41:25 -0700781 var $checkbox = $('<input />')
Joshua Heskethae230f62014-03-22 22:14:44 +1100782 .attr('type', 'checkbox')
783 .attr('id', 'expand_by_default')
784 .prop('checked', expand_by_default)
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000785 .change(this.handle_expand_by_default);
Joshua Heskethae230f62014-03-22 22:14:44 +1100786
Monty Taylor860bb0a2014-03-22 09:41:25 -0700787 var $label = $('<label />')
Joshua Heskethae230f62014-03-22 22:14:44 +1100788 .css('padding-left', '1em')
789 .html('Expand by default: ')
790 .append($checkbox);
791
792 var $form_group = $('<div />')
793 .addClass('checkbox')
794 .append($label);
795 return $form_group;
796 },
797
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000798 handle_filter_change: function() {
799 // Update the filter and save it to a cookie
800 current_filter = $('#filter_string').val();
801 set_cookie('zuul_filter_string', current_filter);
802 if (current_filter === '') {
803 $('#filter_form_clear_box').hide();
Joshua Heskethace48892014-03-22 17:18:31 +1100804 }
805 else {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000806 $('#filter_form_clear_box').show();
Joshua Heskethace48892014-03-22 17:18:31 +1100807 }
Joshua Heskethace48892014-03-22 17:18:31 +1100808
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000809 $('.zuul-change-box').each(function(index, obj) {
810 var $change_box = $(obj);
811 format.display_patchset($change_box, 200);
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100812 });
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000813 return false;
Timo Tijhof51516cd2013-04-09 01:32:29 +0200814 },
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000815
816 handle_expand_by_default: function(e) {
817 // Handle toggling expand by default
818 set_cookie('zuul_expand_by_default', e.target.checked);
819 collapsed_exceptions = [];
820 $('.zuul-change-box').each(function(index, obj) {
821 var $change_box = $(obj);
822 format.display_patchset($change_box, 200);
823 });
824 },
825
826 create_tree: function(pipeline) {
827 var count = 0;
828 var pipeline_max_tree_columns = 1;
829 $.each(pipeline.change_queues, function(change_queue_i,
830 change_queue) {
831 var tree = [];
832 var max_tree_columns = 1;
833 var changes = [];
834 var last_tree_length = 0;
835 $.each(change_queue.heads, function(head_i, head) {
836 $.each(head, function(change_i, change) {
837 changes[change.id] = change;
838 change._tree_position = change_i;
839 });
840 });
841 $.each(change_queue.heads, function(head_i, head) {
842 $.each(head, function(change_i, change) {
James E. Blaird1d3ce32015-02-11 17:56:45 -0800843 if (change.live === true) {
844 count += 1;
845 }
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000846 var idx = tree.indexOf(change.id);
847 if (idx > -1) {
848 change._tree_index = idx;
849 // remove...
850 tree[idx] = null;
851 while (tree[tree.length - 1] === null) {
852 tree.pop();
853 }
854 } else {
855 change._tree_index = 0;
856 }
857 change._tree_branches = [];
858 change._tree = [];
859 if (typeof(change.items_behind) === 'undefined') {
860 change.items_behind = [];
861 }
862 change.items_behind.sort(function(a, b) {
863 return (changes[b]._tree_position -
864 changes[a]._tree_position);
865 });
866 $.each(change.items_behind, function(i, id) {
867 tree.push(id);
868 if (tree.length>last_tree_length &&
869 last_tree_length > 0) {
870 change._tree_branches.push(
871 tree.length - 1);
872 }
873 });
874 if (tree.length > max_tree_columns) {
875 max_tree_columns = tree.length;
876 }
877 if (tree.length > pipeline_max_tree_columns) {
878 pipeline_max_tree_columns = tree.length;
879 }
880 change._tree = tree.slice(0); // make a copy
881 last_tree_length = tree.length;
882 });
883 });
884 change_queue._tree_columns = max_tree_columns;
885 });
886 pipeline._tree_columns = pipeline_max_tree_columns;
887 return count;
888 },
889 };
890
891 $jq = $(app);
892 return {
893 options: options,
894 format: format,
895 app: app,
896 jq: $jq
897 };
898 }
Timo Tijhof51516cd2013-04-09 01:32:29 +0200899}(jQuery));