From a4603ea536919055bddf8b37db61b46454358140 Mon Sep 17 00:00:00 2001 From: aglkm <39521015+aglkm@users.noreply.github.com> Date: Sun, 22 Sep 2024 17:14:03 +0300 Subject: [PATCH] adding hashrate chart --- src/data.rs | 14 +++-- src/main.rs | 4 +- src/requests.rs | 21 ++++++- src/worker.rs | 2 +- templates/stats.html.tera | 120 ++++++++++++++------------------------ 5 files changed, 76 insertions(+), 85 deletions(-) diff --git a/src/data.rs b/src/data.rs index 0d1e89e..3712b31 100644 --- a/src/data.rs +++ b/src/data.rs @@ -213,19 +213,25 @@ impl Output { // Statistics data -#[derive(Debug)] +#[derive(Debug, Serialize)] pub struct Statistics { + pub timing: u32, pub user_agent: Vec, pub count: Vec, pub total: u32, + pub hashrate: Vec, + pub hash_date: Vec, } impl Statistics { pub fn new() -> Statistics { Statistics { - user_agent: Vec::new(), - count: Vec::new(), - total: 0, + timing: 0, + user_agent: Vec::new(), + count: Vec::new(), + total: 0, + hashrate: Vec::new(), + hash_date: Vec::new(), } } } diff --git a/src/main.rs b/src/main.rs index b77417b..f732d4c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -223,6 +223,8 @@ fn stats(statistics: &State>>) -> Template { user_agent: data.user_agent.clone(), count: data.count.clone(), total: data.total, + hashrate: data.hashrate.clone(), + hash_date: data.hash_date.clone(), }) } @@ -673,7 +675,7 @@ async fn main() { error!("{}", e); }, } - + tokio::time::sleep(Duration::from_secs(15)).await; } }); diff --git a/src/requests.rs b/src/requests.rs index 9df4796..c0922ee 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>) -> Result<(), anyhow::Error> { +pub async fn get_mining_stats(dashboard: Arc>, statistics: Arc>) -> Result<(), anyhow::Error> { let difficulty_window = 1440; let height = get_current_height(dashboard.clone()); @@ -356,7 +356,7 @@ pub async fn get_mining_stats(dashboard: Arc>) -> Result<(), an } else { data.hashrate = format!("{:.2} G/s", hashrate); } - + data.difficulty = net_diff.to_string(); if CONFIG.coingecko_api == "enabled" { @@ -374,6 +374,23 @@ pub async fn get_mining_stats(dashboard: Arc>) -> Result<(), an / (120.0 / 1000.0 * (1.0 / coins_per_hour))); } } + + let mut stats = statistics.lock().unwrap(); + + // Save hashrate into statistics every 24H + if stats.timing == 0 || stats.timing >= 86400 { + if stats.hashrate.len() == 30 { + stats.hashrate.remove(0); + } + + stats.hashrate.push(format!("{:.2}", hashrate / 1000.0)); + stats.hash_date.push(format!("\"{}\"", Utc::now().format("%d-%m-%Y"))); + + stats.timing = 0; + } + + // Increasing timing by 15 seconds (our data update period) + stats.timing = stats.timing + 15; } } diff --git a/src/worker.rs b/src/worker.rs index a5ecf3e..075150e 100644 --- a/src/worker.rs +++ b/src/worker.rs @@ -17,7 +17,7 @@ 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()).await?; + let _ = requests::get_mining_stats(dash.clone(), stats.clone()).await?; let _ = requests::get_recent_blocks(dash.clone(), blocks.clone()).await?; let _ = requests::get_txn_stats(dash.clone(), txns.clone()).await?; diff --git a/templates/stats.html.tera b/templates/stats.html.tera index 396621b..9270036 100644 --- a/templates/stats.html.tera +++ b/templates/stats.html.tera @@ -9,95 +9,30 @@
-
NODE VERSION STATS ({{ total }})
+
NODE VERSIONS ({{ total }})
-
-
-
-
-
- # -
-
-
-
-
-
- VERSION -
-
-
-
-
-
- COUNT -
-
+
+
+
+
HASHRATE (kG/s)
+
- - {% for v in user_agent %} -
-
-
-
- {{ loop.index }} -
-
-
-
-
-
- {{ v }} -
-
-
-
-
-
- {{ count[loop.index0] }} -
-
-
-
- {% endfor %} -
- -
- {% for v in user_agent %} - {% if loop.index0 == 0 %} -
- {% else %} -
- {% endif %} -
-
-
- #{{ loop.index }} -
-
-
-
- {{ v }} -
-
- {{ count[loop.index0] }} -
-
-
-
- {% endfor %}