Exposing counters through HTTP
This commit is contained in:
parent
3797a91be9
commit
e214dd47a5
2 changed files with 118 additions and 1 deletions
|
|
@ -399,6 +399,88 @@ mod timed_existence {
|
|||
}
|
||||
}
|
||||
|
||||
mod peer_stats_snapshot {
|
||||
use std::{collections::HashMap, sync::atomic::Ordering};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::peer_state::PeerState;
|
||||
|
||||
use super::AggregatePeerStats;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct PeerCounters {
|
||||
pub fetched_bytes: u64,
|
||||
pub total_time_connecting_ms: u64,
|
||||
pub connection_attempts: u32,
|
||||
pub connections: u32,
|
||||
pub errors: u32,
|
||||
pub fetched_chunks: u32,
|
||||
pub downloaded_and_checked_pieces: u32,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct PeerStats {
|
||||
pub counters: PeerCounters,
|
||||
pub state: &'static str,
|
||||
}
|
||||
|
||||
impl From<&crate::peer_state::PeerCounters> for PeerCounters {
|
||||
fn from(counters: &crate::peer_state::PeerCounters) -> Self {
|
||||
Self {
|
||||
fetched_bytes: counters.fetched_bytes.load(Ordering::Relaxed),
|
||||
total_time_connecting_ms: counters.total_time_connecting_ms.load(Ordering::Relaxed),
|
||||
connection_attempts: counters.connection_attempts.load(Ordering::Relaxed),
|
||||
connections: counters.connections.load(Ordering::Relaxed),
|
||||
errors: counters.errors.load(Ordering::Relaxed),
|
||||
fetched_chunks: counters.fetched_chunks.load(Ordering::Relaxed),
|
||||
downloaded_and_checked_pieces: counters
|
||||
.downloaded_and_checked_pieces
|
||||
.load(Ordering::Relaxed),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&crate::peer_state::Peer> for PeerStats {
|
||||
fn from(peer: &crate::peer_state::Peer) -> Self {
|
||||
Self {
|
||||
counters: peer.stats.counters.as_ref().into(),
|
||||
state: peer.state.get().name(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct PeerStatsSnapshot {
|
||||
pub peers: HashMap<String, PeerStats>,
|
||||
pub aggregate: AggregatePeerStats,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Default, Deserialize)]
|
||||
pub enum PeerStatsFilterState {
|
||||
All,
|
||||
#[default]
|
||||
Live,
|
||||
}
|
||||
|
||||
impl PeerStatsFilterState {
|
||||
pub fn matches(&self, s: &PeerState) -> bool {
|
||||
match (self, s) {
|
||||
(Self::All, _) => true,
|
||||
(Self::Live, PeerState::Live(_)) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Deserialize)]
|
||||
pub struct PeerStatsFilter {
|
||||
pub state: PeerStatsFilterState,
|
||||
}
|
||||
}
|
||||
|
||||
pub use peer_stats_snapshot::{PeerStatsFilter, PeerStatsSnapshot};
|
||||
|
||||
pub use timed_existence::{timeit, TimedExistence};
|
||||
|
||||
impl TorrentState {
|
||||
|
|
@ -701,6 +783,22 @@ impl TorrentState {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn per_peer_stats_snapshot(
|
||||
&self,
|
||||
filter: PeerStatsFilter,
|
||||
) -> peer_stats_snapshot::PeerStatsSnapshot {
|
||||
peer_stats_snapshot::PeerStatsSnapshot {
|
||||
peers: self
|
||||
.peers
|
||||
.states
|
||||
.iter()
|
||||
.filter(|e| filter.state.matches(e.value().state.get()))
|
||||
.map(|e| (e.key().to_string(), e.value().into()))
|
||||
.collect(),
|
||||
aggregate: self.peers.stats(),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn wait_until_completed(&self) {
|
||||
if self.is_finished() {
|
||||
return;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue