From 45f23c32e53b21c0b4735cf4695b675ce1db7161 Mon Sep 17 00:00:00 2001 From: aglkm <39521015+aglkm@users.noreply.github.com> Date: Tue, 24 Sep 2024 21:46:19 +0300 Subject: [PATCH] more charts code refactoring --- src/data.rs | 2 ++ src/requests.rs | 36 +++++++----------------------------- src/worker.rs | 24 +++++++++++++++++++----- 3 files changed, 28 insertions(+), 34 deletions(-) diff --git a/src/data.rs b/src/data.rs index 4e5290c..8666de9 100644 --- a/src/data.rs +++ b/src/data.rs @@ -25,6 +25,7 @@ pub struct Dashboard { pub disk_usage: String, // hashrate pub hashrate: String, + pub hashrate_kgs: String, pub difficulty: String, // mining pub production_cost: String, @@ -55,6 +56,7 @@ impl Dashboard { cap_btc: String::new(), disk_usage: String::new(), hashrate: String::new(), + hashrate_kgs: String::new(), difficulty: String::new(), production_cost: String::new(), reward_ratio: String::new(), diff --git a/src/requests.rs b/src/requests.rs index abd47f9..a58e649 100644 --- a/src/requests.rs +++ b/src/requests.rs @@ -325,7 +325,7 @@ pub fn get_disk_usage(dashboard: Arc>) -> Result<(), Error> { // Collecting: hashrate, difficulty, production cost, breakeven cost. -pub async fn get_mining_stats(dashboard: Arc>, statistics: Arc>) -> Result<(), anyhow::Error> { +pub async fn get_mining_stats(dashboard: Arc>) -> Result<(), anyhow::Error> { let difficulty_window = 1440; let height = get_current_height(dashboard.clone()); @@ -351,12 +351,15 @@ pub async fn get_mining_stats(dashboard: Arc>, statistics: Arc< // kG/s if hashrate > 1000.0 { - data.hashrate = format!("{:.2} kG/s", hashrate / 1000.0); + data.hashrate = format!("{:.2} kG/s", hashrate / 1000.0); // G/s } else { - data.hashrate = format!("{:.2} G/s", hashrate); + data.hashrate = format!("{:.2} G/s", hashrate); } + // Save hashrate as kG/s for chart stats + data.hashrate_kgs = format!("{:.2}", hashrate / 1000.0); + data.difficulty = net_diff.to_string(); if CONFIG.coingecko_api == "enabled" { @@ -374,17 +377,6 @@ pub async fn get_mining_stats(dashboard: Arc>, statistics: Arc< / (120.0 / 1000.0 * (1.0 / coins_per_hour))); } } - - let mut stats = statistics.lock().unwrap(); - - // Collect stats every day, by checking if new date was pushed into the date vector - if stats.date.len() != stats.hashrate.len() { - if stats.hashrate.len() == 30 { - stats.hashrate.remove(0); - } - - stats.hashrate.push(format!("{:.2}", hashrate / 1000.0)); - } } } @@ -630,8 +622,7 @@ pub async fn get_block_kernels(height: &String, blocks: &mut Vec) // Collecting: period_1h, period_24h, fees_1h, fees_24h. pub async fn get_txn_stats(dashboard: Arc>, - transactions: Arc>, - statistics: Arc>)-> Result<(), Error> { + transactions: Arc>)-> Result<(), Error> { let mut blocks = Vec::::new(); let height = get_current_height(dashboard.clone()); @@ -677,19 +668,6 @@ pub async fn get_txn_stats(dashboard: Arc>, txns.period_24h = ker_count_24h.to_string(); txns.fees_1h = format!("{:.2}", fees_1h / 1000000000.0); txns.fees_24h = format!("{:.2}", fees_24h / 1000000000.0); - - let mut stats = statistics.lock().unwrap(); - - // Collect stats every day, by checking if new date was pushed into the date vector - if stats.date.len() != stats.txns.len() { - if stats.txns.len() == 30 { - stats.txns.remove(0); - stats.fees.remove(0); - } - - stats.txns.push(txns.period_24h.clone()); - stats.fees.push(txns.fees_24h.clone()); - } } } diff --git a/src/worker.rs b/src/worker.rs index 0d2bb66..fe74116 100644 --- a/src/worker.rs +++ b/src/worker.rs @@ -18,21 +18,35 @@ pub async fn run(dash: Arc>, blocks: Arc>>, let _ = requests::get_connected_peers(dash.clone(), stats.clone()).await?; let _ = requests::get_market(dash.clone()).await?; requests::get_disk_usage(dash.clone())?; - let _ = requests::get_mining_stats(dash.clone(), stats.clone()).await?; + let _ = requests::get_mining_stats(dash.clone()).await?; let _ = requests::get_recent_blocks(dash.clone(), blocks.clone()).await?; - let _ = requests::get_txn_stats(dash.clone(), txns.clone(), stats.clone()).await?; + let _ = requests::get_txn_stats(dash.clone(), txns.clone()).await?; let mut stats = stats.lock().unwrap(); + let dash = dash.lock().unwrap(); + let txns = txns.lock().unwrap(); - // Every day push new date into the vector + // Every day push new stats into the vectors if let Some(v) = stats.date.last() { - // If new day, pushing date into the vector + // If new day, push the stats if *v != format!("\"{}\"", Utc::now().format("%d-%m-%Y")) { + if stats.date.len() == 30 { + stats.date.remove(0); + stats.hashrate.remove(0); + stats.txns.remove(0); + stats.fees.remove(0); + } stats.date.push(format!("\"{}\"", Utc::now().format("%d-%m-%Y"))); + stats.hashrate.push(dash.hashrate_kgs.clone()); + stats.txns.push(txns.period_24h.clone()); + stats.fees.push(txns.fees_24h.clone()); } } else { - // Vector is empty, pushing current date + // Vector is empty, pushing current stats stats.date.push(format!("\"{}\"", Utc::now().format("%d-%m-%Y"))); + stats.hashrate.push(dash.hashrate_kgs.clone()); + stats.txns.push(txns.period_24h.clone()); + stats.fees.push(txns.fees_24h.clone()); } Ok(())