From e79545e9a293a912685392d152bbccb0b1b30f1a Mon Sep 17 00:00:00 2001 From: Michael Schmoock Date: Fri, 10 Feb 2023 14:55:48 +0100 Subject: [PATCH] summary: save datastore usage by compater objects --- summary/summary.py | 8 ++++---- summary/summary_avail.py | 34 ++++++++++++++++++++++++---------- summary/test_summary.py | 25 +++++++++++++------------ 3 files changed, 41 insertions(+), 26 deletions(-) diff --git a/summary/summary.py b/summary/summary.py index 754c07f..a1429f2 100755 --- a/summary/summary.py +++ b/summary/summary.py @@ -161,7 +161,7 @@ def summary(plugin, exclude='', sortkey=None): c['private'], p['connected'], c['short_channel_id'], - plugin.persist['peerstate'][pid]['avail'], + plugin.persist['p'][pid]['a'], Millisatoshi(c['fee_base_msat']), c['fee_proportional_millionths'], )) @@ -250,10 +250,10 @@ def load_datastore(plugin): entries = plugin.rpc.listdatastore(key=datastore_key)['datastore'] if len(entries) == 0: plugin.log(f"Creating a new datastore '{datastore_key}'", 'debug') - return {'version': 1, 'peerstate': {}, 'availcount': 0} + return {'p': {}, 'r': 0, 'v': 1} # see summary_avail.py for structure persist = pickle.loads(bytearray.fromhex(entries[0]["hex"])) - plugin.log(f"Reopened datastore '{datastore_key}' with {persist['availcount']} " - f"runs and {len(persist['peerstate'])} entries", 'debug') + plugin.log(f"Reopened datastore '{datastore_key}' with {persist['r']} " + f"runs and {len(persist['p'])} entries", 'debug') return persist diff --git a/summary/summary_avail.py b/summary/summary_avail.py index d327a95..9768bbf 100644 --- a/summary/summary_avail.py +++ b/summary/summary_avail.py @@ -1,17 +1,31 @@ +# This is the persist object structure: +# +# { +# "p": { # peerstate +# "PEER_ID" : { # the peers id +# "c": True, # connected or not +# "a": 1.0 # the availability value +# } +# }, +# "r": 123, # the number of runs +# "v": 1 # version +# } + + # ensure an rpc peer is added def addpeer(p, rpcpeer): pid = rpcpeer['id'] - if pid not in p.persist['peerstate']: - p.persist['peerstate'][pid] = { - 'connected': rpcpeer['connected'], - 'avail': 1.0 if rpcpeer['connected'] else 0.0 + if pid not in p.persist['p']: + p.persist['p'][pid] = { + 'c': rpcpeer['connected'], + 'a': 1.0 if rpcpeer['connected'] else 0.0 } # exponetially smooth online/offline states of peers def trace_availability(p, rpcpeers): - p.persist['availcount'] += 1 - leadwin = max(min(p.avail_window, p.persist['availcount'] * p.avail_interval), p.avail_interval) + p.persist['r'] += 1 + leadwin = max(min(p.avail_window, p.persist['r'] * p.avail_interval), p.avail_interval) samples = leadwin / p.avail_interval alpha = 1.0 / samples beta = 1.0 - alpha @@ -21,8 +35,8 @@ def trace_availability(p, rpcpeers): addpeer(p, rpcpeer) if rpcpeer['connected']: - p.persist['peerstate'][pid]['connected'] = True - p.persist['peerstate'][pid]['avail'] = 1.0 * alpha + p.persist['peerstate'][pid]['avail'] * beta + p.persist['p'][pid]['c'] = True + p.persist['p'][pid]['a'] = 1.0 * alpha + p.persist['p'][pid]['a'] * beta else: - p.persist['peerstate'][pid]['connected'] = False - p.persist['peerstate'][pid]['avail'] = 0.0 * alpha + p.persist['peerstate'][pid]['avail'] * beta + p.persist['p'][pid]['c'] = False + p.persist['p'][pid]['a'] = 0.0 * alpha + p.persist['p'][pid]['a'] * beta diff --git a/summary/test_summary.py b/summary/test_summary.py index 707b49b..0a47e6a 100644 --- a/summary/test_summary.py +++ b/summary/test_summary.py @@ -18,8 +18,9 @@ def get_stub(): plugin.avail_interval = 60 plugin.avail_window = 3600 plugin.persist = {} - plugin.persist['peerstate'] = {} - plugin.persist['availcount'] = 0 + plugin.persist['p'] = {} + plugin.persist['r'] = 0 + plugin.persist['v'] = 1 return plugin @@ -63,12 +64,12 @@ def test_summary_avail_101(): trace_availability(plugin, rpcpeers) # then - assert(plugin.persist['peerstate']['1']['avail'] == 1.0) - assert(plugin.persist['peerstate']['2']['avail'] == 0.0) - assert(plugin.persist['peerstate']['3']['avail'] == 1.0) - assert(plugin.persist['peerstate']['1']['connected'] is True) - assert(plugin.persist['peerstate']['2']['connected'] is False) - assert(plugin.persist['peerstate']['3']['connected'] is True) + assert(plugin.persist['p']['1']['a'] == 1.0) + assert(plugin.persist['p']['2']['a'] == 0.0) + assert(plugin.persist['p']['3']['a'] == 1.0) + assert(plugin.persist['p']['1']['c'] is True) + assert(plugin.persist['p']['2']['c'] is False) + assert(plugin.persist['p']['3']['c'] is True) # tests for 50% downtime @@ -93,7 +94,7 @@ def test_summary_avail_50(): trace_availability(plugin, rpcpeers_off) # then - assert(round(plugin.persist['peerstate']['1']['avail'], 3) == 0.5) + assert(round(plugin.persist['p']['1']['a'], 3) == 0.5) # tests for 2/3 downtime @@ -118,7 +119,7 @@ def test_summary_avail_33(): trace_availability(plugin, rpcpeers_off) # then - assert(round(plugin.persist['peerstate']['1']['avail'], 3) == 0.333) + assert(round(plugin.persist['p']['1']['a'], 3) == 0.333) # tests for 1/3 downtime @@ -143,7 +144,7 @@ def test_summary_avail_66(): trace_availability(plugin, rpcpeers_off) # then - assert(round(plugin.persist['peerstate']['1']['avail'], 3) == 0.667) + assert(round(plugin.persist['p']['1']['a'], 3) == 0.667) # checks the leading window is smaller if interval count is low @@ -168,7 +169,7 @@ def test_summary_avail_leadwin(): trace_availability(plugin, rpcpeers_off) # then - assert(round(plugin.persist['peerstate']['1']['avail'], 3) == 0.667) + assert(round(plugin.persist['p']['1']['a'], 3) == 0.667) # checks whether the peerstate is persistent