Timo Tijhof | 51516cd | 2013-04-09 01:32:29 +0200 | [diff] [blame] | 1 | // Client script for Zuul status page |
| 2 | // |
| 3 | // Copyright 2012 OpenStack Foundation |
| 4 | // Copyright 2013 Timo Tijhof |
| 5 | // Copyright 2013 Wikimedia Foundation |
| 6 | // |
| 7 | // Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | // not use this file except in compliance with the License. You may obtain |
| 9 | // a copy of the License at |
| 10 | // |
| 11 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | // |
| 13 | // Unless required by applicable law or agreed to in writing, software |
| 14 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | // License for the specific language governing permissions and limitations |
| 17 | // under the License. |
| 18 | |
| 19 | (function ($) { |
Joshua Hesketh | 4863b60 | 2014-03-21 14:19:06 +1100 | [diff] [blame^] | 20 | var $container, $msg, $indicator, $queueInfo, $queueEventsNum, |
Joshua Hesketh | cbdcca1 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 21 | $queueResultsNum, $pipelines, $jq; |
Joshua Hesketh | 298c491 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 22 | var xhr, zuul, |
Timo Tijhof | 51516cd | 2013-04-09 01:32:29 +0200 | [diff] [blame] | 23 | demo = location.search.match(/[?&]demo=([^?&]*)/), |
| 24 | source = demo ? |
| 25 | './status-' + (demo[1] || 'basic') + '.json-sample' : |
James E. Blair | 7c7ed7a | 2013-05-15 13:13:26 -0700 | [diff] [blame] | 26 | 'status.json'; |
Timo Tijhof | 51516cd | 2013-04-09 01:32:29 +0200 | [diff] [blame] | 27 | |
| 28 | zuul = { |
| 29 | enabled: true, |
| 30 | |
| 31 | schedule: function () { |
| 32 | if (!zuul.enabled) { |
| 33 | setTimeout(zuul.schedule, 5000); |
| 34 | return; |
| 35 | } |
| 36 | zuul.update().complete(function () { |
| 37 | setTimeout(zuul.schedule, 5000); |
| 38 | }); |
| 39 | }, |
| 40 | |
| 41 | /** @return {jQuery.Promise} */ |
| 42 | update: function () { |
| 43 | // Cancel the previous update if it hasn't completed yet. |
| 44 | if (xhr) { |
| 45 | xhr.abort(); |
| 46 | } |
| 47 | |
| 48 | zuul.emit('update-start'); |
| 49 | |
Joshua Hesketh | 298c491 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 50 | xhr = $.getJSON(source) |
| 51 | .done(function (data) { |
| 52 | if ('message' in data) { |
Joshua Hesketh | 4863b60 | 2014-03-21 14:19:06 +1100 | [diff] [blame^] | 53 | $msg.removeClass('alert-danger').addClass('alert-info'); |
Joshua Hesketh | 298c491 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 54 | $msg.text(data.message); |
Joshua Hesketh | 4863b60 | 2014-03-21 14:19:06 +1100 | [diff] [blame^] | 55 | $msg.show(); |
Joshua Hesketh | 298c491 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 56 | } else { |
| 57 | $msg.empty(); |
Joshua Hesketh | 4863b60 | 2014-03-21 14:19:06 +1100 | [diff] [blame^] | 58 | $msg.hide(); |
Joshua Hesketh | 298c491 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 59 | } |
Timo Tijhof | 51516cd | 2013-04-09 01:32:29 +0200 | [diff] [blame] | 60 | |
Joshua Hesketh | 298c491 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 61 | if ('zuul_version' in data) { |
| 62 | $('#zuul-version-span').text(data['zuul_version']); |
| 63 | } |
| 64 | if ('last_reconfigured' in data) { |
| 65 | var last_reconfigured = |
| 66 | new Date(data['last_reconfigured']); |
| 67 | $('#last-reconfigured-span').text( |
| 68 | last_reconfigured.toString()); |
| 69 | } |
| 70 | |
| 71 | $pipelines.html(''); |
| 72 | $.each(data.pipelines, function (i, pipeline) { |
| 73 | $pipelines.append(zuul.format.pipeline(pipeline)); |
| 74 | }); |
| 75 | |
| 76 | $queueEventsNum.text( |
| 77 | data.trigger_event_queue ? |
| 78 | data.trigger_event_queue.length : '0' |
| 79 | ); |
| 80 | $queueResultsNum.text( |
| 81 | data.result_event_queue ? |
| 82 | data.result_event_queue.length : '0' |
| 83 | ); |
| 84 | }) |
| 85 | .fail(function (err, jqXHR, errMsg) { |
| 86 | $msg.text(source + ': ' + errMsg).show(); |
Timo Tijhof | 51516cd | 2013-04-09 01:32:29 +0200 | [diff] [blame] | 87 | $msgWrap.removeClass('zuul-msg-wrap-off'); |
Joshua Hesketh | 298c491 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 88 | }) |
| 89 | .complete(function () { |
| 90 | xhr = undefined; |
| 91 | zuul.emit('update-end'); |
Timo Tijhof | 51516cd | 2013-04-09 01:32:29 +0200 | [diff] [blame] | 92 | }); |
| 93 | |
Timo Tijhof | 51516cd | 2013-04-09 01:32:29 +0200 | [diff] [blame] | 94 | return xhr; |
| 95 | }, |
| 96 | |
| 97 | format: { |
| 98 | change: function (change) { |
Joshua Hesketh | 298c491 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 99 | if (change.id.length === 40) { |
| 100 | change.id = change.id.substr(0, 7); |
| 101 | } |
Timo Tijhof | 51516cd | 2013-04-09 01:32:29 +0200 | [diff] [blame] | 102 | |
Joshua Hesketh | 298c491 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 103 | var $html = $('<div />') |
Joshua Hesketh | 4863b60 | 2014-03-21 14:19:06 +1100 | [diff] [blame^] | 104 | .addClass('panel panel-default zuul-change') |
| 105 | |
| 106 | var $change_header = $('<div />').text(change.project); |
| 107 | $change_header.addClass('panel-heading'); |
Joshua Hesketh | 298c491 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 108 | |
| 109 | if (change.url !== null) { |
| 110 | var $id_span = $('<span />').append( |
| 111 | $("<a />").attr("href", change.url).text(change.id) |
| 112 | ); |
Timo Tijhof | 51516cd | 2013-04-09 01:32:29 +0200 | [diff] [blame] | 113 | } |
Joshua Hesketh | 298c491 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 114 | else { |
| 115 | var $id_span = $('<span />').text(change.id); |
Timo Tijhof | 51516cd | 2013-04-09 01:32:29 +0200 | [diff] [blame] | 116 | } |
Joshua Hesketh | 4863b60 | 2014-03-21 14:19:06 +1100 | [diff] [blame^] | 117 | $change_header.append($id_span.addClass('zuul-change-id')); |
| 118 | $html.append($change_header); |
Timo Tijhof | 51516cd | 2013-04-09 01:32:29 +0200 | [diff] [blame] | 119 | |
Joshua Hesketh | 4863b60 | 2014-03-21 14:19:06 +1100 | [diff] [blame^] | 120 | var $list = $('<ul />'); |
| 121 | $list.addClass('list-group'); |
Timo Tijhof | 51516cd | 2013-04-09 01:32:29 +0200 | [diff] [blame] | 122 | $.each(change.jobs, function (i, job) { |
Joshua Hesketh | 298c491 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 123 | var $item = $('<li />'); |
Joshua Hesketh | 4863b60 | 2014-03-21 14:19:06 +1100 | [diff] [blame^] | 124 | $item.addClass('list-group-item'); |
| 125 | $item.addClass('zuul-change-job'); |
| 126 | |
| 127 | if (job.url !== null) { |
| 128 | $job_line = $('<a href="' + job.url + '" />'). |
| 129 | addClass('zuul-change-job-link'); |
| 130 | } |
| 131 | else{ |
| 132 | $job_line = $('<span />'). |
| 133 | addClass('zuul-change-job-link'); |
| 134 | } |
| 135 | $job_line.text(job.name); |
Joshua Hesketh | 298c491 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 136 | |
| 137 | var result = job.result ? job.result.toLowerCase() : null; |
Timo Tijhof | 51516cd | 2013-04-09 01:32:29 +0200 | [diff] [blame] | 138 | if (result === null) { |
| 139 | result = job.url ? 'in progress' : 'queued'; |
| 140 | } |
| 141 | switch (result) { |
Joshua Hesketh | 298c491 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 142 | case 'success': |
| 143 | resultClass = ' label-success'; |
| 144 | break; |
| 145 | case 'failure': |
Joshua Hesketh | 4863b60 | 2014-03-21 14:19:06 +1100 | [diff] [blame^] | 146 | resultClass = ' label-danger'; |
Joshua Hesketh | 298c491 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 147 | break; |
Joshua Hesketh | 298c491 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 148 | case 'unstable': |
| 149 | resultClass = ' label-warning'; |
| 150 | break; |
Joshua Hesketh | 4863b60 | 2014-03-21 14:19:06 +1100 | [diff] [blame^] | 151 | case 'in progress': |
| 152 | case 'queued': |
| 153 | case 'lost': |
| 154 | resultClass = ' label-default'; |
| 155 | break; |
Timo Tijhof | 51516cd | 2013-04-09 01:32:29 +0200 | [diff] [blame] | 156 | } |
Joshua Hesketh | 298c491 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 157 | |
Joshua Hesketh | 4863b60 | 2014-03-21 14:19:06 +1100 | [diff] [blame^] | 158 | $job_line.append( |
| 159 | $('<span />').addClass('zuul-result label'). |
| 160 | addClass(resultClass).text(result) |
Joshua Hesketh | 298c491 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 161 | ); |
| 162 | |
Timo Tijhof | 51516cd | 2013-04-09 01:32:29 +0200 | [diff] [blame] | 163 | if (job.voting === false) { |
Joshua Hesketh | 298c491 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 164 | $job_line.append( |
| 165 | $(' <span />').addClass('muted'). |
| 166 | text(' (non-voting)') |
| 167 | ); |
Timo Tijhof | 51516cd | 2013-04-09 01:32:29 +0200 | [diff] [blame] | 168 | } |
Joshua Hesketh | 298c491 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 169 | $item.append($job_line); |
| 170 | $list.append($item); |
Timo Tijhof | 51516cd | 2013-04-09 01:32:29 +0200 | [diff] [blame] | 171 | }); |
| 172 | |
Joshua Hesketh | 298c491 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 173 | $html.append($list); |
| 174 | return $html; |
Timo Tijhof | 51516cd | 2013-04-09 01:32:29 +0200 | [diff] [blame] | 175 | }, |
| 176 | |
| 177 | pipeline: function (pipeline) { |
Joshua Hesketh | 298c491 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 178 | var $html = $('<div />') |
Joshua Hesketh | 4863b60 | 2014-03-21 14:19:06 +1100 | [diff] [blame^] | 179 | .addClass('zuul-pipeline col-md-4') |
Joshua Hesketh | 298c491 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 180 | .append($('<h3 />').text(pipeline.name)); |
| 181 | |
Timo Tijhof | 51516cd | 2013-04-09 01:32:29 +0200 | [diff] [blame] | 182 | if (typeof pipeline.description === 'string') { |
Joshua Hesketh | 298c491 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 183 | $html.append( |
| 184 | $('<p />').append( |
| 185 | $('<small />').text(pipeline.description) |
| 186 | ) |
| 187 | ); |
Timo Tijhof | 51516cd | 2013-04-09 01:32:29 +0200 | [diff] [blame] | 188 | } |
| 189 | |
Joshua Hesketh | cbdcca1 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 190 | $.each(pipeline.change_queues, |
| 191 | function (queueNum, changeQueue) { |
Timo Tijhof | 51516cd | 2013-04-09 01:32:29 +0200 | [diff] [blame] | 192 | $.each(changeQueue.heads, function (headNum, changes) { |
Joshua Hesketh | cbdcca1 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 193 | if (pipeline.change_queues.length > 1 && |
| 194 | headNum === 0) { |
Timo Tijhof | 51516cd | 2013-04-09 01:32:29 +0200 | [diff] [blame] | 195 | var name = changeQueue.name; |
Timo Tijhof | 51516cd | 2013-04-09 01:32:29 +0200 | [diff] [blame] | 196 | if (name.length > 32) { |
Joshua Hesketh | 298c491 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 197 | short_name = name.substr(0, 32) + '...'; |
Timo Tijhof | 51516cd | 2013-04-09 01:32:29 +0200 | [diff] [blame] | 198 | } |
Joshua Hesketh | 298c491 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 199 | $html.append( |
| 200 | $('<p />') |
| 201 | .text('Queue: ') |
| 202 | .append( |
| 203 | $('<abbr />') |
| 204 | .attr('title', name) |
| 205 | .text(short_name) |
| 206 | ) |
| 207 | ); |
Timo Tijhof | 51516cd | 2013-04-09 01:32:29 +0200 | [diff] [blame] | 208 | } |
| 209 | $.each(changes, function (changeNum, change) { |
Joshua Hesketh | 298c491 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 210 | $html.append(zuul.format.change(change)) |
Timo Tijhof | 51516cd | 2013-04-09 01:32:29 +0200 | [diff] [blame] | 211 | }); |
| 212 | }); |
| 213 | }); |
Joshua Hesketh | 298c491 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 214 | return $html; |
Timo Tijhof | 51516cd | 2013-04-09 01:32:29 +0200 | [diff] [blame] | 215 | } |
| 216 | }, |
| 217 | |
| 218 | emit: function () { |
| 219 | $jq.trigger.apply($jq, arguments); |
| 220 | return this; |
| 221 | }, |
| 222 | on: function () { |
| 223 | $jq.on.apply($jq, arguments); |
| 224 | return this; |
| 225 | }, |
| 226 | one: function () { |
| 227 | $jq.one.apply($jq, arguments); |
| 228 | return this; |
| 229 | } |
| 230 | }; |
| 231 | |
| 232 | $jq = $(zuul); |
| 233 | |
| 234 | $jq.on('update-start', function () { |
| 235 | $container.addClass('zuul-container-loading'); |
| 236 | $indicator.addClass('zuul-spinner-on'); |
| 237 | }); |
| 238 | |
| 239 | $jq.on('update-end', function () { |
| 240 | $container.removeClass('zuul-container-loading'); |
| 241 | setTimeout(function () { |
| 242 | $indicator.removeClass('zuul-spinner-on'); |
| 243 | }, 550); |
| 244 | }); |
| 245 | |
| 246 | $jq.one('update-end', function () { |
Joshua Hesketh | cbdcca1 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 247 | // Do this asynchronous so that if the first update adds a message, it |
| 248 | // will not animate while we fade in the content. Instead it simply |
| 249 | // appears with the rest of the content. |
Timo Tijhof | 51516cd | 2013-04-09 01:32:29 +0200 | [diff] [blame] | 250 | setTimeout(function () { |
Joshua Hesketh | cbdcca1 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 251 | // Fade in the content |
| 252 | $container.addClass('zuul-container-ready'); |
Timo Tijhof | 51516cd | 2013-04-09 01:32:29 +0200 | [diff] [blame] | 253 | }); |
| 254 | }); |
| 255 | |
| 256 | $(function ($) { |
Joshua Hesketh | 4863b60 | 2014-03-21 14:19:06 +1100 | [diff] [blame^] | 257 | $msg = $('<div />').addClass('alert').hide(); |
| 258 | $indicator = $('<button class="btn pull-right zuul-spinner">updating ' |
| 259 | + '<span class="glyphicon glyphicon-refresh"></span>' |
| 260 | + '</button>'); |
Joshua Hesketh | cbdcca1 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 261 | $queueInfo = $('<p>Queue lengths: <span>0</span> events, ' + |
| 262 | '<span>0</span> results.</p>'); |
Joshua Hesketh | 298c491 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 263 | $queueEventsNum = $queueInfo.find('span').eq(0); |
| 264 | $queueResultsNum = $queueEventsNum.next(); |
Timo Tijhof | 51516cd | 2013-04-09 01:32:29 +0200 | [diff] [blame] | 265 | $pipelines = $('<div class="row"></div>'); |
Joshua Hesketh | cbdcca1 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 266 | $zuulVersion = $('<p>Zuul version: <span id="zuul-version-span">' + |
| 267 | '</span></p>'); |
| 268 | $lastReconf = $('<p>Last reconfigured: ' + |
| 269 | '<span id="last-reconfigured-span"></span></p>'); |
Timo Tijhof | 51516cd | 2013-04-09 01:32:29 +0200 | [diff] [blame] | 270 | |
Joshua Hesketh | 4863b60 | 2014-03-21 14:19:06 +1100 | [diff] [blame^] | 271 | $container = $('#zuul-container').append($msg, $indicator, |
Joshua Hesketh | cbdcca1 | 2014-03-20 16:06:25 +1100 | [diff] [blame] | 272 | $queueInfo, $pipelines, |
| 273 | $zuulVersion, $lastReconf); |
Timo Tijhof | 51516cd | 2013-04-09 01:32:29 +0200 | [diff] [blame] | 274 | |
| 275 | zuul.schedule(); |
| 276 | |
| 277 | $(document).on({ |
| 278 | 'show.visibility': function () { |
| 279 | zuul.enabled = true; |
| 280 | zuul.update(); |
| 281 | }, |
| 282 | 'hide.visibility': function () { |
| 283 | zuul.enabled = false; |
| 284 | } |
| 285 | }); |
| 286 | }); |
| 287 | }(jQuery)); |