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');
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]);
92 rawDetailData = intervalMatch[2];
94 // for the series name use the task number (4th term) and then the first word after 'crunchstat:'
95 var series = 'T' + match[4] + '-' + match[5];
97 // special calculation for cpus
98 if( /-cpu$/.test(series) ) {
99 // divide the stat by the number of cpus
100 var cpuCountMatch = intervalMatch[1].match(/(\d+) cpus/);
101 if( cpuCountMatch ) {
102 datum = datum / cpuCountMatch[1];
106 addJobGraphDatum( timestamp, datum, series, rawDetailData );
108 // we are also interested in memory ("mem") recordings
109 var memoryMatch = match[6].match(/(\d+) cache (\d+) swap (\d+) pgmajfault (\d+) rss/);
111 rawDetailData = match[6];
112 // one datapoint for rss and one for swap - only show the rawDetailData for rss
113 addJobGraphDatum( timestamp, parseInt(memoryMatch[4]), 'T' + match[4] + "-rss", rawDetailData );
114 addJobGraphDatum( timestamp, parseInt(memoryMatch[2]), 'T' + match[4] + "-swap", '' );
121 window.redraw = true;
124 console.log( 'Ignoring error trying to process log line: ' + err);
128 function addJobGraphDatum(timestamp, datum, series, rawDetailData) {
129 // check for new series
130 if( $.inArray( series, jobGraphSeries ) < 0 ) {
131 var newIndex = jobGraphSeries.push(series) - 1;
132 jobGraphSortedSeries.push(newIndex);
133 jobGraphSortedSeries.sort( function(a,b) {
134 var matchA = jobGraphSeries[a].match(/^T(\d+)-(.*)/);
135 var matchB = jobGraphSeries[b].match(/^T(\d+)-(.*)/);
136 var termA = ('000000' + matchA[1]).slice(-6) + matchA[2];
137 var termB = ('000000' + matchB[1]).slice(-6) + matchB[2];
138 return termA > termB ? 1 : -1;
140 jobGraphMaxima[series] = null;
141 window.recreate = true;
144 if( datum !== 0 && ( jobGraphMaxima[series] === null || jobGraphMaxima[series] < datum ) ) {
145 if( isJobSeriesRescalable(series) ) {
146 // use old maximum to get a scale conversion
147 var scaleConversion = jobGraphMaxima[series]/datum;
148 // set new maximum and rescale the series
149 jobGraphMaxima[series] = datum;
150 rescaleJobGraphSeries( series, scaleConversion );
155 var scaledDatum = null;
156 if( isJobSeriesRescalable(series) && jobGraphMaxima[series] !== null && jobGraphMaxima[series] !== 0 ) {
157 scaledDatum = datum/jobGraphMaxima[series]
161 // identify x axis point, searching from the end of the array (most recent)
163 for( var i = jobGraphData.length - 1; i >= 0; i-- ) {
164 if( jobGraphData[i]['t'] === timestamp ) {
166 jobGraphData[i][series] = scaledDatum;
167 jobGraphData[i]['raw-'+series] = rawDetailData;
169 } else if( jobGraphData[i]['t'] < timestamp ) {
170 // we've gone far enough back in time and this data is supposed to be sorted
174 // index counter from previous loop will have gone one too far, so add one
177 // create a new x point for this previously unrecorded timestamp
178 var entry = { 't': timestamp };
179 entry[series] = scaledDatum;
180 entry['raw-'+series] = rawDetailData;
181 jobGraphData.splice( insertAt, 0, entry );
183 // now let's see about "scrolling" the graph, dropping entries that are too old (>10 minutes)
184 while( jobGraphData.length > 0
185 && (Date.parse( jobGraphData[0]['t'] ).valueOf() + 10*60000 < Date.parse( jobGraphData[jobGraphData.length-1]['t'] ).valueOf()) ) {
186 shifted.push(jobGraphData.shift());
188 if( shifted.length > 0 ) {
189 // from those that we dropped, are any of them maxima? if so we need to rescale
190 jobGraphSeries.forEach( function(series) {
191 // test that every shifted entry in this series was either not a number (in which case we don't care)
192 // or else approximately (to 2 decimal places) smaller than the scaled maximum (i.e. 1),
193 // because otherwise we just scrolled off something that was a maximum point
194 // and so we need to recalculate a new maximum point by looking at all remaining displayed points in the series
195 if( isJobSeriesRescalable(series) && jobGraphMaxima[series] !== null
196 && !shifted.every( function(e) { return( !$.isNumeric(e[series]) || e[series].toFixed(2) < 1.0 ) } ) ) {
197 // check the remaining displayed points and find the new (scaled) maximum
198 var seriesMax = null;
199 jobGraphData.forEach( function(entry) {
200 if( $.isNumeric(entry[series]) && (seriesMax === null || entry[series] > seriesMax)) {
201 seriesMax = entry[series];
204 if( seriesMax !== null && seriesMax !== 0 ) {
205 // set new actual maximum using the new maximum as the conversion conversion and rescale the series
206 jobGraphMaxima[series] *= seriesMax;
207 var scaleConversion = 1/seriesMax;
208 rescaleJobGraphSeries( series, scaleConversion );
211 // we no longer have any data points displaying for this series
212 jobGraphMaxima[series] = null;
217 // add a 10 minute old null data point to keep the chart honest if the oldest point is less than 9.9 minutes old
218 if( jobGraphData.length > 0
219 && (Date.parse( jobGraphData[0]['t'] ).valueOf() + 9.9*60000 > Date.parse( jobGraphData[jobGraphData.length-1]['t'] ).valueOf()) ) {
220 var tenMinutesBefore = (new Date(Date.parse( jobGraphData[jobGraphData.length-1]['t'] ).valueOf() - 600*1000)).toISOString().replace('Z','');
221 jobGraphData.unshift( { 't': tenMinutesBefore } );
227 function createJobGraph(elementName) {
229 var emptyGraph = false;
230 if( jobGraphData.length === 0 ) {
231 // If there is no data we still want to show an empty graph,
232 // so add an empty datum and placeholder series to fool it into displaying itself.
233 // Note that when finally a new series is added, the graph will be recreated anyway.
234 jobGraphData.push( {} );
235 jobGraphSeries.push( '' );
238 var graphteristics = {
239 element: elementName,
242 yLabelFormat: function () { return ''; },
244 ykeys: jobGraphSeries,
245 labels: jobGraphSeries,
249 hoverCallback: function(index, options, content) {
250 var s = "<div class='morris-hover-row-label'>";
251 s += options.data[index][options.xkey];
253 for( i = 0; i < jobGraphSortedSeries.length; i++ ) {
254 var sortedIndex = jobGraphSortedSeries[i];
255 var series = options.ykeys[sortedIndex];
256 var datum = options.data[index][series];
258 point += "<div class='morris-hover-point' style='color: ";
259 point += options.lineColors[sortedIndex];
261 var labelMatch = options.labels[sortedIndex].match(/^T(\d+)-(.*)/);
262 point += 'Task ' + labelMatch[1] + ' ' + labelMatch[2];
264 if ( datum !== undefined ) {
265 if( isJobSeriesRescalable( series ) ) {
266 datum *= jobGraphMaxima[series];
268 if( parseFloat(datum) !== 0 ) {
269 if( /-cpu$/.test(series) ){
270 datum = $.number(datum * 100, 1) + '%';
271 } else if( datum < 10 ) {
272 datum = $.number(datum, 2);
274 datum = $.number(datum);
276 datum += ' (' + options.data[index]['raw-'+series] + ')';
289 graphteristics['axes'] = false;
290 graphteristics['parseTime'] = false;
291 graphteristics['hideHover'] = 'always';
293 window.jobGraph = Morris.Line( graphteristics );
300 function rescaleJobGraphSeries( series, scaleConversion ) {
301 if( isJobSeriesRescalable() ) {
302 $.each( jobGraphData, function( i, entry ) {
303 if( entry[series] !== null && entry[series] !== undefined ) {
304 entry[series] *= scaleConversion;
310 // that's right - we never do this for the 'cpu' series, which will always be between 0 and 1 anyway
311 function isJobSeriesRescalable( series ) {
312 return !/-cpu$/.test(series);
315 $(document).on('arv-log-event', '#log_graph_div', function(event, eventData) {
316 if( eventData.properties.text ) {
317 processLogLineForChart( eventData.properties.text );
321 $(document).on('ready', function(){
322 window.recreate = false;
323 window.redraw = false;
324 setInterval( function() {
326 window.recreate = false;
327 window.redraw = false;
328 // series have changed, draw entirely new graph
329 $('#log_graph_div').html('');
330 createJobGraph('log_graph_div');
331 } else if( redraw ) {
332 window.redraw = false;
333 jobGraph.setData( jobGraphData );