Skip to content

Commit

Permalink
Add with_stats=true to torrents list
Browse files Browse the repository at this point in the history
  • Loading branch information
ikatson committed Nov 7, 2024
1 parent 30bfb3e commit a90f245
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 19 deletions.
44 changes: 30 additions & 14 deletions crates/librqbit/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Session>,
Expand All @@ -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(),

// These will be filled in /details and /stats endpoints
files: None,
stats: None,
.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,
};
if opts.with_stats {
r.stats = Some(mgr.stats());
}
r
})
.collect()
});
Expand Down
9 changes: 6 additions & 3 deletions crates/librqbit/src/http_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -103,8 +103,11 @@ impl HttpApi {
axum::Json(state.api_session_stats())
}

async fn torrents_list(State(state): State<ApiState>) -> impl IntoResponse {
axum::Json(state.api_torrent_list())
async fn torrents_list(
State(state): State<ApiState>,
Query(opts): Query<ApiTorrentListOpts>,
) -> impl IntoResponse {
axum::Json(state.api_torrent_list_ext(opts))
}

async fn torrents_post(
Expand Down
2 changes: 1 addition & 1 deletion crates/librqbit/src/torrent_state/live/stats/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
2 changes: 1 addition & 1 deletion crates/rqbit/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit a90f245

Please sign in to comment.