19744: Better incorporation of the text report data into HTML
[arvados.git] / tools / crunchstat-summary / crunchstat_summary / dygraphs.js
index 6a6225de6f4068c92b19ffcbd55fc3c27c019da4..0f28bc59322f70fe72fb612b2dd4264434be3f88 100644 (file)
@@ -13,22 +13,37 @@ window.onload = function() {
             return y.toFixed(2).replace(/\.0+$/, '')+s;
         },
         time: function(s) {
-            var u='s';
-            if (s < 60) return s;
-            var u = 'm'+(s%60)+'s';
-            var m = Math.floor(s/60);
-            if (m < 60) return ''+m+u;
-            u = 'h'+(m%60)+u;
-            var h = Math.floor(m/60);
-            if (h < 24) return ''+h+u;
-            u = 'd'+(h%24)+s;
-            return ''+Math.floor(h/24)+u;
+            var ret = ''
+            if (s >= 86400) ret += Math.floor(s/86400) + 'd'
+            if (s >= 3600) ret += Math.floor(s/3600)%24 + 'h'
+            if (s >= 60) ret += Math.floor(s/60)%60 + 'm'
+            ret += Math.floor(s)%60 + 's'
+            // finally, strip trailing zeroes: 1d0m0s -> 1d
+            return ret.replace(/(\D)(0\D)*$/, '$1')
+        },
+    }
+    var ticker = {
+        time: function(min, max, pixels, opts, dg) {
+            var max_ticks = Math.floor(pixels / opts('pixelsPerLabel'))
+            var natural = [1, 5, 10, 30, 60,
+                           120, 300, 600, 1800, 3600,
+                           7200, 14400, 43200, 86400]
+            var interval = natural.shift()
+            while (max>min && (max-min)/interval > max_ticks) {
+                interval = natural.shift() || (interval * 2)
+            }
+            var ticks = []
+            for (var i=Math.ceil(min/interval)*interval; i<=max; i+=interval) {
+                ticks.push({v: i, label: fmt.time(i)})
+            }
+            return ticks
         },
     }
     chartdata.forEach(function(section, section_idx) {
-        var h1 = document.createElement('h1');
-        h1.appendChild(document.createTextNode(section.label));
-        document.body.appendChild(h1);
+        //var h1 = document.createElement('h1');
+        //h1.appendChild(document.createTextNode(section.label));
+        //document.body.appendChild(h1);
+        var chartDiv = document.getElementById("chart");
         section.charts.forEach(function(chart, chart_idx) {
             // Skip chart if every series has zero data points
             if (0 == chart.data.reduce(function(len, series) {
@@ -40,13 +55,14 @@ window.onload = function() {
             var div = document.createElement('div');
             div.setAttribute('id', id);
             div.setAttribute('style', 'width: 100%; height: 150px');
-            document.body.appendChild(div);
+            chartDiv.appendChild(div);
             chart.options.valueFormatter = function(y) {
             }
             chart.options.axes = {
                 x: {
                     axisLabelFormatter: fmt.time,
                     valueFormatter: fmt.time,
+                    ticker: ticker.time,
                 },
                 y: {
                     axisLabelFormatter: fmt.iso,
@@ -57,6 +73,8 @@ window.onload = function() {
         });
     });
 
+    var sync = Dygraph.synchronize(Object.values(charts), {range: false});
+
     if (typeof window.debug === 'undefined')
         window.debug = {};
     window.debug.charts = charts;