Merge branch '8784-dir-listings'
[arvados.git] / apps / workbench / app / assets / javascripts / event_log.js
index 27a88fea5551fc98a94d105c7a629784b2a35e5a..e576ba97a35785ec72dd4c68c9b7dd69e6cff099 100644 (file)
@@ -1,3 +1,7 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
 /*
  * This js establishes a websockets connection with the API Server.
  */
@@ -56,161 +60,3 @@ $(document).on('ajax:complete ready', function() {
         subscribeToEventLog();
     }
 });
-
-/* Assumes existence of:
-  window.jobGraphData = [];
-  window.jobGraphSeries = [];
-  window.jobGraphMaxima = {};
- */
-function processLogLineForChart( logLine ) {
-    var recreate = false;
-    var rescale = false;
-    // TODO: make this more robust: anything could go wrong in here
-    var match = logLine.match(/(.*)crunchstat:(.*)-- interval(.*)/);
-    if( match ) {
-        var series = match[2].trim().split(' ')[0];
-        if( $.inArray( series, jobGraphSeries) < 0 ) {
-            jobGraphSeries.push(series);
-            jobGraphMaxima[series] = null;
-            recreate = true;
-        }
-        var intervalData = match[3].trim().split(' ');
-        var dt = parseFloat(intervalData[0]);
-        var dsum = 0.0;
-        for(var i=2; i < intervalData.length; i += 2 ) {
-            dsum += parseFloat(intervalData[i]);
-        }
-        var datum = dsum/dt;
-        if( datum !== 0 && ( jobGraphMaxima[series] === null || jobGraphMaxima[series] < datum ) ) {
-            if( isJobSeriesRescalable(series) ) {
-                // use old maximum to get a scale conversion
-                var scaleConversion = jobGraphMaxima[series]/datum;
-                // set new maximum and rescale the series
-                jobGraphMaxima[series] = datum;
-                rescaleJobGraphSeries( series, scaleConversion );
-            }
-            // and special calculation for cpus
-            if( series === 'cpu' ) {
-                var cpuCountMatch = match[2].match(/(\d+) cpus/);
-                if( cpuCountMatch ) {
-                    datum = datum / cpuCountMatch[1];
-                }
-            }
-        }
-        // scale
-        // FIXME: what about negative numbers?
-        var scaledDatum = null;
-        if( isJobSeriesRescalable(series) && jobGraphMaxima[series] !== null && jobGraphMaxima[series] !== 0 ) {
-            scaledDatum = datum/jobGraphMaxima[series]
-        } else {
-            scaledDatum = datum;
-        }
-        // more parsing
-        var preamble = match[1].trim().split(' ');
-        var timestamp = preamble[0].replace('_','T');
-        // identify x axis point
-        var found = false;
-        for( var i = jobGraphData.length - 1; i >= 0; i-- ) {
-            if( jobGraphData[i]['t'] === timestamp ) {
-                found = true;
-                jobGraphData[i][series] = scaledDatum;
-                break;
-            } else if( jobGraphData[i]['t'] < timestamp  ) {
-                // we've gone far enough back in time and this data is supposed to be sorted
-                break;
-            }
-        }
-        // index counter from previous loop will have gone one too far, so add one
-        var insertAt = i+1;
-        if(!found) {
-            // create a new x point for this previously unrecorded timestamp
-            var entry = { 't': timestamp };
-            entry[series] = scaledDatum;
-            jobGraphData.splice( insertAt, 0, entry );
-            var shifted = [];
-            // now let's see about "scrolling" the graph, dropping entries that are too old (>3 minutes)
-            while( jobGraphData.length > 0
-                     && (Date.parse( jobGraphData[0]['t'] ).valueOf() + 3*60000 < Date.parse( jobGraphData[jobGraphData.length-1]['t'] ).valueOf()) ) {
-                shifted.push(jobGraphData.shift());
-            }
-            if( shifted.length > 0 ) {
-                // from those that we dropped, are 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 smaller than the scaled maximum (i.e. 1), because otherwise we just scrolled off something that was a maximum point
-                    // and so we need to recalculate a new maximum point by looking at all remaining displayed points in the series
-                    if( isJobSeriesRescalable(series) && jobGraphMaxima[series] !== null
-                          && !shifted.every( function(e) { return( !($.isNumeric(e[series])) || e[series] < 1 ) } ) ) {
-                        // check the remaining displayed points and find the new (scaled) maximum
-                        var seriesMax = null;
-                        jobGraphData.forEach( function(entry) {
-                            if( $.isNumeric(entry[series]) && (seriesMax === null || entry[series] > seriesMax)) {
-                                seriesMax = entry[series];
-                            }
-                        });
-                        if( seriesMax !== null && seriesMax !== 0 ) {
-                            // set new actual maximum using the new maximum as the conversion conversion and rescale the series
-                            jobGraphMaxima[series] *= seriesMax;
-                            var scaleConversion = 1/seriesMax;
-                            rescaleJobGraphSeries( series, scaleConversion );
-                        }
-                        else {
-                            // we no longer have any data points displaying for this series
-                            jobGraphMaxima[series] = null;
-                        }
-                    }
-                });
-            }
-        }
-    }
-    return recreate;
-}
-
-function rescaleJobGraphSeries( series, scaleConversion ) {
-    if( isJobSeriesRescalable() ) {
-        $.each( jobGraphData, function( i, entry ) {
-            if( entry[series] !== null && entry[series] !== undefined ) {
-                entry[series] *= scaleConversion;
-            }
-        });
-    }
-}
-
-// that's right - we never do this for the 'cpu' series, which will always be between 0 and 1 anyway
-function isJobSeriesRescalable( series ) {
-    return series != 'cpu';
-}
-
-$(document).on('arv-log-event', '#log_graph_div', function(event, eventData) {
-    if( eventData.properties.text ) {
-        var causeRecreate = processLogLineForChart( eventData.properties.text );
-        if( causeRecreate && !window.recreate ) {
-            window.recreate = true;
-        } else {
-            window.redraw = true;
-        }
-    }
-} );
-
-$(document).on('ready', function(){
-    window.recreate = false;
-    window.redraw = false;
-    setInterval( function() {
-        if( recreate ) {
-            window.recreate = false;
-            // series have changed, draw entirely new graph
-            $('#log_graph_div').html('');
-            window.jobGraph = Morris.Line({
-                element: 'log_graph_div',
-                data: jobGraphData,
-                ymax: 1.0,
-                xkey: 't',
-                ykeys: jobGraphSeries,
-                labels: jobGraphSeries
-            });
-        } else if( redraw ) {
-            window.redraw = false;
-            jobGraph.setData( jobGraphData );
-        }
-    }, 5000);
-});