From 35fdb39fc122873753be0a49ee94c9b11362b4a7 Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Thu, 24 Oct 2024 12:26:37 +0200 Subject: [PATCH] pool: add `SyncProgress::percentage` Signed-off-by: Yuki Kishimoto --- crates/nostr-relay-pool/src/relay/options.rs | 10 ++++++++++ crates/nostr-sdk/examples/negentropy.rs | 6 +++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/crates/nostr-relay-pool/src/relay/options.rs b/crates/nostr-relay-pool/src/relay/options.rs index db3fd445c..4241acc66 100644 --- a/crates/nostr-relay-pool/src/relay/options.rs +++ b/crates/nostr-relay-pool/src/relay/options.rs @@ -326,6 +326,16 @@ impl SyncProgress { pub fn channel() -> (Sender, Receiver) { watch::channel(SyncProgress::default()) } + + /// Calculate progress % + #[inline] + pub fn percentage(&self) -> f64 { + if self.total > 0 { + self.current as f64 / self.total as f64 + } else { + 0.0 + } + } } /// Sync (negentropy reconciliation) options diff --git a/crates/nostr-sdk/examples/negentropy.rs b/crates/nostr-sdk/examples/negentropy.rs index 05c6a7da4..92196a806 100644 --- a/crates/nostr-sdk/examples/negentropy.rs +++ b/crates/nostr-sdk/examples/negentropy.rs @@ -24,9 +24,9 @@ async fn main() -> Result<()> { tokio::spawn(async move { while rx.changed().await.is_ok() { - let SyncProgress { total, current } = *rx.borrow_and_update(); - if total > 0 { - println!("{:.2}%", (current as f64 / total as f64) * 100.0); + let progress = *rx.borrow_and_update(); + if progress.total > 0 { + println!("{:.2}%", progress.percentage() * 100.0); } } });