Merge branch '4525-remove-stale-clippy-code-TC' into 4525-remove-stale-clippy-code
[arvados.git] / apps / workbench / app / assets / javascripts / event_log.js
index 22bcc9d860ccbfadaf583576ae263a74e2a95899..29ea74c417cb904f5b5da1cab364c1f1000f2018 100644 (file)
@@ -65,9 +65,9 @@ $(document).on('ajax:complete ready', function() {
  */
 function processLogLineForChart( logLine ) {
     try {
-        var match = logLine.match(/(\S+) (\S+) (\S+) (\S+) stderr crunchstat: (\S+) (.*)/);
+        var match = logLine.match(/^(\S+) (\S+) (\S+) (\S+) stderr crunchstat: (\S+) (.*)/);
         if( !match ) {
-            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+) (.*) -- interval (.*)/);
+            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+) (.*)/);
             if( match ) {
                 match[1] = (new Date(match[1] + ' UTC')).toISOString().replace('Z','');
             }
@@ -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 (.*)/);
@@ -89,6 +89,12 @@ function processLogLineForChart( logLine ) {
                     dsum += parseFloat(intervalData[i]);
                 }
                 datum = dsum/dt;
+
+                if( datum < 0 ) {
+                    // not interested in negative deltas
+                    return;
+                }
+
                 rawDetailData = intervalMatch[2];
 
                 // for the series name use the task number (4th term) and then the first word after 'crunchstat:'
@@ -96,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];
+                        }
                     }
                 }
 
@@ -182,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),
@@ -215,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 } );
+            }
         }
     }
 
@@ -254,12 +265,13 @@ function createJobGraph(elementName) {
                 var sortedIndex = jobGraphSortedSeries[i];
                 var series = options.ykeys[sortedIndex];
                 var datum = options.data[index][series];
-                s += "<div class='morris-hover-point' style='color: ";
-                s += options.lineColors[sortedIndex];
-                s += "'>";
+                var point = ''
+                point += "<div class='morris-hover-point' style='color: ";
+                point += options.lineColors[sortedIndex % options.lineColors.length];
+                point += "'>";
                 var labelMatch = options.labels[sortedIndex].match(/^T(\d+)-(.*)/);
-                s += 'Task ' + labelMatch[1] + ' ' + labelMatch[2];
-                s += ": ";
+                point += 'Task ' + labelMatch[1] + ' ' + labelMatch[2];
+                point += ": ";
                 if ( datum !== undefined ) {
                     if( isJobSeriesRescalable( series ) ) {
                         datum *= jobGraphMaxima[series];
@@ -272,13 +284,16 @@ function createJobGraph(elementName) {
                         } else {
                             datum = $.number(datum);
                         }
-                        datum += ' (' + options.data[index]['raw-'+series] + ')';
+                        if(options.data[index]['raw-'+series]) {
+                            datum += ' (' + options.data[index]['raw-'+series] + ')';
+                        }
                     }
-                    s += datum;
+                    point += datum;
                 } else {
-                    s += '-';
+                    continue;
                 }
-                s += "</div> ";
+                point += "</div> ";
+                s += point;
             }
             return s;
         }
@@ -312,23 +327,44 @@ 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 );
+        } );
     }
 } );
 
-$(document).on('ready', function(){
-    window.recreate = false;
-    window.redraw = false;
-    setInterval( function() {
-        if( recreate ) {
-            window.recreate = false;
-            window.redraw = false;
-            // series have changed, draw entirely new graph
-            $('#log_graph_div').html('');
-            createJobGraph('log_graph_div');
-        } else if( redraw ) {
-            window.redraw = false;
-            jobGraph.setData( jobGraphData );
-        }
-    }, 5000);
+$(document).on('ready ajax:complete', function() {
+    $('#log_graph_div').not('.graph-is-setup').addClass('graph-is-setup').each( function( index, graph_div ) {
+        window.jobGraphData = [];
+        window.jobGraphSeries = [];
+        window.jobGraphSortedSeries = [];
+        window.jobGraphMaxima = {};
+        window.recreate = false;
+        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') + '/logs.json', function(data) {
+            data.forEach( function( entry ) {
+                $(matches).trigger('arv-log-event', entry);
+            });
+        });
+
+        setInterval( function() {
+            if( recreate ) {
+                window.recreate = false;
+                window.redraw = false;
+                // series have changed, draw entirely new graph
+                $(graph_div).html('');
+                createJobGraph($(graph_div).attr('id'));
+            } else if( redraw ) {
+                window.redraw = false;
+                jobGraph.setData( jobGraphData );
+            }
+        }, 5000);
+    });
 });