Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: rename TorrentsMetrics to AggregateSwarmMetadata #1361

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ pub struct Stats {
impl From<TrackerMetrics> for Stats {
fn from(metrics: TrackerMetrics) -> Self {
Self {
torrents: metrics.torrents_metrics.torrents,
seeders: metrics.torrents_metrics.complete,
completed: metrics.torrents_metrics.downloaded,
leechers: metrics.torrents_metrics.incomplete,
torrents: metrics.torrents_metrics.total_torrents,
seeders: metrics.torrents_metrics.total_complete,
completed: metrics.torrents_metrics.total_downloaded,
leechers: metrics.torrents_metrics.total_incomplete,
// TCP
tcp4_connections_handled: metrics.protocol_metrics.tcp4_connections_handled,
tcp4_announces_handled: metrics.protocol_metrics.tcp4_announces_handled,
Expand Down Expand Up @@ -119,19 +119,19 @@ impl From<TrackerMetrics> for Stats {
mod tests {
use torrust_rest_tracker_api_core::statistics::metrics::Metrics;
use torrust_rest_tracker_api_core::statistics::services::TrackerMetrics;
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_primitives::swarm_metadata::AggregateSwarmMetadata;

use super::Stats;

#[test]
fn stats_resource_should_be_converted_from_tracker_metrics() {
assert_eq!(
Stats::from(TrackerMetrics {
torrents_metrics: TorrentsMetrics {
complete: 1,
downloaded: 2,
incomplete: 3,
torrents: 4
torrents_metrics: AggregateSwarmMetadata {
total_complete: 1,
total_downloaded: 2,
total_incomplete: 3,
total_torrents: 4
},
protocol_metrics: Metrics {
// TCP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ pub fn stats_response(tracker_metrics: TrackerMetrics) -> Response {
pub fn metrics_response(tracker_metrics: &TrackerMetrics) -> Response {
let mut lines = vec![];

lines.push(format!("torrents {}", tracker_metrics.torrents_metrics.torrents));
lines.push(format!("seeders {}", tracker_metrics.torrents_metrics.complete));
lines.push(format!("completed {}", tracker_metrics.torrents_metrics.downloaded));
lines.push(format!("leechers {}", tracker_metrics.torrents_metrics.incomplete));
lines.push(format!("torrents {}", tracker_metrics.torrents_metrics.total_torrents));
lines.push(format!("seeders {}", tracker_metrics.torrents_metrics.total_complete));
lines.push(format!("completed {}", tracker_metrics.torrents_metrics.total_downloaded));
lines.push(format!("leechers {}", tracker_metrics.torrents_metrics.total_incomplete));

// TCP

Expand Down
8 changes: 4 additions & 4 deletions packages/http-tracker-core/src/statistics/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use std::sync::Arc;

use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository;
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_primitives::swarm_metadata::AggregateSwarmMetadata;

use crate::statistics::metrics::Metrics;
use crate::statistics::repository::Repository;
Expand All @@ -34,7 +34,7 @@ pub struct TrackerMetrics {
/// Domain level metrics.
///
/// General metrics for all torrents (number of seeders, leechers, etcetera)
pub torrents_metrics: TorrentsMetrics,
pub torrents_metrics: AggregateSwarmMetadata,

/// Application level metrics. Usage statistics/metrics.
///
Expand Down Expand Up @@ -72,7 +72,7 @@ mod tests {
use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository;
use bittorrent_tracker_core::{self};
use torrust_tracker_configuration::Configuration;
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_primitives::swarm_metadata::AggregateSwarmMetadata;
use torrust_tracker_test_helpers::configuration;

use crate::statistics;
Expand All @@ -96,7 +96,7 @@ mod tests {
assert_eq!(
tracker_metrics,
TrackerMetrics {
torrents_metrics: TorrentsMetrics::default(),
torrents_metrics: AggregateSwarmMetadata::default(),
protocol_metrics: statistics::metrics::Metrics::default(),
}
);
Expand Down
1 change: 0 additions & 1 deletion packages/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ pub mod core;
pub mod pagination;
pub mod peer;
pub mod swarm_metadata;
pub mod torrent_metrics;

use std::collections::BTreeMap;
use std::time::Duration;
Expand Down
46 changes: 41 additions & 5 deletions packages/primitives/src/swarm_metadata.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
use std::ops::AddAssign;

use derive_more::Constructor;

/// Swarm statistics for one torrent.
///
/// Swarm metadata dictionary in the scrape response.
///
/// See [BEP 48: Tracker Protocol Extension: Scrape](https://www.bittorrent.org/beps/bep_0048.html)
#[derive(Copy, Clone, Debug, PartialEq, Default, Constructor)]
pub struct SwarmMetadata {
/// (i.e `completed`): The number of peers that have ever completed downloading
pub downloaded: u32, //
/// (i.e `seeders`): The number of active peers that have completed downloading (seeders)
pub complete: u32, //seeders
/// (i.e `leechers`): The number of active peers that have not completed downloading (leechers)
/// (i.e `completed`): The number of peers that have ever completed
/// downloading a given torrent.
pub downloaded: u32,

/// (i.e `seeders`): The number of active peers that have completed
/// downloading (seeders) a given torrent.
pub complete: u32,

/// (i.e `leechers`): The number of active peers that have not completed
/// downloading (leechers) a given torrent.
pub incomplete: u32,
}

Expand All @@ -20,3 +28,31 @@ impl SwarmMetadata {
Self::default()
}
}

/// Structure that holds aggregate swarm metadata.
///
/// Metrics are aggregate values for all torrents.
#[derive(Copy, Clone, Debug, PartialEq, Default)]
pub struct AggregateSwarmMetadata {
/// Total number of peers that have ever completed downloading for all
/// torrents.
pub total_downloaded: u64,

/// Total number of seeders for all torrents.
pub total_complete: u64,

/// Total number of leechers for all torrents.
pub total_incomplete: u64,

/// Total number of torrents.
pub total_torrents: u64,
}

impl AddAssign for AggregateSwarmMetadata {
fn add_assign(&mut self, rhs: Self) {
self.total_complete += rhs.total_complete;
self.total_downloaded += rhs.total_downloaded;
self.total_incomplete += rhs.total_incomplete;
self.total_torrents += rhs.total_torrents;
}
}
25 changes: 0 additions & 25 deletions packages/primitives/src/torrent_metrics.rs

This file was deleted.

8 changes: 4 additions & 4 deletions packages/rest-tracker-api-core/src/statistics/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepo
use bittorrent_udp_tracker_core::services::banning::BanService;
use bittorrent_udp_tracker_core::{self, statistics as udp_core_statistics};
use tokio::sync::RwLock;
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_primitives::swarm_metadata::AggregateSwarmMetadata;
use torrust_udp_tracker_server::statistics as udp_server_statistics;

use crate::statistics::metrics::Metrics;
Expand All @@ -15,7 +15,7 @@ pub struct TrackerMetrics {
/// Domain level metrics.
///
/// General metrics for all torrents (number of seeders, leechers, etcetera)
pub torrents_metrics: TorrentsMetrics,
pub torrents_metrics: AggregateSwarmMetadata,

/// Application level metrics. Usage statistics/metrics.
///
Expand Down Expand Up @@ -83,7 +83,7 @@ mod tests {
use bittorrent_udp_tracker_core::MAX_CONNECTION_ID_ERRORS_PER_IP;
use tokio::sync::RwLock;
use torrust_tracker_configuration::Configuration;
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_primitives::swarm_metadata::AggregateSwarmMetadata;
use torrust_tracker_test_helpers::configuration;

use crate::statistics::metrics::Metrics;
Expand Down Expand Up @@ -127,7 +127,7 @@ mod tests {
assert_eq!(
tracker_metrics,
TrackerMetrics {
torrents_metrics: TorrentsMetrics::default(),
torrents_metrics: AggregateSwarmMetadata::default(),
protocol_metrics: Metrics::default(),
}
);
Expand Down
15 changes: 7 additions & 8 deletions packages/torrent-repository/src/repository/dash_map_mutex_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use bittorrent_primitives::info_hash::InfoHash;
use dashmap::DashMap;
use torrust_tracker_configuration::TrackerPolicy;
use torrust_tracker_primitives::pagination::Pagination;
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_primitives::swarm_metadata::{AggregateSwarmMetadata, SwarmMetadata};
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents};

use super::Repository;
Expand Down Expand Up @@ -47,15 +46,15 @@ where
maybe_entry.map(|entry| entry.clone())
}

fn get_metrics(&self) -> TorrentsMetrics {
let mut metrics = TorrentsMetrics::default();
fn get_metrics(&self) -> AggregateSwarmMetadata {
let mut metrics = AggregateSwarmMetadata::default();

for entry in &self.torrents {
let stats = entry.value().lock().expect("it should get a lock").get_swarm_metadata();
metrics.complete += u64::from(stats.complete);
metrics.downloaded += u64::from(stats.downloaded);
metrics.incomplete += u64::from(stats.incomplete);
metrics.torrents += 1;
metrics.total_complete += u64::from(stats.complete);
metrics.total_downloaded += u64::from(stats.downloaded);
metrics.total_incomplete += u64::from(stats.incomplete);
metrics.total_torrents += 1;
}

metrics
Expand Down
7 changes: 3 additions & 4 deletions packages/torrent-repository/src/repository/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use bittorrent_primitives::info_hash::InfoHash;
use torrust_tracker_configuration::TrackerPolicy;
use torrust_tracker_primitives::pagination::Pagination;
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_primitives::swarm_metadata::{AggregateSwarmMetadata, SwarmMetadata};
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents};

pub mod dash_map_mutex_std;
Expand All @@ -18,7 +17,7 @@ use std::fmt::Debug;

pub trait Repository<T>: Debug + Default + Sized + 'static {
fn get(&self, key: &InfoHash) -> Option<T>;
fn get_metrics(&self) -> TorrentsMetrics;
fn get_metrics(&self) -> AggregateSwarmMetadata;
fn get_paginated(&self, pagination: Option<&Pagination>) -> Vec<(InfoHash, T)>;
fn import_persistent(&self, persistent_torrents: &PersistentTorrents);
fn remove(&self, key: &InfoHash) -> Option<T>;
Expand All @@ -31,7 +30,7 @@ pub trait Repository<T>: Debug + Default + Sized + 'static {
#[allow(clippy::module_name_repetitions)]
pub trait RepositoryAsync<T>: Debug + Default + Sized + 'static {
fn get(&self, key: &InfoHash) -> impl std::future::Future<Output = Option<T>> + Send;
fn get_metrics(&self) -> impl std::future::Future<Output = TorrentsMetrics> + Send;
fn get_metrics(&self) -> impl std::future::Future<Output = AggregateSwarmMetadata> + Send;
fn get_paginated(&self, pagination: Option<&Pagination>) -> impl std::future::Future<Output = Vec<(InfoHash, T)>> + Send;
fn import_persistent(&self, persistent_torrents: &PersistentTorrents) -> impl std::future::Future<Output = ()> + Send;
fn remove(&self, key: &InfoHash) -> impl std::future::Future<Output = Option<T>> + Send;
Expand Down
15 changes: 7 additions & 8 deletions packages/torrent-repository/src/repository/rw_lock_std.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use bittorrent_primitives::info_hash::InfoHash;
use torrust_tracker_configuration::TrackerPolicy;
use torrust_tracker_primitives::pagination::Pagination;
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_primitives::swarm_metadata::{AggregateSwarmMetadata, SwarmMetadata};
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents};

use super::Repository;
Expand Down Expand Up @@ -65,15 +64,15 @@ where
db.get(key).cloned()
}

fn get_metrics(&self) -> TorrentsMetrics {
let mut metrics = TorrentsMetrics::default();
fn get_metrics(&self) -> AggregateSwarmMetadata {
let mut metrics = AggregateSwarmMetadata::default();

for entry in self.get_torrents().values() {
let stats = entry.get_swarm_metadata();
metrics.complete += u64::from(stats.complete);
metrics.downloaded += u64::from(stats.downloaded);
metrics.incomplete += u64::from(stats.incomplete);
metrics.torrents += 1;
metrics.total_complete += u64::from(stats.complete);
metrics.total_downloaded += u64::from(stats.downloaded);
metrics.total_incomplete += u64::from(stats.incomplete);
metrics.total_torrents += 1;
}

metrics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use std::sync::Arc;
use bittorrent_primitives::info_hash::InfoHash;
use torrust_tracker_configuration::TrackerPolicy;
use torrust_tracker_primitives::pagination::Pagination;
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_primitives::swarm_metadata::{AggregateSwarmMetadata, SwarmMetadata};
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents};

use super::Repository;
Expand Down Expand Up @@ -60,15 +59,15 @@ where
db.get(key).cloned()
}

fn get_metrics(&self) -> TorrentsMetrics {
let mut metrics = TorrentsMetrics::default();
fn get_metrics(&self) -> AggregateSwarmMetadata {
let mut metrics = AggregateSwarmMetadata::default();

for entry in self.get_torrents().values() {
let stats = entry.lock().expect("it should get a lock").get_swarm_metadata();
metrics.complete += u64::from(stats.complete);
metrics.downloaded += u64::from(stats.downloaded);
metrics.incomplete += u64::from(stats.incomplete);
metrics.torrents += 1;
metrics.total_complete += u64::from(stats.complete);
metrics.total_downloaded += u64::from(stats.downloaded);
metrics.total_incomplete += u64::from(stats.incomplete);
metrics.total_torrents += 1;
}

metrics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use futures::future::join_all;
use futures::{Future, FutureExt};
use torrust_tracker_configuration::TrackerPolicy;
use torrust_tracker_primitives::pagination::Pagination;
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_primitives::swarm_metadata::{AggregateSwarmMetadata, SwarmMetadata};
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents};

use super::RepositoryAsync;
Expand Down Expand Up @@ -86,17 +85,17 @@ where
}
}

async fn get_metrics(&self) -> TorrentsMetrics {
let mut metrics = TorrentsMetrics::default();
async fn get_metrics(&self) -> AggregateSwarmMetadata {
let mut metrics = AggregateSwarmMetadata::default();

let entries: Vec<_> = self.get_torrents().values().cloned().collect();

for entry in entries {
let stats = entry.lock().await.get_swarm_metadata();
metrics.complete += u64::from(stats.complete);
metrics.downloaded += u64::from(stats.downloaded);
metrics.incomplete += u64::from(stats.incomplete);
metrics.torrents += 1;
metrics.total_complete += u64::from(stats.complete);
metrics.total_downloaded += u64::from(stats.downloaded);
metrics.total_incomplete += u64::from(stats.incomplete);
metrics.total_torrents += 1;
}

metrics
Expand Down
Loading
Loading