From 88404a635b6d5bb1b1ad2e15ca2c3e795746681a Mon Sep 17 00:00:00 2001 From: Revertron Date: Fri, 21 May 2021 23:32:46 +0200 Subject: [PATCH] Added statistics to main screen. --- src/blockchain/chain.rs | 18 ++++++++++++++++++ src/event.rs | 2 +- src/main.rs | 1 + src/p2p/network.rs | 21 ++++++++++++++------- src/web_ui.rs | 9 ++++++--- src/webview/index.html | 31 +++++++++++++++++++++++++++++-- src/webview/scripts.js | 8 +++++--- src/webview/styles.css | 6 ++++++ 8 files changed, 80 insertions(+), 16 deletions(-) diff --git a/src/blockchain/chain.rs b/src/blockchain/chain.rs index c46457d..07a20f1 100644 --- a/src/blockchain/chain.rs +++ b/src/blockchain/chain.rs @@ -37,6 +37,8 @@ const SQL_GET_LAST_FULL_BLOCK_FOR_KEY: &str = "SELECT * FROM blocks WHERE id < ? const SQL_GET_DOMAIN_OWNER_BY_ID: &str = "SELECT signing FROM domains WHERE id < ? AND identity = ? ORDER BY id DESC LIMIT 1;"; const SQL_GET_DOMAIN_BY_ID: &str = "SELECT * FROM domains WHERE identity = ? ORDER BY id DESC LIMIT 1;"; const SQL_GET_DOMAINS_BY_KEY: &str = "SELECT * FROM domains WHERE signing = ? ORDER BY id;"; +const SQL_GET_DOMAINS_COUNT: &str = "SELECT count(DISTINCT identity) FROM domains;"; +const SQL_GET_USERS_COUNT: &str = "SELECT count(DISTINCT pub_key) FROM blocks;"; const SQL_GET_OPTIONS: &str = "SELECT * FROM options;"; @@ -601,6 +603,22 @@ impl Chain { } } + pub fn get_domains_count(&self) -> i64 { + let mut statement = self.db.prepare(SQL_GET_DOMAINS_COUNT).unwrap(); + while let State::Row = statement.next().unwrap() { + return statement.read::(0).unwrap(); + } + 0 + } + + pub fn get_users_count(&self) -> i64 { + let mut statement = self.db.prepare(SQL_GET_USERS_COUNT).unwrap(); + while let State::Row = statement.next().unwrap() { + return statement.read::(0).unwrap(); + } + 0 + } + pub fn get_my_domains(&self, keystore: Option<&Keystore>) -> HashMap { if keystore.is_none() { return HashMap::new(); diff --git a/src/event.rs b/src/event.rs index fd0327c..db45dfd 100644 --- a/src/event.rs +++ b/src/event.rs @@ -12,7 +12,7 @@ pub enum Event { BlockchainChanged { index: u64 }, ActionStopMining, ActionQuit, - NetworkStatus { nodes: usize, blocks: u64 }, + NetworkStatus { blocks: u64, domains: i64, keys: i64, nodes: usize }, Syncing { have: u64, height: u64 }, SyncFinished, } diff --git a/src/main.rs b/src/main.rs index 1d1c8cd..1dbf756 100644 --- a/src/main.rs +++ b/src/main.rs @@ -119,6 +119,7 @@ fn main() { } return; } + info!("Blocks count: {}, domains count: {}, users count: {}", chain.get_height(), chain.get_domains_count(), chain.get_users_count()); let settings_copy = settings.clone(); let mut keys = Vec::new(); if settings.key_files.len() > 0 { diff --git a/src/p2p/network.rs b/src/p2p/network.rs index 3303a60..141b239 100644 --- a/src/p2p/network.rs +++ b/src/p2p/network.rs @@ -129,8 +129,10 @@ impl Network { token => { if !handle_connection_event(Arc::clone(&context), &mut peers, &poll.registry(), &event) { let _ = peers.close_peer(poll.registry(), &token); - let blocks_count = context.lock().unwrap().chain.get_height(); - post(crate::event::Event::NetworkStatus { nodes: peers.get_peers_active_count(), blocks: blocks_count }); + let blocks = context.lock().unwrap().chain.get_height(); + let keys = context.lock().unwrap().chain.get_users_count(); + let domains = context.lock().unwrap().chain.get_domains_count(); + post(crate::event::Event::NetworkStatus { blocks, domains, keys, nodes: peers.get_peers_active_count() }); } } } @@ -151,13 +153,16 @@ impl Network { // Send pings to idle peers let (height, hash) = { let context = context.lock().unwrap(); - let height = context.chain.get_height(); + let blocks = context.chain.get_height(); let nodes = peers.get_peers_active_count(); let banned = peers.get_peers_banned_count(); - post(crate::event::Event::NetworkStatus { nodes, blocks: height }); + + let keys = context.chain.get_users_count(); + let domains = context.chain.get_domains_count(); + post(crate::event::Event::NetworkStatus { blocks, domains, keys, nodes }); if log_timer.elapsed().as_secs() > LOG_REFRESH_DELAY_SEC { - info!("Active nodes count: {}, banned count: {}, blocks count: {}", nodes, banned, height); + info!("Active nodes count: {}, banned count: {}, blocks count: {}", nodes, banned, blocks); let elapsed = last_events_time.elapsed().as_secs(); if elapsed >= 10 { warn!("Last network events time {} seconds ago", elapsed); @@ -168,7 +173,7 @@ impl Network { peers.connect_new_peers(poll.registry(), &mut unique_token, yggdrasil_only); connect_timer = Instant::now(); } - (height, context.chain.get_last_hash()) + (blocks, context.chain.get_last_hash()) }; peers.update(poll.registry(), height, hash); ui_timer = Instant::now(); @@ -534,7 +539,9 @@ fn handle_block(context: Arc>, peers: &mut Peers, token: &Token, let event = crate::event::Event::Syncing { have: my_height, height: max(max_height, my_height) }; post(event); } - post(crate::event::Event::NetworkStatus { nodes: peers_count, blocks: my_height }); + let domains = context.chain.get_domains_count(); + let keys = context.chain.get_users_count(); + post(crate::event::Event::NetworkStatus { blocks: my_height, domains, keys, nodes: peers_count }); } BlockQuality::Twin => { debug!("Ignoring duplicate block {}", block.index); } BlockQuality::Future => { debug!("Ignoring future block {}", block.index); } diff --git a/src/web_ui.rs b/src/web_ui.rs index a440e23..b340630 100644 --- a/src/web_ui.rs +++ b/src/web_ui.rs @@ -303,11 +303,11 @@ fn action_loaded(context: &Arc>, web_view: &mut WebView<()>) { format!("setLeftStatusBarText('Idle'); showMiningIndicator(false, false);") } } - Event::NetworkStatus { nodes, blocks } => { + Event::NetworkStatus { blocks, domains, keys, nodes } => { if status.mining || status.syncing || nodes < 3 { - format!("setRightStatusBarText('Nodes: {}, Blocks: {}')", nodes, blocks) + format!("setStats({}, {}, {}, {});", blocks, domains, keys, nodes) } else { - format!("setLeftStatusBarText('Idle'); setRightStatusBarText('Nodes: {}, Blocks: {}')", nodes, blocks) + format!("setLeftStatusBarText('Idle'); setStats({}, {}, {}, {});", blocks, domains, keys, nodes) } } Event::BlockchainChanged {index} => { @@ -343,6 +343,9 @@ fn action_loaded(context: &Arc>, web_view: &mut WebView<()>) { let _ = web_view.eval(&format!("zonesChanged('{}');", &zones)); } send_keys_to_ui(&c, &web_view.handle()); + if let Err(e) = web_view.eval(&format!("setStats({}, {}, {}, {});", c.chain.get_height(), c.chain.get_domains_count(), c.chain.get_users_count(), 0)) { + error!("Error evaluating stats: {}", e); + } event_info(web_view, "Application loaded"); } diff --git a/src/webview/index.html b/src/webview/index.html index 5d5c139..f1294fe 100644 --- a/src/webview/index.html +++ b/src/webview/index.html @@ -82,6 +82,33 @@

To mine domains you need to mine a strong pair of signing keys and a pair of encryption keys.

+ + @@ -138,7 +165,7 @@
-
+
@@ -152,7 +179,7 @@
-
No data
+
🚀
diff --git a/src/webview/scripts.js b/src/webview/scripts.js index 1c2bcdd..40080b3 100644 --- a/src/webview/scripts.js +++ b/src/webview/scripts.js @@ -510,9 +510,11 @@ function setLeftStatusBarText(text) { bar.innerHTML = text; } -function setRightStatusBarText(text) { - var bar = document.getElementById("status_bar_right"); - bar.innerHTML = text; +function setStats(blocks, domains, keys, nodes) { + document.getElementById("stat_blocks").innerHTML = blocks; + document.getElementById("stat_domains").innerHTML = domains; + document.getElementById("stat_keys").innerHTML = keys; + document.getElementById("stat_nodes").innerHTML = nodes; } function addEvent(type, time, message) { diff --git a/src/webview/styles.css b/src/webview/styles.css index 2b683e8..a34bc63 100644 --- a/src/webview/styles.css +++ b/src/webview/styles.css @@ -148,6 +148,12 @@ th, td { background-color: rgba(250, 250, 250, 0.7); } +nav.level { + width: 100%; + position: absolute; + bottom: 6em; +} + .list { overflow-y: auto; }