From c523064632359d04124f80d07bfdb597486e32b0 Mon Sep 17 00:00:00 2001 From: Neil Booth Date: Sat, 10 Dec 2016 07:59:19 +0900 Subject: [PATCH] Improve RPC groups call Show columns correctly for RPC sessions call --- electrumx_rpc.py | 4 +-- server/protocol.py | 63 +++++++++++++++++++++++++++++++++++++--------- 2 files changed, 53 insertions(+), 14 deletions(-) diff --git a/electrumx_rpc.py b/electrumx_rpc.py index 1a6e0e2..b7e5708 100755 --- a/electrumx_rpc.py +++ b/electrumx_rpc.py @@ -46,8 +46,8 @@ class RPCClient(JSONRPC): await request.process(1) async def handle_response(self, result, error, method): - if result and method == 'sessions': - for line in ServerManager.sessions_text_lines(result): + if result and method in ('groups', 'sessions'): + for line in ServerManager.text_lines(method, result): print(line) else: value = {'error': error} if error else result diff --git a/server/protocol.py b/server/protocol.py index 2d11222..0adece4 100644 --- a/server/protocol.py +++ b/server/protocol.py @@ -542,6 +542,52 @@ class ServerManager(util.LoggedClass): 'watched': self.subscription_count, } + @staticmethod + def text_lines(method, data): + if method == 'sessions': + return ServerManager.sessions_text_lines(data) + else: + return ServerManager.groups_text_lines(data) + + @staticmethod + def groups_text_lines(data): + '''A generator returning lines for a list of groups. + + data is the return value of rpc_groups().''' + + fmt = ('{:<6} {:>9} {:>6} {:>6} {:>8}' + '{:>7} {:>9} {:>7} {:>9}') + yield fmt.format('ID', 'Bw Qta KB', 'Reqs', 'Txs', 'Subs', + 'Recv', 'Recv KB', 'Sent', 'Sent KB') + for (id_, bandwidth, reqs, txs_sent, subs, + recv_count, recv_size, send_count, send_size) in data: + yield fmt.format(id_, + '{:,d}'.format(bandwidth // 1024), + '{:,d}'.format(reqs), + '{:,d}'.format(txs_sent), + '{:,d}'.format(subs), + '{:,d}'.format(recv_count), + '{:,d}'.format(recv_size // 1024), + '{:,d}'.format(send_count), + '{:,d}'.format(send_size // 1024)) + + def group_data(self): + '''Returned to the RPC 'groups' call.''' + result = [] + for group_id in sorted(self.groups.keys()): + sessions = self.groups[group_id] + result.append([group_id, + sum(s.bandwidth_used for s in sessions), + sum(s.requests_remaining() for s in sessions), + sum(s.txs_sent for s in sessions), + sum(s.sub_count() for s in sessions), + sum(s.recv_count for s in sessions), + sum(s.recv_size for s in sessions), + sum(s.send_count for s in sessions), + sum(s.send_size for s in sessions), + ]) + return result + @staticmethod def sessions_text_lines(data): '''A generator returning lines for a list of sessions. @@ -553,11 +599,10 @@ class ServerManager(util.LoggedClass): return ('{:3d}:{:02d}:{:02d}' .format(t // 3600, (t % 3600) // 60, t % 60)) - fmt = ('{:<6} {:<5} {:>23} {:>15} {:>7} ' - '{:>7} {:>7} {:>7} {:>7} {:>5} {:>9}') - yield fmt.format('ID', 'Flags', 'Peer', 'Client', 'Reqs', - 'Txs', 'Subs', 'Recv', 'Recv KB', 'Sent', - 'Sent KB', 'Time') + fmt = ('{:<6} {:<5} {:>23} {:>15} {:>5} {:>5} ' + '{:>7} {:>7} {:>7} {:>7} {:>7} {:>9}') + yield fmt.format('ID', 'Flags', 'Peer', 'Client', 'Reqs', 'Txs', + 'Subs', 'Recv', 'Recv KB', 'Sent', 'Sent KB', 'Time') for (id_, flags, peer, client, reqs, txs_sent, subs, recv_count, recv_size, send_count, send_size, time) in data: yield fmt.format(id_, flags, peer, client, @@ -617,13 +662,7 @@ class ServerManager(util.LoggedClass): return self.server_summary() async def rpc_groups(self, params): - result = {} - msg = '{:,d} sessions, {:,d} requests, {:,d}KB b/w quota used' - for group, sessions in self.groups.items(): - bandwidth = sum(s.bandwidth_used for s in sessions) - reqs = sum(s.requests_remaining() for s in sessions) - result[group] = msg.format(len(sessions), reqs, bandwidth // 1024) - return result + return self.group_data() async def rpc_sessions(self, params): return self.session_data(for_log=False)