blob: a74711e2645121d089474001dc2b64b1b91e5053 [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 Hesketh4863b602014-03-21 14:19:06 +110022 var $container, $msg, $indicator, $queueInfo, $queueEventsNum,
Joshua Heskethcbdcca12014-03-20 16:06:25 +110023 $queueResultsNum, $pipelines, $jq;
Joshua Hesketh298c4912014-03-20 16:06:25 +110024 var xhr, zuul,
Joshua Heskethace48892014-03-22 17:18:31 +110025 current_filter = '',
Timo Tijhof51516cd2013-04-09 01:32:29 +020026 demo = location.search.match(/[?&]demo=([^?&]*)/),
Joshua Hesketh668700b2014-03-21 14:25:53 +110027 source_url = location.search.match(/[?&]source_url=([^?&]*)/),
Timo Tijhof51516cd2013-04-09 01:32:29 +020028 source = demo ?
29 './status-' + (demo[1] || 'basic') + '.json-sample' :
James E. Blair7c7ed7a2013-05-15 13:13:26 -070030 'status.json';
Monty Taylor860bb0a2014-03-22 09:41:25 -070031 source = source_url ? source_url[1] : source;
Timo Tijhof51516cd2013-04-09 01:32:29 +020032
Joshua Heskethace48892014-03-22 17:18:31 +110033 function set_cookie(name, value) {
Monty Taylor860bb0a2014-03-22 09:41:25 -070034 document.cookie = name + '=' + value + '; path=/';
Joshua Heskethace48892014-03-22 17:18:31 +110035 }
36
37 function read_cookie(name, default_value) {
Monty Taylor860bb0a2014-03-22 09:41:25 -070038 var nameEQ = name + '=';
Joshua Heskethace48892014-03-22 17:18:31 +110039 var ca = document.cookie.split(';');
40 for(var i=0;i < ca.length;i++) {
41 var c = ca[i];
Monty Taylor860bb0a2014-03-22 09:41:25 -070042 while (c.charAt(0) === ' ') {
43 c = c.substring(1, c.length);
44 }
45 if (c.indexOf(nameEQ) === 0) {
Joshua Heskethace48892014-03-22 17:18:31 +110046 return c.substring(nameEQ.length, c.length);
47 }
48 }
49 return default_value;
50 }
51
52
Timo Tijhof51516cd2013-04-09 01:32:29 +020053 zuul = {
54 enabled: true,
Joshua Heskethdb8046e2014-03-21 18:42:25 +110055 collapsed_exceptions: [],
Timo Tijhof51516cd2013-04-09 01:32:29 +020056
57 schedule: function () {
58 if (!zuul.enabled) {
59 setTimeout(zuul.schedule, 5000);
60 return;
61 }
62 zuul.update().complete(function () {
63 setTimeout(zuul.schedule, 5000);
64 });
65 },
66
67 /** @return {jQuery.Promise} */
68 update: function () {
69 // Cancel the previous update if it hasn't completed yet.
70 if (xhr) {
71 xhr.abort();
72 }
73
74 zuul.emit('update-start');
75
Joshua Hesketh298c4912014-03-20 16:06:25 +110076 xhr = $.getJSON(source)
77 .done(function (data) {
78 if ('message' in data) {
Joshua Hesketh4863b602014-03-21 14:19:06 +110079 $msg.removeClass('alert-danger').addClass('alert-info');
Joshua Hesketh298c4912014-03-20 16:06:25 +110080 $msg.text(data.message);
Joshua Hesketh4863b602014-03-21 14:19:06 +110081 $msg.show();
Joshua Hesketh298c4912014-03-20 16:06:25 +110082 } else {
83 $msg.empty();
Joshua Hesketh4863b602014-03-21 14:19:06 +110084 $msg.hide();
Joshua Hesketh298c4912014-03-20 16:06:25 +110085 }
Timo Tijhof51516cd2013-04-09 01:32:29 +020086
Joshua Hesketh298c4912014-03-20 16:06:25 +110087 if ('zuul_version' in data) {
Monty Taylor860bb0a2014-03-22 09:41:25 -070088 $('#zuul-version-span').text(data.zuul_version);
Joshua Hesketh298c4912014-03-20 16:06:25 +110089 }
90 if ('last_reconfigured' in data) {
91 var last_reconfigured =
Monty Taylor860bb0a2014-03-22 09:41:25 -070092 new Date(data.last_reconfigured);
Joshua Hesketh298c4912014-03-20 16:06:25 +110093 $('#last-reconfigured-span').text(
94 last_reconfigured.toString());
95 }
96
97 $pipelines.html('');
98 $.each(data.pipelines, function (i, pipeline) {
99 $pipelines.append(zuul.format.pipeline(pipeline));
100 });
101
102 $queueEventsNum.text(
103 data.trigger_event_queue ?
104 data.trigger_event_queue.length : '0'
105 );
106 $queueResultsNum.text(
107 data.result_event_queue ?
108 data.result_event_queue.length : '0'
109 );
110 })
111 .fail(function (err, jqXHR, errMsg) {
112 $msg.text(source + ': ' + errMsg).show();
Monty Taylor860bb0a2014-03-22 09:41:25 -0700113 $msg.removeClass('zuul-msg-wrap-off');
Joshua Hesketh298c4912014-03-20 16:06:25 +1100114 })
115 .complete(function () {
116 xhr = undefined;
117 zuul.emit('update-end');
Timo Tijhof51516cd2013-04-09 01:32:29 +0200118 });
119
Timo Tijhof51516cd2013-04-09 01:32:29 +0200120 return xhr;
121 },
122
123 format: {
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100124 job: function(job) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700125 var $job_line;
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100126 if (job.url !== null) {
127 $job_line = $('<a href="' + job.url + '" />');
128 }
129 else{
130 $job_line = $('<span />');
131 }
132 $job_line.text(job.name)
133 .append(zuul.format.job_status(job));
134
135 if (job.voting === false) {
136 $job_line.append(
137 $(' <small />').text(' (non-voting)')
138 );
139 }
140 return $job_line;
141 },
142
143 job_status: function(job) {
144 var result = job.result ? job.result.toLowerCase() : null;
145 if (result === null) {
146 result = job.url ? 'in progress' : 'queued';
147 }
148
Monty Taylor860bb0a2014-03-22 09:41:25 -0700149 if (result === 'in progress') {
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100150 return zuul.format.job_progress_bar(job.elapsed_time,
151 job.remaining_time);
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100152 }
153 else {
154 return zuul.format.status_label(result);
155 }
156 },
157
158 status_label: function(result) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700159 var $status = $('<span />');
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100160 $status.addClass('zuul-job-result label');
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100161
162 switch (result) {
163 case 'success':
164 $status.addClass('label-success');
165 break;
166 case 'failure':
167 $status.addClass('label-danger');
168 break;
169 case 'unstable':
170 $status.addClass('label-warning');
171 break;
172 case 'in progress':
173 case 'queued':
174 case 'lost':
175 $status.addClass('label-default');
176 break;
177 }
178 $status.text(result);
179 return $status;
180 },
181
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100182 job_progress_bar: function(elapsed_time, remaining_time) {
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100183 var progress_percent = 100 * (elapsed_time / (elapsed_time +
184 remaining_time));
185 var $bar_inner = $('<div />')
186 .addClass('progress-bar')
187 .attr('role', 'progressbar')
188 .attr('aria-valuenow', 'progressbar')
189 .attr('aria-valuemin', progress_percent)
190 .attr('aria-valuemin', '0')
191 .attr('aria-valuemax', '100')
192 .css('width', progress_percent + '%');
193
194 var $bar_outter = $('<div />')
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100195 .addClass('progress zuul-job-result')
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100196 .append($bar_inner);
197
198 return $bar_outter;
199 },
200
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100201 enqueue_time: function(ms) {
202 // Special format case for enqueue time to add style
203 var hours = 60 * 60 * 1000;
204 var now = Date.now();
205 var delta = now - ms;
Monty Taylor860bb0a2014-03-22 09:41:25 -0700206 var status = 'text-success';
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100207 var text = zuul.format.time(delta, true);
208 if (delta > (4 * hours)) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700209 status = 'text-danger';
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100210 } else if (delta > (2 * hours)) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700211 status = 'text-warning';
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100212 }
213 return '<span class="' + status + '">' + text + '</span>';
214 },
215
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100216 time: function(ms, words) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700217 if (typeof(words) === 'undefined') {
218 words = false;
219 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100220 var seconds = (+ms)/1000;
221 var minutes = Math.floor(seconds/60);
222 var hours = Math.floor(minutes/60);
223 seconds = Math.floor(seconds % 60);
224 minutes = Math.floor(minutes % 60);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700225 var r = '';
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100226 if (words) {
227 if (hours) {
228 r += hours;
229 r += ' hr ';
230 }
231 r += minutes + ' min';
232 } else {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700233 if (hours < 10) {
234 r += '0';
235 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100236 r += hours + ':';
Monty Taylor860bb0a2014-03-22 09:41:25 -0700237 if (minutes < 10) {
238 r += '0';
239 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100240 r += minutes + ':';
Monty Taylor860bb0a2014-03-22 09:41:25 -0700241 if (seconds < 10) {
242 r += '0';
243 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100244 r += seconds;
245 }
246 return r;
247 },
248
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100249 change_total_progress_bar: function(change) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700250 var job_percent = Math.floor(100 / change.jobs.length);
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100251 var $bar_outter = $('<div />')
252 .addClass('progress zuul-change-total-result');
253
254 $.each(change.jobs, function (i, job) {
255 var result = job.result ? job.result.toLowerCase() : null;
256 if (result === null) {
257 result = job.url ? 'in progress' : 'queued';
258 }
259
Monty Taylor860bb0a2014-03-22 09:41:25 -0700260 if (result !== 'queued') {
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100261 var $bar_inner = $('<div />')
262 .addClass('progress-bar');
263
264 switch (result) {
265 case 'success':
266 $bar_inner.addClass('progress-bar-success');
267 break;
268 case 'lost':
269 case 'failure':
270 $bar_inner.addClass('progress-bar-danger');
271 break;
272 case 'unstable':
273 $bar_inner.addClass('progress-bar-warning');
274 break;
275 case 'in progress':
276 case 'queued':
277 break;
278 }
279 $bar_inner.attr('title', job.name)
280 .css('width', job_percent + '%');
281 $bar_outter.append($bar_inner);
282 }
283 });
284 return $bar_outter;
285 },
286
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100287 change_header: function(change) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700288 var change_id = change.id || 'NA';
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100289 if (change_id.length === 40) {
290 change_id = change_id.substr(0, 7);
Joshua Hesketh298c4912014-03-20 16:06:25 +1100291 }
Timo Tijhof51516cd2013-04-09 01:32:29 +0200292
Monty Taylor860bb0a2014-03-22 09:41:25 -0700293 var $change_link = $('<small />');
Joshua Hesketh298c4912014-03-20 16:06:25 +1100294 if (change.url !== null) {
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100295 $change_link.append(
Monty Taylor860bb0a2014-03-22 09:41:25 -0700296 $('<a />').attr('href', change.url).text(change.id)
Joshua Hesketh298c4912014-03-20 16:06:25 +1100297 );
Timo Tijhof51516cd2013-04-09 01:32:29 +0200298 }
Joshua Hesketh298c4912014-03-20 16:06:25 +1100299 else {
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100300 $change_link.text(change_id);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200301 }
Timo Tijhof51516cd2013-04-09 01:32:29 +0200302
Monty Taylor860bb0a2014-03-22 09:41:25 -0700303 var $change_progress_row_left = $('<div />')
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100304 .addClass('col-xs-3')
305 .append($change_link);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700306 var $change_progress_row_right = $('<div />')
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100307 .addClass('col-xs-9')
Monty Taylor860bb0a2014-03-22 09:41:25 -0700308 .append(zuul.format.change_total_progress_bar(change));
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100309
Monty Taylor860bb0a2014-03-22 09:41:25 -0700310 var $change_progress_row = $('<div />')
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100311 .addClass('row')
312 .append($change_progress_row_left)
Monty Taylor860bb0a2014-03-22 09:41:25 -0700313 .append($change_progress_row_right);
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100314
Monty Taylor860bb0a2014-03-22 09:41:25 -0700315 var $project_span = $('<span />')
Joshua Heskethace48892014-03-22 17:18:31 +1100316 .addClass('change_project')
317 .text(change.project);
318
Monty Taylor860bb0a2014-03-22 09:41:25 -0700319 var $left = $('<div />')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100320 .addClass('col-xs-8')
Joshua Heskethace48892014-03-22 17:18:31 +1100321 .append($project_span, $('<br />'), $change_progress_row);
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100322
Monty Taylor860bb0a2014-03-22 09:41:25 -0700323 var remaining_time = zuul.format.time(
324 change.remaining_time, true);
325 var enqueue_time = zuul.format.enqueue_time(
326 change.enqueue_time);
327 var $remaining_time = $('<small />').addClass('time')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100328 .attr('title', 'Remaining Time').html(remaining_time);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700329 var $enqueue_time = $('<small />').addClass('time')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100330 .attr('title', 'Elapsed Time').html(enqueue_time);
331
Monty Taylor860bb0a2014-03-22 09:41:25 -0700332 var $right = $('<div />')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100333 .addClass('col-xs-4 text-right')
334 .append($remaining_time, $('<br />'), $enqueue_time);
335
Monty Taylor860bb0a2014-03-22 09:41:25 -0700336 var $header = $('<div />')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100337 .addClass('row')
338 .append($left, $right);
339 return $header;
340 },
341
342 change_list: function(jobs) {
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100343 var $list = $('<ul />')
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100344 .addClass('list-group zuul-patchset-body');
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100345
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100346 $.each(jobs, function (i, job) {
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100347 var $item = $('<li />')
348 .addClass('list-group-item')
349 .addClass('zuul-change-job')
350 .append(zuul.format.job(job));
Joshua Hesketh298c4912014-03-20 16:06:25 +1100351 $list.append($item);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200352 });
353
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100354 return $list;
355 },
356
357 change_panel: function (change) {
358 var $header = $('<div />')
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100359 .addClass('panel-heading zuul-patchset-header')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100360 .append(zuul.format.change_header(change));
361
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100362 var panel_id = change.id ? change.id.replace(',', '_')
363 : change.project.replace('/', '_') +
Monty Taylor860bb0a2014-03-22 09:41:25 -0700364 '-' + change.enqueue_time;
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100365 var $panel = $('<div />')
Monty Taylor860bb0a2014-03-22 09:41:25 -0700366 .attr('id', panel_id)
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100367 .addClass('panel panel-default zuul-change')
368 .append($header)
369 .append(zuul.format.change_list(change.jobs));
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100370
371 $header.click(zuul.toggle_patchset);
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100372 return $panel;
Timo Tijhof51516cd2013-04-09 01:32:29 +0200373 },
374
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100375 change_status_icon: function(change) {
376 var icon_name = 'green.png';
377 var icon_title = 'Succeeding';
378
379 if (change.active !== true) {
380 // Grey icon
381 icon_name = 'grey.png';
382 icon_title = 'Waiting until closer to head of queue to' +
383 ' start jobs';
384 }
385 else if (change.failing_reasons &&
386 change.failing_reasons.length > 0) {
387 var reason = change.failing_reasons.join(', ');
388 icon_title = 'Failing because ' + reason;
389 if (reason.match(/merge conflict/)) {
390 // Black icon
391 icon_name = 'black.png';
392 }
393 else {
394 // Red icon
395 icon_name = 'red.png';
396 }
397 }
398
399 var $icon = $('<img />')
400 .attr('src', 'images/' + icon_name)
401 .attr('title', icon_title)
402 .css('margin-top', '-6px');
403
404 return $icon;
405 },
406
407 change_with_status_tree: function(change, change_queue) {
408 var $change_row = $('<tr />');
409
410 for (var i = 0; i < change_queue._tree_columns; i++) {
411 var $tree_cell = $('<td />')
412 .css('height', '100%')
413 .css('padding', '0 0 10px 0')
414 .css('margin', '0')
415 .css('width', '16px')
416 .css('min-width', '16px')
417 .css('overflow', 'hidden')
418 .css('vertical-align', 'top');
419
420 if (i < change._tree.length && change._tree[i] !== null) {
421 $tree_cell.css('background-image',
422 'url(\'images/line.png\')')
423 .css('background-repeat', 'repeat-y');
424 }
425
426 if (i === change._tree_index) {
427 $tree_cell.append(
428 zuul.format.change_status_icon(change));
429 }
430 if (change._tree_branches.indexOf(i) !== -1) {
431 var $image = $('<img />')
432 .css('vertical-align', 'baseline');
433 if (change._tree_branches.indexOf(i) ===
434 change._tree_branches.length - 1) {
435 // Angle line
436 $image.attr('src', 'images/line-angle.png');
437 }
438 else {
439 // T line
440 $image.attr('src', 'images/line-t.png');
441 }
442 $tree_cell.append($image);
443 }
444 $change_row.append($tree_cell);
445 }
446
447 var change_width = 360 - 16*change_queue._tree_columns;
448 var $change_column = $('<td />')
449 .css('width', change_width + 'px')
450 .addClass('zuul-change-cell')
451 .append(zuul.format.change_panel(change));
452
453 $change_row.append($change_column);
454
455 var $change_table = $('<table />')
456 .addClass('zuul-change-box')
457 .css('-moz-box-sizing', 'content-box')
458 .css('box-sizing', 'content-box')
459 .append($change_row);
460
461 return $change_table;
462 },
463
Timo Tijhof51516cd2013-04-09 01:32:29 +0200464 pipeline: function (pipeline) {
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100465 var count = zuul.create_tree(pipeline);
Joshua Hesketh298c4912014-03-20 16:06:25 +1100466 var $html = $('<div />')
Joshua Hesketh4863b602014-03-21 14:19:06 +1100467 .addClass('zuul-pipeline col-md-4')
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100468 .append(
469 $('<h3 />')
470 .css('vertical-align', 'middle')
471 .text(pipeline.name)
472 .append(
473 $('<span />')
474 .addClass('badge pull-right')
475 .css('vertical-align', 'middle')
476 .css('margin-top', '0.5em')
477 .text(count)
478 )
479 );
Joshua Hesketh298c4912014-03-20 16:06:25 +1100480
Timo Tijhof51516cd2013-04-09 01:32:29 +0200481 if (typeof pipeline.description === 'string') {
Joshua Hesketh298c4912014-03-20 16:06:25 +1100482 $html.append(
483 $('<p />').append(
484 $('<small />').text(pipeline.description)
485 )
486 );
Timo Tijhof51516cd2013-04-09 01:32:29 +0200487 }
488
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100489 $.each(pipeline.change_queues,
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100490 function (queue_i, change_queue) {
491 $.each(change_queue.heads, function (head_i, changes) {
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100492 if (pipeline.change_queues.length > 1 &&
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100493 head_i === 0) {
494 var name = change_queue.name;
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100495 var short_name = name;
496 if (short_name.length > 32) {
497 short_name = short_name.substr(0, 32) + '...';
Timo Tijhof51516cd2013-04-09 01:32:29 +0200498 }
Joshua Hesketh298c4912014-03-20 16:06:25 +1100499 $html.append(
500 $('<p />')
501 .text('Queue: ')
502 .append(
503 $('<abbr />')
504 .attr('title', name)
505 .text(short_name)
506 )
507 );
Timo Tijhof51516cd2013-04-09 01:32:29 +0200508 }
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100509
510 $.each(changes, function (change_i, change) {
511 var $change_box =
512 zuul.format.change_with_status_tree(
513 change, change_queue);
514 $html.append($change_box);
515 zuul.display_patchset($change_box);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200516 });
517 });
518 });
Joshua Hesketh298c4912014-03-20 16:06:25 +1100519 return $html;
Joshua Heskethace48892014-03-22 17:18:31 +1100520 },
521
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100522 filter_form_group: function() {
Joshua Heskethace48892014-03-22 17:18:31 +1100523 // Update the filter form with a clear button if required
524
525 var $label = $('<label />')
526 .addClass('control-label')
527 .attr('for', 'filter_string')
528 .text('Filters')
529 .css('padding-right', '0.5em');
530
531 var $input = $('<input />')
532 .attr('type', 'text')
533 .attr('id', 'filter_string')
534 .addClass('form-control')
535 .attr('title',
536 'project(s), pipeline(s) or review(s) comma ' +
537 'separated')
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100538 .attr('value', current_filter);
Joshua Heskethace48892014-03-22 17:18:31 +1100539
540 $input.change(zuul.handle_filter_change);
541
542 var $clear_icon = $('<span />')
543 .addClass('form-control-feedback')
544 .addClass('glyphicon glyphicon-remove-circle')
545 .attr('id', 'filter_form_clear_box')
546 .attr('title', 'clear filter')
547 .css('cursor', 'pointer');
548
549 $clear_icon.click(function() {
550 $('#filter_string').val('').change();
551 });
552
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100553 if (current_filter === '') {
Joshua Heskethace48892014-03-22 17:18:31 +1100554 $clear_icon.hide();
555 }
556
557 var $form_group = $('<div />')
558 .addClass('form-group has-feedback')
559 .append($label, $input, $clear_icon);
560 return $form_group;
561 },
562
Joshua Heskethae230f62014-03-22 22:14:44 +1100563 expand_form_group: function() {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700564 var expand_by_default = (
Joshua Heskethae230f62014-03-22 22:14:44 +1100565 read_cookie('zuul_expand_by_default', false) === 'true');
566
Monty Taylor860bb0a2014-03-22 09:41:25 -0700567 var $checkbox = $('<input />')
Joshua Heskethae230f62014-03-22 22:14:44 +1100568 .attr('type', 'checkbox')
569 .attr('id', 'expand_by_default')
570 .prop('checked', expand_by_default)
571 .change(zuul.handle_expand_by_default);
572
Monty Taylor860bb0a2014-03-22 09:41:25 -0700573 var $label = $('<label />')
Joshua Heskethae230f62014-03-22 22:14:44 +1100574 .css('padding-left', '1em')
575 .html('Expand by default: ')
576 .append($checkbox);
577
578 var $form_group = $('<div />')
579 .addClass('checkbox')
580 .append($label);
581 return $form_group;
582 },
583
584 control_form: function() {
Joshua Heskethace48892014-03-22 17:18:31 +1100585 // Build the filter form filling anything from cookies
586
Monty Taylor860bb0a2014-03-22 09:41:25 -0700587 var $control_form = $('<form />')
Joshua Heskethace48892014-03-22 17:18:31 +1100588 .attr('role', 'form')
589 .addClass('form-inline')
590 .submit(zuul.handle_filter_change);
591
Joshua Heskethae230f62014-03-22 22:14:44 +1100592 $control_form
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100593 .append(zuul.format.filter_form_group())
Joshua Heskethae230f62014-03-22 22:14:44 +1100594 .append(zuul.format.expand_form_group());
595
596 return $control_form;
Joshua Heskethace48892014-03-22 17:18:31 +1100597 },
Timo Tijhof51516cd2013-04-09 01:32:29 +0200598 },
599
600 emit: function () {
601 $jq.trigger.apply($jq, arguments);
602 return this;
603 },
604 on: function () {
605 $jq.on.apply($jq, arguments);
606 return this;
607 },
608 one: function () {
609 $jq.one.apply($jq, arguments);
610 return this;
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100611 },
612
613 toggle_patchset: function(e) {
614 // Toggle showing/hiding the patchset when the header is clicked
615 // Grab the patchset panel
616 var $panel = $(e.target).parents('.zuul-change');
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100617 var $body = $panel.children('.zuul-patchset-body');
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100618 $body.toggle(200);
619 var collapsed_index = zuul.collapsed_exceptions.indexOf(
620 $panel.attr('id'));
Monty Taylor860bb0a2014-03-22 09:41:25 -0700621 if (collapsed_index === -1 ) {
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100622 // Currently not an exception, add it to list
623 zuul.collapsed_exceptions.push($panel.attr('id'));
624 }
625 else {
626 // Currently an except, remove from exceptions
627 zuul.collapsed_exceptions.splice(collapsed_index, 1);
628 }
629 },
630
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100631 display_patchset: function($change_box, animate) {
Joshua Heskethace48892014-03-22 17:18:31 +1100632 // Determine if to show or hide the patchset and/or the results
633 // when loaded
634
635 // See if we should hide the body/results
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100636 var $panel = $change_box.find('.zuul-change');
637 var panel_change = $panel.attr('id');
638 var $body = $panel.children('.zuul-patchset-body');
Joshua Heskethae230f62014-03-22 22:14:44 +1100639 var expand_by_default = $('#expand_by_default').prop('checked');
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100640
641 var collapsed_index = zuul.collapsed_exceptions.indexOf(panel_change);
642
Monty Taylor860bb0a2014-03-22 09:41:25 -0700643 if (expand_by_default && collapsed_index === -1 ||
644 !expand_by_default && collapsed_index !== -1) {
Joshua Heskethae230f62014-03-22 22:14:44 +1100645 // Expand by default, or is an exception
646 $body.show(animate);
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100647 }
648 else {
Joshua Heskethae230f62014-03-22 22:14:44 +1100649 $body.hide(animate);
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100650 }
Joshua Heskethace48892014-03-22 17:18:31 +1100651
652 // Check if we should hide the whole panel
653 var panel_project = $panel.find('.change_project').text()
654 .toLowerCase();
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100655
656
657 var panel_pipeline = $change_box.parents('.zuul-pipeline')
658 .children('h3').html().toLowerCase();
659
Monty Taylor860bb0a2014-03-22 09:41:25 -0700660 if (current_filter !== '') {
661 var show_panel = false;
662 var filter = current_filter.trim().split(/[\s,]+/);
Joshua Heskethace48892014-03-22 17:18:31 +1100663 $.each(filter, function(index, f_val) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700664 if (f_val !== '') {
Joshua Heskethace48892014-03-22 17:18:31 +1100665 f_val = f_val.toLowerCase();
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100666 if (panel_project.indexOf(f_val) !== -1 ||
667 panel_pipeline.indexOf(f_val) !== -1 ||
668 panel_change.indexOf(f_val) !== -1) {
Joshua Heskethace48892014-03-22 17:18:31 +1100669 show_panel = true;
670 }
671 }
672 });
Monty Taylor860bb0a2014-03-22 09:41:25 -0700673 if (show_panel === true) {
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100674 $change_box.show(animate);
Joshua Heskethace48892014-03-22 17:18:31 +1100675 }
676 else {
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100677 $change_box.hide(animate);
Joshua Heskethace48892014-03-22 17:18:31 +1100678 }
679 }
680 else {
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100681 $change_box.show(animate);
Joshua Heskethace48892014-03-22 17:18:31 +1100682 }
683 },
684
Monty Taylor860bb0a2014-03-22 09:41:25 -0700685 handle_filter_change: function() {
Joshua Heskethace48892014-03-22 17:18:31 +1100686 // Update the filter and save it to a cookie
687 current_filter = $('#filter_string').val();
688 set_cookie('zuul_filter_string', current_filter);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700689 if (current_filter === '') {
Joshua Heskethace48892014-03-22 17:18:31 +1100690 $('#filter_form_clear_box').hide();
691 }
692 else {
693 $('#filter_form_clear_box').show();
694 }
695
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100696 $('.zuul-change-box').each(function(index, obj) {
697 var $change_box = $(obj);
698 zuul.display_patchset($change_box, 200);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700699 });
Joshua Heskethace48892014-03-22 17:18:31 +1100700 return false;
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100701 },
Joshua Heskethae230f62014-03-22 22:14:44 +1100702
703 handle_expand_by_default: function(e) {
704 // Handle toggling expand by default
705 set_cookie('zuul_expand_by_default', e.target.checked);
706 zuul.collapsed_exceptions = [];
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100707 $('.zuul-change-box').each(function(index, obj) {
708 var $change_box = $(obj);
709 zuul.display_patchset($change_box, 200);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700710 });
Joshua Heskethae230f62014-03-22 22:14:44 +1100711 },
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100712
713 create_tree: function(pipeline) {
714 var count = 0;
715 var pipeline_max_tree_columns = 1;
716 $.each(pipeline.change_queues, function(change_queue_i,
717 change_queue) {
718 var tree = [];
719 var max_tree_columns = 1;
720 var changes = [];
721 var last_tree_length = 0;
722 $.each(change_queue.heads, function(head_i, head) {
723 $.each(head, function(change_i, change) {
724 changes[change.id] = change;
725 change._tree_position = change_i;
726 });
727 });
728 $.each(change_queue.heads, function(head_i, head) {
729 $.each(head, function(change_i, change) {
730 count += 1;
731 var idx = tree.indexOf(change.id);
732 if (idx > -1) {
733 change._tree_index = idx;
734 // remove...
735 tree[idx] = null;
736 while (tree[tree.length - 1] === null) {
737 tree.pop();
738 }
739 } else {
740 change._tree_index = 0;
741 }
742 change._tree_branches = [];
743 change._tree = [];
744 if (typeof(change.items_behind) === 'undefined') {
745 change.items_behind = [];
746 }
747 change.items_behind.sort(function(a, b) {
748 return (changes[b]._tree_position -
749 changes[a]._tree_position);
750 });
751 $.each(change.items_behind, function(i, id) {
752 tree.push(id);
753 if (tree.length>last_tree_length &&
754 last_tree_length > 0) {
755 change._tree_branches.push(
756 tree.length - 1);
757 }
758 });
759 if (tree.length > max_tree_columns) {
760 max_tree_columns = tree.length;
761 }
762 if (tree.length > pipeline_max_tree_columns) {
763 pipeline_max_tree_columns = tree.length;
764 }
765 change._tree = tree.slice(0); // make a copy
766 last_tree_length = tree.length;
767 });
768 });
769 change_queue._tree_columns = max_tree_columns;
770 });
771 pipeline._tree_columns = pipeline_max_tree_columns;
772 return count;
773 },
Timo Tijhof51516cd2013-04-09 01:32:29 +0200774 };
775
Joshua Heskethace48892014-03-22 17:18:31 +1100776 current_filter = read_cookie('zuul_filter_string', current_filter);
777
Timo Tijhof51516cd2013-04-09 01:32:29 +0200778 $jq = $(zuul);
779
780 $jq.on('update-start', function () {
781 $container.addClass('zuul-container-loading');
782 $indicator.addClass('zuul-spinner-on');
783 });
784
785 $jq.on('update-end', function () {
786 $container.removeClass('zuul-container-loading');
787 setTimeout(function () {
788 $indicator.removeClass('zuul-spinner-on');
Joshua Hesketh876ca512014-03-21 18:44:22 +1100789 }, 500);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200790 });
791
792 $jq.one('update-end', function () {
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100793 // Do this asynchronous so that if the first update adds a message, it
794 // will not animate while we fade in the content. Instead it simply
795 // appears with the rest of the content.
Timo Tijhof51516cd2013-04-09 01:32:29 +0200796 setTimeout(function () {
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100797 // Fade in the content
798 $container.addClass('zuul-container-ready');
Timo Tijhof51516cd2013-04-09 01:32:29 +0200799 });
800 });
801
802 $(function ($) {
Joshua Hesketh4863b602014-03-21 14:19:06 +1100803 $msg = $('<div />').addClass('alert').hide();
Monty Taylor860bb0a2014-03-22 09:41:25 -0700804 $indicator = $('<button class="btn pull-right zuul-spinner">' +
805 'updating ' +
806 '<span class="glyphicon glyphicon-refresh"></span>' +
807 '</button>');
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100808 $queueInfo = $('<p>Queue lengths: <span>0</span> events, ' +
809 '<span>0</span> results.</p>');
Joshua Hesketh298c4912014-03-20 16:06:25 +1100810 $queueEventsNum = $queueInfo.find('span').eq(0);
811 $queueResultsNum = $queueEventsNum.next();
Joshua Heskethace48892014-03-22 17:18:31 +1100812
Monty Taylor860bb0a2014-03-22 09:41:25 -0700813 var $control_form = zuul.format.control_form();
Joshua Heskethace48892014-03-22 17:18:31 +1100814
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100815 $pipelines = $('<div class="row"></div>');
Monty Taylor860bb0a2014-03-22 09:41:25 -0700816 var $zuulVersion = $('<p>Zuul version: <span id="zuul-version-span">' +
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100817 '</span></p>');
Monty Taylor860bb0a2014-03-22 09:41:25 -0700818 var $lastReconf = $('<p>Last reconfigured: ' +
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100819 '<span id="last-reconfigured-span"></span></p>');
Timo Tijhof51516cd2013-04-09 01:32:29 +0200820
Joshua Hesketh4863b602014-03-21 14:19:06 +1100821 $container = $('#zuul-container').append($msg, $indicator,
Joshua Heskethae230f62014-03-22 22:14:44 +1100822 $queueInfo, $control_form,
Joshua Heskethace48892014-03-22 17:18:31 +1100823 $pipelines, $zuulVersion,
824 $lastReconf);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200825
826 zuul.schedule();
827
828 $(document).on({
829 'show.visibility': function () {
830 zuul.enabled = true;
831 zuul.update();
832 },
833 'hide.visibility': function () {
834 zuul.enabled = false;
835 }
836 });
837 });
838}(jQuery));