4233: redraw graph when processing log events
authorPhil Hodgson <bitbucket@philhodgson.net>
Sat, 15 Nov 2014 12:41:13 +0000 (13:41 +0100)
committerPhil Hodgson <bitbucket@philhodgson.net>
Sat, 15 Nov 2014 12:41:13 +0000 (13:41 +0100)
 - still very naive
 - still does not scale y-axis
 - does not try to limit x axis timeline to 20 minutes: just keeps growing
 - seems intolerably inefficient - optimization on several fronts almost certainly still required:
   - initial draw on first page load should do much less client-side processing
   - at accelerated speeds, it cannot keep up, and if we expect so much data throughput we'd want to devise a way to only redraw once in a while to save needless processor work

apps/workbench/app/assets/javascripts/event_log.js
apps/workbench/app/helpers/jobs_helper.rb
apps/workbench/app/views/jobs/_show_log.html.erb

index c081492a814d0dc6c0e8ff2371c0d88f67c0ac99..10fe5be8f10a024b9bb0133bb18cf9ecd903e663 100644 (file)
@@ -74,18 +74,35 @@ function processLogLineForChart( logLine, jobGraphSeries, jobGraphData ) {
         var datum = (dsum/dt).toFixed(4);
         var preamble = match[1].trim().split(' ');
         var timestamp = preamble[0].replace('_','T');
-        var found = false;
-        for( var i=0; i < jobGraphData.length; i++ ) {
-            if( jobGraphData[i]['t'] == timestamp ) {
-                jobGraphData[i][series] = datum;
-                found = true;
-                break;
-            }
-        }
-        if( !found ){
+        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
index 22d74c131e7f38047bb280e54a2a3f4fe0cff9e4..ec6fccf835105f81e0b9ba418bda400ebd45710b 100644 (file)
@@ -18,9 +18,20 @@ module JobsHelper
     return results
   end
 
-  def stderr_log_records(job_uuids)
-    Log.where(event_type: 'stderr',
-              object_uuid: job_uuids).order('id DESC').results
+  def stderr_log_records(job_uuids, extra_filters = nil)
+    filters = [["event_type",  "=", "stderr"],
+               ["object_uuid", "in", job_uuids]]
+    filters += extra_filters if extra_filters
+    last_entry = Log.order('id DESC').limit(1).filter(filters).results.first
+    if last_entry
+      filters += [["event_at", ">=", last_entry.event_at - 3.minutes]]
+      Log.order('id DESC')
+         .limit(10000)
+         .filter(filters)
+         .results
+    else
+      []
+    end
   end
 
 end
index 251388347d81685e2100992c16a7c9dd14ef0d7c..75edf59bd3bd698c391a36b748b9f47d6cc64572 100644 (file)
@@ -1,6 +1,8 @@
 <% if !@object.log %>
 
-<div id="log_graph_div" data-object-uuid="<%= @object.uuid %>"></div>
+<div id="log_graph_div"
+     class="arv-log-event-listener"
+     data-object-uuid="<%= @object.uuid %>"></div>
 
 <% log_history = stderr_log_history([@object.uuid]) %>
 
@@ -18,8 +20,8 @@
 <%= javascript_tag do %>
   var jobGraphData = [];
   var jobGraphSeries = [];
-  <% stderr_log_records([@object.uuid]).each.with_index do |log_record, index| %>
-    <% puts log_record.to_yaml %>
+  <% stderr_log_records([@object.uuid],[['properties','~','crunchstat:.*-- interval']])
+        .each.with_index do |log_record, index| %>
     var logLine = '<%=j log_record.properties[:text] %>';
     processLogLineForChart( logLine, jobGraphSeries, jobGraphData );
   <% end %>