Skip to content

Commit 8d3a6fe

Browse files
committed
refactor: [torrust#1543] extract methods
1 parent 02c33f6 commit 8d3a6fe

File tree

3 files changed

+98
-47
lines changed

3 files changed

+98
-47
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,8 @@ impl From<TrackerLabeledMetrics> for LabeledStats {
134134

135135
#[cfg(test)]
136136
mod tests {
137-
use torrust_rest_tracker_api_core::statistics::metrics::Metrics;
137+
use torrust_rest_tracker_api_core::statistics::metrics::{Metrics, TorrentsMetrics};
138138
use torrust_rest_tracker_api_core::statistics::services::TrackerMetrics;
139-
use torrust_tracker_primitives::swarm_metadata::AggregateActiveSwarmMetadata;
140139

141140
use super::Stats;
142141

@@ -145,7 +144,7 @@ mod tests {
145144
fn stats_resource_should_be_converted_from_tracker_metrics() {
146145
assert_eq!(
147146
Stats::from(TrackerMetrics {
148-
torrents_metrics: AggregateActiveSwarmMetadata {
147+
torrents_metrics: TorrentsMetrics {
149148
total_complete: 1,
150149
total_downloaded: 2,
151150
total_incomplete: 3,

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,33 @@
1-
/// Metrics collected by the tracker.
1+
use torrust_tracker_primitives::swarm_metadata::AggregateActiveSwarmMetadata;
2+
3+
/// Metrics collected by the tracker at the swarm layer.
4+
#[derive(Copy, Clone, Debug, PartialEq, Default)]
5+
pub struct TorrentsMetrics {
6+
/// Total number of peers that have ever completed downloading.
7+
pub total_downloaded: u64,
8+
9+
/// Total number of seeders.
10+
pub total_complete: u64,
11+
12+
/// Total number of leechers.
13+
pub total_incomplete: u64,
14+
15+
/// Total number of torrents.
16+
pub total_torrents: u64,
17+
}
18+
19+
impl From<AggregateActiveSwarmMetadata> for TorrentsMetrics {
20+
fn from(value: AggregateActiveSwarmMetadata) -> Self {
21+
Self {
22+
total_downloaded: value.total_downloaded,
23+
total_complete: value.total_complete,
24+
total_incomplete: value.total_incomplete,
25+
total_torrents: value.total_torrents,
26+
}
27+
}
28+
}
29+
30+
/// Metrics collected by the tracker at the delivery layer.
231
///
332
/// - Number of connections handled
433
/// - Number of `announce` requests handled

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

Lines changed: 66 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ use torrust_tracker_configuration::Core;
99
use torrust_tracker_metrics::label::LabelSet;
1010
use torrust_tracker_metrics::metric_collection::MetricCollection;
1111
use torrust_tracker_metrics::metric_name;
12-
use torrust_tracker_primitives::swarm_metadata::AggregateActiveSwarmMetadata;
1312
use torrust_tracker_torrent_repository::statistics::TORRENT_REPOSITORY_TORRENTS_DOWNLOADS_TOTAL;
1413
use torrust_udp_tracker_server::statistics as udp_server_statistics;
1514

15+
use super::metrics::TorrentsMetrics;
1616
use crate::statistics::metrics::Metrics;
1717

1818
/// All the metrics collected by the tracker.
@@ -21,7 +21,7 @@ pub struct TrackerMetrics {
2121
/// Domain level metrics.
2222
///
2323
/// General metrics for all torrents (number of seeders, leechers, etcetera)
24-
pub torrents_metrics: AggregateActiveSwarmMetadata,
24+
pub torrents_metrics: TorrentsMetrics,
2525

2626
/// Application level metrics. Usage statistics/metrics.
2727
///
@@ -30,7 +30,6 @@ pub struct TrackerMetrics {
3030
}
3131

3232
/// It returns all the [`TrackerMetrics`]
33-
#[allow(deprecated)]
3433
pub async fn get_metrics(
3534
core_config: Arc<Core>,
3635
in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
@@ -40,10 +39,25 @@ pub async fn get_metrics(
4039
http_stats_repository: Arc<bittorrent_http_tracker_core::statistics::repository::Repository>,
4140
udp_server_stats_repository: Arc<udp_server_statistics::repository::Repository>,
4241
) -> TrackerMetrics {
42+
TrackerMetrics {
43+
torrents_metrics: get_torrents_metrics(
44+
core_config,
45+
in_memory_torrent_repository,
46+
torrent_repository_stats_repository,
47+
tracker_core_stats_repository,
48+
)
49+
.await,
50+
protocol_metrics: get_protocol_metrics(ban_service, http_stats_repository, udp_server_stats_repository).await,
51+
}
52+
}
53+
54+
async fn get_torrents_metrics(
55+
core_config: Arc<Core>,
56+
in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
57+
torrent_repository_stats_repository: Arc<torrust_tracker_torrent_repository::statistics::repository::Repository>,
58+
tracker_core_stats_repository: Arc<bittorrent_tracker_core::statistics::repository::Repository>,
59+
) -> TorrentsMetrics {
4360
let aggregate_active_swarm_metadata = in_memory_torrent_repository.get_aggregate_swarm_metadata().await;
44-
let udp_banned_ips_total = ban_service.read().await.get_banned_ips_total();
45-
let http_stats = http_stats_repository.get_stats().await;
46-
let udp_server_stats = udp_server_stats_repository.get_stats().await;
4761

4862
let total_downloaded = if core_config.tracker_policy.persistent_torrent_completed_stat {
4963
let metrics = tracker_core_stats_repository.get_metrics().await;
@@ -73,47 +87,57 @@ pub async fn get_metrics(
7387
}
7488
};
7589

76-
let mut torrents_metrics = aggregate_active_swarm_metadata;
90+
let mut torrents_metrics: TorrentsMetrics = aggregate_active_swarm_metadata.into();
7791
torrents_metrics.total_downloaded = total_downloaded;
7892

93+
torrents_metrics
94+
}
95+
96+
#[allow(deprecated)]
97+
async fn get_protocol_metrics(
98+
ban_service: Arc<RwLock<BanService>>,
99+
http_stats_repository: Arc<bittorrent_http_tracker_core::statistics::repository::Repository>,
100+
udp_server_stats_repository: Arc<udp_server_statistics::repository::Repository>,
101+
) -> Metrics {
102+
let udp_banned_ips_total = ban_service.read().await.get_banned_ips_total();
103+
let http_stats = http_stats_repository.get_stats().await;
104+
let udp_server_stats = udp_server_stats_repository.get_stats().await;
105+
79106
// For backward compatibility we keep the `tcp4_connections_handled` and
80107
// `tcp6_connections_handled` metrics. They don't make sense for the HTTP
81108
// tracker, but we keep them for now. In new major versions we should remove
82109
// them.
83110

84-
TrackerMetrics {
85-
torrents_metrics,
86-
protocol_metrics: Metrics {
87-
// TCPv4
88-
tcp4_connections_handled: http_stats.tcp4_announces_handled + http_stats.tcp4_scrapes_handled,
89-
tcp4_announces_handled: http_stats.tcp4_announces_handled,
90-
tcp4_scrapes_handled: http_stats.tcp4_scrapes_handled,
91-
// TCPv6
92-
tcp6_connections_handled: http_stats.tcp6_announces_handled + http_stats.tcp6_scrapes_handled,
93-
tcp6_announces_handled: http_stats.tcp6_announces_handled,
94-
tcp6_scrapes_handled: http_stats.tcp6_scrapes_handled,
95-
// UDP
96-
udp_requests_aborted: udp_server_stats.udp_requests_aborted,
97-
udp_requests_banned: udp_server_stats.udp_requests_banned,
98-
udp_banned_ips_total: udp_banned_ips_total as u64,
99-
udp_avg_connect_processing_time_ns: udp_server_stats.udp_avg_connect_processing_time_ns,
100-
udp_avg_announce_processing_time_ns: udp_server_stats.udp_avg_announce_processing_time_ns,
101-
udp_avg_scrape_processing_time_ns: udp_server_stats.udp_avg_scrape_processing_time_ns,
102-
// UDPv4
103-
udp4_requests: udp_server_stats.udp4_requests,
104-
udp4_connections_handled: udp_server_stats.udp4_connections_handled,
105-
udp4_announces_handled: udp_server_stats.udp4_announces_handled,
106-
udp4_scrapes_handled: udp_server_stats.udp4_scrapes_handled,
107-
udp4_responses: udp_server_stats.udp4_responses,
108-
udp4_errors_handled: udp_server_stats.udp4_errors_handled,
109-
// UDPv6
110-
udp6_requests: udp_server_stats.udp6_requests,
111-
udp6_connections_handled: udp_server_stats.udp6_connections_handled,
112-
udp6_announces_handled: udp_server_stats.udp6_announces_handled,
113-
udp6_scrapes_handled: udp_server_stats.udp6_scrapes_handled,
114-
udp6_responses: udp_server_stats.udp6_responses,
115-
udp6_errors_handled: udp_server_stats.udp6_errors_handled,
116-
},
111+
Metrics {
112+
// TCPv4
113+
tcp4_connections_handled: http_stats.tcp4_announces_handled + http_stats.tcp4_scrapes_handled,
114+
tcp4_announces_handled: http_stats.tcp4_announces_handled,
115+
tcp4_scrapes_handled: http_stats.tcp4_scrapes_handled,
116+
// TCPv6
117+
tcp6_connections_handled: http_stats.tcp6_announces_handled + http_stats.tcp6_scrapes_handled,
118+
tcp6_announces_handled: http_stats.tcp6_announces_handled,
119+
tcp6_scrapes_handled: http_stats.tcp6_scrapes_handled,
120+
// UDP
121+
udp_requests_aborted: udp_server_stats.udp_requests_aborted,
122+
udp_requests_banned: udp_server_stats.udp_requests_banned,
123+
udp_banned_ips_total: udp_banned_ips_total as u64,
124+
udp_avg_connect_processing_time_ns: udp_server_stats.udp_avg_connect_processing_time_ns,
125+
udp_avg_announce_processing_time_ns: udp_server_stats.udp_avg_announce_processing_time_ns,
126+
udp_avg_scrape_processing_time_ns: udp_server_stats.udp_avg_scrape_processing_time_ns,
127+
// UDPv4
128+
udp4_requests: udp_server_stats.udp4_requests,
129+
udp4_connections_handled: udp_server_stats.udp4_connections_handled,
130+
udp4_announces_handled: udp_server_stats.udp4_announces_handled,
131+
udp4_scrapes_handled: udp_server_stats.udp4_scrapes_handled,
132+
udp4_responses: udp_server_stats.udp4_responses,
133+
udp4_errors_handled: udp_server_stats.udp4_errors_handled,
134+
// UDPv6
135+
udp6_requests: udp_server_stats.udp6_requests,
136+
udp6_connections_handled: udp_server_stats.udp6_connections_handled,
137+
udp6_announces_handled: udp_server_stats.udp6_announces_handled,
138+
udp6_scrapes_handled: udp_server_stats.udp6_scrapes_handled,
139+
udp6_responses: udp_server_stats.udp6_responses,
140+
udp6_errors_handled: udp_server_stats.udp6_errors_handled,
117141
}
118142
}
119143

@@ -184,11 +208,10 @@ mod tests {
184208
use tokio::sync::RwLock;
185209
use torrust_tracker_configuration::Configuration;
186210
use torrust_tracker_events::bus::SenderStatus;
187-
use torrust_tracker_primitives::swarm_metadata::AggregateActiveSwarmMetadata;
188211
use torrust_tracker_test_helpers::configuration;
189212
use torrust_tracker_torrent_repository::container::TorrentRepositoryContainer;
190213

191-
use crate::statistics::metrics::Metrics;
214+
use crate::statistics::metrics::{Metrics, TorrentsMetrics};
192215
use crate::statistics::services::{get_metrics, TrackerMetrics};
193216

194217
pub fn tracker_configuration() -> Configuration {
@@ -235,7 +258,7 @@ mod tests {
235258
assert_eq!(
236259
tracker_metrics,
237260
TrackerMetrics {
238-
torrents_metrics: AggregateActiveSwarmMetadata::default(),
261+
torrents_metrics: TorrentsMetrics::default(),
239262
protocol_metrics: Metrics::default(),
240263
}
241264
);

0 commit comments

Comments
 (0)