Print stats for each managed torrent now
This commit is contained in:
parent
218aa4d9ee
commit
d0757d41c5
2 changed files with 49 additions and 29 deletions
|
|
@ -22,15 +22,17 @@ use crate::{
|
||||||
torrent_manager::{TorrentManagerBuilder, TorrentManagerHandle},
|
torrent_manager::{TorrentManagerBuilder, TorrentManagerHandle},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
pub enum ManagedTorrentState {
|
pub enum ManagedTorrentState {
|
||||||
Initializing,
|
Initializing,
|
||||||
Running(TorrentManagerHandle),
|
Running(TorrentManagerHandle),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct ManagedTorrent {
|
pub struct ManagedTorrent {
|
||||||
info_hash: Id20,
|
pub info_hash: Id20,
|
||||||
output_folder: PathBuf,
|
pub output_folder: PathBuf,
|
||||||
state: ManagedTorrentState,
|
pub state: ManagedTorrentState,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq for ManagedTorrent {
|
impl PartialEq for ManagedTorrent {
|
||||||
|
|
@ -167,6 +169,12 @@ impl Session {
|
||||||
pub fn get_dht(&self) -> Option<Dht> {
|
pub fn get_dht(&self) -> Option<Dht> {
|
||||||
self.dht.clone()
|
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(
|
pub async fn add_torrent(
|
||||||
&self,
|
&self,
|
||||||
url: String,
|
url: String,
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ use clap::Clap;
|
||||||
use librqbit::{
|
use librqbit::{
|
||||||
http_api::HttpApi,
|
http_api::HttpApi,
|
||||||
peer_connection::PeerConnectionOptions,
|
peer_connection::PeerConnectionOptions,
|
||||||
session::{AddTorrentOptions, Session, SessionOptions},
|
session::{AddTorrentOptions, ManagedTorrentState, Session, SessionOptions},
|
||||||
spawn_utils::{spawn, BlockingSpawner},
|
spawn_utils::{spawn, BlockingSpawner},
|
||||||
};
|
};
|
||||||
use log::info;
|
use log::info;
|
||||||
|
|
@ -182,33 +182,45 @@ async fn async_main(opts: Opts, spawner: BlockingSpawner) -> anyhow::Result<()>
|
||||||
};
|
};
|
||||||
|
|
||||||
spawn("Stats printer", {
|
spawn("Stats printer", {
|
||||||
let handle = handle.clone();
|
let session = session.clone();
|
||||||
async move {
|
async move {
|
||||||
loop {
|
loop {
|
||||||
let peer_stats = handle.torrent_state().peer_stats_snapshot();
|
session.with_torrents(|torrents| {
|
||||||
let stats = handle.torrent_state().stats_snapshot();
|
for (idx, torrent) in torrents.iter().enumerate() {
|
||||||
let speed = handle.speed_estimator();
|
match &torrent.state {
|
||||||
let total = stats.total_bytes;
|
ManagedTorrentState::Initializing => {
|
||||||
let progress = stats.total_bytes - stats.remaining_bytes;
|
info!("[{}] initializing", idx);
|
||||||
let downloaded_pct = if stats.remaining_bytes == 0 {
|
},
|
||||||
100f64
|
ManagedTorrentState::Running(handle) => {
|
||||||
} else {
|
let peer_stats = handle.torrent_state().peer_stats_snapshot();
|
||||||
(progress as f64 / total as f64) * 100f64
|
let stats = handle.torrent_state().stats_snapshot();
|
||||||
};
|
let speed = handle.speed_estimator();
|
||||||
info!(
|
let total = stats.total_bytes;
|
||||||
"Stats: {:.2}% ({:.2}), down speed {:.2} Mbps, fetched {}, remaining {:.2} of {:.2}, uploaded {:.2}, peers: {{live: {}, connecting: {}, queued: {}, seen: {}}}",
|
let progress = stats.total_bytes - stats.remaining_bytes;
|
||||||
downloaded_pct,
|
let downloaded_pct = if stats.remaining_bytes == 0 {
|
||||||
SF::new(progress),
|
100f64
|
||||||
speed.download_mbps(),
|
} else {
|
||||||
SF::new(stats.fetched_bytes),
|
(progress as f64 / total as f64) * 100f64
|
||||||
SF::new(stats.remaining_bytes),
|
};
|
||||||
SF::new(total),
|
info!(
|
||||||
SF::new(stats.uploaded_bytes),
|
"[{}]: {:.2}% ({:.2}), down speed {:.2} Mbps, fetched {}, remaining {:.2} of {:.2}, uploaded {:.2}, peers: {{live: {}, connecting: {}, queued: {}, seen: {}}}",
|
||||||
peer_stats.live,
|
idx,
|
||||||
peer_stats.connecting,
|
downloaded_pct,
|
||||||
peer_stats.queued,
|
SF::new(progress),
|
||||||
peer_stats.seen,
|
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;
|
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue