From a90f245dc9c1e402541e4bcd28273245f78df1de Mon Sep 17 00:00:00 2001 From: Igor Katson Date: Wed, 6 Nov 2024 17:19:33 +0000 Subject: [PATCH] Add with_stats=true to torrents list --- crates/librqbit/src/api.rs | 42 +++++++++++++------ crates/librqbit/src/http_api.rs | 9 ++-- .../src/torrent_state/live/stats/snapshot.rs | 2 +- crates/rqbit/src/main.rs | 2 +- 4 files changed, 37 insertions(+), 18 deletions(-) diff --git a/crates/librqbit/src/api.rs b/crates/librqbit/src/api.rs index c7b2db8..c380828 100644 --- a/crates/librqbit/src/api.rs +++ b/crates/librqbit/src/api.rs @@ -168,6 +168,12 @@ impl TorrentIdOrHash { } } +#[derive(Deserialize, Default)] +pub struct ApiTorrentListOpts { + #[serde(default)] + pub with_stats: bool, +} + impl Api { pub fn new( session: Arc, @@ -193,22 +199,32 @@ impl Api { } pub fn api_torrent_list(&self) -> TorrentListResponse { + self.api_torrent_list_ext(ApiTorrentListOpts { with_stats: false }) + } + + pub fn api_torrent_list_ext(&self, opts: ApiTorrentListOpts) -> TorrentListResponse { let items = self.session.with_torrents(|torrents| { torrents - .map(|(id, mgr)| TorrentDetailsResponse { - id: Some(id), - info_hash: mgr.shared().info_hash.as_string(), - name: mgr.shared().info.name.as_ref().map(|n| n.to_string()), - output_folder: mgr - .shared() - .options - .output_folder - .to_string_lossy() - .into_owned(), + .map(|(id, mgr)| { + let mut r = TorrentDetailsResponse { + id: Some(id), + info_hash: mgr.shared().info_hash.as_string(), + name: mgr.shared().info.name.as_ref().map(|n| n.to_string()), + output_folder: mgr + .shared() + .options + .output_folder + .to_string_lossy() + .into_owned(), - // These will be filled in /details and /stats endpoints - files: None, - stats: None, + // These will be filled in /details and /stats endpoints + files: None, + stats: None, + }; + if opts.with_stats { + r.stats = Some(mgr.stats()); + } + r }) .collect() }); diff --git a/crates/librqbit/src/http_api.rs b/crates/librqbit/src/http_api.rs index 651ed2b..e70de8b 100644 --- a/crates/librqbit/src/http_api.rs +++ b/crates/librqbit/src/http_api.rs @@ -22,7 +22,7 @@ use tracing::{debug, error_span, trace, Span}; use axum::Router; -use crate::api::{Api, TorrentIdOrHash}; +use crate::api::{Api, ApiTorrentListOpts, TorrentIdOrHash}; use crate::peer_connection::PeerConnectionOptions; use crate::session::{AddTorrent, AddTorrentOptions, SUPPORTED_SCHEMES}; use crate::torrent_state::peer::stats::snapshot::PeerStatsFilter; @@ -103,8 +103,11 @@ impl HttpApi { axum::Json(state.api_session_stats()) } - async fn torrents_list(State(state): State) -> impl IntoResponse { - axum::Json(state.api_torrent_list()) + async fn torrents_list( + State(state): State, + Query(opts): Query, + ) -> impl IntoResponse { + axum::Json(state.api_torrent_list_ext(opts)) } async fn torrents_post( diff --git a/crates/librqbit/src/torrent_state/live/stats/snapshot.rs b/crates/librqbit/src/torrent_state/live/stats/snapshot.rs index 2825af4..dc12998 100644 --- a/crates/librqbit/src/torrent_state/live/stats/snapshot.rs +++ b/crates/librqbit/src/torrent_state/live/stats/snapshot.rs @@ -4,7 +4,7 @@ use serde::Serialize; use crate::torrent_state::live::peers::stats::snapshot::AggregatePeerStats; -#[derive(Debug, Serialize, Deserialize, Default)] +#[derive(Debug, Serialize, Default)] pub struct StatsSnapshot { pub downloaded_and_checked_bytes: u64, diff --git a/crates/rqbit/src/main.rs b/crates/rqbit/src/main.rs index ebefde8..6806125 100644 --- a/crates/rqbit/src/main.rs +++ b/crates/rqbit/src/main.rs @@ -684,7 +684,7 @@ async fn async_main(opts: Opts, cancel: CancellationToken) -> anyhow::Result<()> if let Some(id) = id { info!("{} added to the server with index {}. Query {}/torrents/{}/(stats/haves) for details", details.info_hash, id, http_api_url, id) } - for file in details.files { + for file in details.files.into_iter().flat_map(|i| i.into_iter()) { info!( "file {:?}, size {}{}", file.name,