blob: 8616201ca29f91ddcee61009bf48b1ebab7c0fe3 [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 Hesketh9d013542014-04-03 13:08:04 +110025 zuul_graph_update_count = 0,
26 zuul_sparkline_urls = {},
Joshua Heskethace48892014-03-22 17:18:31 +110027 current_filter = '',
Timo Tijhof51516cd2013-04-09 01:32:29 +020028 demo = location.search.match(/[?&]demo=([^?&]*)/),
Joshua Hesketh668700b2014-03-21 14:25:53 +110029 source_url = location.search.match(/[?&]source_url=([^?&]*)/),
Timo Tijhof51516cd2013-04-09 01:32:29 +020030 source = demo ?
31 './status-' + (demo[1] || 'basic') + '.json-sample' :
James E. Blair7c7ed7a2013-05-15 13:13:26 -070032 'status.json';
Monty Taylor860bb0a2014-03-22 09:41:25 -070033 source = source_url ? source_url[1] : source;
Timo Tijhof51516cd2013-04-09 01:32:29 +020034
Joshua Heskethace48892014-03-22 17:18:31 +110035 function set_cookie(name, value) {
Monty Taylor860bb0a2014-03-22 09:41:25 -070036 document.cookie = name + '=' + value + '; path=/';
Joshua Heskethace48892014-03-22 17:18:31 +110037 }
38
39 function read_cookie(name, default_value) {
Monty Taylor860bb0a2014-03-22 09:41:25 -070040 var nameEQ = name + '=';
Joshua Heskethace48892014-03-22 17:18:31 +110041 var ca = document.cookie.split(';');
42 for(var i=0;i < ca.length;i++) {
43 var c = ca[i];
Monty Taylor860bb0a2014-03-22 09:41:25 -070044 while (c.charAt(0) === ' ') {
45 c = c.substring(1, c.length);
46 }
47 if (c.indexOf(nameEQ) === 0) {
Joshua Heskethace48892014-03-22 17:18:31 +110048 return c.substring(nameEQ.length, c.length);
49 }
50 }
51 return default_value;
52 }
53
54
Timo Tijhof51516cd2013-04-09 01:32:29 +020055 zuul = {
56 enabled: true,
Joshua Hesketh9d013542014-04-03 13:08:04 +110057 graphite_url: '',
Joshua Heskethdb8046e2014-03-21 18:42:25 +110058 collapsed_exceptions: [],
Timo Tijhof51516cd2013-04-09 01:32:29 +020059
60 schedule: function () {
61 if (!zuul.enabled) {
62 setTimeout(zuul.schedule, 5000);
63 return;
64 }
65 zuul.update().complete(function () {
66 setTimeout(zuul.schedule, 5000);
67 });
Joshua Hesketh9d013542014-04-03 13:08:04 +110068
69 /* Only update graphs every minute */
70 if (zuul_graph_update_count > 11) {
71 zuul_graph_update_count = 0;
72 zuul.update_sparklines();
73 }
Timo Tijhof51516cd2013-04-09 01:32:29 +020074 },
75
76 /** @return {jQuery.Promise} */
77 update: function () {
78 // Cancel the previous update if it hasn't completed yet.
79 if (xhr) {
80 xhr.abort();
81 }
82
83 zuul.emit('update-start');
84
Joshua Hesketh298c4912014-03-20 16:06:25 +110085 xhr = $.getJSON(source)
86 .done(function (data) {
87 if ('message' in data) {
Joshua Hesketh4863b602014-03-21 14:19:06 +110088 $msg.removeClass('alert-danger').addClass('alert-info');
Joshua Hesketh298c4912014-03-20 16:06:25 +110089 $msg.text(data.message);
Joshua Hesketh4863b602014-03-21 14:19:06 +110090 $msg.show();
Joshua Hesketh298c4912014-03-20 16:06:25 +110091 } else {
92 $msg.empty();
Joshua Hesketh4863b602014-03-21 14:19:06 +110093 $msg.hide();
Joshua Hesketh298c4912014-03-20 16:06:25 +110094 }
Timo Tijhof51516cd2013-04-09 01:32:29 +020095
Joshua Hesketh298c4912014-03-20 16:06:25 +110096 if ('zuul_version' in data) {
Monty Taylor860bb0a2014-03-22 09:41:25 -070097 $('#zuul-version-span').text(data.zuul_version);
Joshua Hesketh298c4912014-03-20 16:06:25 +110098 }
99 if ('last_reconfigured' in data) {
100 var last_reconfigured =
Monty Taylor860bb0a2014-03-22 09:41:25 -0700101 new Date(data.last_reconfigured);
Joshua Hesketh298c4912014-03-20 16:06:25 +1100102 $('#last-reconfigured-span').text(
103 last_reconfigured.toString());
104 }
105
106 $pipelines.html('');
107 $.each(data.pipelines, function (i, pipeline) {
108 $pipelines.append(zuul.format.pipeline(pipeline));
109 });
110
111 $queueEventsNum.text(
112 data.trigger_event_queue ?
113 data.trigger_event_queue.length : '0'
114 );
115 $queueResultsNum.text(
116 data.result_event_queue ?
117 data.result_event_queue.length : '0'
118 );
119 })
120 .fail(function (err, jqXHR, errMsg) {
121 $msg.text(source + ': ' + errMsg).show();
Monty Taylor860bb0a2014-03-22 09:41:25 -0700122 $msg.removeClass('zuul-msg-wrap-off');
Joshua Hesketh298c4912014-03-20 16:06:25 +1100123 })
124 .complete(function () {
125 xhr = undefined;
126 zuul.emit('update-end');
Timo Tijhof51516cd2013-04-09 01:32:29 +0200127 });
128
Timo Tijhof51516cd2013-04-09 01:32:29 +0200129 return xhr;
130 },
131
Joshua Hesketh9d013542014-04-03 13:08:04 +1100132 update_sparklines: function() {
133 $.each(zuul_sparkline_urls, function(name, url) {
134 var newimg = new Image();
135 var parts = url.split('#');
136 newimg.src = parts[0] + '#' + new Date().getTime();
137 $(newimg).load(function (x) {
138 zuul_sparkline_urls[name] = newimg.src;
139 });
140 });
141 },
142
Timo Tijhof51516cd2013-04-09 01:32:29 +0200143 format: {
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100144 job: function(job) {
Joshua Hesketh9e6ce532014-04-01 13:11:53 +1100145 var $job_line = $('<span />');
146
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100147 if (job.url !== null) {
Joshua Hesketh9e6ce532014-04-01 13:11:53 +1100148 $job_line.append(
149 $('<a />')
150 .addClass('zuul-job-name')
151 .attr('href', job.url)
152 .text(job.name)
153 );
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100154 }
Joshua Hesketh9e6ce532014-04-01 13:11:53 +1100155 else {
156 $job_line.append(
157 $('<span />')
158 .addClass('zuul-job-name')
159 .text(job.name)
160 );
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100161 }
Joshua Hesketh9e6ce532014-04-01 13:11:53 +1100162
163 $job_line.append(zuul.format.job_status(job));
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100164
165 if (job.voting === false) {
166 $job_line.append(
Joshua Hesketh9e6ce532014-04-01 13:11:53 +1100167 $(' <small />')
168 .addClass('zuul-non-voting-desc')
169 .text(' (non-voting)')
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100170 );
171 }
Joshua Hesketh9e6ce532014-04-01 13:11:53 +1100172
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100173 return $job_line;
174 },
175
176 job_status: function(job) {
177 var result = job.result ? job.result.toLowerCase() : null;
178 if (result === null) {
179 result = job.url ? 'in progress' : 'queued';
180 }
181
Monty Taylor860bb0a2014-03-22 09:41:25 -0700182 if (result === 'in progress') {
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100183 return zuul.format.job_progress_bar(job.elapsed_time,
184 job.remaining_time);
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100185 }
186 else {
187 return zuul.format.status_label(result);
188 }
189 },
190
191 status_label: function(result) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700192 var $status = $('<span />');
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100193 $status.addClass('zuul-job-result label');
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100194
195 switch (result) {
196 case 'success':
197 $status.addClass('label-success');
198 break;
199 case 'failure':
200 $status.addClass('label-danger');
201 break;
202 case 'unstable':
203 $status.addClass('label-warning');
204 break;
205 case 'in progress':
206 case 'queued':
207 case 'lost':
208 $status.addClass('label-default');
209 break;
210 }
211 $status.text(result);
212 return $status;
213 },
214
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100215 job_progress_bar: function(elapsed_time, remaining_time) {
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100216 var progress_percent = 100 * (elapsed_time / (elapsed_time +
217 remaining_time));
218 var $bar_inner = $('<div />')
219 .addClass('progress-bar')
220 .attr('role', 'progressbar')
221 .attr('aria-valuenow', 'progressbar')
222 .attr('aria-valuemin', progress_percent)
223 .attr('aria-valuemin', '0')
224 .attr('aria-valuemax', '100')
225 .css('width', progress_percent + '%');
226
227 var $bar_outter = $('<div />')
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100228 .addClass('progress zuul-job-result')
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100229 .append($bar_inner);
230
231 return $bar_outter;
232 },
233
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100234 enqueue_time: function(ms) {
235 // Special format case for enqueue time to add style
236 var hours = 60 * 60 * 1000;
237 var now = Date.now();
238 var delta = now - ms;
Monty Taylor860bb0a2014-03-22 09:41:25 -0700239 var status = 'text-success';
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100240 var text = zuul.format.time(delta, true);
241 if (delta > (4 * hours)) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700242 status = 'text-danger';
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100243 } else if (delta > (2 * hours)) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700244 status = 'text-warning';
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100245 }
246 return '<span class="' + status + '">' + text + '</span>';
247 },
248
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100249 time: function(ms, words) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700250 if (typeof(words) === 'undefined') {
251 words = false;
252 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100253 var seconds = (+ms)/1000;
254 var minutes = Math.floor(seconds/60);
255 var hours = Math.floor(minutes/60);
256 seconds = Math.floor(seconds % 60);
257 minutes = Math.floor(minutes % 60);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700258 var r = '';
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100259 if (words) {
260 if (hours) {
261 r += hours;
262 r += ' hr ';
263 }
264 r += minutes + ' min';
265 } else {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700266 if (hours < 10) {
267 r += '0';
268 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100269 r += hours + ':';
Monty Taylor860bb0a2014-03-22 09:41:25 -0700270 if (minutes < 10) {
271 r += '0';
272 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100273 r += minutes + ':';
Monty Taylor860bb0a2014-03-22 09:41:25 -0700274 if (seconds < 10) {
275 r += '0';
276 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100277 r += seconds;
278 }
279 return r;
280 },
281
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100282 change_total_progress_bar: function(change) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700283 var job_percent = Math.floor(100 / change.jobs.length);
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100284 var $bar_outter = $('<div />')
285 .addClass('progress zuul-change-total-result');
286
287 $.each(change.jobs, function (i, job) {
288 var result = job.result ? job.result.toLowerCase() : null;
289 if (result === null) {
290 result = job.url ? 'in progress' : 'queued';
291 }
292
Monty Taylor860bb0a2014-03-22 09:41:25 -0700293 if (result !== 'queued') {
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100294 var $bar_inner = $('<div />')
295 .addClass('progress-bar');
296
297 switch (result) {
298 case 'success':
299 $bar_inner.addClass('progress-bar-success');
300 break;
301 case 'lost':
302 case 'failure':
303 $bar_inner.addClass('progress-bar-danger');
304 break;
305 case 'unstable':
306 $bar_inner.addClass('progress-bar-warning');
307 break;
308 case 'in progress':
309 case 'queued':
310 break;
311 }
312 $bar_inner.attr('title', job.name)
313 .css('width', job_percent + '%');
314 $bar_outter.append($bar_inner);
315 }
316 });
317 return $bar_outter;
318 },
319
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100320 change_header: function(change) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700321 var change_id = change.id || 'NA';
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100322 if (change_id.length === 40) {
323 change_id = change_id.substr(0, 7);
Joshua Hesketh298c4912014-03-20 16:06:25 +1100324 }
Timo Tijhof51516cd2013-04-09 01:32:29 +0200325
Monty Taylor860bb0a2014-03-22 09:41:25 -0700326 var $change_link = $('<small />');
Joshua Hesketh298c4912014-03-20 16:06:25 +1100327 if (change.url !== null) {
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100328 $change_link.append(
Monty Taylor860bb0a2014-03-22 09:41:25 -0700329 $('<a />').attr('href', change.url).text(change.id)
Joshua Hesketh298c4912014-03-20 16:06:25 +1100330 );
Timo Tijhof51516cd2013-04-09 01:32:29 +0200331 }
Joshua Hesketh298c4912014-03-20 16:06:25 +1100332 else {
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100333 $change_link.text(change_id);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200334 }
Timo Tijhof51516cd2013-04-09 01:32:29 +0200335
Monty Taylor860bb0a2014-03-22 09:41:25 -0700336 var $change_progress_row_left = $('<div />')
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100337 .addClass('col-xs-3')
338 .append($change_link);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700339 var $change_progress_row_right = $('<div />')
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100340 .addClass('col-xs-9')
Monty Taylor860bb0a2014-03-22 09:41:25 -0700341 .append(zuul.format.change_total_progress_bar(change));
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100342
Monty Taylor860bb0a2014-03-22 09:41:25 -0700343 var $change_progress_row = $('<div />')
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100344 .addClass('row')
345 .append($change_progress_row_left)
Monty Taylor860bb0a2014-03-22 09:41:25 -0700346 .append($change_progress_row_right);
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100347
Monty Taylor860bb0a2014-03-22 09:41:25 -0700348 var $project_span = $('<span />')
Joshua Heskethace48892014-03-22 17:18:31 +1100349 .addClass('change_project')
350 .text(change.project);
351
Monty Taylor860bb0a2014-03-22 09:41:25 -0700352 var $left = $('<div />')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100353 .addClass('col-xs-8')
Joshua Hesketh1751e4f2014-04-01 13:24:51 +1100354 .append($project_span, $change_progress_row);
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100355
Monty Taylor860bb0a2014-03-22 09:41:25 -0700356 var remaining_time = zuul.format.time(
357 change.remaining_time, true);
358 var enqueue_time = zuul.format.enqueue_time(
359 change.enqueue_time);
360 var $remaining_time = $('<small />').addClass('time')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100361 .attr('title', 'Remaining Time').html(remaining_time);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700362 var $enqueue_time = $('<small />').addClass('time')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100363 .attr('title', 'Elapsed Time').html(enqueue_time);
364
Monty Taylor860bb0a2014-03-22 09:41:25 -0700365 var $right = $('<div />')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100366 .addClass('col-xs-4 text-right')
367 .append($remaining_time, $('<br />'), $enqueue_time);
368
Monty Taylor860bb0a2014-03-22 09:41:25 -0700369 var $header = $('<div />')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100370 .addClass('row')
371 .append($left, $right);
372 return $header;
373 },
374
375 change_list: function(jobs) {
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100376 var $list = $('<ul />')
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100377 .addClass('list-group zuul-patchset-body');
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100378
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100379 $.each(jobs, function (i, job) {
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100380 var $item = $('<li />')
381 .addClass('list-group-item')
382 .addClass('zuul-change-job')
383 .append(zuul.format.job(job));
Joshua Hesketh298c4912014-03-20 16:06:25 +1100384 $list.append($item);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200385 });
386
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100387 return $list;
388 },
389
390 change_panel: function (change) {
391 var $header = $('<div />')
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100392 .addClass('panel-heading zuul-patchset-header')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100393 .append(zuul.format.change_header(change));
394
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100395 var panel_id = change.id ? change.id.replace(',', '_')
396 : change.project.replace('/', '_') +
Monty Taylor860bb0a2014-03-22 09:41:25 -0700397 '-' + change.enqueue_time;
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100398 var $panel = $('<div />')
Monty Taylor860bb0a2014-03-22 09:41:25 -0700399 .attr('id', panel_id)
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100400 .addClass('panel panel-default zuul-change')
401 .append($header)
402 .append(zuul.format.change_list(change.jobs));
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100403
404 $header.click(zuul.toggle_patchset);
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100405 return $panel;
Timo Tijhof51516cd2013-04-09 01:32:29 +0200406 },
407
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100408 change_status_icon: function(change) {
409 var icon_name = 'green.png';
410 var icon_title = 'Succeeding';
411
412 if (change.active !== true) {
413 // Grey icon
414 icon_name = 'grey.png';
415 icon_title = 'Waiting until closer to head of queue to' +
416 ' start jobs';
417 }
418 else if (change.failing_reasons &&
419 change.failing_reasons.length > 0) {
420 var reason = change.failing_reasons.join(', ');
421 icon_title = 'Failing because ' + reason;
422 if (reason.match(/merge conflict/)) {
423 // Black icon
424 icon_name = 'black.png';
425 }
426 else {
427 // Red icon
428 icon_name = 'red.png';
429 }
430 }
431
432 var $icon = $('<img />')
433 .attr('src', 'images/' + icon_name)
434 .attr('title', icon_title)
435 .css('margin-top', '-6px');
436
437 return $icon;
438 },
439
440 change_with_status_tree: function(change, change_queue) {
441 var $change_row = $('<tr />');
442
443 for (var i = 0; i < change_queue._tree_columns; i++) {
444 var $tree_cell = $('<td />')
445 .css('height', '100%')
446 .css('padding', '0 0 10px 0')
447 .css('margin', '0')
448 .css('width', '16px')
449 .css('min-width', '16px')
450 .css('overflow', 'hidden')
451 .css('vertical-align', 'top');
452
453 if (i < change._tree.length && change._tree[i] !== null) {
454 $tree_cell.css('background-image',
455 'url(\'images/line.png\')')
456 .css('background-repeat', 'repeat-y');
457 }
458
459 if (i === change._tree_index) {
460 $tree_cell.append(
461 zuul.format.change_status_icon(change));
462 }
463 if (change._tree_branches.indexOf(i) !== -1) {
464 var $image = $('<img />')
465 .css('vertical-align', 'baseline');
466 if (change._tree_branches.indexOf(i) ===
467 change._tree_branches.length - 1) {
468 // Angle line
469 $image.attr('src', 'images/line-angle.png');
470 }
471 else {
472 // T line
473 $image.attr('src', 'images/line-t.png');
474 }
475 $tree_cell.append($image);
476 }
477 $change_row.append($tree_cell);
478 }
479
480 var change_width = 360 - 16*change_queue._tree_columns;
481 var $change_column = $('<td />')
482 .css('width', change_width + 'px')
483 .addClass('zuul-change-cell')
484 .append(zuul.format.change_panel(change));
485
486 $change_row.append($change_column);
487
488 var $change_table = $('<table />')
489 .addClass('zuul-change-box')
490 .css('-moz-box-sizing', 'content-box')
491 .css('box-sizing', 'content-box')
492 .append($change_row);
493
494 return $change_table;
495 },
496
Joshua Hesketh9d013542014-04-03 13:08:04 +1100497 pipeline_sparkline: function(pipeline_name) {
498 if (zuul.graphite_url !== '') {
499 var $sparkline = $('<img />')
500 .addClass('pull-right')
501 .attr('src', zuul.get_sparkline_url(pipeline_name));
502 return $sparkline;
503 }
504 return false;
505 },
506
507 pipeline_header: function(pipeline, count) {
508 // Format the pipeline name, sparkline and description
509 var $header_div = $('<div />')
510 .addClass('zuul-pipeline-header');
511
512 var $heading = $('<h3 />')
513 .css('vertical-align', 'middle')
514 .text(pipeline.name)
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100515 .append(
Joshua Hesketh9d013542014-04-03 13:08:04 +1100516 $('<span />')
517 .addClass('badge pull-right')
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100518 .css('vertical-align', 'middle')
Joshua Hesketh9d013542014-04-03 13:08:04 +1100519 .css('margin-top', '0.5em')
520 .text(count)
521 )
522 .append(zuul.format.pipeline_sparkline(pipeline.name));
523
524 $header_div.append($heading);
Joshua Hesketh298c4912014-03-20 16:06:25 +1100525
Timo Tijhof51516cd2013-04-09 01:32:29 +0200526 if (typeof pipeline.description === 'string') {
Joshua Hesketh9d013542014-04-03 13:08:04 +1100527 $header_div.append(
Joshua Hesketh298c4912014-03-20 16:06:25 +1100528 $('<p />').append(
529 $('<small />').text(pipeline.description)
530 )
531 );
Timo Tijhof51516cd2013-04-09 01:32:29 +0200532 }
Joshua Hesketh9d013542014-04-03 13:08:04 +1100533 return $header_div;
534 },
535
536 pipeline: function (pipeline) {
537 var count = zuul.create_tree(pipeline);
538 var $html = $('<div />')
539 .addClass('zuul-pipeline col-md-4')
540 .append(zuul.format.pipeline_header(pipeline, count));
Timo Tijhof51516cd2013-04-09 01:32:29 +0200541
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100542 $.each(pipeline.change_queues,
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100543 function (queue_i, change_queue) {
544 $.each(change_queue.heads, function (head_i, changes) {
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100545 if (pipeline.change_queues.length > 1 &&
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100546 head_i === 0) {
547 var name = change_queue.name;
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100548 var short_name = name;
549 if (short_name.length > 32) {
550 short_name = short_name.substr(0, 32) + '...';
Timo Tijhof51516cd2013-04-09 01:32:29 +0200551 }
Joshua Hesketh298c4912014-03-20 16:06:25 +1100552 $html.append(
553 $('<p />')
554 .text('Queue: ')
555 .append(
556 $('<abbr />')
557 .attr('title', name)
558 .text(short_name)
559 )
560 );
Timo Tijhof51516cd2013-04-09 01:32:29 +0200561 }
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100562
563 $.each(changes, function (change_i, change) {
564 var $change_box =
565 zuul.format.change_with_status_tree(
566 change, change_queue);
567 $html.append($change_box);
568 zuul.display_patchset($change_box);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200569 });
570 });
571 });
Joshua Hesketh298c4912014-03-20 16:06:25 +1100572 return $html;
Joshua Heskethace48892014-03-22 17:18:31 +1100573 },
574
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100575 filter_form_group: function() {
Joshua Heskethace48892014-03-22 17:18:31 +1100576 // Update the filter form with a clear button if required
577
578 var $label = $('<label />')
579 .addClass('control-label')
580 .attr('for', 'filter_string')
581 .text('Filters')
582 .css('padding-right', '0.5em');
583
584 var $input = $('<input />')
585 .attr('type', 'text')
586 .attr('id', 'filter_string')
587 .addClass('form-control')
588 .attr('title',
589 'project(s), pipeline(s) or review(s) comma ' +
590 'separated')
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100591 .attr('value', current_filter);
Joshua Heskethace48892014-03-22 17:18:31 +1100592
593 $input.change(zuul.handle_filter_change);
594
595 var $clear_icon = $('<span />')
596 .addClass('form-control-feedback')
597 .addClass('glyphicon glyphicon-remove-circle')
598 .attr('id', 'filter_form_clear_box')
599 .attr('title', 'clear filter')
600 .css('cursor', 'pointer');
601
602 $clear_icon.click(function() {
603 $('#filter_string').val('').change();
604 });
605
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100606 if (current_filter === '') {
Joshua Heskethace48892014-03-22 17:18:31 +1100607 $clear_icon.hide();
608 }
609
610 var $form_group = $('<div />')
611 .addClass('form-group has-feedback')
612 .append($label, $input, $clear_icon);
613 return $form_group;
614 },
615
Joshua Heskethae230f62014-03-22 22:14:44 +1100616 expand_form_group: function() {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700617 var expand_by_default = (
Joshua Heskethae230f62014-03-22 22:14:44 +1100618 read_cookie('zuul_expand_by_default', false) === 'true');
619
Monty Taylor860bb0a2014-03-22 09:41:25 -0700620 var $checkbox = $('<input />')
Joshua Heskethae230f62014-03-22 22:14:44 +1100621 .attr('type', 'checkbox')
622 .attr('id', 'expand_by_default')
623 .prop('checked', expand_by_default)
624 .change(zuul.handle_expand_by_default);
625
Monty Taylor860bb0a2014-03-22 09:41:25 -0700626 var $label = $('<label />')
Joshua Heskethae230f62014-03-22 22:14:44 +1100627 .css('padding-left', '1em')
628 .html('Expand by default: ')
629 .append($checkbox);
630
631 var $form_group = $('<div />')
632 .addClass('checkbox')
633 .append($label);
634 return $form_group;
635 },
636
637 control_form: function() {
Joshua Heskethace48892014-03-22 17:18:31 +1100638 // Build the filter form filling anything from cookies
639
Monty Taylor860bb0a2014-03-22 09:41:25 -0700640 var $control_form = $('<form />')
Joshua Heskethace48892014-03-22 17:18:31 +1100641 .attr('role', 'form')
642 .addClass('form-inline')
643 .submit(zuul.handle_filter_change);
644
Joshua Heskethae230f62014-03-22 22:14:44 +1100645 $control_form
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100646 .append(zuul.format.filter_form_group())
Joshua Heskethae230f62014-03-22 22:14:44 +1100647 .append(zuul.format.expand_form_group());
648
649 return $control_form;
Joshua Heskethace48892014-03-22 17:18:31 +1100650 },
Timo Tijhof51516cd2013-04-09 01:32:29 +0200651 },
652
653 emit: function () {
654 $jq.trigger.apply($jq, arguments);
655 return this;
656 },
657 on: function () {
658 $jq.on.apply($jq, arguments);
659 return this;
660 },
661 one: function () {
662 $jq.one.apply($jq, arguments);
663 return this;
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100664 },
665
666 toggle_patchset: function(e) {
667 // Toggle showing/hiding the patchset when the header is clicked
668 // Grab the patchset panel
669 var $panel = $(e.target).parents('.zuul-change');
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100670 var $body = $panel.children('.zuul-patchset-body');
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100671 $body.toggle(200);
672 var collapsed_index = zuul.collapsed_exceptions.indexOf(
673 $panel.attr('id'));
Monty Taylor860bb0a2014-03-22 09:41:25 -0700674 if (collapsed_index === -1 ) {
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100675 // Currently not an exception, add it to list
676 zuul.collapsed_exceptions.push($panel.attr('id'));
677 }
678 else {
679 // Currently an except, remove from exceptions
680 zuul.collapsed_exceptions.splice(collapsed_index, 1);
681 }
682 },
683
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100684 display_patchset: function($change_box, animate) {
Joshua Heskethace48892014-03-22 17:18:31 +1100685 // Determine if to show or hide the patchset and/or the results
686 // when loaded
687
688 // See if we should hide the body/results
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100689 var $panel = $change_box.find('.zuul-change');
690 var panel_change = $panel.attr('id');
691 var $body = $panel.children('.zuul-patchset-body');
Joshua Heskethae230f62014-03-22 22:14:44 +1100692 var expand_by_default = $('#expand_by_default').prop('checked');
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100693
694 var collapsed_index = zuul.collapsed_exceptions.indexOf(panel_change);
695
Monty Taylor860bb0a2014-03-22 09:41:25 -0700696 if (expand_by_default && collapsed_index === -1 ||
697 !expand_by_default && collapsed_index !== -1) {
Joshua Heskethae230f62014-03-22 22:14:44 +1100698 // Expand by default, or is an exception
699 $body.show(animate);
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100700 }
701 else {
Joshua Heskethae230f62014-03-22 22:14:44 +1100702 $body.hide(animate);
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100703 }
Joshua Heskethace48892014-03-22 17:18:31 +1100704
705 // Check if we should hide the whole panel
706 var panel_project = $panel.find('.change_project').text()
707 .toLowerCase();
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100708
709
Joshua Hesketh9d013542014-04-03 13:08:04 +1100710 var panel_pipeline = $change_box
711 .parents('.zuul-pipeline')
712 .find('.zuul-pipeline-header > h3')
713 .html()
714 .toLowerCase();
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100715
Monty Taylor860bb0a2014-03-22 09:41:25 -0700716 if (current_filter !== '') {
717 var show_panel = false;
718 var filter = current_filter.trim().split(/[\s,]+/);
Joshua Heskethace48892014-03-22 17:18:31 +1100719 $.each(filter, function(index, f_val) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700720 if (f_val !== '') {
Joshua Heskethace48892014-03-22 17:18:31 +1100721 f_val = f_val.toLowerCase();
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100722 if (panel_project.indexOf(f_val) !== -1 ||
723 panel_pipeline.indexOf(f_val) !== -1 ||
724 panel_change.indexOf(f_val) !== -1) {
Joshua Heskethace48892014-03-22 17:18:31 +1100725 show_panel = true;
726 }
727 }
728 });
Monty Taylor860bb0a2014-03-22 09:41:25 -0700729 if (show_panel === true) {
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100730 $change_box.show(animate);
Joshua Heskethace48892014-03-22 17:18:31 +1100731 }
732 else {
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100733 $change_box.hide(animate);
Joshua Heskethace48892014-03-22 17:18:31 +1100734 }
735 }
736 else {
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100737 $change_box.show(animate);
Joshua Heskethace48892014-03-22 17:18:31 +1100738 }
739 },
740
Monty Taylor860bb0a2014-03-22 09:41:25 -0700741 handle_filter_change: function() {
Joshua Heskethace48892014-03-22 17:18:31 +1100742 // Update the filter and save it to a cookie
743 current_filter = $('#filter_string').val();
744 set_cookie('zuul_filter_string', current_filter);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700745 if (current_filter === '') {
Joshua Heskethace48892014-03-22 17:18:31 +1100746 $('#filter_form_clear_box').hide();
747 }
748 else {
749 $('#filter_form_clear_box').show();
750 }
751
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100752 $('.zuul-change-box').each(function(index, obj) {
753 var $change_box = $(obj);
754 zuul.display_patchset($change_box, 200);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700755 });
Joshua Heskethace48892014-03-22 17:18:31 +1100756 return false;
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100757 },
Joshua Heskethae230f62014-03-22 22:14:44 +1100758
759 handle_expand_by_default: function(e) {
760 // Handle toggling expand by default
761 set_cookie('zuul_expand_by_default', e.target.checked);
762 zuul.collapsed_exceptions = [];
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100763 $('.zuul-change-box').each(function(index, obj) {
764 var $change_box = $(obj);
765 zuul.display_patchset($change_box, 200);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700766 });
Joshua Heskethae230f62014-03-22 22:14:44 +1100767 },
Joshua Hesketh5caf4f62014-04-01 12:52:43 +1100768
769 create_tree: function(pipeline) {
770 var count = 0;
771 var pipeline_max_tree_columns = 1;
772 $.each(pipeline.change_queues, function(change_queue_i,
773 change_queue) {
774 var tree = [];
775 var max_tree_columns = 1;
776 var changes = [];
777 var last_tree_length = 0;
778 $.each(change_queue.heads, function(head_i, head) {
779 $.each(head, function(change_i, change) {
780 changes[change.id] = change;
781 change._tree_position = change_i;
782 });
783 });
784 $.each(change_queue.heads, function(head_i, head) {
785 $.each(head, function(change_i, change) {
786 count += 1;
787 var idx = tree.indexOf(change.id);
788 if (idx > -1) {
789 change._tree_index = idx;
790 // remove...
791 tree[idx] = null;
792 while (tree[tree.length - 1] === null) {
793 tree.pop();
794 }
795 } else {
796 change._tree_index = 0;
797 }
798 change._tree_branches = [];
799 change._tree = [];
800 if (typeof(change.items_behind) === 'undefined') {
801 change.items_behind = [];
802 }
803 change.items_behind.sort(function(a, b) {
804 return (changes[b]._tree_position -
805 changes[a]._tree_position);
806 });
807 $.each(change.items_behind, function(i, id) {
808 tree.push(id);
809 if (tree.length>last_tree_length &&
810 last_tree_length > 0) {
811 change._tree_branches.push(
812 tree.length - 1);
813 }
814 });
815 if (tree.length > max_tree_columns) {
816 max_tree_columns = tree.length;
817 }
818 if (tree.length > pipeline_max_tree_columns) {
819 pipeline_max_tree_columns = tree.length;
820 }
821 change._tree = tree.slice(0); // make a copy
822 last_tree_length = tree.length;
823 });
824 });
825 change_queue._tree_columns = max_tree_columns;
826 });
827 pipeline._tree_columns = pipeline_max_tree_columns;
828 return count;
829 },
Joshua Hesketh9d013542014-04-03 13:08:04 +1100830
831 get_sparkline_url: function(pipeline_name) {
832 if (zuul.graphite_url !== '') {
833 if (!(pipeline_name in zuul_sparkline_urls)) {
834 zuul_sparkline_urls[pipeline_name] = $.fn.graphite.geturl({
835 url: zuul.graphite_url,
836 from: "-8hours",
837 width: 100,
838 height: 26,
839 margin: 0,
840 hideLegend: true,
841 hideAxes: true,
842 hideGrid: true,
843 target: [
844 "color(stats.gauges.zuul.pipeline."+pipeline_name+".current_changes, '6b8182')"
845 ]
846 });
847 }
848 return zuul_sparkline_urls[pipeline_name];
849 }
850 return false;
851 },
Timo Tijhof51516cd2013-04-09 01:32:29 +0200852 };
853
Joshua Heskethace48892014-03-22 17:18:31 +1100854 current_filter = read_cookie('zuul_filter_string', current_filter);
855
Timo Tijhof51516cd2013-04-09 01:32:29 +0200856 $jq = $(zuul);
857
858 $jq.on('update-start', function () {
859 $container.addClass('zuul-container-loading');
860 $indicator.addClass('zuul-spinner-on');
861 });
862
863 $jq.on('update-end', function () {
864 $container.removeClass('zuul-container-loading');
865 setTimeout(function () {
866 $indicator.removeClass('zuul-spinner-on');
Joshua Hesketh876ca512014-03-21 18:44:22 +1100867 }, 500);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200868 });
869
870 $jq.one('update-end', function () {
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100871 // Do this asynchronous so that if the first update adds a message, it
872 // will not animate while we fade in the content. Instead it simply
873 // appears with the rest of the content.
Timo Tijhof51516cd2013-04-09 01:32:29 +0200874 setTimeout(function () {
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100875 // Fade in the content
876 $container.addClass('zuul-container-ready');
Timo Tijhof51516cd2013-04-09 01:32:29 +0200877 });
878 });
879
880 $(function ($) {
Joshua Hesketh4863b602014-03-21 14:19:06 +1100881 $msg = $('<div />').addClass('alert').hide();
Monty Taylor860bb0a2014-03-22 09:41:25 -0700882 $indicator = $('<button class="btn pull-right zuul-spinner">' +
883 'updating ' +
884 '<span class="glyphicon glyphicon-refresh"></span>' +
885 '</button>');
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100886 $queueInfo = $('<p>Queue lengths: <span>0</span> events, ' +
887 '<span>0</span> results.</p>');
Joshua Hesketh298c4912014-03-20 16:06:25 +1100888 $queueEventsNum = $queueInfo.find('span').eq(0);
889 $queueResultsNum = $queueEventsNum.next();
Joshua Heskethace48892014-03-22 17:18:31 +1100890
Monty Taylor860bb0a2014-03-22 09:41:25 -0700891 var $control_form = zuul.format.control_form();
Joshua Heskethace48892014-03-22 17:18:31 +1100892
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100893 $pipelines = $('<div class="row"></div>');
Monty Taylor860bb0a2014-03-22 09:41:25 -0700894 var $zuulVersion = $('<p>Zuul version: <span id="zuul-version-span">' +
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100895 '</span></p>');
Monty Taylor860bb0a2014-03-22 09:41:25 -0700896 var $lastReconf = $('<p>Last reconfigured: ' +
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100897 '<span id="last-reconfigured-span"></span></p>');
Timo Tijhof51516cd2013-04-09 01:32:29 +0200898
Joshua Hesketh4863b602014-03-21 14:19:06 +1100899 $container = $('#zuul-container').append($msg, $indicator,
Joshua Heskethae230f62014-03-22 22:14:44 +1100900 $queueInfo, $control_form,
Joshua Heskethace48892014-03-22 17:18:31 +1100901 $pipelines, $zuulVersion,
902 $lastReconf);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200903
Joshua Hesketh9d013542014-04-03 13:08:04 +1100904 //zuul.graphite_url = 'http://graphite.openstack.org/render/'
Timo Tijhof51516cd2013-04-09 01:32:29 +0200905 zuul.schedule();
906
907 $(document).on({
908 'show.visibility': function () {
909 zuul.enabled = true;
910 zuul.update();
911 },
912 'hide.visibility': function () {
913 zuul.enabled = false;
914 }
915 });
916 });
917}(jQuery));