added averaging support
authorAndrew Tridgell <tridge@samba.org>
Thu, 14 Oct 2010 00:10:39 +0000 (11:10 +1100)
committerAndrew Tridgell <tridge@samba.org>
Thu, 14 Oct 2010 00:10:39 +0000 (11:10 +1100)
live/graphs.js
live/index.html

index 1951f1b106759961c2c6770117827f3cacaa8aed..a9179a2fee78e79c3adeee6fb6d1949322a658ce 100644 (file)
@@ -132,7 +132,7 @@ global_queue = new Array();
 if (is_IE) {
   /* IE is _very_ slow at digraphs, we need bigger pauses to stop
      it complaining */
-  job_delay = 100;
+  job_delay = 10;
 } else {
   job_delay = 10;
 }
@@ -170,9 +170,7 @@ function parse_date(s) {
     /* its a time since midnight */
     var h = (+s.substring(0, 2));
     var m = (+s.substring(3));
-    var d = new Date(pvdate);
-    d.setHours(h);
-    d.setMinutes(m);
+    var d = pvdate_base + 1000*(h*60*60 + m*60);
     return d;
   }
   if (s.search("-") != -1) {
@@ -518,11 +516,13 @@ function load_annotations(g) {
     annotations = [];
     for (var i=0; i<d.data.length; i++) {
       var xval = d.data[i][0] + (tz_difference*60*60*1000);
-      if (xval < pvdate.valueOf() || xval >= (pvdate.valueOf() + (24*60*60*1000))) {
+      xval = round_time(xval, defaultAttrs.averaging);
+      if (xval.valueOf() < pvdate.valueOf() || 
+         xval.valueOf() >= (pvdate.valueOf() + (24*60*60*1000))) {
        continue;
       }
       var ann = {
-      xval: xval,
+      xval: xval.valueOf(),
       series: d.data[i][1],
       shortText: '!',
       text: decodeURIComponent(d.data[i][2])
@@ -625,12 +625,42 @@ defaultAttrs = {
  rollPeriod: 1,
  strokeWidth: 1,
  showRoller: true,
+ averaging: 1,
  annotationMouseOverHandler: annotation_highlight,
  annotationMouseOutHandler: annotation_unhighlight,
  annotationClickHandler: annotation_click,
  pointClickCallback: annotation_add
 };
 
+/*
+  round to averaged time
+ */
+function round_time(t, n) {
+  var t2 = t / (60*1000);
+  t2 = Math.round(t2/n);
+  t2 *= n * 60 * 1000;
+  return new Date(t2);
+}
+
+/*
+  average some data over time
+ */
+function average_data(data, n) {
+  var ret = new Array();
+  for (var y=0; y<data.length; y++) {
+    var y2 = Math.round(y/n);
+    var t = round_time(data[y][0], n);
+    if (ret[y2] == undefined) {
+      ret[y2] = data[y];
+    } else {
+      for (var x=1; x<ret[y2].length; x++) {
+       ret[y2][x] += data[y][x]/n;
+      }
+    }
+    ret[y2][0] = t;
+  }
+  return ret;
+}
 
 /*
   graph results from a set of CSV files:
@@ -737,12 +767,16 @@ function graph_csv_files_func(divname, filenames, columns, func1, func2, attrs)
        }
     }
 
+    var avg_data = average_data(d2.data, defaultAttrs.averaging);
+
     /* create a new dygraph */
-    g = new Dygraph(document.getElementById(divname), d2.data, caller.attrs);
-    g.series_names = caller.attrs.labels;
-    g.divname = divname;
-    g.setAnnotations(annotations);
-    global_graphs.push(g);
+    if (hashvars['nograph'] != '1') {
+      g = new Dygraph(document.getElementById(divname), avg_data, caller.attrs);
+      g.series_names = caller.attrs.labels;
+      g.divname = divname;
+      g.setAnnotations(annotations);
+      global_graphs.push(g);
+    }
 
     loading(false);
   }
@@ -791,6 +825,8 @@ function graph_sum_csv_files(divname, filenames, column, attrs) {
   show all the live data graphs
  */
 function show_graphs() {
+  pvdate_base = pvdate.getTime();
+
   graph_sum_csv_files("Total AC Power (W)",
                      days_csv_files(),
                       "Pac",
index 4b7b7e4f0cbb59ae5f2b6e2f16821a0ac4319868..2aeaba1c4a2771e0fa2a8c0f1636e00f006ccb52 100755 (executable)
@@ -66,9 +66,15 @@ if (hashvars['date'] != undefined) {
 } else {
   pvdate = canberraDate();
 }
+if (is_IE) {
+  defaultAttrs.averaging = 10;
+}
 if (hashvars['roll'] != undefined) {
   defaultAttrs.rollPeriod = (+hashvars['roll']);
 }
+if (hashvars['averaging'] != undefined) {
+  defaultAttrs.averaging = (+hashvars['averaging']);
+}
 serialnums = [ 2001551093, 2001511859, 2001551109, 2001559615, 2001512099, 2001511912 ];
 CSV_directory = "../CSV/";
 setup_datepicker();