From 0396f19fa6553e624a92b323488aaca36ec4ac1b Mon Sep 17 00:00:00 2001 From: Arthur Date: Mon, 27 May 2024 19:10:30 +0100 Subject: [PATCH] Added elided hash And a couple of bits cleaned up --- src/main.rs | 47 +++++++++++++++++++++++++-------------- src/monero_rpc.rs | 7 +++--- templates/index.html.tera | 4 ++-- 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/src/main.rs b/src/main.rs index 01d24fb..280e045 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,8 @@ extern crate rocket; use readable::byte::Byte; -use readable::up::UptimeFull; use readable::date::Date; use readable::time::{secs_to_clock, unix_clock}; -use reqwest; +use readable::up::UptimeFull; use rocket::serde::{Deserialize, Serialize}; use rocket::{fairing::AdHoc, get, launch, routes, State}; use rocket_dyn_templates::{context, Template}; @@ -14,7 +13,7 @@ use tokio::task; use vnstat_parse::Vnstat; mod monero_rpc; -use monero_rpc::{NodeInfoOuter, NodeInfo, BlockHeaderInfosOuter, BlockHeaderInfo}; +use monero_rpc::{BlockHeaderInfo, BlockHeaderInfosOuter, NodeInfo, NodeInfoOuter}; #[derive(Serialize, Debug)] struct ComponentInfo { @@ -67,7 +66,11 @@ struct SysInfo { restart_needed: bool, } -fn get_system_info(network_if_names: &Vec, mount_names: &Vec, temperature_sensors: &Vec>) -> SysInfo { +fn get_system_info( + network_if_names: &Vec, + mount_names: &Vec, + temperature_sensors: &Vec>, +) -> SysInfo { let mut sys = System::new_all(); sys.refresh_all(); let cpu_load = System::load_average(); @@ -167,11 +170,7 @@ fn get_node_info(json_rpc_url: &str) -> NodeInfo { let client = reqwest::blocking::Client::new(); // TODO: Properly handle this unwrap - half the point of this is to capture // when the node is down - don't want it crashing when - let resp = client - .post(json_rpc_url) - .json(&map) - .send() - .unwrap(); + let resp = client.post(json_rpc_url).json(&map).send().unwrap(); resp.json::() }) .unwrap(); @@ -181,6 +180,16 @@ fn get_node_info(json_rpc_url: &str) -> NodeInfo { node_info.time_up_pretty = UptimeFull::from(approx_uptime).to_string(); node_info.db_size_pretty = Byte::from(node_info.database_size).to_string(); node_info.free_space_pretty = Byte::from(node_info.free_space).to_string(); + node_info.top_block_hash_elided = format!( + "{}...{}", + node_info.top_block_hash.chars().take(8).collect::(), + node_info + .top_block_hash + .chars() + .skip(56) + .take(8) + .collect::() + ); node_info } @@ -213,18 +222,18 @@ fn get_latest_twenty_blocks(json_rpc_url: &str, current_height: u64) -> Vec() - }).unwrap(); + }) + .unwrap(); let mut latest_twenty_blocks = blocks.result.headers; for i in 0..latest_twenty_blocks.len() { let clock_time = secs_to_clock(unix_clock(latest_twenty_blocks[i].timestamp)); let date = Date::from_unix(latest_twenty_blocks[i].timestamp).unwrap(); - latest_twenty_blocks[i].timestamp_pretty = format!("{} {:02}-{:02}-{:02}", date, clock_time.0, clock_time.1, clock_time.2); + latest_twenty_blocks[i].timestamp_pretty = format!( + "{} {:02}-{:02}-{:02}", + date, clock_time.0, clock_time.1, clock_time.2 + ); } latest_twenty_blocks } @@ -255,7 +264,11 @@ impl Default for Config { fn index(config: &State) -> Template { let node_info = get_node_info(&config.json_rpc_url); let latest_twenty_blocks = get_latest_twenty_blocks(&config.json_rpc_url, node_info.height); - let system_context = get_system_info(&config.network_interfaces, &config.mount_names, &config.temperature_sensors); + let system_context = get_system_info( + &config.network_interfaces, + &config.mount_names, + &config.temperature_sensors, + ); let context = context! { system: system_context, node_info: node_info, diff --git a/src/monero_rpc.rs b/src/monero_rpc.rs index d2758e1..89c13b9 100644 --- a/src/monero_rpc.rs +++ b/src/monero_rpc.rs @@ -41,7 +41,7 @@ pub struct NodeInfo { target: u64, target_height: u64, testnet: bool, - top_block_hash: String, + pub top_block_hash: String, top_hash: String, tx_count: u64, tx_pool_size: u64, @@ -58,6 +58,8 @@ pub struct NodeInfo { pub free_space_pretty: String, #[serde(skip_deserializing, default = "none")] pub db_size_pretty: String, + #[serde(skip_deserializing, default = "none")] + pub top_block_hash_elided: String, } #[derive(Serialize, Deserialize, Debug)] @@ -101,10 +103,9 @@ pub struct BlockHeaderInfo { wide_cumulative_difficulty: String, wide_difficulty: String, #[serde(skip_deserializing, default = "none")] - pub timestamp_pretty: String + pub timestamp_pretty: String, } - fn none() -> String { "".to_string() } diff --git a/templates/index.html.tera b/templates/index.html.tera index 14ae5e7..7ea6c9f 100644 --- a/templates/index.html.tera +++ b/templates/index.html.tera @@ -124,7 +124,7 @@

Blockheight: {{ node_info.height }}

-

Latest block hash: {{ node_info.top_block_hash }}

+

Latest block hash: {{ node_info.top_block_hash_elided }}

Free Space: {{ node_info.free_space_pretty }}

@@ -138,7 +138,7 @@ Height - Size + Size (Bytes) Transactions Timestamp (UTC) Hash