From 7f81b822e26f65ce30643582166b325ab5c33f8a Mon Sep 17 00:00:00 2001 From: fuwa Date: Wed, 27 Nov 2019 16:27:32 +0800 Subject: [PATCH] refactor to rpcView --- cyberwow/lib/controller/rpc/rpc.dart | 89 +-------------- cyberwow/lib/controller/rpc/rpc2.dart | 133 ++++------------------ cyberwow/lib/controller/rpc/rpc2View.dart | 110 ++++++++++++++++++ cyberwow/lib/controller/rpc/rpcView.dart | 106 +++++++++++++++++ cyberwow/lib/helper.dart | 1 + 5 files changed, 245 insertions(+), 194 deletions(-) create mode 100644 cyberwow/lib/controller/rpc/rpc2View.dart create mode 100644 cyberwow/lib/controller/rpc/rpcView.dart diff --git a/cyberwow/lib/controller/rpc/rpc.dart b/cyberwow/lib/controller/rpc/rpc.dart index 4d4aa38..ed94923 100644 --- a/cyberwow/lib/controller/rpc/rpc.dart +++ b/cyberwow/lib/controller/rpc/rpc.dart @@ -28,6 +28,7 @@ import 'package:flutter/foundation.dart'; import '../../config.dart' as config; import '../../helper.dart'; import '../../logging.dart'; +import 'rpcView.dart'; int rpcID = 0; @@ -114,7 +115,7 @@ Future incomingConnectionsCount() => rpc('get_info', field: 'incoming_connections_count').then(asInt); Future> getConnectionsSimple() async { - final _connections = await rpc('get_connections', field: 'connections').then(asList); + final _connections = await rpc('get_connections', field: 'connections').then(asJsonArray); const minActiveTime = 8; final _activeConnections = _connections.where((x) => x['live_time'] > minActiveTime); @@ -128,91 +129,7 @@ Future> getConnectionsSimple() async { } ); - return _sortedConn.map - ( - (x) { - const _remove = - [ - 'address_type', - 'connection_id', - 'host', - 'ip', - 'local_ip', - 'localhost', - 'peer_id', - 'port', - 'recv_count', - 'rpc_port', - 'send_count', - 'support_flags', - - // 'avg_download', - // 'avg_upload', - // 'current_download', - // 'current_upload', - 'rpc_credits_per_hash', - 'state', - 'recv_idle_time', - 'send_idle_time', - 'incoming', - ]; - - final _filteredConn = x..removeWhere - ( - (k,v) => _remove.contains(k) - ); - - final _conn = _filteredConn.map - ( - (k, v) { - if (k == 'connection_id') { - return MapEntry(k, v.substring(0, config.hashLength) + '...'); - } - - const speedField = - [ - 'avg_download', - 'avg_upload', - 'current_download', - 'current_upload', - ]; - if (speedField.contains(k)) { - return MapEntry(k, '${v} kB/s'); - } - - else if (k == 'live_time') { - final _duration = Duration(seconds: v); - format(Duration d) => d.toString().split('.').first.padLeft(8, "0"); - return MapEntry(k, format(_duration)); - } - - else { - return MapEntry(k, v); - } - } - ); - - final List keys = - [ - 'address', - 'height', - 'live_time', - 'current_download', - 'current_upload', - 'avg_download', - 'avg_upload', - 'pruning_seed', - ] - .where((k) => _conn.keys.contains(k)) - .toList(); - - final _sortedConn = { - for (var k in keys) k: _conn[k] - }; - - return _sortedConn; - } - ).toList(); + return _sortedConn.map(rpcPeerView).toList(); } diff --git a/cyberwow/lib/controller/rpc/rpc2.dart b/cyberwow/lib/controller/rpc/rpc2.dart index f11a81a..b59de90 100644 --- a/cyberwow/lib/controller/rpc/rpc2.dart +++ b/cyberwow/lib/controller/rpc/rpc2.dart @@ -30,6 +30,7 @@ import 'package:intl/intl.dart'; import '../../config.dart' as config; import '../../helper.dart'; import '../../logging.dart'; +import 'rpc2View.dart'; Future rpc2(final String method) async { final url = 'http://${config.host}:${config.c.port}/${method}'; @@ -77,113 +78,29 @@ Future> getTransactionPoolSimple() async { return []; } else { final responseBody = json.decode(response.body); - final result = responseBody['transactions']; - if (result == null) { - return []; - } - else { - final _sortedPool = result..sort - ( - (x, y) { - final int a = x['receive_time']; - final int b = y['receive_time']; - return b.compareTo(a); - } - ); - return Stream.fromIterable(_sortedPool).asyncMap - ( - (x) async { - const _remove = - [ - 'tx_blob', - // 'tx_json', - 'last_failed_id_hash', - 'max_used_block_id_hash', - // fields not useful for noobs - 'last_relayed_time', - 'kept_by_block', - 'double_spend_seen', - 'relayed', - 'do_not_relay', - 'last_failed_height', - 'max_used_block_height', - 'weight', - // 'blob_size', - ]; - - final _filteredTx = x..removeWhere - ( - (k,v) => _remove.contains(k) - ); - - final String _tx_json = _filteredTx['tx_json']; - final _tx_json_decoded = await compute(jsonDecode, _tx_json); - - final _decodedTx = { - ..._filteredTx, - ...{'tx_decoded': _tx_json_decoded}, - }; - - final _tx = _decodedTx.map - ( - (k, v) { - if (k == 'id_hash') { - return MapEntry('id', v.substring(0, config.hashLength) + '...'); - } - - else if (k == 'blob_size') { - return MapEntry('size', (v / 1024).toStringAsFixed(2) + ' kB'); - } - - else if (k == 'fee') { - final formatter = NumberFormat.currency - ( - symbol: '', - decimalDigits: 2, - ); - return MapEntry(k, formatter.format(v / pow(10, 11)) + ' ⍵'); - } - - else if (k == 'receive_time') { - final _dateTime = DateTime.fromMillisecondsSinceEpoch(v * 1000); - final _dateFormat = DateFormat.yMd().add_jm() ; - return MapEntry('time', _dateFormat.format(_dateTime)); - } - - else if (k == 'tx_decoded') { - final _out = - { - 'vin': v['vin'].length, - 'vout': v['vout'].length, - }; - final _outString = _out['vin'].toString() + '/' + _out['vout'].toString(); - return MapEntry('in/out', _outString); - } - - else { - return MapEntry(k, v); - } - } - ); - - final List keys = - [ - 'id', - 'time', - 'fee', - 'in/out', - 'size', - ] - .where((k) => _tx.keys.contains(k)) - .toList(); - - final _sortedTx = { - for (var k in keys) k: _tx[k] - }; - - return _sortedTx; - } - ).toList(); - } + final result = asJsonArray(responseBody['transactions']); + final _sortedPool = result..sort + ( + (x, y) { + final int a = x['receive_time']; + final int b = y['receive_time']; + return b.compareTo(a); + } + ); + + final _decodedPool = await Stream.fromIterable(_sortedPool).asyncMap + ( + (x) async { + final String _tx_json = x['tx_json']; + final _tx_json_decoded = await compute(jsonDecode, _tx_json); + + return { + ...x, + ...{'tx_decoded': _tx_json_decoded}, + }; + } + ); + + return _decodedPool.map(rpcTxView).toList(); } } diff --git a/cyberwow/lib/controller/rpc/rpc2View.dart b/cyberwow/lib/controller/rpc/rpc2View.dart new file mode 100644 index 0000000..c8db8aa --- /dev/null +++ b/cyberwow/lib/controller/rpc/rpc2View.dart @@ -0,0 +1,110 @@ +/* + +Copyright 2019 fuwa + +This file is part of CyberWOW. + +CyberWOW is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +CyberWOW is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with CyberWOW. If not, see . + +*/ + +import 'dart:math'; + +import 'package:intl/intl.dart'; + +import '../../config.dart' as config; + +Map rpcTxView(x) { + const _remove = + [ + 'tx_blob', + // 'tx_json', + 'last_failed_id_hash', + 'max_used_block_id_hash', + // fields not useful for noobs + 'last_relayed_time', + 'kept_by_block', + 'double_spend_seen', + 'relayed', + 'do_not_relay', + 'last_failed_height', + 'max_used_block_height', + 'weight', + // 'blob_size', + ]; + + final _filteredTx = x..removeWhere + ( + (k,v) => _remove.contains(k) + ); + + final _tx = _filteredTx.map + ( + (k, v) { + if (k == 'id_hash') { + return MapEntry('id', v.substring(0, config.hashLength) + '...'); + } + + else if (k == 'blob_size') { + return MapEntry('size', (v / 1024).toStringAsFixed(2) + ' kB'); + } + + else if (k == 'fee') { + final formatter = NumberFormat.currency + ( + symbol: '', + decimalDigits: 2, + ); + return MapEntry(k, formatter.format(v / pow(10, 11)) + ' ⍵'); + } + + else if (k == 'receive_time') { + final _dateTime = DateTime.fromMillisecondsSinceEpoch(v * 1000); + final _dateFormat = DateFormat.yMd().add_jm() ; + return MapEntry('time', _dateFormat.format(_dateTime)); + } + + else if (k == 'tx_decoded') { + final _out = + { + 'vin': v['vin'].length, + 'vout': v['vout'].length, + }; + final _outString = _out['vin'].toString() + '/' + _out['vout'].toString(); + return MapEntry('in/out', _outString); + } + + else { + return MapEntry(k, v); + } + } + ); + + final List keys = + [ + 'id', + 'time', + 'fee', + 'in/out', + 'size', + ] + .where((k) => _tx.keys.contains(k)) + .toList(); + + final _sortedTx = { + for (var k in keys) k: _tx[k] + }; + + return _sortedTx; +} diff --git a/cyberwow/lib/controller/rpc/rpcView.dart b/cyberwow/lib/controller/rpc/rpcView.dart new file mode 100644 index 0000000..ee5a778 --- /dev/null +++ b/cyberwow/lib/controller/rpc/rpcView.dart @@ -0,0 +1,106 @@ +/* + +Copyright 2019 fuwa + +This file is part of CyberWOW. + +CyberWOW is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +CyberWOW is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with CyberWOW. If not, see . + +*/ + +import '../../config.dart' as config; + +Map rpcPeerView(Map x) { + const _remove = + [ + 'address_type', + 'connection_id', + 'host', + 'ip', + 'local_ip', + 'localhost', + 'peer_id', + 'port', + 'recv_count', + 'rpc_port', + 'send_count', + 'support_flags', + + // 'avg_download', + // 'avg_upload', + // 'current_download', + // 'current_upload', + 'rpc_credits_per_hash', + 'state', + 'recv_idle_time', + 'send_idle_time', + 'incoming', + ]; + + final _filteredConn = x..removeWhere + ( + (k,v) => _remove.contains(k) + ); + + final _conn = _filteredConn.map + ( + (k, v) { + if (k == 'connection_id') { + return MapEntry(k, v.substring(0, config.hashLength) + '...'); + } + + const speedField = + [ + 'avg_download', + 'avg_upload', + 'current_download', + 'current_upload', + ]; + if (speedField.contains(k)) { + return MapEntry(k, '${v} kB/s'); + } + + else if (k == 'live_time') { + final _duration = Duration(seconds: v); + format(Duration d) => d.toString().split('.').first.padLeft(8, "0"); + return MapEntry(k, format(_duration)); + } + + else { + return MapEntry(k, v); + } + } + ); + + final List keys = + [ + 'address', + 'height', + 'live_time', + 'current_download', + 'current_upload', + 'avg_download', + 'avg_upload', + 'pruning_seed', + ] + .where((k) => _conn.keys.contains(k)) + .toList(); + + final _sortedConn = { + for (var k in keys) k: _conn[k] + }; + + return _sortedConn; +} + diff --git a/cyberwow/lib/helper.dart b/cyberwow/lib/helper.dart index 8e9540d..cdabea8 100644 --- a/cyberwow/lib/helper.dart +++ b/cyberwow/lib/helper.dart @@ -32,5 +32,6 @@ int asInt(dynamic x) => x?.toInt() ?? 0; bool asBool(dynamic x) => x ?? false; List asList(dynamic x) => x ?? []; +List> asJsonArray(dynamic x) => x.cast>() ?? []; Map asMap(dynamic x) => x ?? {};