mirror of
https://github.com/transatoshi-mw/grin-explorer.git
synced 2025-10-21 13:33:41 +00:00
more charts code refactoring
This commit is contained in:
@@ -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(),
|
||||
|
@@ -325,7 +325,7 @@ pub fn get_disk_usage(dashboard: Arc<Mutex<Dashboard>>) -> Result<(), Error> {
|
||||
|
||||
|
||||
// Collecting: hashrate, difficulty, production cost, breakeven cost.
|
||||
pub async fn get_mining_stats(dashboard: Arc<Mutex<Dashboard>>, statistics: Arc<Mutex<Statistics>>) -> Result<(), anyhow::Error> {
|
||||
pub async fn get_mining_stats(dashboard: Arc<Mutex<Dashboard>>) -> 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<Mutex<Dashboard>>, 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<Mutex<Dashboard>>, 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<Block>)
|
||||
|
||||
// Collecting: period_1h, period_24h, fees_1h, fees_24h.
|
||||
pub async fn get_txn_stats(dashboard: Arc<Mutex<Dashboard>>,
|
||||
transactions: Arc<Mutex<Transactions>>,
|
||||
statistics: Arc<Mutex<Statistics>>)-> Result<(), Error> {
|
||||
transactions: Arc<Mutex<Transactions>>)-> Result<(), Error> {
|
||||
let mut blocks = Vec::<Block>::new();
|
||||
let height = get_current_height(dashboard.clone());
|
||||
|
||||
@@ -677,19 +668,6 @@ pub async fn get_txn_stats(dashboard: Arc<Mutex<Dashboard>>,
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -18,21 +18,35 @@ pub async fn run(dash: Arc<Mutex<Dashboard>>, blocks: Arc<Mutex<Vec<Block>>>,
|
||||
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(())
|
||||
|
Reference in New Issue
Block a user