Add with_stats=true to torrents list

This commit is contained in:
Igor Katson 2024-11-06 17:19:33 +00:00
parent 30bfb3ea19
commit a90f245dc9
4 changed files with 37 additions and 18 deletions

View file

@ -168,6 +168,12 @@ impl TorrentIdOrHash {
} }
} }
#[derive(Deserialize, Default)]
pub struct ApiTorrentListOpts {
#[serde(default)]
pub with_stats: bool,
}
impl Api { impl Api {
pub fn new( pub fn new(
session: Arc<Session>, session: Arc<Session>,
@ -193,22 +199,32 @@ impl Api {
} }
pub fn api_torrent_list(&self) -> TorrentListResponse { 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| { let items = self.session.with_torrents(|torrents| {
torrents torrents
.map(|(id, mgr)| TorrentDetailsResponse { .map(|(id, mgr)| {
id: Some(id), let mut r = TorrentDetailsResponse {
info_hash: mgr.shared().info_hash.as_string(), id: Some(id),
name: mgr.shared().info.name.as_ref().map(|n| n.to_string()), info_hash: mgr.shared().info_hash.as_string(),
output_folder: mgr name: mgr.shared().info.name.as_ref().map(|n| n.to_string()),
.shared() output_folder: mgr
.options .shared()
.output_folder .options
.to_string_lossy() .output_folder
.into_owned(), .to_string_lossy()
.into_owned(),
// These will be filled in /details and /stats endpoints // These will be filled in /details and /stats endpoints
files: None, files: None,
stats: None, stats: None,
};
if opts.with_stats {
r.stats = Some(mgr.stats());
}
r
}) })
.collect() .collect()
}); });

View file

@ -22,7 +22,7 @@ use tracing::{debug, error_span, trace, Span};
use axum::Router; use axum::Router;
use crate::api::{Api, TorrentIdOrHash}; use crate::api::{Api, ApiTorrentListOpts, TorrentIdOrHash};
use crate::peer_connection::PeerConnectionOptions; use crate::peer_connection::PeerConnectionOptions;
use crate::session::{AddTorrent, AddTorrentOptions, SUPPORTED_SCHEMES}; use crate::session::{AddTorrent, AddTorrentOptions, SUPPORTED_SCHEMES};
use crate::torrent_state::peer::stats::snapshot::PeerStatsFilter; use crate::torrent_state::peer::stats::snapshot::PeerStatsFilter;
@ -103,8 +103,11 @@ impl HttpApi {
axum::Json(state.api_session_stats()) axum::Json(state.api_session_stats())
} }
async fn torrents_list(State(state): State<ApiState>) -> impl IntoResponse { async fn torrents_list(
axum::Json(state.api_torrent_list()) State(state): State<ApiState>,
Query(opts): Query<ApiTorrentListOpts>,
) -> impl IntoResponse {
axum::Json(state.api_torrent_list_ext(opts))
} }
async fn torrents_post( async fn torrents_post(

View file

@ -4,7 +4,7 @@ use serde::Serialize;
use crate::torrent_state::live::peers::stats::snapshot::AggregatePeerStats; use crate::torrent_state::live::peers::stats::snapshot::AggregatePeerStats;
#[derive(Debug, Serialize, Deserialize, Default)] #[derive(Debug, Serialize, Default)]
pub struct StatsSnapshot { pub struct StatsSnapshot {
pub downloaded_and_checked_bytes: u64, pub downloaded_and_checked_bytes: u64,

View file

@ -684,7 +684,7 @@ async fn async_main(opts: Opts, cancel: CancellationToken) -> anyhow::Result<()>
if let Some(id) = id { 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) 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!( info!(
"file {:?}, size {}{}", "file {:?}, size {}{}",
file.name, file.name,