blob: d6f93c82f686b3fd8f4e8a5892fc7cf352ce5cb2 [file] [log] [blame]
Timo Tijhof51516cd2013-04-09 01:32:29 +02001// Client script for Zuul status page
2//
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 = [];
53 var current_filter = read_cookie('zuul_filter_string', current_filter);
54 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 Hesketh6b1a2182014-03-21 14:40:04 +1100114 return $job_line;
115 },
116
117 job_status: function(job) {
118 var result = job.result ? job.result.toLowerCase() : null;
119 if (result === null) {
120 result = job.url ? 'in progress' : 'queued';
121 }
122
Monty Taylor860bb0a2014-03-22 09:41:25 -0700123 if (result === 'in progress') {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000124 return this.job_progress_bar(job.elapsed_time,
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100125 job.remaining_time);
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100126 }
127 else {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000128 return this.status_label(result);
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100129 }
130 },
131
132 status_label: function(result) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700133 var $status = $('<span />');
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100134 $status.addClass('zuul-job-result label');
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100135
136 switch (result) {
137 case 'success':
138 $status.addClass('label-success');
139 break;
140 case 'failure':
141 $status.addClass('label-danger');
142 break;
143 case 'unstable':
144 $status.addClass('label-warning');
145 break;
146 case 'in progress':
147 case 'queued':
148 case 'lost':
149 $status.addClass('label-default');
150 break;
151 }
152 $status.text(result);
153 return $status;
154 },
155
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100156 job_progress_bar: function(elapsed_time, remaining_time) {
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100157 var progress_percent = 100 * (elapsed_time / (elapsed_time +
158 remaining_time));
159 var $bar_inner = $('<div />')
160 .addClass('progress-bar')
161 .attr('role', 'progressbar')
162 .attr('aria-valuenow', 'progressbar')
163 .attr('aria-valuemin', progress_percent)
164 .attr('aria-valuemin', '0')
165 .attr('aria-valuemax', '100')
166 .css('width', progress_percent + '%');
167
168 var $bar_outter = $('<div />')
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100169 .addClass('progress zuul-job-result')
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100170 .append($bar_inner);
171
172 return $bar_outter;
173 },
174
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100175 enqueue_time: function(ms) {
176 // Special format case for enqueue time to add style
177 var hours = 60 * 60 * 1000;
178 var now = Date.now();
179 var delta = now - ms;
Monty Taylor860bb0a2014-03-22 09:41:25 -0700180 var status = 'text-success';
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000181 var text = this.time(delta, true);
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100182 if (delta > (4 * hours)) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700183 status = 'text-danger';
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100184 } else if (delta > (2 * hours)) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700185 status = 'text-warning';
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100186 }
187 return '<span class="' + status + '">' + text + '</span>';
188 },
189
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100190 time: function(ms, words) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700191 if (typeof(words) === 'undefined') {
192 words = false;
193 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100194 var seconds = (+ms)/1000;
195 var minutes = Math.floor(seconds/60);
196 var hours = Math.floor(minutes/60);
197 seconds = Math.floor(seconds % 60);
198 minutes = Math.floor(minutes % 60);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700199 var r = '';
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100200 if (words) {
201 if (hours) {
202 r += hours;
203 r += ' hr ';
204 }
205 r += minutes + ' min';
206 } else {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700207 if (hours < 10) {
208 r += '0';
209 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100210 r += hours + ':';
Monty Taylor860bb0a2014-03-22 09:41:25 -0700211 if (minutes < 10) {
212 r += '0';
213 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100214 r += minutes + ':';
Monty Taylor860bb0a2014-03-22 09:41:25 -0700215 if (seconds < 10) {
216 r += '0';
217 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100218 r += seconds;
219 }
220 return r;
221 },
222
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100223 change_total_progress_bar: function(change) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700224 var job_percent = Math.floor(100 / change.jobs.length);
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100225 var $bar_outter = $('<div />')
226 .addClass('progress zuul-change-total-result');
227
228 $.each(change.jobs, function (i, job) {
229 var result = job.result ? job.result.toLowerCase() : null;
230 if (result === null) {
231 result = job.url ? 'in progress' : 'queued';
232 }
233
Monty Taylor860bb0a2014-03-22 09:41:25 -0700234 if (result !== 'queued') {
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100235 var $bar_inner = $('<div />')
236 .addClass('progress-bar');
237
238 switch (result) {
239 case 'success':
240 $bar_inner.addClass('progress-bar-success');
241 break;
242 case 'lost':
243 case 'failure':
244 $bar_inner.addClass('progress-bar-danger');
245 break;
246 case 'unstable':
247 $bar_inner.addClass('progress-bar-warning');
248 break;
249 case 'in progress':
250 case 'queued':
251 break;
252 }
253 $bar_inner.attr('title', job.name)
254 .css('width', job_percent + '%');
255 $bar_outter.append($bar_inner);
256 }
257 });
258 return $bar_outter;
259 },
260
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100261 change_header: function(change) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700262 var change_id = change.id || 'NA';
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100263 if (change_id.length === 40) {
264 change_id = change_id.substr(0, 7);
Joshua Hesketh298c4912014-03-20 16:06:25 +1100265 }
Timo Tijhof51516cd2013-04-09 01:32:29 +0200266
Monty Taylor860bb0a2014-03-22 09:41:25 -0700267 var $change_link = $('<small />');
Joshua Hesketh298c4912014-03-20 16:06:25 +1100268 if (change.url !== null) {
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100269 $change_link.append(
Monty Taylor860bb0a2014-03-22 09:41:25 -0700270 $('<a />').attr('href', change.url).text(change.id)
Joshua Hesketh298c4912014-03-20 16:06:25 +1100271 );
Timo Tijhof51516cd2013-04-09 01:32:29 +0200272 }
Joshua Hesketh298c4912014-03-20 16:06:25 +1100273 else {
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100274 $change_link.text(change_id);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200275 }
Timo Tijhof51516cd2013-04-09 01:32:29 +0200276
Monty Taylor860bb0a2014-03-22 09:41:25 -0700277 var $change_progress_row_left = $('<div />')
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100278 .addClass('col-xs-3')
279 .append($change_link);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700280 var $change_progress_row_right = $('<div />')
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100281 .addClass('col-xs-9')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000282 .append(this.change_total_progress_bar(change));
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100283
Monty Taylor860bb0a2014-03-22 09:41:25 -0700284 var $change_progress_row = $('<div />')
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100285 .addClass('row')
286 .append($change_progress_row_left)
Monty Taylor860bb0a2014-03-22 09:41:25 -0700287 .append($change_progress_row_right);
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100288
Monty Taylor860bb0a2014-03-22 09:41:25 -0700289 var $project_span = $('<span />')
Joshua Heskethace48892014-03-22 17:18:31 +1100290 .addClass('change_project')
291 .text(change.project);
292
Monty Taylor860bb0a2014-03-22 09:41:25 -0700293 var $left = $('<div />')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100294 .addClass('col-xs-8')
Joshua Hesketh1751e4f2014-04-01 13:24:51 +1100295 .append($project_span, $change_progress_row);
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100296
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000297 var remaining_time = this.time(
Monty Taylor860bb0a2014-03-22 09:41:25 -0700298 change.remaining_time, true);
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000299 var enqueue_time = this.enqueue_time(
Monty Taylor860bb0a2014-03-22 09:41:25 -0700300 change.enqueue_time);
301 var $remaining_time = $('<small />').addClass('time')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100302 .attr('title', 'Remaining Time').html(remaining_time);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700303 var $enqueue_time = $('<small />').addClass('time')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100304 .attr('title', 'Elapsed Time').html(enqueue_time);
305
Monty Taylor860bb0a2014-03-22 09:41:25 -0700306 var $right = $('<div />')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100307 .addClass('col-xs-4 text-right')
308 .append($remaining_time, $('<br />'), $enqueue_time);
309
Monty Taylor860bb0a2014-03-22 09:41:25 -0700310 var $header = $('<div />')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100311 .addClass('row')
312 .append($left, $right);
313 return $header;
314 },
315
316 change_list: function(jobs) {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000317 var format = this;
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100318 var $list = $('<ul />')
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100319 .addClass('list-group zuul-patchset-body');
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100320
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100321 $.each(jobs, function (i, job) {
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100322 var $item = $('<li />')
323 .addClass('list-group-item')
324 .addClass('zuul-change-job')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000325 .append(format.job(job));
Joshua Hesketh298c4912014-03-20 16:06:25 +1100326 $list.append($item);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200327 });
328
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100329 return $list;
330 },
331
332 change_panel: function (change) {
333 var $header = $('<div />')
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100334 .addClass('panel-heading zuul-patchset-header')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000335 .append(this.change_header(change));
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100336
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100337 var panel_id = change.id ? change.id.replace(',', '_')
338 : change.project.replace('/', '_') +
Monty Taylor860bb0a2014-03-22 09:41:25 -0700339 '-' + change.enqueue_time;
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100340 var $panel = $('<div />')
Monty Taylor860bb0a2014-03-22 09:41:25 -0700341 .attr('id', panel_id)
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100342 .addClass('panel panel-default zuul-change')
343 .append($header)
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000344 .append(this.change_list(change.jobs));
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100345
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000346 $header.click(this.toggle_patchset);
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100347 return $panel;
Timo Tijhof51516cd2013-04-09 01:32:29 +0200348 },
349
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100350 change_status_icon: function(change) {
351 var icon_name = 'green.png';
352 var icon_title = 'Succeeding';
353
354 if (change.active !== true) {
355 // Grey icon
356 icon_name = 'grey.png';
357 icon_title = 'Waiting until closer to head of queue to' +
358 ' start jobs';
359 }
360 else if (change.failing_reasons &&
361 change.failing_reasons.length > 0) {
362 var reason = change.failing_reasons.join(', ');
363 icon_title = 'Failing because ' + reason;
364 if (reason.match(/merge conflict/)) {
365 // Black icon
366 icon_name = 'black.png';
367 }
368 else {
369 // Red icon
370 icon_name = 'red.png';
371 }
372 }
373
374 var $icon = $('<img />')
375 .attr('src', 'images/' + icon_name)
376 .attr('title', icon_title)
377 .css('margin-top', '-6px');
378
379 return $icon;
380 },
381
382 change_with_status_tree: function(change, change_queue) {
383 var $change_row = $('<tr />');
384
385 for (var i = 0; i < change_queue._tree_columns; i++) {
386 var $tree_cell = $('<td />')
387 .css('height', '100%')
388 .css('padding', '0 0 10px 0')
389 .css('margin', '0')
390 .css('width', '16px')
391 .css('min-width', '16px')
392 .css('overflow', 'hidden')
393 .css('vertical-align', 'top');
394
395 if (i < change._tree.length && change._tree[i] !== null) {
396 $tree_cell.css('background-image',
397 'url(\'images/line.png\')')
398 .css('background-repeat', 'repeat-y');
399 }
400
401 if (i === change._tree_index) {
402 $tree_cell.append(
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000403 this.change_status_icon(change));
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100404 }
405 if (change._tree_branches.indexOf(i) !== -1) {
406 var $image = $('<img />')
407 .css('vertical-align', 'baseline');
408 if (change._tree_branches.indexOf(i) ===
409 change._tree_branches.length - 1) {
410 // Angle line
411 $image.attr('src', 'images/line-angle.png');
412 }
413 else {
414 // T line
415 $image.attr('src', 'images/line-t.png');
416 }
417 $tree_cell.append($image);
418 }
419 $change_row.append($tree_cell);
420 }
421
422 var change_width = 360 - 16*change_queue._tree_columns;
423 var $change_column = $('<td />')
424 .css('width', change_width + 'px')
425 .addClass('zuul-change-cell')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000426 .append(this.change_panel(change));
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100427
428 $change_row.append($change_column);
429
430 var $change_table = $('<table />')
431 .addClass('zuul-change-box')
432 .css('-moz-box-sizing', 'content-box')
433 .css('box-sizing', 'content-box')
434 .append($change_row);
435
436 return $change_table;
437 },
438
Joshua Hesketh9d013542014-04-03 13:08:04 +1100439 pipeline_sparkline: function(pipeline_name) {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000440 if (options.graphite_url !== '') {
Joshua Hesketh9d013542014-04-03 13:08:04 +1100441 var $sparkline = $('<img />')
442 .addClass('pull-right')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000443 .attr('src', get_sparkline_url(pipeline_name));
Joshua Hesketh9d013542014-04-03 13:08:04 +1100444 return $sparkline;
445 }
446 return false;
447 },
448
449 pipeline_header: function(pipeline, count) {
450 // Format the pipeline name, sparkline and description
451 var $header_div = $('<div />')
452 .addClass('zuul-pipeline-header');
453
454 var $heading = $('<h3 />')
455 .css('vertical-align', 'middle')
456 .text(pipeline.name)
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100457 .append(
Joshua Hesketh9d013542014-04-03 13:08:04 +1100458 $('<span />')
459 .addClass('badge pull-right')
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100460 .css('vertical-align', 'middle')
Joshua Hesketh9d013542014-04-03 13:08:04 +1100461 .css('margin-top', '0.5em')
462 .text(count)
463 )
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000464 .append(this.pipeline_sparkline(pipeline.name));
Joshua Hesketh9d013542014-04-03 13:08:04 +1100465
466 $header_div.append($heading);
Joshua Hesketh298c4912014-03-20 16:06:25 +1100467
Timo Tijhof51516cd2013-04-09 01:32:29 +0200468 if (typeof pipeline.description === 'string') {
Joshua Hesketh9d013542014-04-03 13:08:04 +1100469 $header_div.append(
Joshua Hesketh298c4912014-03-20 16:06:25 +1100470 $('<p />').append(
471 $('<small />').text(pipeline.description)
472 )
473 );
Timo Tijhof51516cd2013-04-09 01:32:29 +0200474 }
Joshua Hesketh9d013542014-04-03 13:08:04 +1100475 return $header_div;
476 },
477
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000478 pipeline: function (pipeline, count) {
479 var format = this;
Joshua Hesketh9d013542014-04-03 13:08:04 +1100480 var $html = $('<div />')
481 .addClass('zuul-pipeline col-md-4')
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000482 .append(this.pipeline_header(pipeline, count));
Timo Tijhof51516cd2013-04-09 01:32:29 +0200483
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100484 $.each(pipeline.change_queues,
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100485 function (queue_i, change_queue) {
486 $.each(change_queue.heads, function (head_i, changes) {
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100487 if (pipeline.change_queues.length > 1 &&
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100488 head_i === 0) {
489 var name = change_queue.name;
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100490 var short_name = name;
491 if (short_name.length > 32) {
492 short_name = short_name.substr(0, 32) + '...';
Timo Tijhof51516cd2013-04-09 01:32:29 +0200493 }
Joshua Hesketh298c4912014-03-20 16:06:25 +1100494 $html.append(
495 $('<p />')
496 .text('Queue: ')
497 .append(
498 $('<abbr />')
499 .attr('title', name)
500 .text(short_name)
501 )
502 );
Timo Tijhof51516cd2013-04-09 01:32:29 +0200503 }
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100504
505 $.each(changes, function (change_i, change) {
506 var $change_box =
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000507 format.change_with_status_tree(
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100508 change, change_queue);
509 $html.append($change_box);
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000510 format.display_patchset($change_box);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200511 });
512 });
513 });
Joshua Hesketh298c4912014-03-20 16:06:25 +1100514 return $html;
Joshua Heskethace48892014-03-22 17:18:31 +1100515 },
516
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000517 toggle_patchset: function(e) {
518 // Toggle showing/hiding the patchset when the header is
519 // clicked.
520
521 // Grab the patchset panel
522 var $panel = $(e.target).parents('.zuul-change');
523 var $body = $panel.children('.zuul-patchset-body');
524 $body.toggle(200);
525 var collapsed_index = collapsed_exceptions.indexOf(
526 $panel.attr('id'));
527 if (collapsed_index === -1 ) {
528 // Currently not an exception, add it to list
529 collapsed_exceptions.push($panel.attr('id'));
530 }
531 else {
532 // Currently an except, remove from exceptions
533 collapsed_exceptions.splice(collapsed_index, 1);
534 }
535 },
536
537 display_patchset: function($change_box, animate) {
538 // Determine if to show or hide the patchset and/or the results
539 // when loaded
540
541 // See if we should hide the body/results
542 var $panel = $change_box.find('.zuul-change');
543 var panel_change = $panel.attr('id');
544 var $body = $panel.children('.zuul-patchset-body');
545 var expand_by_default = $('#expand_by_default')
546 .prop('checked');
547
548 var collapsed_index = collapsed_exceptions
549 .indexOf(panel_change);
550
551 if (expand_by_default && collapsed_index === -1 ||
552 !expand_by_default && collapsed_index !== -1) {
553 // Expand by default, or is an exception
554 $body.show(animate);
555 }
556 else {
557 $body.hide(animate);
558 }
559
560 // Check if we should hide the whole panel
561 var panel_project = $panel.find('.change_project').text()
562 .toLowerCase();
563
564
565 var panel_pipeline = $change_box
566 .parents('.zuul-pipeline')
567 .find('.zuul-pipeline-header > h3')
568 .html()
569 .toLowerCase();
570
571 if (current_filter !== '') {
572 var show_panel = false;
573 var filter = current_filter.trim().split(/[\s,]+/);
574 $.each(filter, function(index, f_val) {
575 if (f_val !== '') {
576 f_val = f_val.toLowerCase();
577 if (panel_project.indexOf(f_val) !== -1 ||
578 panel_pipeline.indexOf(f_val) !== -1 ||
579 panel_change.indexOf(f_val) !== -1) {
580 show_panel = true;
581 }
582 }
583 });
584 if (show_panel === true) {
585 $change_box.show(animate);
586 }
587 else {
588 $change_box.hide(animate);
589 }
590 }
591 else {
592 $change_box.show(animate);
593 }
594 },
595 };
596
597 var app = {
598 schedule: function (app) {
599 var app = app || this;
600 if (!options.enabled) {
601 setTimeout(function() {app.schedule(app);}, 5000);
602 return;
603 }
604 app.update().complete(function () {
605 setTimeout(function() {app.schedule(app);}, 5000);
606 });
607
608 /* Only update graphs every minute */
609 if (zuul_graph_update_count > 11) {
610 zuul_graph_update_count = 0;
611 zuul.update_sparklines();
612 }
613 },
614
615 /** @return {jQuery.Promise} */
616 update: function () {
617 // Cancel the previous update if it hasn't completed yet.
618 if (xhr) {
619 xhr.abort();
620 }
621
622 this.emit('update-start');
623 var app = this;
624
625 var $msg = $(options.msg_id)
626 xhr = $.getJSON(options.source)
627 .done(function (data) {
628 if ('message' in data) {
629 $msg.removeClass('alert-danger')
630 .addClass('alert-info')
631 .text(data.message)
632 .show();
633 } else {
634 $msg.empty()
635 .hide();
636 }
637
638 if ('zuul_version' in data) {
639 $('#zuul-version-span').text(data.zuul_version);
640 }
641 if ('last_reconfigured' in data) {
642 var last_reconfigured =
643 new Date(data.last_reconfigured);
644 $('#last-reconfigured-span').text(
645 last_reconfigured.toString());
646 }
647
648 var $pipelines = $(options.pipelines_id);
649 $pipelines.html('');
650 $.each(data.pipelines, function (i, pipeline) {
651 var count = app.create_tree(pipeline);
652 $pipelines.append(
653 format.pipeline(pipeline, count));
654 });
655
656 $(options.queue_events_num).text(
657 data.trigger_event_queue ?
658 data.trigger_event_queue.length : '0'
659 );
660 $(options.queue_results_num).text(
661 data.result_event_queue ?
662 data.result_event_queue.length : '0'
663 );
664 })
665 .fail(function (err, jqXHR, errMsg) {
666 $msg.text(source + ': ' + errMsg).show();
667 $msg.removeClass('zuul-msg-wrap-off');
668 })
669 .complete(function () {
670 xhr = undefined;
671 app.emit('update-end');
672 });
673
674 return xhr;
675 },
676
677 update_sparklines: function() {
678 $.each(zuul_sparkline_urls, function(name, url) {
679 var newimg = new Image();
680 var parts = url.split('#');
681 newimg.src = parts[0] + '#' + new Date().getTime();
682 $(newimg).load(function (x) {
683 zuul_sparkline_urls[name] = newimg.src;
684 });
685 });
686 },
687
688 emit: function () {
689 $jq.trigger.apply($jq, arguments);
690 return this;
691 },
692 on: function () {
693 $jq.on.apply($jq, arguments);
694 return this;
695 },
696 one: function () {
697 $jq.one.apply($jq, arguments);
698 return this;
699 },
700
701 control_form: function() {
702 // Build the filter form filling anything from cookies
703
704 var $control_form = $('<form />')
705 .attr('role', 'form')
706 .addClass('form-inline')
707 .submit(this.handle_filter_change);
708
709 $control_form
710 .append(this.filter_form_group())
711 .append(this.expand_form_group());
712
713 return $control_form;
714 },
715
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100716 filter_form_group: function() {
Joshua Heskethace48892014-03-22 17:18:31 +1100717 // Update the filter form with a clear button if required
718
719 var $label = $('<label />')
720 .addClass('control-label')
721 .attr('for', 'filter_string')
722 .text('Filters')
723 .css('padding-right', '0.5em');
724
725 var $input = $('<input />')
726 .attr('type', 'text')
727 .attr('id', 'filter_string')
728 .addClass('form-control')
729 .attr('title',
730 'project(s), pipeline(s) or review(s) comma ' +
731 'separated')
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100732 .attr('value', current_filter);
Joshua Heskethace48892014-03-22 17:18:31 +1100733
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000734 $input.change(this.handle_filter_change);
Joshua Heskethace48892014-03-22 17:18:31 +1100735
736 var $clear_icon = $('<span />')
737 .addClass('form-control-feedback')
738 .addClass('glyphicon glyphicon-remove-circle')
739 .attr('id', 'filter_form_clear_box')
740 .attr('title', 'clear filter')
741 .css('cursor', 'pointer');
742
743 $clear_icon.click(function() {
744 $('#filter_string').val('').change();
745 });
746
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100747 if (current_filter === '') {
Joshua Heskethace48892014-03-22 17:18:31 +1100748 $clear_icon.hide();
749 }
750
751 var $form_group = $('<div />')
752 .addClass('form-group has-feedback')
753 .append($label, $input, $clear_icon);
754 return $form_group;
755 },
756
Joshua Heskethae230f62014-03-22 22:14:44 +1100757 expand_form_group: function() {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700758 var expand_by_default = (
Joshua Heskethae230f62014-03-22 22:14:44 +1100759 read_cookie('zuul_expand_by_default', false) === 'true');
760
Monty Taylor860bb0a2014-03-22 09:41:25 -0700761 var $checkbox = $('<input />')
Joshua Heskethae230f62014-03-22 22:14:44 +1100762 .attr('type', 'checkbox')
763 .attr('id', 'expand_by_default')
764 .prop('checked', expand_by_default)
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000765 .change(this.handle_expand_by_default);
Joshua Heskethae230f62014-03-22 22:14:44 +1100766
Monty Taylor860bb0a2014-03-22 09:41:25 -0700767 var $label = $('<label />')
Joshua Heskethae230f62014-03-22 22:14:44 +1100768 .css('padding-left', '1em')
769 .html('Expand by default: ')
770 .append($checkbox);
771
772 var $form_group = $('<div />')
773 .addClass('checkbox')
774 .append($label);
775 return $form_group;
776 },
777
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000778 handle_filter_change: function() {
779 // Update the filter and save it to a cookie
780 current_filter = $('#filter_string').val();
781 set_cookie('zuul_filter_string', current_filter);
782 if (current_filter === '') {
783 $('#filter_form_clear_box').hide();
Joshua Heskethace48892014-03-22 17:18:31 +1100784 }
785 else {
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000786 $('#filter_form_clear_box').show();
Joshua Heskethace48892014-03-22 17:18:31 +1100787 }
Joshua Heskethace48892014-03-22 17:18:31 +1100788
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000789 $('.zuul-change-box').each(function(index, obj) {
790 var $change_box = $(obj);
791 format.display_patchset($change_box, 200);
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100792 });
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000793 return false;
Timo Tijhof51516cd2013-04-09 01:32:29 +0200794 },
Joshua Hesketh0f5c66a2014-04-30 19:23:36 +1000795
796 handle_expand_by_default: function(e) {
797 // Handle toggling expand by default
798 set_cookie('zuul_expand_by_default', e.target.checked);
799 collapsed_exceptions = [];
800 $('.zuul-change-box').each(function(index, obj) {
801 var $change_box = $(obj);
802 format.display_patchset($change_box, 200);
803 });
804 },
805
806 create_tree: function(pipeline) {
807 var count = 0;
808 var pipeline_max_tree_columns = 1;
809 $.each(pipeline.change_queues, function(change_queue_i,
810 change_queue) {
811 var tree = [];
812 var max_tree_columns = 1;
813 var changes = [];
814 var last_tree_length = 0;
815 $.each(change_queue.heads, function(head_i, head) {
816 $.each(head, function(change_i, change) {
817 changes[change.id] = change;
818 change._tree_position = change_i;
819 });
820 });
821 $.each(change_queue.heads, function(head_i, head) {
822 $.each(head, function(change_i, change) {
823 count += 1;
824 var idx = tree.indexOf(change.id);
825 if (idx > -1) {
826 change._tree_index = idx;
827 // remove...
828 tree[idx] = null;
829 while (tree[tree.length - 1] === null) {
830 tree.pop();
831 }
832 } else {
833 change._tree_index = 0;
834 }
835 change._tree_branches = [];
836 change._tree = [];
837 if (typeof(change.items_behind) === 'undefined') {
838 change.items_behind = [];
839 }
840 change.items_behind.sort(function(a, b) {
841 return (changes[b]._tree_position -
842 changes[a]._tree_position);
843 });
844 $.each(change.items_behind, function(i, id) {
845 tree.push(id);
846 if (tree.length>last_tree_length &&
847 last_tree_length > 0) {
848 change._tree_branches.push(
849 tree.length - 1);
850 }
851 });
852 if (tree.length > max_tree_columns) {
853 max_tree_columns = tree.length;
854 }
855 if (tree.length > pipeline_max_tree_columns) {
856 pipeline_max_tree_columns = tree.length;
857 }
858 change._tree = tree.slice(0); // make a copy
859 last_tree_length = tree.length;
860 });
861 });
862 change_queue._tree_columns = max_tree_columns;
863 });
864 pipeline._tree_columns = pipeline_max_tree_columns;
865 return count;
866 },
867 };
868
869 $jq = $(app);
870 return {
871 options: options,
872 format: format,
873 app: app,
874 jq: $jq
875 };
876 }
Timo Tijhof51516cd2013-04-09 01:32:29 +0200877}(jQuery));