2 * This js establishes a websockets connection with the API Server.
5 /* Subscribe to websockets event log. Do nothing if already connected. */
6 function subscribeToEventLog () {
7 // if websockets are not supported by browser, do not subscribe for events
8 websocketsSupported = ('WebSocket' in window);
9 if (websocketsSupported == false) {
13 // check if websocket connection is already stored on the window
14 event_log_disp = $(window).data("arv-websocket");
15 if (event_log_disp == null) {
16 // need to create new websocket and event log dispatcher
17 websocket_url = $('meta[name=arv-websocket-url]').attr("content");
18 if (websocket_url == null)
21 event_log_disp = new WebSocket(websocket_url);
23 event_log_disp.onopen = onEventLogDispatcherOpen;
24 event_log_disp.onmessage = onEventLogDispatcherMessage;
26 // store websocket in window to allow reuse when multiple divs subscribe for events
27 $(window).data("arv-websocket", event_log_disp);
31 /* Send subscribe message to the websockets server. Without any filters
32 arguments, this subscribes to all events */
33 function onEventLogDispatcherOpen(event) {
34 this.send('{"method":"subscribe"}');
37 /* Trigger event for all applicable elements waiting for this event */
38 function onEventLogDispatcherMessage(event) {
39 parsedData = JSON.parse(event.data);
40 object_uuid = parsedData.object_uuid;
46 // if there are any listeners for this object uuid or "all", trigger the event
47 matches = ".arv-log-event-listener[data-object-uuid=\"" + object_uuid + "\"],.arv-log-event-listener[data-object-uuids~=\"" + object_uuid + "\"],.arv-log-event-listener[data-object-uuid=\"all\"],.arv-log-event-listener[data-object-kind=\"" + parsedData.object_kind + "\"]";
48 $(matches).trigger('arv-log-event', parsedData);
51 /* Automatically connect if there are any elements on the page that want to
52 receive event log events. */
53 $(document).on('ajax:complete ready', function() {
54 var a = $('.arv-log-event-listener');
56 subscribeToEventLog();
60 /* Assumes existence of:
61 window.jobGraphData = [];
62 window.jobGraphSeries = [];
63 window.jobGraphSortedSeries = [];
64 window.jobGraphMaxima = {};
66 function processLogLineForChart( logLine ) {
68 var match = logLine.match(/^(\S+) (\S+) (\S+) (\S+) stderr crunchstat: (\S+) (.*)/);
70 match = logLine.match(/^((?:Sun|Mon|Tue|Wed|Thu|Fri|Sat) (?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d{1,2} \d\d:\d\d:\d\d \d{4}) (\S+) (\S+) (\S+) stderr crunchstat: (\S+) (.*)/);
72 match[1] = (new Date(match[1] + ' UTC')).toISOString().replace('Z','');
76 var rawDetailData = '';
79 // the timestamp comes first
80 var timestamp = match[1].replace('_','T') + 'Z';
82 // we are interested in "-- interval" recordings
83 var intervalMatch = match[6].match(/(.*) -- interval (.*)/);
85 var intervalData = intervalMatch[2].trim().split(' ');
86 var dt = parseFloat(intervalData[0]);
88 for(var i=2; i < intervalData.length; i += 2 ) {
89 dsum += parseFloat(intervalData[i]);
94 // not interested in negative deltas
98 rawDetailData = intervalMatch[2];
100 // for the series name use the task number (4th term) and then the first word after 'crunchstat:'
101 var series = 'T' + match[4] + '-' + match[5];
103 // special calculation for cpus
104 if( /-cpu$/.test(series) ) {
105 // divide the stat by the number of cpus
106 var cpuCountMatch = intervalMatch[1].match(/(\d+) cpus/);
107 if( cpuCountMatch ) {
108 datum = datum / cpuCountMatch[1];
112 addJobGraphDatum( timestamp, datum, series, rawDetailData );
114 // we are also interested in memory ("mem") recordings
115 var memoryMatch = match[6].match(/(\d+) cache (\d+) swap (\d+) pgmajfault (\d+) rss/);
117 rawDetailData = match[6];
118 // one datapoint for rss and one for swap - only show the rawDetailData for rss
119 addJobGraphDatum( timestamp, parseInt(memoryMatch[4]), 'T' + match[4] + "-rss", rawDetailData );
120 addJobGraphDatum( timestamp, parseInt(memoryMatch[2]), 'T' + match[4] + "-swap", '' );
127 window.redraw = true;
130 console.log( 'Ignoring error trying to process log line: ' + err);
134 function addJobGraphDatum(timestamp, datum, series, rawDetailData) {
135 // check for new series
136 if( $.inArray( series, jobGraphSeries ) < 0 ) {
137 var newIndex = jobGraphSeries.push(series) - 1;
138 jobGraphSortedSeries.push(newIndex);
139 jobGraphSortedSeries.sort( function(a,b) {
140 var matchA = jobGraphSeries[a].match(/^T(\d+)-(.*)/);
141 var matchB = jobGraphSeries[b].match(/^T(\d+)-(.*)/);
142 var termA = ('000000' + matchA[1]).slice(-6) + matchA[2];
143 var termB = ('000000' + matchB[1]).slice(-6) + matchB[2];
144 return termA > termB ? 1 : -1;
146 jobGraphMaxima[series] = null;
147 window.recreate = true;
150 if( datum !== 0 && ( jobGraphMaxima[series] === null || jobGraphMaxima[series] < datum ) ) {
151 if( isJobSeriesRescalable(series) ) {
152 // use old maximum to get a scale conversion
153 var scaleConversion = jobGraphMaxima[series]/datum;
154 // set new maximum and rescale the series
155 jobGraphMaxima[series] = datum;
156 rescaleJobGraphSeries( series, scaleConversion );
161 var scaledDatum = null;
162 if( isJobSeriesRescalable(series) && jobGraphMaxima[series] !== null && jobGraphMaxima[series] !== 0 ) {
163 scaledDatum = datum/jobGraphMaxima[series]
167 // identify x axis point, searching from the end of the array (most recent)
169 for( var i = jobGraphData.length - 1; i >= 0; i-- ) {
170 if( jobGraphData[i]['t'] === timestamp ) {
172 jobGraphData[i][series] = scaledDatum;
173 jobGraphData[i]['raw-'+series] = rawDetailData;
175 } else if( jobGraphData[i]['t'] < timestamp ) {
176 // we've gone far enough back in time and this data is supposed to be sorted
180 // index counter from previous loop will have gone one too far, so add one
183 // create a new x point for this previously unrecorded timestamp
184 var entry = { 't': timestamp };
185 entry[series] = scaledDatum;
186 entry['raw-'+series] = rawDetailData;
187 jobGraphData.splice( insertAt, 0, entry );
189 // now let's see about "scrolling" the graph, dropping entries that are too old (>10 minutes)
190 while( jobGraphData.length > 0
191 && (Date.parse( jobGraphData[0]['t'] ) + 10*60000 < Date.parse( jobGraphData[jobGraphData.length-1]['t'] )) ) {
192 shifted.push(jobGraphData.shift());
194 if( shifted.length > 0 ) {
195 // from those that we dropped, are any of them maxima? if so we need to rescale
196 jobGraphSeries.forEach( function(series) {
197 // test that every shifted entry in this series was either not a number (in which case we don't care)
198 // or else approximately (to 2 decimal places) smaller than the scaled maximum (i.e. 1),
199 // because otherwise we just scrolled off something that was a maximum point
200 // and so we need to recalculate a new maximum point by looking at all remaining displayed points in the series
201 if( isJobSeriesRescalable(series) && jobGraphMaxima[series] !== null
202 && !shifted.every( function(e) { return( !$.isNumeric(e[series]) || e[series].toFixed(2) < 1.0 ) } ) ) {
203 // check the remaining displayed points and find the new (scaled) maximum
204 var seriesMax = null;
205 jobGraphData.forEach( function(entry) {
206 if( $.isNumeric(entry[series]) && (seriesMax === null || entry[series] > seriesMax)) {
207 seriesMax = entry[series];
210 if( seriesMax !== null && seriesMax !== 0 ) {
211 // set new actual maximum using the new maximum as the conversion conversion and rescale the series
212 jobGraphMaxima[series] *= seriesMax;
213 var scaleConversion = 1/seriesMax;
214 rescaleJobGraphSeries( series, scaleConversion );
217 // we no longer have any data points displaying for this series
218 jobGraphMaxima[series] = null;
223 // add a 10 minute old null data point to keep the chart honest if the oldest point is less than 9.9 minutes old
224 if( jobGraphData.length > 0 ) {
225 var earliestTimestamp = jobGraphData[0]['t'];
226 var mostRecentTimestamp = jobGraphData[jobGraphData.length-1]['t'];
227 if( (Date.parse( earliestTimestamp ) + 9.9*60000 > Date.parse( mostRecentTimestamp )) ) {
228 var tenMinutesBefore = (new Date(Date.parse( mostRecentTimestamp ) - 600*1000)).toISOString();
229 jobGraphData.unshift( { 't': tenMinutesBefore } );
236 function createJobGraph(elementName) {
238 var emptyGraph = false;
239 if( jobGraphData.length === 0 ) {
240 // If there is no data we still want to show an empty graph,
241 // so add an empty datum and placeholder series to fool it into displaying itself.
242 // Note that when finally a new series is added, the graph will be recreated anyway.
243 jobGraphData.push( {} );
244 jobGraphSeries.push( '' );
247 var graphteristics = {
248 element: elementName,
251 yLabelFormat: function () { return ''; },
253 ykeys: jobGraphSeries,
254 labels: jobGraphSeries,
258 hoverCallback: function(index, options, content) {
259 var s = "<div class='morris-hover-row-label'>";
260 s += options.data[index][options.xkey];
262 for( i = 0; i < jobGraphSortedSeries.length; i++ ) {
263 var sortedIndex = jobGraphSortedSeries[i];
264 var series = options.ykeys[sortedIndex];
265 var datum = options.data[index][series];
267 point += "<div class='morris-hover-point' style='color: ";
268 point += options.lineColors[sortedIndex];
270 var labelMatch = options.labels[sortedIndex].match(/^T(\d+)-(.*)/);
271 point += 'Task ' + labelMatch[1] + ' ' + labelMatch[2];
273 if ( datum !== undefined ) {
274 if( isJobSeriesRescalable( series ) ) {
275 datum *= jobGraphMaxima[series];
277 if( parseFloat(datum) !== 0 ) {
278 if( /-cpu$/.test(series) ){
279 datum = $.number(datum * 100, 1) + '%';
280 } else if( datum < 10 ) {
281 datum = $.number(datum, 2);
283 datum = $.number(datum);
285 if(options.data[index]['raw-'+series]) {
286 datum += ' (' + options.data[index]['raw-'+series] + ')';
300 graphteristics['axes'] = false;
301 graphteristics['parseTime'] = false;
302 graphteristics['hideHover'] = 'always';
304 window.jobGraph = Morris.Line( graphteristics );
311 function rescaleJobGraphSeries( series, scaleConversion ) {
312 if( isJobSeriesRescalable() ) {
313 $.each( jobGraphData, function( i, entry ) {
314 if( entry[series] !== null && entry[series] !== undefined ) {
315 entry[series] *= scaleConversion;
321 // that's right - we never do this for the 'cpu' series, which will always be between 0 and 1 anyway
322 function isJobSeriesRescalable( series ) {
323 return !/-cpu$/.test(series);
326 $(document).on('arv-log-event', '#log_graph_div', function(event, eventData) {
327 if( eventData.properties.text ) {
328 processLogLineForChart( eventData.properties.text );
332 $(document).on('ready ajax:complete', function() {
333 $('#log_graph_div').not('.graph-is-setup').addClass('graph-is-setup').each( function( index, graph_div ) {
334 window.jobGraphData = [];
335 window.jobGraphSeries = [];
336 window.jobGraphSortedSeries = [];
337 window.jobGraphMaxima = {};
338 window.recreate = false;
339 window.redraw = false;
341 createJobGraph($(graph_div).attr('id'));
342 var object_uuid = $(graph_div).data('object-uuid');
343 // if there are any listeners for this object uuid or "all", we will trigger the event
344 var matches = ".arv-log-event-listener[data-object-uuid=\"" + object_uuid + "\"],.arv-log-event-listener[data-object-uuids~=\"" + object_uuid + "\"]";
346 $(document).trigger('ajax:send');
347 $.get('/jobs/' + $(graph_div).data('object-uuid') + '/logs.json', function(data) {
348 data.forEach( function( entry ) {
349 $(matches).trigger('arv-log-event', entry);
353 setInterval( function() {
355 window.recreate = false;
356 window.redraw = false;
357 // series have changed, draw entirely new graph
358 $(graph_div).html('');
359 createJobGraph($(graph_div).attr('id'));
360 } else if( redraw ) {
361 window.redraw = false;
362 jobGraph.setData( jobGraphData );