Merge branch '11842-crunchstat-summary-dygraphs'
[arvados.git] / tools / crunchstat-summary / crunchstat_summary / dygraphs.js
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 window.onload = function() {
6     var charts = {};
7     var fmt = {
8         iso: function(y) {
9             var s='';
10             if (y > 1000000000) { y=y/1000000000; s='G'; }
11             else if (y > 1000000) { y=y/1000000; s='M'; }
12             else if (y > 1000) { y=y/1000; s='K'; }
13             return y.toFixed(2).replace(/\.0+$/, '')+s;
14         },
15         time: function(s) {
16             var u='s';
17             if (s < 60) return s;
18             var u = 'm'+(s%60)+'s';
19             var m = Math.floor(s/60);
20             if (m < 60) return ''+m+u;
21             u = 'h'+(m%60)+u;
22             var h = Math.floor(m/60);
23             if (h < 24) return ''+h+u;
24             u = 'd'+(h%24)+s;
25             return ''+Math.floor(h/24)+u;
26         },
27     }
28     chartdata.forEach(function(section, section_idx) {
29         var h1 = document.createElement('h1');
30         h1.appendChild(document.createTextNode(section.label));
31         document.body.appendChild(h1);
32         section.charts.forEach(function(chart, chart_idx) {
33             // Skip chart if every series has zero data points
34             if (0 == chart.data.reduce(function(len, series) {
35                 return len + series.length;
36             }, 0)) {
37                 return;
38             }
39             var id = 'chart-'+section_idx+'-'+chart_idx;
40             var div = document.createElement('div');
41             div.setAttribute('id', id);
42             div.setAttribute('style', 'width: 100%; height: 150px');
43             document.body.appendChild(div);
44             chart.options.valueFormatter = function(y) {
45             }
46             chart.options.axes = {
47                 x: {
48                     axisLabelFormatter: fmt.time,
49                     valueFormatter: fmt.time,
50                 },
51                 y: {
52                     axisLabelFormatter: fmt.iso,
53                     valueFormatter: fmt.iso,
54                 },
55             }
56             charts[id] = new Dygraph(div, chart.data, chart.options);
57         });
58     });
59
60     if (typeof window.debug === 'undefined')
61         window.debug = {};
62     window.debug.charts = charts;
63 };