Print stats for each managed torrent now

This commit is contained in:
Igor Katson 2021-10-10 11:52:21 +01:00
parent 218aa4d9ee
commit d0757d41c5
2 changed files with 49 additions and 29 deletions

View file

@ -22,15 +22,17 @@ use crate::{
torrent_manager::{TorrentManagerBuilder, TorrentManagerHandle},
};
#[derive(Clone)]
pub enum ManagedTorrentState {
Initializing,
Running(TorrentManagerHandle),
}
#[derive(Clone)]
pub struct ManagedTorrent {
info_hash: Id20,
output_folder: PathBuf,
state: ManagedTorrentState,
pub info_hash: Id20,
pub output_folder: PathBuf,
pub state: ManagedTorrentState,
}
impl PartialEq for ManagedTorrent {
@ -167,6 +169,12 @@ impl Session {
pub fn get_dht(&self) -> Option<Dht> {
self.dht.clone()
}
pub fn with_torrents<F>(&self, callback: F)
where
F: Fn(&[ManagedTorrent]),
{
callback(&self.locked.read().torrents)
}
pub async fn add_torrent(
&self,
url: String,

View file

@ -5,7 +5,7 @@ use clap::Clap;
use librqbit::{
http_api::HttpApi,
peer_connection::PeerConnectionOptions,
session::{AddTorrentOptions, Session, SessionOptions},
session::{AddTorrentOptions, ManagedTorrentState, Session, SessionOptions},
spawn_utils::{spawn, BlockingSpawner},
};
use log::info;
@ -182,33 +182,45 @@ async fn async_main(opts: Opts, spawner: BlockingSpawner) -> anyhow::Result<()>
};
spawn("Stats printer", {
let handle = handle.clone();
let session = session.clone();
async move {
loop {
let peer_stats = handle.torrent_state().peer_stats_snapshot();
let stats = handle.torrent_state().stats_snapshot();
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 {
(progress as f64 / total as f64) * 100f64
};
info!(
"Stats: {:.2}% ({:.2}), down speed {:.2} Mbps, fetched {}, remaining {:.2} of {:.2}, uploaded {:.2}, peers: {{live: {}, connecting: {}, queued: {}, seen: {}}}",
downloaded_pct,
SF::new(progress),
speed.download_mbps(),
SF::new(stats.fetched_bytes),
SF::new(stats.remaining_bytes),
SF::new(total),
SF::new(stats.uploaded_bytes),
peer_stats.live,
peer_stats.connecting,
peer_stats.queued,
peer_stats.seen,
);
session.with_torrents(|torrents| {
for (idx, torrent) in torrents.iter().enumerate() {
match &torrent.state {
ManagedTorrentState::Initializing => {
info!("[{}] initializing", idx);
},
ManagedTorrentState::Running(handle) => {
let peer_stats = handle.torrent_state().peer_stats_snapshot();
let stats = handle.torrent_state().stats_snapshot();
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 {
(progress as f64 / total as f64) * 100f64
};
info!(
"[{}]: {:.2}% ({:.2}), down speed {:.2} Mbps, fetched {}, remaining {:.2} of {:.2}, uploaded {:.2}, peers: {{live: {}, connecting: {}, queued: {}, seen: {}}}",
idx,
downloaded_pct,
SF::new(progress),
speed.download_mbps(),
SF::new(stats.fetched_bytes),
SF::new(stats.remaining_bytes),
SF::new(total),
SF::new(stats.uploaded_bytes),
peer_stats.live,
peer_stats.connecting,
peer_stats.queued,
peer_stats.seen,
);
},
}
}
});
tokio::time::sleep(Duration::from_secs(1)).await;
}
}