From 3a08d82902ff92c32f3dbfad3125ae876074e086 Mon Sep 17 00:00:00 2001 From: aglkm <39521015+aglkm@users.noreply.github.com> Date: Mon, 23 Sep 2024 02:24:17 +0300 Subject: [PATCH] adding txns & fees chart --- src/data.rs | 21 +++++++---- src/main.rs | 4 ++- src/requests.rs | 22 ++++++++---- src/worker.rs | 14 +++++++- templates/stats.html.tera | 74 ++++++++++++++++++++++++++++++++++++++- 5 files changed, 118 insertions(+), 17 deletions(-) diff --git a/src/data.rs b/src/data.rs index 3712b31..5758863 100644 --- a/src/data.rs +++ b/src/data.rs @@ -216,22 +216,29 @@ impl Output { #[derive(Debug, Serialize)] pub struct Statistics { pub timing: u32, + pub date: Vec, + // Node versions pub user_agent: Vec, pub count: Vec, pub total: u32, + // Hashrate pub hashrate: Vec, - pub hash_date: Vec, + // Transactions & fees + pub txns: Vec, + pub fees: Vec, } impl Statistics { pub fn new() -> Statistics { Statistics { - timing: 0, - user_agent: Vec::new(), - count: Vec::new(), - total: 0, - hashrate: Vec::new(), - hash_date: Vec::new(), + timing: 0, + date: Vec::new(), + user_agent: Vec::new(), + count: Vec::new(), + total: 0, + hashrate: Vec::new(), + txns: Vec::new(), + fees: Vec::new(), } } } diff --git a/src/main.rs b/src/main.rs index f732d4c..feb8408 100644 --- a/src/main.rs +++ b/src/main.rs @@ -220,11 +220,13 @@ fn stats(statistics: &State>>) -> Template { Template::render("stats", context! { route: "stats", + date: data.date.clone(), user_agent: data.user_agent.clone(), count: data.count.clone(), total: data.total, hashrate: data.hashrate.clone(), - hash_date: data.hash_date.clone(), + txns: data.txns.clone(), + fees: data.fees.clone(), }) } diff --git a/src/requests.rs b/src/requests.rs index c0922ee..1280005 100644 --- a/src/requests.rs +++ b/src/requests.rs @@ -384,13 +384,7 @@ pub async fn get_mining_stats(dashboard: Arc>, statistics: Arc< } 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; } } @@ -636,7 +630,8 @@ 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>) -> Result<(), Error> { + transactions: Arc>, + statistics: Arc>)-> Result<(), Error> { let mut blocks = Vec::::new(); let height = get_current_height(dashboard.clone()); @@ -682,6 +677,19 @@ 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(); + + // Save txns into statistics every 24H + if stats.timing == 0 || stats.timing >= 86400 { + 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 075150e..26b28b1 100644 --- a/src/worker.rs +++ b/src/worker.rs @@ -1,3 +1,4 @@ +use chrono::Utc; use std::sync::{Arc, Mutex}; use crate::data::Block; @@ -19,7 +20,18 @@ pub async fn run(dash: Arc>, blocks: Arc>>, requests::get_disk_usage(dash.clone())?; 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?; + let _ = requests::get_txn_stats(dash.clone(), txns.clone(), stats.clone()).await?; + + let mut stats = stats.lock().unwrap(); + + // Push new date and restart timer every 24H + if stats.timing == 0 || stats.timing >= 86400 { + 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(()) } diff --git a/templates/stats.html.tera b/templates/stats.html.tera index 5922cc5..d1071b4 100644 --- a/templates/stats.html.tera +++ b/templates/stats.html.tera @@ -23,6 +23,15 @@ + +
+
+
+
TRANSACTIONS & FEES
+
+
+
+
@@ -60,6 +69,7 @@ options: options }); + var options = { maintainAspectRatio: false, @@ -70,11 +80,23 @@ legend: { display: false }, + }, + scales: { + x: { + grid: { + display: false + } + }, + y: { + grid: { + display: false + } + }, } }; var data = { - labels: {{ hash_date }}, + labels: {{ date }}, datasets: [{ label: 'Hashrate (kG/s)', data: {{ hashrate }}, @@ -91,6 +113,56 @@ options: options }); + + + var options = { + maintainAspectRatio: false, + interaction: { + intersect: false, + }, + plugins: { + legend: { + display: true + }, + }, + scales: { + x: { + grid: { + display: false + } + }, + y: { + grid: { + display: false + } + }, + } + }; + + var data = { + labels: {{ date }}, + datasets: [ + { + label: 'Transactions', + data: {{ txns }}, + fill: false, + tension: 0.1 + }, + { + label: 'Fees', + data: {{ fees }}, + fill: false, + tension: 0.1 + } + ] + }; + + new Chart(document.getElementById("3"), { + type: 'line', + data: data, + options: options + }); +