split up librqbit torrent state into smaller files

This commit is contained in:
Igor Katson 2023-11-23 16:27:55 +00:00
parent 7cd6102c6a
commit edaf3e0997
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
17 changed files with 502 additions and 440 deletions

View file

@ -0,0 +1,25 @@
use std::{
sync::atomic::{AtomicU64, Ordering},
time::Duration,
};
#[derive(Default, Debug)]
pub struct AtomicStats {
pub have_bytes: AtomicU64,
pub downloaded_and_checked_bytes: AtomicU64,
pub downloaded_and_checked_pieces: AtomicU64,
pub uploaded_bytes: AtomicU64,
pub fetched_bytes: AtomicU64,
pub total_piece_download_ms: AtomicU64,
}
impl AtomicStats {
pub fn average_piece_download_time(&self) -> Option<Duration> {
let d = self.downloaded_and_checked_pieces.load(Ordering::Acquire);
let t = self.total_piece_download_ms.load(Ordering::Acquire);
if d == 0 {
return None;
}
Some(Duration::from_secs_f64(t as f64 / d as f64 / 1000f64))
}
}

View file

@ -0,0 +1,2 @@
pub mod atomic;
pub mod snapshot;

View file

@ -0,0 +1,32 @@
use std::time::{Duration, Instant};
use serde::Serialize;
use crate::torrent_state::live::peers::stats::snapshot::AggregatePeerStats;
#[derive(Debug, Serialize)]
pub struct StatsSnapshot {
pub have_bytes: u64,
pub downloaded_and_checked_bytes: u64,
pub downloaded_and_checked_pieces: u64,
pub fetched_bytes: u64,
pub uploaded_bytes: u64,
pub initially_needed_bytes: u64,
pub remaining_bytes: u64,
pub total_bytes: u64,
#[serde(skip)]
pub time: Instant,
pub total_piece_download_ms: u64,
pub peer_stats: AggregatePeerStats,
}
impl StatsSnapshot {
pub fn average_piece_download_time(&self) -> Option<Duration> {
let d = self.downloaded_and_checked_pieces;
let t = self.total_piece_download_ms;
if d == 0 {
return None;
}
Some(Duration::from_secs_f64(t as f64 / d as f64 / 1000f64))
}
}