charts code refactoring

This commit is contained in:
aglkm
2024-09-24 13:30:08 +03:00
parent 3a08d82902
commit 5d25108125
3 changed files with 12 additions and 13 deletions

View File

@@ -1,6 +1,5 @@
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
// Dashboard data // Dashboard data
#[derive(Debug)] #[derive(Debug)]
pub struct Dashboard { pub struct Dashboard {
@@ -215,7 +214,6 @@ impl Output {
// Statistics data // Statistics data
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
pub struct Statistics { pub struct Statistics {
pub timing: u32,
pub date: Vec<String>, pub date: Vec<String>,
// Node versions // Node versions
pub user_agent: Vec<String>, pub user_agent: Vec<String>,
@@ -231,7 +229,6 @@ pub struct Statistics {
impl Statistics { impl Statistics {
pub fn new() -> Statistics { pub fn new() -> Statistics {
Statistics { Statistics {
timing: 0,
date: Vec::new(), date: Vec::new(),
user_agent: Vec::new(), user_agent: Vec::new(),
count: Vec::new(), count: Vec::new(),

View File

@@ -377,8 +377,8 @@ pub async fn get_mining_stats(dashboard: Arc<Mutex<Dashboard>>, statistics: Arc<
let mut stats = statistics.lock().unwrap(); let mut stats = statistics.lock().unwrap();
// Save hashrate into statistics every 24H // Collect stats every day, by checking if new date was pushed into the date vector
if stats.timing == 0 || stats.timing >= 86400 { if stats.date.len() != stats.hashrate.len() {
if stats.hashrate.len() == 30 { if stats.hashrate.len() == 30 {
stats.hashrate.remove(0); stats.hashrate.remove(0);
} }
@@ -680,8 +680,8 @@ pub async fn get_txn_stats(dashboard: Arc<Mutex<Dashboard>>,
let mut stats = statistics.lock().unwrap(); let mut stats = statistics.lock().unwrap();
// Save txns into statistics every 24H // Collect stats every day, by checking if new date was pushed into the date vector
if stats.timing == 0 || stats.timing >= 86400 { if stats.date.len() != stats.txns.len() {
if stats.txns.len() == 30 { if stats.txns.len() == 30 {
stats.txns.remove(0); stats.txns.remove(0);
stats.fees.remove(0); stats.fees.remove(0);

View File

@@ -24,15 +24,17 @@ pub async fn run(dash: Arc<Mutex<Dashboard>>, blocks: Arc<Mutex<Vec<Block>>>,
let mut stats = stats.lock().unwrap(); let mut stats = stats.lock().unwrap();
// Push new date and restart timer every 24H // Every day push new date into the vector
if stats.timing == 0 || stats.timing >= 86400 { 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.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(()) Ok(())
} }