refactor to rpcView

pull/2/head
fuwa 4 years ago
parent ce0c6754cf
commit 7f81b822e2

@ -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<int> incomingConnectionsCount() =>
rpc('get_info', field: 'incoming_connections_count').then(asInt);
Future<List<dynamic>> 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<List<dynamic>> 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<String> 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();
}

@ -30,6 +30,7 @@ import 'package:intl/intl.dart';
import '../../config.dart' as config;
import '../../helper.dart';
import '../../logging.dart';
import 'rpc2View.dart';
Future<http.Response> rpc2(final String method) async {
final url = 'http://${config.host}:${config.c.port}/${method}';
@ -77,113 +78,29 @@ Future<List<dynamic>> 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<String> 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();
}
}

@ -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 <https://www.gnu.org/licenses/>.
*/
import 'dart:math';
import 'package:intl/intl.dart';
import '../../config.dart' as config;
Map<String, dynamic> 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<String> 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;
}

@ -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 <https://www.gnu.org/licenses/>.
*/
import '../../config.dart' as config;
Map<String, dynamic> rpcPeerView(Map<String, dynamic> 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<String> 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;
}

@ -32,5 +32,6 @@ int asInt(dynamic x) => x?.toInt() ?? 0;
bool asBool(dynamic x) => x ?? false;
List<dynamic> asList(dynamic x) => x ?? [];
List<Map<String, dynamic>> asJsonArray(dynamic x) => x.cast<Map<String, dynamic>>() ?? [];
Map<String, dynamic> asMap(dynamic x) => x ?? {};