From fcb4018bd42fee52515fde33ee0e19c23cbe8179 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 26 Feb 2019 16:16:43 +1030 Subject: [PATCH] summary: add bar graphs showing balances. Signed-off-by: Rusty Russell --- summary/README.md | 56 ++++++++++++++++++++++++++++++++++++---------- summary/summary.py | 36 +++++++++++++++++++++++++---- 2 files changed, 76 insertions(+), 16 deletions(-) diff --git a/summary/README.md b/summary/README.md index 464ba22..82ad731 100644 --- a/summary/README.md +++ b/summary/README.md @@ -10,17 +10,49 @@ fiat amounts. ## Example Usage +Unfortunately the python plugin framework doesn't pretty-print, nor does +lightning-cli, and json_pp doesn't maintain order, so I use a hacky 'tr': + ``` -$ lightning-cli summary | json_pp -{ - "network" : "TESTNET", - "utxo_amount" : "1.20119332000btc = USD $4582.22", - "avail_in" : "2.06940379btc = USD $7894.20", - "avail_out" : "0.27095103btc = USD $1033.60", - "my_address" : "031a3478d481b92e3c28810228252898c5f0d82fc4d07f5210c4f34d4aba56b769@165.227.30.200", - "num_channels" : 31, - "num_utxos" : 5, - "num_connected" : 1, - "num_gossipers" : 32 -} +$ lightning-cli summary | tr ',' '\n' +{"network": "TESTNET" + "my_address": "031a3478d481b92e3c28810228252898c5f0d82fc4d07f5210c4f34d4aba56b769@165.227.30.200" + "num_utxos": 5 + "utxo_amount": "1.20119332000btc = USD $4589.24" + "num_channels": 31 + "num_connected": 1 + "num_gossipers": 32 + "avail_out": "0.27095103btc = USD $1035.19" + "avail_in": "2.06940379btc = USD $7906.30" + "channels": [" ---------------------------/ :02ac05912f89e43b88de3472e8c3003b" + " -------------------------/- :02dd4cef0192611bc34cd1c3a0a7eb0f" + " /--------------------------- :02a13878947a133d7c96e70303a9bf27 (priv)" + " /- :033e2db012833d997e3c" + " /-- :Kenny_Loggins" + "/--------------------------------------------- :DeutscheTestnetBank" + "/--------------------------------------------- :BlueLagoon1" + "/--------------------------------------------- :0270dd38e8af9a64b4a483ab12b6aeb1" + " /-- :btctest.lnetwork.tokyo" + " /----- :microbet.fun" + "/--------------------------------------------- :02fcab6e34a2ad21be2a752ab96d13f5 (priv)" + "/--------------------------------------------- :htlc.me" + " /-------- :02229ea9a7a4f9bf8bf25ce225079aed" + "/--------------------------------------------- :025d5b572a94235cfcbdc429181b2b88" + " /------------------------- :03c56de3a84336b4a939777ace9ecbef (priv)" + " /------------------ :LiteStrikeBTClnd" + " /---------------------------------- :037c9cf1cde4414c59407d547b7eac08 (priv)" + " / :03490a74e4def9125a84aee2d84e8cfe" + " ---------------------/--------------------- :aranguren.org" + " / :03cc6603e1f6df535dd8b423284f2c09 (priv)" + " /- :cyclopes" + "/--------------------------------------------- :02b73a2160863e925e9fa978b0ddc56b (priv)" + " /-------- :lnd-testnet.ignios.net" + " /----- :0327a104108173d4a4f34ab2cbc3084c (priv)" + " /---- :dwarf" + " /- :028133777757ce281658804dd82f5758 (priv)" + " /------------------------- :02db62ffff5c35be74e7f856bba136db (priv)" + " / :Lightning Tea" + " /-- :03015ac044f5fa9768ededf6fed9c0ff (priv)" + " /-- :LND-Neutrino-TEST" + "/--------------------------------------------- :0270685ca81a8e4d4d01"]} ``` diff --git a/summary/summary.py b/summary/summary.py index b9391b8..71c1b50 100755 --- a/summary/summary.py +++ b/summary/summary.py @@ -54,6 +54,7 @@ def summary(plugin): avail_out = Millisatoshi(0) avail_in = Millisatoshi(0) + chans = [] reply['num_channels'] = 0 reply['num_connected'] = 0 reply['num_gossipers'] = info['num_peers'] @@ -65,12 +66,21 @@ def summary(plugin): reply['num_connected'] += 1 reply['num_gossipers'] -= 1 if c['our_reserve_msat'] < c['to_us_msat']: - avail_out += c['to_us_msat'] - c['our_reserve_msat'] + to_us = c['to_us_msat'] - c['our_reserve_msat'] + else: + to_us = Millisatoshi(0) + avail_out += to_us + # We have to derive amount to them - to_them_msat = c['total_msat'] - c['to_us_msat'] - if c['their_reserve_msat'] < to_them_msat: - avail_in += to_them_msat - c['their_reserve_msat'] + to_them = c['total_msat'] - c['to_us_msat'] + if c['their_reserve_msat'] < to_them: + to_them = to_them - c['their_reserve_msat'] + else: + to_them = Millisatoshi(0) + avail_in += to_them reply['num_channels'] += 1 + chans.append((c['total_msat'], to_us, to_them, p['id'], c['private'])) + reply['avail_out'] = avail_out.to_btc_str() reply['avail_in'] = avail_in.to_btc_str() @@ -79,6 +89,24 @@ def summary(plugin): reply['avail_out'] += ' = ' + to_fiatstr(avail_out) reply['avail_in'] += ' = ' + to_fiatstr(avail_in) + if chans != []: + reply['channels'] = [] + biggest = max(int(c[0]) for c in chans) + for c in chans: + # Create simple line graph + s = ('-' * int((int(c[1]) / biggest * 46)) + + '/' + '-' * int((int(c[2]) / biggest * 46))) + # Center it + s = "{:^47}".format(s) + node = plugin.rpc.listnodes(c[3])['nodes'] + if len(node) != 0: + s += ':' + node[0]['alias'] + else: + s += ':' + c[3][0:32] + if c[4]: + s += ' (priv)' + reply['channels'].append(s) + return reply