added separate tracking of stale types to graphs
authorForrest Voight <forrest@forre.st>
Fri, 4 May 2012 09:36:27 +0000 (05:36 -0400)
committerForrest Voight <forrest@forre.st>
Tue, 8 May 2012 19:12:47 +0000 (15:12 -0400)
p2pool/data.py
p2pool/util/graph.py
p2pool/web.py
web-static/graphs.html

index af4cb14..f615a08 100644 (file)
@@ -530,6 +530,18 @@ def get_average_stale_prop(tracker, share_hash, lookbehind):
     stales = sum(1 for share in tracker.get_chain(share_hash, lookbehind) if share.share_data['stale_info'] is not None)
     return stales/(lookbehind + stales)
 
+def get_stale_counts(tracker, share_hash, lookbehind, rates=False):
+    res = {}
+    for share in tracker.get_chain(share_hash, lookbehind - 1):
+        res['good'] = res.get('good', 0) + bitcoin_data.target_to_average_attempts(share.target)
+        s = share.share_data['stale_info']
+        if s is not None:
+            res[s] = res.get(s, 0) + bitcoin_data.target_to_average_attempts(share.target)
+    if rates:
+        dt = tracker.shares[share_hash].timestamp - tracker.shares[tracker.get_nth_parent_hash(share_hash, lookbehind - 1)].timestamp
+        res = dict((k, v/dt) for k, v in res.iteritems())
+    return res
+
 def get_expected_payouts(tracker, best_share_hash, block_target, subsidy, net):
     weights, total_weight, donation_weight = tracker.get_cumulative_weights(best_share_hash, min(tracker.get_height(best_share_hash), net.REAL_CHAIN_LENGTH), 65535*net.SPREAD*bitcoin_data.target_to_average_attempts(block_target))
     res = dict((script, subsidy*weight//total_weight) for script, weight in weights.iteritems())
index 3a80af9..068dbf3 100644 (file)
@@ -41,6 +41,8 @@ class DataView(object):
     def _add_datum(self, t, value):
         if not self.ds_desc.multivalues:
             value = {'null': value}
+        elif self.ds_desc.multivalue_undefined_means_0 and 'null' not in value:
+            value = dict(value, null=0) # use null to hold sample counter
         shift = max(0, int(math.ceil((t - self.last_bin_end)/self.desc.bin_width)))
         self.bins = _shift(self.bins, shift, {})
         self.last_bin_end += shift*self.desc.bin_width
@@ -59,22 +61,33 @@ class DataView(object):
         def _((i, bin)):
             left, right = last_bin_end - self.desc.bin_width*(i + 1), min(t, last_bin_end - self.desc.bin_width*i)
             center, width = (left+right)/2, right-left
-            if self.ds_desc.is_gauge:
+            if self.ds_desc.is_gauge and self.ds_desc.multivalue_undefined_means_0:
+                real_count = max([0] + [count for total, count in bin.itervalues()])
+                if real_count == 0:
+                    val = None
+                else:
+                    val = dict((k, total/real_count) for k, (total, count) in bin.iteritems())
+                default = 0
+            elif self.ds_desc.is_gauge and not self.ds_desc.multivalue_undefined_means_0:
                 val = dict((k, total/count) for k, (total, count) in bin.iteritems())
+                default = None
             else:
                 val = dict((k, total/width) for k, (total, count) in bin.iteritems())
+                default = 0
             if not self.ds_desc.multivalues:
-                val = val.get('null', None if self.ds_desc.is_gauge else 0)
-            return center, val, width
+                val = None if val is None else val.get('null', default)
+            return center, val, width, default
         return map(_, enumerate(bins))
 
 
 class DataStreamDescription(object):
-    def __init__(self, dataview_descriptions, is_gauge=True, multivalues=False, multivalues_keep=20, multivalues_squash_key=None):
+    def __init__(self, dataview_descriptions, is_gauge=True, multivalues=False, multivalues_keep=20, multivalues_squash_key=None, multivalue_undefined_means_0=False, default_func=None):
         self.dataview_descriptions = dataview_descriptions
         self.is_gauge = is_gauge
         self.multivalues = multivalues
         self.keep_largest_func = keep_largest(multivalues_keep, multivalues_squash_key, key=lambda (t, c): t/c if self.is_gauge else t, add_func=lambda (a1, b1), (a2, b2): (a1+a2, b1+b2))
+        self.multivalue_undefined_means_0 = multivalue_undefined_means_0
+        self.default_func = default_func
 
 class DataStream(object):
     def __init__(self, desc, dataviews):
@@ -103,7 +116,10 @@ class HistoryDatabase(object):
                     dv_data = ds_data[dv_name]
                     if dv_data['bin_width'] == dv_desc.bin_width and len(dv_data['bins']) == dv_desc.bin_count:
                         return DataView(dv_desc, ds_desc, dv_data['last_bin_end'], map(convert_bin, dv_data['bins']))
-            return DataView(dv_desc, ds_desc, 0, dv_desc.bin_count*[{}])
+            elif ds_desc.default_func is None:
+                return DataView(dv_desc, ds_desc, 0, dv_desc.bin_count*[{}])
+            else:
+                return ds_desc.default_func(ds_name, ds_desc, dv_name, dv_desc, obj)
         return cls(dict(
             (ds_name, DataStream(ds_desc, dict(
                 (dv_name, get_dataview(ds_name, ds_desc, dv_name, dv_desc))
index 0ed0af8..f30485f 100644 (file)
@@ -202,6 +202,7 @@ def get_web_root(tracker, current_work, current_work2, get_current_txouts, datad
     web_root.putChild('payout_addr', WebInterface(lambda: bitcoin_data.pubkey_hash_to_address(my_pubkey_hash, net.PARENT)))
     web_root.putChild('recent_blocks', WebInterface(lambda: [dict(ts=s.timestamp, hash='%064x' % s.header_hash) for s in tracker.get_chain(current_work.value['best_share_hash'], 24*60*60//net.SHARE_PERIOD) if s.pow_hash <= s.header['bits'].target]))
     web_root.putChild('uptime', WebInterface(lambda: time.time() - start_time))
+    web_root.putChild('stale_rates', WebInterface(lambda: p2pool_data.get_stale_counts(tracker, current_work.value['best_share_hash'], 720, rates=True)))
     
     new_root = resource.Resource()
     web_root.putChild('web', new_root)
@@ -317,20 +318,46 @@ def get_web_root(tracker, current_work, current_work2, get_current_txouts, datad
         'last_month': graph.DataViewDescription(300, 60*60*24*30),
         'last_year': graph.DataViewDescription(300, 60*60*24*365.25),
     }
+    def build_pool_rates(ds_name, ds_desc, dv_name, dv_desc, obj):
+        if not obj:
+            last_bin_end = 0
+            bins = dv_desc.bin_count*[{}]
+        else:
+            pool_rate = obj['pool_rate'][dv_name]
+            pool_stale_rate = obj['pool_stale_rate'][dv_name]
+            last_bin_end = max(pool_rate['last_bin_end'], pool_stale_rate['last_bin_end'])
+            bins = dv_desc.bin_count*[{}]
+            def get_value(obj, t):
+                n = int((obj['last_bin_end'] - t)/dv_desc.bin_width)
+                if n < 0 or n >= dv_desc.bin_count:
+                    return None
+                total, count = obj['bins'][n].get('null', [0, 0])
+                if count == 0:
+                    return None
+                return total/count
+            def get_bin(t):
+                total = get_value(pool_rate, t)
+                bad = get_value(pool_stale_rate, t)
+                if total is None or bad is None:
+                    return {}
+                return dict(good=[total-bad, 1], bad=[bad, 1])
+            bins = [get_bin(last_bin_end - (i+1/2)*dv_desc.bin_width) for i in xrange(dv_desc.bin_count)]
+        return graph.DataView(dv_desc, ds_desc, last_bin_end, bins)
     hd = graph.HistoryDatabase.from_obj({
         'local_hash_rate': graph.DataStreamDescription(dataview_descriptions, is_gauge=False),
         'local_dead_hash_rate': graph.DataStreamDescription(dataview_descriptions, is_gauge=False),
         'local_share_hash_rate': graph.DataStreamDescription(dataview_descriptions, is_gauge=False),
         'local_dead_share_hash_rate': graph.DataStreamDescription(dataview_descriptions, is_gauge=False),
-        'pool_rate': graph.DataStreamDescription(dataview_descriptions),
-        'pool_stale_rate': graph.DataStreamDescription(dataview_descriptions),
+        'pool_rates': graph.DataStreamDescription(dataview_descriptions, multivalues=True,
+            multivalue_undefined_means_0=True, default_func=build_pool_rates),
         'current_payout': graph.DataStreamDescription(dataview_descriptions),
         'current_payouts': graph.DataStreamDescription(dataview_descriptions, multivalues=True),
         'incoming_peers': graph.DataStreamDescription(dataview_descriptions),
         'outgoing_peers': graph.DataStreamDescription(dataview_descriptions),
         'miner_hash_rates': graph.DataStreamDescription(dataview_descriptions, is_gauge=False, multivalues=True),
         'miner_dead_hash_rates': graph.DataStreamDescription(dataview_descriptions, is_gauge=False, multivalues=True),
-        'desired_versions': graph.DataStreamDescription(dataview_descriptions, multivalues=True),
+        'desired_versions': graph.DataStreamDescription(dataview_descriptions, multivalues=True,
+            multivalue_undefined_means_0=True),
     }, hd_obj)
     task.LoopingCall(lambda: _atomic_write(hd_path, json.dumps(hd.to_obj()))).start(100)
     @pseudoshare_received.watch
@@ -352,11 +379,8 @@ def get_web_root(tracker, current_work, current_work2, get_current_txouts, datad
     def add_point():
         if tracker.get_height(current_work.value['best_share_hash']) < 720:
             return
-        nonstalerate = p2pool_data.get_pool_attempts_per_second(tracker, current_work.value['best_share_hash'], 720)
-        poolrate = nonstalerate / (1 - p2pool_data.get_average_stale_prop(tracker, current_work.value['best_share_hash'], 720))
         t = time.time()
-        hd.datastreams['pool_rate'].add_datum(t, poolrate)
-        hd.datastreams['pool_stale_rate'].add_datum(t, poolrate - nonstalerate)
+        hd.datastreams['pool_rates'].add_datum(t, p2pool_data.get_stale_counts(tracker, current_work.value['best_share_hash'], 720, rates=True))
         current_txouts = get_current_txouts()
         hd.datastreams['current_payout'].add_datum(t, current_txouts.get(bitcoin_data.pubkey_hash_to_script2(my_pubkey_hash), 0)*1e-8)
         miner_hash_rates, miner_dead_hash_rates = get_local_rates()
index 72d330c..e2ce765 100644 (file)
             }
             
             function data_to_lines(data, sort_key) {
-                var vers = {}; for(var i = 0; i < data.length; ++i) for(var v in data[i][1]) vers[v] = null;
+                var vers = {}; for(var i = 0; i < data.length; ++i) if(data[i][1] != null) for(var v in data[i][1]) vers[v] = null;
                 var verlist = []; for(var v in vers) verlist.push(v);
                 verlist.sort();
                 
                 lines = [];
                 for(var i = 0; i < verlist.length; i++) {
                     lines.push({
-                        data: data.map(function(d){ return [d[0], verlist[i] in d[1] ? d[1][verlist[i]] : null, d[2]] }),
+                        data: data.map(function(d){ return [d[0], d[1] == null ? null : (verlist[i] in d[1] ? d[1][verlist[i]] : d[3]), d[2]] }),
                         color: d3.hsl(i/verlist.length*360, 0.5, 0.5),
                         label: verlist[i]
                     });
                 plot_later(d3.select("#payout"), currency_info.symbol, null, [
                     {"url": "/web/graph_data/current_payout/last_" + lowerperiod, "color": "#0000FF"}
                 ]);
-                plot_later(d3.select("#pool"), "H/s", "H", [
-                    {"url": "/web/graph_data/pool_rate/last_" + lowerperiod, "color": "#0000FF", "label": "Total"},
-                    {"url": "/web/graph_data/pool_stale_rate/last_" + lowerperiod, "color": "#FF0000", "label": "Stale"}
-                ]);
+                d3.json("/web/graph_data/pool_rates/last_" + lowerperiod, function(data) {
+                    plot(d3.select('#pool'), 'H/s', 'H', data_to_lines(data));
+                });
                 plot_later(d3.select("#peers"), "", null, [
                     {"url": "/web/graph_data/incoming_peers/last_" + lowerperiod, "color": "#0000FF", "label": "Incoming"},
                     {"url": "/web/graph_data/outgoing_peers/last_" + lowerperiod, "color": "#FF0000", "label": "Outgoing"}
                             div.append("h3").text(function(u) { return u });
                             div.append("svg:svg").each(function(u) {
                                 plot(d3.select(this), "H/s", "H", [
-                                    {"data": data.map(function(d){ return [d[0], u in d[1] ? d[1][u] : 0, d[2]] }), "color": "#0000FF", "label": "Total"},
-                                    {"data": dead_data.map(function(d){ return [d[0], u in d[1] ? d[1][u] : 0, d[2]] }), "color": "#FF0000", "label": "Dead"}
+                                    {"data": data.map(function(d){ return [d[0], u in d[1] ? d[1][u] : d[3], d[2]] }), "color": "#0000FF", "label": "Total"},
+                                    {"data": dead_data.map(function(d){ return [d[0], u in d[1] ? d[1][u] : d[3], d[2]] }), "color": "#FF0000", "label": "Dead"}
                                 ]);
                             });
                             div.append("svg:svg").each(function(u) {
                                 plot(d3.select(this), currency_info.symbol, null, [
-                                    {"data": current_payouts.map(function(d){ return [d[0], u in d[1] ? d[1][u] : null, d[2]] }), "color": "#0000FF"}
+                                    {"data": current_payouts.map(function(d){ return [d[0], u in d[1] ? d[1][u] : d[3], d[2]] }), "color": "#0000FF"}
                                 ]);
                             });
                         });