Expose HTTP /stats endpoint
This commit is contained in:
parent
5d3a93b8bd
commit
561c8b8a1d
5 changed files with 46 additions and 1 deletions
|
|
@ -6,5 +6,5 @@ use crate::torrent_state::live::peers::stats::atomic::AggregatePeerStatsAtomic;
|
|||
pub struct AtomicSessionStats {
|
||||
pub fetched_bytes: AtomicU64,
|
||||
pub uploaded_bytes: AtomicU64,
|
||||
pub peers: AggregatePeerStatsAtomic,
|
||||
pub(crate) peers: AggregatePeerStatsAtomic,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,11 +5,13 @@ use std::{
|
|||
|
||||
use atomic::AtomicSessionStats;
|
||||
use librqbit_core::speed_estimator::SpeedEstimator;
|
||||
use snapshot::SessionStatsSnapshot;
|
||||
use tracing::error_span;
|
||||
|
||||
use crate::Session;
|
||||
|
||||
pub mod atomic;
|
||||
pub mod snapshot;
|
||||
|
||||
pub struct SessionStats {
|
||||
pub atomic: Arc<AtomicSessionStats>,
|
||||
|
|
@ -27,6 +29,12 @@ impl SessionStats {
|
|||
}
|
||||
}
|
||||
|
||||
impl Default for SessionStats {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Session {
|
||||
pub(crate) fn start_speed_estimator_updater(self: &Arc<Self>) {
|
||||
self.spawn(error_span!(parent: self.rs(), "speed_estimator"), {
|
||||
|
|
@ -47,4 +55,8 @@ impl Session {
|
|||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn stats_snapshot(&self) -> SessionStatsSnapshot {
|
||||
SessionStatsSnapshot::from(&self.stats)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
22
crates/librqbit/src/session_stats/snapshot.rs
Normal file
22
crates/librqbit/src/session_stats/snapshot.rs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
use serde::Serialize;
|
||||
|
||||
use crate::torrent_state::{peers::stats::snapshot::AggregatePeerStats, stats::Speed};
|
||||
|
||||
use super::SessionStats;
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct SessionStatsSnapshot {
|
||||
download_speed: Speed,
|
||||
upload_speed: Speed,
|
||||
peers: AggregatePeerStats,
|
||||
}
|
||||
|
||||
impl From<&SessionStats> for SessionStatsSnapshot {
|
||||
fn from(s: &SessionStats) -> Self {
|
||||
Self {
|
||||
download_speed: s.down_speed_estimator.mbps().into(),
|
||||
upload_speed: s.up_speed_estimator.mbps().into(),
|
||||
peers: AggregatePeerStats::from(&s.atomic.peers),
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue