Skip to content

Commit 144a338

Browse files
committed
refactor: [#1243] move and rename struct and fields (AggregateSwarmMetadata)
To avoid confusion with `SwarmMetadata` - `SwarmMetadata`: metrics for one torrent. - `AggregateSwarmMetadata`: metrics for all torrents.
1 parent 2020162 commit 144a338

File tree

21 files changed

+191
-201
lines changed

21 files changed

+191
-201
lines changed

packages/axum-rest-tracker-api-server/src/v1/context/stats/resources.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ pub struct Stats {
7979
impl From<TrackerMetrics> for Stats {
8080
fn from(metrics: TrackerMetrics) -> Self {
8181
Self {
82-
torrents: metrics.torrents_metrics.torrents,
83-
seeders: metrics.torrents_metrics.complete,
84-
completed: metrics.torrents_metrics.downloaded,
85-
leechers: metrics.torrents_metrics.incomplete,
82+
torrents: metrics.torrents_metrics.total_torrents,
83+
seeders: metrics.torrents_metrics.total_complete,
84+
completed: metrics.torrents_metrics.total_downloaded,
85+
leechers: metrics.torrents_metrics.total_incomplete,
8686
// TCP
8787
tcp4_connections_handled: metrics.protocol_metrics.tcp4_connections_handled,
8888
tcp4_announces_handled: metrics.protocol_metrics.tcp4_announces_handled,
@@ -119,19 +119,19 @@ impl From<TrackerMetrics> for Stats {
119119
mod tests {
120120
use torrust_rest_tracker_api_core::statistics::metrics::Metrics;
121121
use torrust_rest_tracker_api_core::statistics::services::TrackerMetrics;
122-
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
122+
use torrust_tracker_primitives::swarm_metadata::AggregateSwarmMetadata;
123123

124124
use super::Stats;
125125

126126
#[test]
127127
fn stats_resource_should_be_converted_from_tracker_metrics() {
128128
assert_eq!(
129129
Stats::from(TrackerMetrics {
130-
torrents_metrics: TorrentsMetrics {
131-
complete: 1,
132-
downloaded: 2,
133-
incomplete: 3,
134-
torrents: 4
130+
torrents_metrics: AggregateSwarmMetadata {
131+
total_complete: 1,
132+
total_downloaded: 2,
133+
total_incomplete: 3,
134+
total_torrents: 4
135135
},
136136
protocol_metrics: Metrics {
137137
// TCP

packages/axum-rest-tracker-api-server/src/v1/context/stats/responses.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ pub fn stats_response(tracker_metrics: TrackerMetrics) -> Response {
1616
pub fn metrics_response(tracker_metrics: &TrackerMetrics) -> Response {
1717
let mut lines = vec![];
1818

19-
lines.push(format!("torrents {}", tracker_metrics.torrents_metrics.torrents));
20-
lines.push(format!("seeders {}", tracker_metrics.torrents_metrics.complete));
21-
lines.push(format!("completed {}", tracker_metrics.torrents_metrics.downloaded));
22-
lines.push(format!("leechers {}", tracker_metrics.torrents_metrics.incomplete));
19+
lines.push(format!("torrents {}", tracker_metrics.torrents_metrics.total_torrents));
20+
lines.push(format!("seeders {}", tracker_metrics.torrents_metrics.total_complete));
21+
lines.push(format!("completed {}", tracker_metrics.torrents_metrics.total_downloaded));
22+
lines.push(format!("leechers {}", tracker_metrics.torrents_metrics.total_incomplete));
2323

2424
// TCP
2525

packages/http-tracker-core/src/statistics/services.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
use std::sync::Arc;
2424

2525
use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository;
26-
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
26+
use torrust_tracker_primitives::swarm_metadata::AggregateSwarmMetadata;
2727

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

3939
/// Application level metrics. Usage statistics/metrics.
4040
///
@@ -72,7 +72,7 @@ mod tests {
7272
use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository;
7373
use bittorrent_tracker_core::{self};
7474
use torrust_tracker_configuration::Configuration;
75-
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
75+
use torrust_tracker_primitives::swarm_metadata::AggregateSwarmMetadata;
7676
use torrust_tracker_test_helpers::configuration;
7777

7878
use crate::statistics;
@@ -96,7 +96,7 @@ mod tests {
9696
assert_eq!(
9797
tracker_metrics,
9898
TrackerMetrics {
99-
torrents_metrics: TorrentsMetrics::default(),
99+
torrents_metrics: AggregateSwarmMetadata::default(),
100100
protocol_metrics: statistics::metrics::Metrics::default(),
101101
}
102102
);

packages/primitives/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ pub mod core;
88
pub mod pagination;
99
pub mod peer;
1010
pub mod swarm_metadata;
11-
pub mod torrent_metrics;
1211

1312
use std::collections::BTreeMap;
1413
use std::time::Duration;
+34-3
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1+
use std::ops::AddAssign;
2+
13
use derive_more::Constructor;
24

35
/// Swarm statistics for one torrent.
6+
///
47
/// Swarm metadata dictionary in the scrape response.
58
///
69
/// See [BEP 48: Tracker Protocol Extension: Scrape](https://www.bittorrent.org/beps/bep_0048.html)
710
#[derive(Copy, Clone, Debug, PartialEq, Default, Constructor)]
811
pub struct SwarmMetadata {
9-
/// (i.e `completed`): The number of peers that have ever completed
12+
/// (i.e `completed`): The number of peers that have ever completed
1013
/// downloading a given torrent.
1114
pub downloaded: u32,
1215

13-
/// (i.e `seeders`): The number of active peers that have completed
16+
/// (i.e `seeders`): The number of active peers that have completed
1417
/// downloading (seeders) a given torrent.
1518
pub complete: u32,
1619

17-
/// (i.e `leechers`): The number of active peers that have not completed
20+
/// (i.e `leechers`): The number of active peers that have not completed
1821
/// downloading (leechers) a given torrent.
1922
pub incomplete: u32,
2023
}
@@ -25,3 +28,31 @@ impl SwarmMetadata {
2528
Self::default()
2629
}
2730
}
31+
32+
/// Structure that holds aggregate swarm metadata.
33+
///
34+
/// Metrics are aggregate values for all torrents.
35+
#[derive(Copy, Clone, Debug, PartialEq, Default)]
36+
pub struct AggregateSwarmMetadata {
37+
/// Total number of peers that have ever completed downloading for all
38+
/// torrents.
39+
pub total_downloaded: u64,
40+
41+
/// Total number of seeders for all torrents.
42+
pub total_complete: u64,
43+
44+
/// Total number of leechers for all torrents.
45+
pub total_incomplete: u64,
46+
47+
/// Total number of torrents.
48+
pub total_torrents: u64,
49+
}
50+
51+
impl AddAssign for AggregateSwarmMetadata {
52+
fn add_assign(&mut self, rhs: Self) {
53+
self.total_complete += rhs.total_complete;
54+
self.total_downloaded += rhs.total_downloaded;
55+
self.total_incomplete += rhs.total_incomplete;
56+
self.total_torrents += rhs.total_torrents;
57+
}
58+
}

packages/primitives/src/torrent_metrics.rs

-29
This file was deleted.

packages/rest-tracker-api-core/src/statistics/services.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepo
44
use bittorrent_udp_tracker_core::services::banning::BanService;
55
use bittorrent_udp_tracker_core::{self, statistics as udp_core_statistics};
66
use tokio::sync::RwLock;
7-
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
7+
use torrust_tracker_primitives::swarm_metadata::AggregateSwarmMetadata;
88
use torrust_udp_tracker_server::statistics as udp_server_statistics;
99

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

2020
/// Application level metrics. Usage statistics/metrics.
2121
///
@@ -83,7 +83,7 @@ mod tests {
8383
use bittorrent_udp_tracker_core::MAX_CONNECTION_ID_ERRORS_PER_IP;
8484
use tokio::sync::RwLock;
8585
use torrust_tracker_configuration::Configuration;
86-
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
86+
use torrust_tracker_primitives::swarm_metadata::AggregateSwarmMetadata;
8787
use torrust_tracker_test_helpers::configuration;
8888

8989
use crate::statistics::metrics::Metrics;
@@ -127,7 +127,7 @@ mod tests {
127127
assert_eq!(
128128
tracker_metrics,
129129
TrackerMetrics {
130-
torrents_metrics: TorrentsMetrics::default(),
130+
torrents_metrics: AggregateSwarmMetadata::default(),
131131
protocol_metrics: Metrics::default(),
132132
}
133133
);

packages/torrent-repository/src/repository/dash_map_mutex_std.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ use bittorrent_primitives::info_hash::InfoHash;
44
use dashmap::DashMap;
55
use torrust_tracker_configuration::TrackerPolicy;
66
use torrust_tracker_primitives::pagination::Pagination;
7-
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
8-
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
7+
use torrust_tracker_primitives::swarm_metadata::{AggregateSwarmMetadata, SwarmMetadata};
98
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents};
109

1110
use super::Repository;
@@ -47,15 +46,15 @@ where
4746
maybe_entry.map(|entry| entry.clone())
4847
}
4948

50-
fn get_metrics(&self) -> TorrentsMetrics {
51-
let mut metrics = TorrentsMetrics::default();
49+
fn get_metrics(&self) -> AggregateSwarmMetadata {
50+
let mut metrics = AggregateSwarmMetadata::default();
5251

5352
for entry in &self.torrents {
5453
let stats = entry.value().lock().expect("it should get a lock").get_swarm_metadata();
55-
metrics.complete += u64::from(stats.complete);
56-
metrics.downloaded += u64::from(stats.downloaded);
57-
metrics.incomplete += u64::from(stats.incomplete);
58-
metrics.torrents += 1;
54+
metrics.total_complete += u64::from(stats.complete);
55+
metrics.total_downloaded += u64::from(stats.downloaded);
56+
metrics.total_incomplete += u64::from(stats.incomplete);
57+
metrics.total_torrents += 1;
5958
}
6059

6160
metrics

packages/torrent-repository/src/repository/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use bittorrent_primitives::info_hash::InfoHash;
22
use torrust_tracker_configuration::TrackerPolicy;
33
use torrust_tracker_primitives::pagination::Pagination;
4-
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
5-
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
4+
use torrust_tracker_primitives::swarm_metadata::{AggregateSwarmMetadata, SwarmMetadata};
65
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents};
76

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

1918
pub trait Repository<T>: Debug + Default + Sized + 'static {
2019
fn get(&self, key: &InfoHash) -> Option<T>;
21-
fn get_metrics(&self) -> TorrentsMetrics;
20+
fn get_metrics(&self) -> AggregateSwarmMetadata;
2221
fn get_paginated(&self, pagination: Option<&Pagination>) -> Vec<(InfoHash, T)>;
2322
fn import_persistent(&self, persistent_torrents: &PersistentTorrents);
2423
fn remove(&self, key: &InfoHash) -> Option<T>;
@@ -31,7 +30,7 @@ pub trait Repository<T>: Debug + Default + Sized + 'static {
3130
#[allow(clippy::module_name_repetitions)]
3231
pub trait RepositoryAsync<T>: Debug + Default + Sized + 'static {
3332
fn get(&self, key: &InfoHash) -> impl std::future::Future<Output = Option<T>> + Send;
34-
fn get_metrics(&self) -> impl std::future::Future<Output = TorrentsMetrics> + Send;
33+
fn get_metrics(&self) -> impl std::future::Future<Output = AggregateSwarmMetadata> + Send;
3534
fn get_paginated(&self, pagination: Option<&Pagination>) -> impl std::future::Future<Output = Vec<(InfoHash, T)>> + Send;
3635
fn import_persistent(&self, persistent_torrents: &PersistentTorrents) -> impl std::future::Future<Output = ()> + Send;
3736
fn remove(&self, key: &InfoHash) -> impl std::future::Future<Output = Option<T>> + Send;

packages/torrent-repository/src/repository/rw_lock_std.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use bittorrent_primitives::info_hash::InfoHash;
22
use torrust_tracker_configuration::TrackerPolicy;
33
use torrust_tracker_primitives::pagination::Pagination;
4-
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
5-
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
4+
use torrust_tracker_primitives::swarm_metadata::{AggregateSwarmMetadata, SwarmMetadata};
65
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents};
76

87
use super::Repository;
@@ -65,15 +64,15 @@ where
6564
db.get(key).cloned()
6665
}
6766

68-
fn get_metrics(&self) -> TorrentsMetrics {
69-
let mut metrics = TorrentsMetrics::default();
67+
fn get_metrics(&self) -> AggregateSwarmMetadata {
68+
let mut metrics = AggregateSwarmMetadata::default();
7069

7170
for entry in self.get_torrents().values() {
7271
let stats = entry.get_swarm_metadata();
73-
metrics.complete += u64::from(stats.complete);
74-
metrics.downloaded += u64::from(stats.downloaded);
75-
metrics.incomplete += u64::from(stats.incomplete);
76-
metrics.torrents += 1;
72+
metrics.total_complete += u64::from(stats.complete);
73+
metrics.total_downloaded += u64::from(stats.downloaded);
74+
metrics.total_incomplete += u64::from(stats.incomplete);
75+
metrics.total_torrents += 1;
7776
}
7877

7978
metrics

packages/torrent-repository/src/repository/rw_lock_std_mutex_std.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ use std::sync::Arc;
33
use bittorrent_primitives::info_hash::InfoHash;
44
use torrust_tracker_configuration::TrackerPolicy;
55
use torrust_tracker_primitives::pagination::Pagination;
6-
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
7-
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
6+
use torrust_tracker_primitives::swarm_metadata::{AggregateSwarmMetadata, SwarmMetadata};
87
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents};
98

109
use super::Repository;
@@ -60,15 +59,15 @@ where
6059
db.get(key).cloned()
6160
}
6261

63-
fn get_metrics(&self) -> TorrentsMetrics {
64-
let mut metrics = TorrentsMetrics::default();
62+
fn get_metrics(&self) -> AggregateSwarmMetadata {
63+
let mut metrics = AggregateSwarmMetadata::default();
6564

6665
for entry in self.get_torrents().values() {
6766
let stats = entry.lock().expect("it should get a lock").get_swarm_metadata();
68-
metrics.complete += u64::from(stats.complete);
69-
metrics.downloaded += u64::from(stats.downloaded);
70-
metrics.incomplete += u64::from(stats.incomplete);
71-
metrics.torrents += 1;
67+
metrics.total_complete += u64::from(stats.complete);
68+
metrics.total_downloaded += u64::from(stats.downloaded);
69+
metrics.total_incomplete += u64::from(stats.incomplete);
70+
metrics.total_torrents += 1;
7271
}
7372

7473
metrics

packages/torrent-repository/src/repository/rw_lock_std_mutex_tokio.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ use futures::future::join_all;
77
use futures::{Future, FutureExt};
88
use torrust_tracker_configuration::TrackerPolicy;
99
use torrust_tracker_primitives::pagination::Pagination;
10-
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
11-
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
10+
use torrust_tracker_primitives::swarm_metadata::{AggregateSwarmMetadata, SwarmMetadata};
1211
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents};
1312

1413
use super::RepositoryAsync;
@@ -86,17 +85,17 @@ where
8685
}
8786
}
8887

89-
async fn get_metrics(&self) -> TorrentsMetrics {
90-
let mut metrics = TorrentsMetrics::default();
88+
async fn get_metrics(&self) -> AggregateSwarmMetadata {
89+
let mut metrics = AggregateSwarmMetadata::default();
9190

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

9493
for entry in entries {
9594
let stats = entry.lock().await.get_swarm_metadata();
96-
metrics.complete += u64::from(stats.complete);
97-
metrics.downloaded += u64::from(stats.downloaded);
98-
metrics.incomplete += u64::from(stats.incomplete);
99-
metrics.torrents += 1;
95+
metrics.total_complete += u64::from(stats.complete);
96+
metrics.total_downloaded += u64::from(stats.downloaded);
97+
metrics.total_incomplete += u64::from(stats.incomplete);
98+
metrics.total_torrents += 1;
10099
}
101100

102101
metrics

0 commit comments

Comments
 (0)