From c028e7f48512044262667aaef783c9c204837a86 Mon Sep 17 00:00:00 2001 From: fuwa Date: Thu, 28 Nov 2019 09:47:35 +0000 Subject: [PATCH] tweak info rpc view, make rpc views larger and padded --- cyberwow/lib/config/cyberwow.dart | 4 +- cyberwow/lib/controller/rpc/rpc.dart | 13 +---- cyberwow/lib/controller/rpc/rpc2.dart | 4 +- cyberwow/lib/controller/rpc/rpc2View.dart | 6 +- cyberwow/lib/controller/rpc/rpcView.dart | 68 ++++++++++++++++++++++- cyberwow/lib/helper.dart | 27 ++++++++- 6 files changed, 102 insertions(+), 20 deletions(-) diff --git a/cyberwow/lib/config/cyberwow.dart b/cyberwow/lib/config/cyberwow.dart index 9ecd8b4..4c7d674 100644 --- a/cyberwow/lib/config/cyberwow.dart +++ b/cyberwow/lib/config/cyberwow.dart @@ -57,13 +57,13 @@ final _theme = ThemeData body1: TextStyle ( fontFamily: 'VT323', - fontSize: 16, + fontSize: 17, height: 1, ), body2: TextStyle ( fontFamily: 'RobotoMono', - fontSize: 11, + fontSize: 12.5, ), ).apply ( diff --git a/cyberwow/lib/controller/rpc/rpc.dart b/cyberwow/lib/controller/rpc/rpc.dart index 9cef207..fa74c6b 100644 --- a/cyberwow/lib/controller/rpc/rpc.dart +++ b/cyberwow/lib/controller/rpc/rpc.dart @@ -93,16 +93,7 @@ Future getInfo() => rpc('get_info'); Future> getInfoSimple() async { final _getInfo = await rpc('get_info').then(asMap); - return _getInfo.map - ( - (k, v) { - if (k == 'top_block_hash') { - return MapEntry(k, trimHash(v)); - } else { - return MapEntry(k, v); - } - } - ); + return cleanKey(rpcView.getInfoView(_getInfo)); } Future getInfoString() => rpcString('get_info'); @@ -129,7 +120,7 @@ Future> getConnectionsSimple() async { } ); - return _sortedConn.map(rpcView.rpcPeerView).toList(); + return _sortedConn.map(rpcView.getConnectionView).map(cleanKey).toList(); } diff --git a/cyberwow/lib/controller/rpc/rpc2.dart b/cyberwow/lib/controller/rpc/rpc2.dart index b59de90..cadf9b0 100644 --- a/cyberwow/lib/controller/rpc/rpc2.dart +++ b/cyberwow/lib/controller/rpc/rpc2.dart @@ -30,7 +30,7 @@ import 'package:intl/intl.dart'; import '../../config.dart' as config; import '../../helper.dart'; import '../../logging.dart'; -import 'rpc2View.dart'; +import 'rpc2View.dart' as rpc2View; Future rpc2(final String method) async { final url = 'http://${config.host}:${config.c.port}/${method}'; @@ -101,6 +101,6 @@ Future> getTransactionPoolSimple() async { } ); - return _decodedPool.map(rpcTxView).toList(); + return _decodedPool.map(rpc2View.txView).map(cleanKey).toList(); } } diff --git a/cyberwow/lib/controller/rpc/rpc2View.dart b/cyberwow/lib/controller/rpc/rpc2View.dart index 54e8ed1..c81083c 100644 --- a/cyberwow/lib/controller/rpc/rpc2View.dart +++ b/cyberwow/lib/controller/rpc/rpc2View.dart @@ -26,7 +26,7 @@ import 'package:intl/intl.dart'; import '../../config.dart' as config; import '../../helper.dart'; -Map rpcTxView(Map x) { +Map txView(Map x) { const _remove = [ 'tx_blob', @@ -85,7 +85,7 @@ Map rpcTxView(Map x) { 'vout': v['vout'].length, }; final _outString = _out['vin'].toString() + '/' + _out['vout'].toString(); - return MapEntry('in/out', _outString); + return MapEntry('i/o', _outString); } else { @@ -99,7 +99,7 @@ Map rpcTxView(Map x) { 'id', 'age', 'fee', - 'in/out', + 'i/o', 'size', ] .where((k) => _tx.keys.contains(k)) diff --git a/cyberwow/lib/controller/rpc/rpcView.dart b/cyberwow/lib/controller/rpc/rpcView.dart index c17734a..613711d 100644 --- a/cyberwow/lib/controller/rpc/rpcView.dart +++ b/cyberwow/lib/controller/rpc/rpcView.dart @@ -19,10 +19,12 @@ along with CyberWOW. If not, see . */ +import 'package:intl/intl.dart'; + import '../../config.dart' as config; import '../../helper.dart'; -Map rpcPeerView(Map x) { +Map getConnectionView(Map x) { const _remove = [ 'address_type', @@ -137,3 +139,67 @@ Map simpleHeight(int height, Map x) { } ); } + +Map getInfoView(Map x) { + const _remove = + [ + 'difficulty_top64', + 'stagenet', + 'testnet', + 'top_hash', + 'update_available', + 'was_bootstrap_ever_used', + 'bootstrap_daemon_address', + 'height_without_bootstrap', + 'wide_cumulative_difficulty', + 'wide_difficulty', + 'cumulative_difficulty_top64', + 'credits', + ]; + + final _filteredInfo = x..removeWhere + ( + (k,v) => _remove.contains(k) + ); + + return _filteredInfo.map + ( + (k, v) { + if (k == 'top_block_hash') { + return MapEntry(k, trimHash(v)); + } + + + const sizeField = + [ + 'block_size_limit', + 'block_size_median', + 'block_weight_limit', + 'block_weight_median', + 'difficulty', + 'height', + 'tx_count', + 'cumulative_difficulty', + 'free_space', + 'database_size', + ]; + if (sizeField.contains(k)) { + final formatter = NumberFormat.compact(); + return MapEntry(k, formatter.format(v)); + } + + else if (k == 'start_time') { + final _receive_time = DateTime.fromMillisecondsSinceEpoch(v * 1000); + final _diff = DateTime.now().difference(_receive_time); + + format(Duration d) => d.toString().split('.').first.padLeft(8, "0"); + return MapEntry('age', format(_diff)); + } + + + else { + return MapEntry(k, v); + } + } + ); +} diff --git a/cyberwow/lib/helper.dart b/cyberwow/lib/helper.dart index 654c09b..6f907dd 100644 --- a/cyberwow/lib/helper.dart +++ b/cyberwow/lib/helper.dart @@ -20,6 +20,7 @@ along with CyberWOW. If not, see . */ import 'dart:convert'; +import 'dart:math'; import 'config.dart' as config; @@ -27,11 +28,35 @@ String pretty(dynamic x) { final JsonEncoder encoder = JsonEncoder.withIndent(''); return encoder.convert(x) .replaceAll(RegExp(r'^{'), '\n') - .replaceAll(RegExp(r'["\[\],{}]'), '') + .replaceAll(RegExp(r'(["\[\],{}]|: )'), '') ; } String trimHash(String x) => x.substring(0, config.hashLength) + ' ...'; +Map cleanKey(Map x) { + final _cleaned = x.map + ( + (k, v) => MapEntry + ( + k + .replaceAll('cumulative', 'Σ') + .replaceAll('current_', '') + .replaceAll('_count', '') + .replaceAll('_', ' ') + , + v + ) + ); + + + final int _maxLength = _cleaned.keys.map((x) => x.length).reduce(max); + final _padded = _cleaned.map + ( + (k, v) => MapEntry(k.padRight(_maxLength + 2, ' '), v) + ); + + return _padded; +} int asInt(dynamic x) => x?.toInt() ?? 0;