Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,624 changes: 1,565 additions & 59 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ anyhow = "1.0.100"
config = "0.15.19"
crossterm = "0.29.0"
ratatui = "0.29.0"
tokio = { version = "1.49.0", features = ["full"] }
reqwest = { version = "0.13.2", features = ["json"] }
serde_json = "1.0.149"

[dev-dependencies]
insta = "1.44.3"
Expand Down
10 changes: 9 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

use crate::components::file_explorer::FileExplorer;
use crate::components::metrics::BitcoinMetrics;
use crate::config::BitcoinConfig;
use std::path::PathBuf;

#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum CurrentScreen {
Home,
BitcoinStatus,
BitcoinConfig,
FileExplorer,
Exiting,
Expand All @@ -18,6 +21,8 @@ pub struct App {
pub sidebar_index: usize,
pub bitcoin_conf_path: Option<PathBuf>,
pub explorer: FileExplorer,
pub bitcoin_config: Option<BitcoinConfig>,
pub bitcoin_metrics: Option<BitcoinMetrics>,
}

impl App {
Expand All @@ -27,6 +32,8 @@ impl App {
sidebar_index: 0,
bitcoin_conf_path: None,
explorer: FileExplorer::new(),
bitcoin_config: None,
bitcoin_metrics: None,
}
}

Expand All @@ -35,6 +42,7 @@ impl App {
match self.sidebar_index {
0 => self.current_screen = CurrentScreen::Home,
1 => self.current_screen = CurrentScreen::BitcoinConfig,
2 => self.current_screen = CurrentScreen::BitcoinStatus,
_ => {}
}
}
Expand Down
69 changes: 69 additions & 0 deletions src/components/metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// SPDX-FileCopyrightText: 2024 PDM Authors
// SPDX-License-Identifier: AGPL-3.0-or-later

use reqwest::Client;
use serde_json::{Value, json};

#[derive(Debug, Default, Clone, PartialEq)]
pub struct BitcoinMetrics {
pub blocks: u64,
pub headers: u64,
pub connections: u32,
pub verification_progress: f64,
pub chain: String,
}

impl BitcoinMetrics {
/// Asynchronously fetches and parses metrics from a Bitcoin Core RPC node
pub async fn fetch(client: &Client, url: &str, user: &str, pass: &str) -> anyhow::Result<Self> {
// Fetch Blockchain Info (Blocks, Headers, Sync Progress, Chain)
let body_chain = json!({
"jsonrpc": "1.0",
"id": "pdm",
"method": "getblockchaininfo",
"params": []
});

let res_chain = client
.post(url)
.basic_auth(user, Some(pass))
.json(&body_chain)
.send()
.await?
.json::<Value>()
.await?;

let result = &res_chain["result"];
let blocks = result["blocks"].as_u64().unwrap_or(0);
let headers = result["headers"].as_u64().unwrap_or(0);
let verification_progress = result["verificationprogress"].as_f64().unwrap_or(0.0);
let chain = result["chain"].as_str().unwrap_or("unknown").to_string();

// Fetch Network Info (Peer Connections)
let body_net = json!({
"jsonrpc": "1.0",
"id": "pdm",
"method": "getnetworkinfo",
"params": []
});

let res_net = client
.post(url)
.basic_auth(user, Some(pass))
.json(&body_net)
.send()
.await?
.json::<Value>()
.await?;

let connections = res_net["result"]["connections"].as_u64().unwrap_or(0) as u32;

Ok(BitcoinMetrics {
blocks,
headers,
connections,
verification_progress,
chain,
})
}
}
1 change: 1 addition & 0 deletions src/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

pub mod file_explorer;
pub mod metrics;
Loading