diff --git a/crates/librqbit/src/torrent_state.rs b/crates/librqbit/src/torrent_state.rs index 64c87fd..a240eff 100644 --- a/crates/librqbit/src/torrent_state.rs +++ b/crates/librqbit/src/torrent_state.rs @@ -201,6 +201,7 @@ pub struct StatsSnapshot { pub uploaded_bytes: u64, pub initially_needed_bytes: u64, pub remaining_bytes: u64, + pub total_bytes: u64, pub live_peers: u32, pub seen_peers: u32, pub connecting_peers: u32, @@ -234,6 +235,7 @@ pub struct TorrentState { peer_id: Id20, lengths: Lengths, needed: u64, + have_plus_needed: u64, stats: AtomicStats, options: TorrentStateOptions, @@ -271,6 +273,7 @@ impl TorrentState { ..Default::default() }, needed: needed_bytes, + have_plus_needed: needed_bytes + have_bytes, lengths, options, @@ -565,6 +568,7 @@ impl TorrentState { downloaded_and_checked_pieces: self.stats.downloaded_pieces.load(Relaxed), fetched_bytes: self.stats.fetched_bytes.load(Relaxed), uploaded_bytes: self.stats.uploaded.load(Relaxed), + total_bytes: self.have_plus_needed, live_peers: peer_stats.live as u32, seen_peers: g.peers.seen.len() as u32, connecting_peers: peer_stats.connecting as u32, diff --git a/crates/librqbit_core/src/speed_estimator.rs b/crates/librqbit_core/src/speed_estimator.rs index 0c7d834..da94e21 100644 --- a/crates/librqbit_core/src/speed_estimator.rs +++ b/crates/librqbit_core/src/speed_estimator.rs @@ -6,6 +6,7 @@ use std::{ use parking_lot::Mutex; +#[derive(Clone, Copy)] struct ProgressSnapshot { downloaded_bytes: u64, instant: Instant, @@ -44,15 +45,26 @@ impl SpeedEstimator { } pub fn add_snapshot(&self, downloaded_bytes: u64, remaining_bytes: u64, instant: Instant) { - let mut g = self.latest_per_second_snapshots.lock(); - if g.len() < g.capacity() { - g.push_back(ProgressSnapshot { + let first = { + let mut g = self.latest_per_second_snapshots.lock(); + + let current = ProgressSnapshot { downloaded_bytes, instant, - }); - return; - } - let first = g.pop_front().unwrap(); + }; + + if g.is_empty() { + g.push_back(current); + return; + } else if g.len() < g.capacity() { + g.push_back(current); + g.front().copied().unwrap() + } else { + let first = g.pop_front().unwrap(); + g.push_back(current); + first + } + }; let downloaded_bytes_diff = downloaded_bytes - first.downloaded_bytes; let elapsed = instant - first.instant; @@ -68,10 +80,5 @@ impl SpeedEstimator { .store(time_remaining_millis_rounded, Ordering::Relaxed); self.download_bytes_per_second .store(bps as u64, Ordering::Relaxed); - - g.push_back(ProgressSnapshot { - downloaded_bytes, - instant, - }); } } diff --git a/crates/rqbit/src/main.rs b/crates/rqbit/src/main.rs index 396220b..3ae930e 100644 --- a/crates/rqbit/src/main.rs +++ b/crates/rqbit/src/main.rs @@ -376,21 +376,23 @@ async fn main_torrent_info( loop { let peer_stats = handle.torrent_state().peer_stats_snapshot(); let stats = handle.torrent_state().stats_snapshot(); - let needed = stats.initially_needed_bytes; + let speed = handle.speed_estimator(); + let total = stats.total_bytes; + let progress = stats.total_bytes - stats.remaining_bytes; let downloaded_pct = if stats.remaining_bytes == 0 { 100f64 } else { - (stats.downloaded_and_checked_bytes as f64 / needed as f64) * 100f64 + (progress as f64 / total as f64) * 100f64 }; info!( - "Stats: downloaded {:.2}% ({:.2}), fetched {}, remaining {:.2} out of {:.2}, uploaded {:.2}, have {:.2}, peers: {{live: {}, connecting: {}, queued: {}, seen: {}}}", + "Stats: {:.2}% ({:.2}), down speed {:.2} Mbps, fetched {}, remaining {:.2} of {:.2}, uploaded {:.2}, peers: {{live: {}, connecting: {}, queued: {}, seen: {}}}", downloaded_pct, - SF::new(stats.downloaded_and_checked_bytes), + SF::new(progress), + speed.download_mbps(), SF::new(stats.fetched_bytes), SF::new(stats.remaining_bytes), - SF::new(needed), + SF::new(total), SF::new(stats.uploaded_bytes), - SF::new(stats.have_bytes), peer_stats.live, peer_stats.connecting, peer_stats.queued,