3781: Merge branch 'master' into 3781-browser-upload
[arvados.git] / apps / workbench / app / assets / javascripts / event_log.js
index bd5e78c8d35f319572c217f26c285312fe338814..29ea74c417cb904f5b5da1cab364c1f1000f2018 100644 (file)
@@ -77,7 +77,7 @@ function processLogLineForChart( logLine ) {
             var datum = null;
 
             // the timestamp comes first
-            var timestamp = match[1].replace('_','T');
+            var timestamp = match[1].replace('_','T') + 'Z';
 
             // we are interested in "-- interval" recordings
             var intervalMatch = match[6].match(/(.*) -- interval (.*)/);
@@ -102,10 +102,12 @@ function processLogLineForChart( logLine ) {
 
                 // special calculation for cpus
                 if( /-cpu$/.test(series) ) {
-                    // divide the stat by the number of cpus
-                    var cpuCountMatch = intervalMatch[1].match(/(\d+) cpus/);
-                    if( cpuCountMatch ) {
-                        datum = datum / cpuCountMatch[1];
+                    // divide the stat by the number of cpus unless the time count is less than the interval length
+                    if( dsum.toFixed(1) > dt.toFixed(1) ) {
+                        var cpuCountMatch = intervalMatch[1].match(/(\d+) cpus/);
+                        if( cpuCountMatch ) {
+                            datum = datum / cpuCountMatch[1];
+                        }
                     }
                 }
 
@@ -188,11 +190,11 @@ function addJobGraphDatum(timestamp, datum, series, rawDetailData) {
         var shifted = [];
         // now let's see about "scrolling" the graph, dropping entries that are too old (>10 minutes)
         while( jobGraphData.length > 0
-                 && (Date.parse( jobGraphData[0]['t'] ).valueOf() + 10*60000 < Date.parse( jobGraphData[jobGraphData.length-1]['t'] ).valueOf()) ) {
+                 && (Date.parse( jobGraphData[0]['t'] ) + 10*60000 < Date.parse( jobGraphData[jobGraphData.length-1]['t'] )) ) {
             shifted.push(jobGraphData.shift());
         }
         if( shifted.length > 0 ) {
-            // from those that we dropped, are any of them maxima? if so we need to rescale
+            // from those that we dropped, were any of them maxima? if so we need to rescale
             jobGraphSeries.forEach( function(series) {
                 // test that every shifted entry in this series was either not a number (in which case we don't care)
                 // or else approximately (to 2 decimal places) smaller than the scaled maximum (i.e. 1),
@@ -221,10 +223,13 @@ function addJobGraphDatum(timestamp, datum, series, rawDetailData) {
             });
         }
         // add a 10 minute old null data point to keep the chart honest if the oldest point is less than 9.9 minutes old
-        if( jobGraphData.length > 0
-              && (Date.parse( jobGraphData[0]['t'] ).valueOf() + 9.9*60000 > Date.parse( jobGraphData[jobGraphData.length-1]['t'] ).valueOf()) ) {
-            var tenMinutesBefore = (new Date(Date.parse( jobGraphData[jobGraphData.length-1]['t'] ).valueOf() - 600*1000)).toISOString().replace('Z','');
-            jobGraphData.unshift( { 't': tenMinutesBefore } );
+        if( jobGraphData.length > 0 ) {
+            var earliestTimestamp = jobGraphData[0]['t'];
+            var mostRecentTimestamp = jobGraphData[jobGraphData.length-1]['t'];
+            if( (Date.parse( earliestTimestamp ) + 9.9*60000 > Date.parse( mostRecentTimestamp )) ) {
+                var tenMinutesBefore = (new Date(Date.parse( mostRecentTimestamp ) - 600*1000)).toISOString();
+                jobGraphData.unshift( { 't': tenMinutesBefore } );
+            }
         }
     }
 
@@ -262,7 +267,7 @@ function createJobGraph(elementName) {
                 var datum = options.data[index][series];
                 var point = ''
                 point += "<div class='morris-hover-point' style='color: ";
-                point += options.lineColors[sortedIndex];
+                point += options.lineColors[sortedIndex % options.lineColors.length];
                 point += "'>";
                 var labelMatch = options.labels[sortedIndex].match(/^T(\d+)-(.*)/);
                 point += 'Task ' + labelMatch[1] + ' ' + labelMatch[2];
@@ -322,7 +327,9 @@ function isJobSeriesRescalable( series ) {
 
 $(document).on('arv-log-event', '#log_graph_div', function(event, eventData) {
     if( eventData.properties.text ) {
-        processLogLineForChart( eventData.properties.text );
+        eventData.properties.text.split('\n').forEach( function( logLine ) {
+            processLogLineForChart( logLine );
+        } );
     }
 } );
 
@@ -336,9 +343,16 @@ $(document).on('ready ajax:complete', function() {
         window.redraw = false;
 
         createJobGraph($(graph_div).attr('id'));
+        var object_uuid = $(graph_div).data('object-uuid');
+        // if there are any listeners for this object uuid or "all", we will trigger the event
+        var matches = ".arv-log-event-listener[data-object-uuid=\"" + object_uuid + "\"],.arv-log-event-listener[data-object-uuids~=\"" + object_uuid + "\"]";
 
         $(document).trigger('ajax:send');
-        $.get('/jobs/' + $(graph_div).data('object-uuid') + '/push_logs.js');
+        $.get('/jobs/' + $(graph_div).data('object-uuid') + '/logs.json', function(data) {
+            data.forEach( function( entry ) {
+                $(matches).trigger('arv-log-event', entry);
+            });
+        });
 
         setInterval( function() {
             if( recreate ) {