improved graphs - the range is now based on the current time instead of the last...
authorForrest Voight <forrest@forre.st>
Sat, 17 Mar 2012 05:18:00 +0000 (01:18 -0400)
committerForrest Voight <forrest@forre.st>
Sat, 17 Mar 2012 05:18:14 +0000 (01:18 -0400)
p2pool/util/graph.py
p2pool/web.py
web-static/graphs.html

index 47bf7bb..11f0412 100644 (file)
@@ -3,12 +3,19 @@ from __future__ import division
 
 import math
 
+from p2pool.util import math as math2
+
 
 class DataViewDescription(object):
     def __init__(self, bin_count, total_width):
         self.bin_count = bin_count
         self.bin_width = total_width/bin_count
 
+def _shift(x, shift, pad_item):
+    left_pad = math2.clip(shift, (0, len(x)))
+    right_pad = math2.clip(-shift, (0, len(x)))
+    return [pad_item]*left_pad + x[right_pad:-left_pad if left_pad else None] + [pad_item]*right_pad
+
 class DataView(object):
     def __init__(self, desc, ds_desc, last_bin_end, bins):
         assert len(bins) == desc.bin_count
@@ -20,7 +27,7 @@ class DataView(object):
     
     def _add_datum(self, t, value):
         shift = max(0, int(math.ceil((t - self.last_bin_end)/self.desc.bin_width)))
-        self.bins = [(0, 0)]*min(shift, self.desc.bin_count) + self.bins[:max(0, len(self.bins) - shift)]
+        self.bins = _shift(self.bins, shift, (0, 0))
         self.last_bin_end += shift*self.desc.bin_width
         
         bin = int(math.ceil((self.last_bin_end - self.desc.bin_width - t)/self.desc.bin_width))
@@ -31,8 +38,18 @@ class DataView(object):
         prev_total, prev_count = self.bins[bin]
         self.bins[bin] = prev_total + value, prev_count + 1
     
-    def get_data(self):
-        return [(self.last_bin_end - self.desc.bin_width*(i + 1/2), (total/count if count else None) if self.ds_desc.source_is_cumulative else total/self.desc.bin_width) for i, (total, count) in enumerate(self.bins)]
+    def get_data(self, t):
+        shift = max(0, int(math.ceil((t - self.last_bin_end)/self.desc.bin_width)))
+        bins = _shift(self.bins, shift, (0, 0))
+        last_bin_end = self.last_bin_end + shift*self.desc.bin_width
+        
+        assert last_bin_end - self.desc.bin_width <= t <= last_bin_end
+        
+        return [(
+            last_bin_end - self.desc.bin_width*(i + 1/2),
+            (total/count if count else None) if self.ds_desc.source_is_cumulative
+                else total/(min(t, last_bin_end - self.desc.bin_width*i) - (last_bin_end - self.desc.bin_width*(i + 1))),
+        ) for i, (total, count) in enumerate(bins)]
 
 
 class DataStreamDescription(object):
index d08b186..74c2d3e 100644 (file)
@@ -362,7 +362,7 @@ def get_web_root(tracker, current_work, current_work2, get_current_txouts, datad
         hd.datastreams['local_hash_rate'].add_datum(t, work)
         if dead:
             hd.datastreams['local_dead_hash_rate'].add_datum(t, work)
-    new_root.putChild('graph_data', WebInterface(lambda source, view: json.dumps(hd.datastreams[source].dataviews[view].get_data()), 'application/json'))
+    new_root.putChild('graph_data', WebInterface(lambda source, view: json.dumps(hd.datastreams[source].dataviews[view].get_data(time.time())), 'application/json'))
     
     web_root.putChild('static', static.File(os.path.join(os.path.dirname(sys.argv[0]), 'web-static')))
     
index 9955840..87f8374 100644 (file)
@@ -56,7 +56,6 @@
                 xmlhttp.open("GET",  url, false);
                 xmlhttp.send();
                 var data = JSON.parse(xmlhttp.responseText);
-                data.shift(); // remove current bin
                 
                 return data;
             }