4233: in hover popup only show datapoints that are defined
[arvados.git] / apps / workbench / app / assets / javascripts / event_log.js
1 /*
2  * This js establishes a websockets connection with the API Server.
3  */
4
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) {
10         return;
11     }
12
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)
19             return;
20
21         event_log_disp = new WebSocket(websocket_url);
22
23         event_log_disp.onopen = onEventLogDispatcherOpen;
24         event_log_disp.onmessage = onEventLogDispatcherMessage;
25
26         // store websocket in window to allow reuse when multiple divs subscribe for events
27         $(window).data("arv-websocket", event_log_disp);
28     }
29 }
30
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"}');
35 }
36
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;
41
42     if (!object_uuid) {
43         return;
44     }
45
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);
49 }
50
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');
55     if (a.length > 0) {
56         subscribeToEventLog();
57     }
58 });
59
60 /* Assumes existence of:
61   window.jobGraphData = [];
62   window.jobGraphSeries = [];
63   window.jobGraphSortedSeries = [];
64   window.jobGraphMaxima = {};
65  */
66 function processLogLineForChart( logLine ) {
67     try {
68         var match = logLine.match(/^(\S+) (\S+) (\S+) (\S+) stderr crunchstat: (\S+) (.*)/);
69         if( !match ) {
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+) (.*)/);
71             if( match ) {
72                 match[1] = (new Date(match[1] + ' UTC')).toISOString().replace('Z','');
73             }
74         }
75         if( match ) {
76             var rawDetailData = '';
77             var datum = null;
78
79             // the timestamp comes first
80             var timestamp = match[1].replace('_','T');
81
82             // we are interested in "-- interval" recordings
83             var intervalMatch = match[6].match(/(.*) -- interval (.*)/);
84             if( intervalMatch ) {
85                 var intervalData = intervalMatch[2].trim().split(' ');
86                 var dt = parseFloat(intervalData[0]);
87                 var dsum = 0.0;
88                 for(var i=2; i < intervalData.length; i += 2 ) {
89                     dsum += parseFloat(intervalData[i]);
90                 }
91                 datum = dsum/dt;
92                 rawDetailData = intervalMatch[2];
93
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];
96
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];
103                     }
104                 }
105
106                 addJobGraphDatum( timestamp, datum, series, rawDetailData );
107             } else {
108                 // we are also interested in memory ("mem") recordings
109                 var memoryMatch = match[6].match(/(\d+) cache (\d+) swap (\d+) pgmajfault (\d+) rss/);
110                 if( memoryMatch ) {
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", '' );
115                 } else {
116                     // not interested
117                     return;
118                 }
119             }
120
121             window.redraw = true;
122         }
123     } catch( err ) {
124         console.log( 'Ignoring error trying to process log line: ' + err);
125     }
126 }
127
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;
139         });
140         jobGraphMaxima[series] = null;
141         window.recreate = true;
142     }
143
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 );
151         }
152     }
153
154     // scale
155     var scaledDatum = null;
156     if( isJobSeriesRescalable(series) && jobGraphMaxima[series] !== null && jobGraphMaxima[series] !== 0 ) {
157         scaledDatum = datum/jobGraphMaxima[series]
158     } else {
159         scaledDatum = datum;
160     }
161     // identify x axis point, searching from the end of the array (most recent)
162     var found = false;
163     for( var i = jobGraphData.length - 1; i >= 0; i-- ) {
164         if( jobGraphData[i]['t'] === timestamp ) {
165             found = true;
166             jobGraphData[i][series] = scaledDatum;
167             jobGraphData[i]['raw-'+series] = rawDetailData;
168             break;
169         } else if( jobGraphData[i]['t'] < timestamp  ) {
170             // we've gone far enough back in time and this data is supposed to be sorted
171             break;
172         }
173     }
174     // index counter from previous loop will have gone one too far, so add one
175     var insertAt = i+1;
176     if(!found) {
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 );
182         var shifted = [];
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());
187         }
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];
202                         }
203                     });
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 );
209                     }
210                     else {
211                         // we no longer have any data points displaying for this series
212                         jobGraphMaxima[series] = null;
213                     }
214                 }
215             });
216         }
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 } );
222         }
223     }
224
225 }
226
227 function createJobGraph(elementName) {
228     delete jobGraph;
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( '' );
236         emptyGraph = true;
237     }
238     var graphteristics = {
239         element: elementName,
240         data: jobGraphData,
241         ymax: 1.0,
242         yLabelFormat: function () { return ''; },
243         xkey: 't',
244         ykeys: jobGraphSeries,
245         labels: jobGraphSeries,
246         resize: true,
247         hideHover: 'auto',
248         parseTime: true,
249         hoverCallback: function(index, options, content) {
250             var s = "<div class='morris-hover-row-label'>";
251             s += options.data[index][options.xkey];
252             s += "</div> ";
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];
257                 var point = ''
258                 point += "<div class='morris-hover-point' style='color: ";
259                 point += options.lineColors[sortedIndex];
260                 point += "'>";
261                 var labelMatch = options.labels[sortedIndex].match(/^T(\d+)-(.*)/);
262                 point += 'Task ' + labelMatch[1] + ' ' + labelMatch[2];
263                 point += ": ";
264                 if ( datum !== undefined ) {
265                     if( isJobSeriesRescalable( series ) ) {
266                         datum *= jobGraphMaxima[series];
267                     }
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);
273                         } else {
274                             datum = $.number(datum);
275                         }
276                         datum += ' (' + options.data[index]['raw-'+series] + ')';
277                     }
278                     point += datum;
279                 } else {
280                     continue;
281                 }
282                 point += "</div> ";
283                 s += point;
284             }
285             return s;
286         }
287     }
288     if( emptyGraph ) {
289         graphteristics['axes'] = false;
290         graphteristics['parseTime'] = false;
291         graphteristics['hideHover'] = 'always';
292     }
293     window.jobGraph = Morris.Line( graphteristics );
294     if( emptyGraph ) {
295         jobGraphData = [];
296         jobGraphSeries = [];
297     }
298 }
299
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;
305             }
306         });
307     }
308 }
309
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);
313 }
314
315 $(document).on('arv-log-event', '#log_graph_div', function(event, eventData) {
316     if( eventData.properties.text ) {
317         processLogLineForChart( eventData.properties.text );
318     }
319 } );
320
321 $(document).on('ready', function(){
322     window.recreate = false;
323     window.redraw = false;
324     setInterval( function() {
325         if( recreate ) {
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 );
334         }
335     }, 5000);
336 });