From 8b5d7ff2ea1066fbb11369f78d2a66e8085e88b8 Mon Sep 17 00:00:00 2001 From: aglkm <39521015+aglkm@users.noreply.github.com> Date: Tue, 1 Oct 2024 00:13:07 +0300 Subject: [PATCH] adding outputs & kernels into the dashboard --- src/data.rs | 3 ++ src/main.rs | 50 +++++++++++++++++++-- src/requests.rs | 23 ++++++---- templates/index.html.tera | 95 ++++++++++++++++++++++++++------------- 4 files changed, 128 insertions(+), 43 deletions(-) diff --git a/src/data.rs b/src/data.rs index 46d5b45..66aedb7 100644 --- a/src/data.rs +++ b/src/data.rs @@ -49,6 +49,8 @@ pub struct Dashboard { pub stem: String, // utxo pub utxo_count: String, + // kernel + pub kernel_mmr_size: String, } impl Dashboard { @@ -79,6 +81,7 @@ impl Dashboard { txns: String::new(), stem: String::new(), utxo_count: String::new(), + kernel_mmr_size: String::new(), } } } diff --git a/src/main.rs b/src/main.rs index 02a2fe9..f437884 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ #[macro_use] extern crate rocket; use chrono::Utc; use either::Either; +use num_format::{Locale, ToFormattedString}; use rocket_dyn_templates::{Template, context}; use rocket::fs::FileServer; use rocket::{State, tokio}; @@ -10,7 +11,7 @@ use std::sync::{Arc, Mutex}; use std::time::Duration; use serde_json::Value; -use crate::data::{Block, Dashboard, Kernel, Output, Statistics, Transactions, OUTPUT_SIZE}; +use crate::data::{Block, Dashboard, Kernel, Output, Statistics, Transactions, OUTPUT_SIZE, KERNEL_SIZE}; use crate::requests::CONFIG; mod data; @@ -649,7 +650,50 @@ fn block_list_index(dashboard: &State>>) -> String { "".to_string() } -// End of HTMX backends. + + +#[get("/rpc/blockchain/unspent_outputs")] +fn unspent_outputs(dashboard: &State>>) -> String { + let data = dashboard.lock().unwrap(); + + if data.utxo_count.is_empty() == false { + let utxo_count = data.utxo_count.parse::().unwrap(); + let mut utxo_size = utxo_count as f64 * OUTPUT_SIZE as f64 / 1000.0 / 1000.0; + let mut unit = "MB"; + + if utxo_size > 1000000000.0 { + unit = "GB"; + utxo_size = utxo_size / 1000.0; + } + + return format!("{} ({:.2} {})", utxo_count.to_formatted_string(&Locale::en), utxo_size, unit); + } + + "".to_string() +} + + +#[get("/rpc/blockchain/kernels")] +fn kernels(dashboard: &State>>) -> String { + let data = dashboard.lock().unwrap(); + + if data.utxo_count.is_empty() == false { + let kernel_count = data.kernel_mmr_size.parse::().unwrap() / 2; + let mut kernel_size = kernel_count as f64 * KERNEL_SIZE as f64 / 1000.0 / 1000.0; + let mut unit = "MB"; + + if kernel_size > 1000000000.0 { + unit = "GB"; + kernel_size = kernel_size / 1000.0; + } + + return format!("{} ({:.2} {})", kernel_count.to_formatted_string(&Locale::en), kernel_size, unit); + } + + "".to_string() +} + +// End of HTMX routes. // Main @@ -731,7 +775,7 @@ async fn main() { block_size, block_weight, block_details_by_height, block_header_by_hash, soft_supply, production_cost, reward_ratio, breakeven_cost, last_block_age, block_list_by_height, block_list_index, search, kernel, - output, api_owner, api_foreign, stats]) + output, api_owner, api_foreign, stats, unspent_outputs, kernels]) .mount("/static", FileServer::from("static")) .attach(Template::fairing()) .launch() diff --git a/src/requests.rs b/src/requests.rs index 94a9349..34e3e9d 100644 --- a/src/requests.rs +++ b/src/requests.rs @@ -148,17 +148,24 @@ pub async fn call_external(method: &str, params: &str, id: &str, rpc_type: &str, } -// Collecting: height, sync, node_ver, proto_ver. +// Collecting: height, sync, node_ver, proto_ver, kernel_mmr_size. pub async fn get_status(dashboard: Arc>) -> Result<(), anyhow::Error> { - let resp = call("get_status", "[]", "1", "owner").await?; + let resp1 = call("get_status", "[]", "1", "owner").await?; - let mut data = dashboard.lock().unwrap(); + if resp1 != Value::Null { + let params = &format!("[{}, null, null]", resp1["result"]["Ok"]["tip"]["height"])[..]; + let resp2 = call("get_block", params, "1", "foreign").await?; - if resp != Value::Null { - data.height = resp["result"]["Ok"]["tip"]["height"].to_string(); - data.sync = resp["result"]["Ok"]["sync_status"].as_str().unwrap().to_string(); - data.node_ver = resp["result"]["Ok"]["user_agent"].as_str().unwrap().to_string(); - data.proto_ver = resp["result"]["Ok"]["protocol_version"].to_string(); + let mut data = dashboard.lock().unwrap(); + + if resp2 != Value::Null { + data.kernel_mmr_size = resp2["result"]["Ok"]["header"]["kernel_mmr_size"].to_string(); + } + + data.height = resp1["result"]["Ok"]["tip"]["height"].to_string(); + data.sync = resp1["result"]["Ok"]["sync_status"].as_str().unwrap().to_string(); + data.node_ver = resp1["result"]["Ok"]["user_agent"].as_str().unwrap().to_string(); + data.proto_ver = resp1["result"]["Ok"]["protocol_version"].to_string(); } Ok(()) diff --git a/templates/index.html.tera b/templates/index.html.tera index 1d6e9e6..4abe1d0 100644 --- a/templates/index.html.tera +++ b/templates/index.html.tera @@ -66,39 +66,31 @@
-
-
-
-
BLOCKCHAIN
-
-
-
Size 
-
-
-
-
Block Height 
-
-
-
-
Time Since Last Block 
-
+
+
+
BLOCKCHAIN
+
+
+
Size 
-
-
-
-
TRANSACTIONS & FEES
-
-
-
1H Period 
-
-
-
-
24H Period 
-
+
+
+
Block Height 
+
+
+
+
Time Since Last Block 
+
+
+
+
Unspent Outputs 
+
+
+
+
Kernels 
-
MINING
@@ -146,7 +138,20 @@
-
+
+
+
TRANSACTIONS & FEES
+
+
+
1H Period 
+
+
+
+
24H Period 
+
+
+
+
MEMPOOL

@@ -159,7 +164,9 @@
-
+
+
+
CONNECTIONS

@@ -172,7 +179,7 @@
-
+
NODE

@@ -264,6 +271,14 @@
Time Since Last Block 
+
+
+
Unspent Outputs 
+
+
+
+
Kernels 
+
@@ -519,6 +534,14 @@
Time Since Last Block 
+
+
+
Unspent Outputs 
+
+
+
+
Kernels 
+
@@ -626,6 +649,14 @@
Time Since Last Block 
+
+
+
Unspent Outputs 
+
+
+
+
Kernels 
+