From 5d251081258d9da2d520f28c3c98d61f1ee99d0f Mon Sep 17 00:00:00 2001 From: aglkm <39521015+aglkm@users.noreply.github.com> Date: Tue, 24 Sep 2024 13:30:08 +0300 Subject: [PATCH] charts code refactoring --- src/data.rs | 3 --- src/requests.rs | 8 ++++---- src/worker.rs | 14 ++++++++------ 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/data.rs b/src/data.rs index 5758863..4e5290c 100644 --- a/src/data.rs +++ b/src/data.rs @@ -1,6 +1,5 @@ use serde::{Serialize, Deserialize}; - // Dashboard data #[derive(Debug)] pub struct Dashboard { @@ -215,7 +214,6 @@ impl Output { // Statistics data #[derive(Debug, Serialize)] pub struct Statistics { - pub timing: u32, pub date: Vec, // Node versions pub user_agent: Vec, @@ -231,7 +229,6 @@ pub struct Statistics { impl Statistics { pub fn new() -> Statistics { Statistics { - timing: 0, date: Vec::new(), user_agent: Vec::new(), count: Vec::new(), diff --git a/src/requests.rs b/src/requests.rs index 1280005..abd47f9 100644 --- a/src/requests.rs +++ b/src/requests.rs @@ -377,8 +377,8 @@ pub async fn get_mining_stats(dashboard: Arc>, statistics: Arc< let mut stats = statistics.lock().unwrap(); - // Save hashrate into statistics every 24H - if stats.timing == 0 || stats.timing >= 86400 { + // 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); } @@ -680,8 +680,8 @@ pub async fn get_txn_stats(dashboard: Arc>, let mut stats = statistics.lock().unwrap(); - // Save txns into statistics every 24H - if stats.timing == 0 || stats.timing >= 86400 { + // 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); diff --git a/src/worker.rs b/src/worker.rs index 26b28b1..0d2bb66 100644 --- a/src/worker.rs +++ b/src/worker.rs @@ -24,15 +24,17 @@ pub async fn run(dash: Arc>, blocks: Arc>>, let mut stats = stats.lock().unwrap(); - // Push new date and restart timer every 24H - if stats.timing == 0 || stats.timing >= 86400 { + // Every day push new date into the vector + if let Some(v) = stats.date.last() { + // If new day, pushing date into the vector + if *v != format!("\"{}\"", Utc::now().format("%d-%m-%Y")) { + stats.date.push(format!("\"{}\"", Utc::now().format("%d-%m-%Y"))); + } + } else { + // Vector is empty, pushing current date stats.date.push(format!("\"{}\"", Utc::now().format("%d-%m-%Y"))); - stats.timing = 0; } - // Increasing timing by 15 seconds (data update period) - stats.timing = stats.timing + 15; - Ok(()) }