+
+/* Automatically connect if there are any elements on the page that want to
+ receive event log events. */
+$(document).on('ajax:complete ready', function() {
+ var a = $('.arv-log-event-listener');
+ if (a.length > 0) {
+ subscribeToEventLog();
+ }
+});
+
+
+function processLogLineForChart( logLine, jobGraphSeries, jobGraphData ) {
+ var match = logLine.match(/(.*)crunchstat:(.*)-- interval(.*)/);
+ if( match ) {
+ var series = match[2].trim().split(' ')[0];
+ if( $.inArray( series, jobGraphSeries) < 0 ) {
+ jobGraphSeries.push(series);
+ }
+ 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).toFixed(4);
+ var preamble = match[1].trim().split(' ');
+ var timestamp = preamble[0].replace('_','T');
+ var xpoints = $.grep( jobGraphData, function(e){ return e['t'] === timestamp; });
+ if(xpoints.length) {
+ // xpoints[0] is the x point that that matched the timestamp and so already existed: add the new datum
+ xpoints[0][series] = datum;
+ } else {
+ var entry = { 't': timestamp };
+ entry[series] = datum;
+ jobGraphData.push( entry );
+ }
+ }
+}
+
+$(document).on('arv-log-event', '#log_graph_div', function(event, eventData) {
+ if( eventData.properties.text ) {
+ var series_length = jobGraphSeries.length;
+ processLogLineForChart( eventData.properties.text, jobGraphSeries, jobGraphData);
+ if( series_length < jobGraphSeries.length) {
+ // series have changed, draw entirely new graph
+ $('#log_graph_div').html('');
+ jobGraph = Morris.Line({
+ element: 'log_graph_div',
+ data: jobGraphData,
+ xkey: 't',
+ ykeys: jobGraphSeries,
+ labels: jobGraphSeries
+ });
+ } else {
+ jobGraph.setData( jobGraphData );
+ }
+ }
+
+} );
\ No newline at end of file