// Client script for Zuul status page // // Copyright 2012 OpenStack Foundation // Copyright 2013 Timo Tijhof // Copyright 2013 Wikimedia Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); you may // not use this file except in compliance with the License. You may obtain // a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the // License for the specific language governing permissions and limitations // under the License. (function ($) { var $container, $msg, $msgWrap, $indicator, $queueInfo, $queueEventsNum, $queueResultsNum, $pipelines, $jq; var xhr, prevHtml, zuul, demo = location.search.match(/[?&]demo=([^?&]*)/), source = demo ? './status-' + (demo[1] || 'basic') + '.json-sample' : 'status.json'; zuul = { enabled: true, schedule: function () { if (!zuul.enabled) { setTimeout(zuul.schedule, 5000); return; } zuul.update().complete(function () { setTimeout(zuul.schedule, 5000); }); }, /** @return {jQuery.Promise} */ update: function () { // Cancel the previous update if it hasn't completed yet. if (xhr) { xhr.abort(); } zuul.emit('update-start'); xhr = $.ajax({ url: source, dataType: 'json', cache: false }) .done(function (data) { var html = ''; data = data || {}; if ('message' in data) { $msg.html(data.message); $msgWrap.removeClass('zuul-msg-wrap-off'); } else { $msg.empty(); $msgWrap.addClass('zuul-msg-wrap-off'); } if ('zuul_version' in data) { $('#zuul-version-span').text(data['zuul_version']); } if ('last_reconfigured' in data) { var last_reconfigured = new Date(data['last_reconfigured']); $('#last-reconfigured-span').text( last_reconfigured.toString()); } $.each(data.pipelines, function (i, pipeline) { html += zuul.format.pipeline(pipeline); }); // Only re-parse the DOM if we have to if (html !== prevHtml) { prevHtml = html; $pipelines.html(html); } $queueEventsNum.text( data.trigger_event_queue ? data.trigger_event_queue.length : '0' ); $queueResultsNum.text( data.result_event_queue ? data.result_event_queue.length : '0' ); }) .fail(function (err, jqXHR, errMsg) { $msg.text(source + ': ' + errMsg).show(); $msgWrap.removeClass('zuul-msg-wrap-off'); }) .complete(function () { xhr = undefined; zuul.emit('update-end'); }); return xhr; }, format: { change: function (change) { var html = '
' + '
'; return html; }, pipeline: function (pipeline) { var html = '

' + pipeline.name + '

'; if (typeof pipeline.description === 'string') { html += '

' + pipeline.description + '

'; } $.each(pipeline.change_queues, function (queueNum, changeQueue) { $.each(changeQueue.heads, function (headNum, changes) { if (pipeline.change_queues.length > 1 && headNum === 0) { var name = changeQueue.name; html += '

Queue: '; if (name.length > 32) { name = name.substr(0, 32) + '...'; } html += name + '

'; } $.each(changes, function (changeNum, change) { // If there are multiple changes in the same head // it means they're connected if (changeNum > 0) { html += '
' + '↑
'; } html += zuul.format.change(change); }); }); }); html += '
'; return html; } }, emit: function () { $jq.trigger.apply($jq, arguments); return this; }, on: function () { $jq.on.apply($jq, arguments); return this; }, one: function () { $jq.one.apply($jq, arguments); return this; } }; $jq = $(zuul); $jq.on('update-start', function () { $container.addClass('zuul-container-loading'); $indicator.addClass('zuul-spinner-on'); }); $jq.on('update-end', function () { $container.removeClass('zuul-container-loading'); setTimeout(function () { $indicator.removeClass('zuul-spinner-on'); }, 550); }); $jq.one('update-end', function () { // Do this asynchronous so that if the first update adds a message, it // will not animate while we fade in the content. Instead it simply // appears with the rest of the content. setTimeout(function () { // Fade in the content $container.addClass('zuul-container-ready'); }); }); $(function ($) { $msg = $('
'); $msgWrap = $msg.wrap('
' + '
').parent(); $indicator = $('updating ' + ''); $queueInfo = $('

Queue lengths: 0 events, ' + '0 results.

'); $queueEventsNum = $queueInfo.find('span').eq(0); $queueResultsNum = $queueEventsNum.next(); $pipelines = $('
'); $zuulVersion = $('

Zuul version: ' + '

'); $lastReconf = $('

Last reconfigured: ' + '

'); $container = $('#zuul-container').append($msgWrap, $indicator, $queueInfo, $pipelines, $zuulVersion, $lastReconf); zuul.schedule(); $(document).on({ 'show.visibility': function () { zuul.enabled = true; zuul.update(); }, 'hide.visibility': function () { zuul.enabled = false; } }); }); }(jQuery));