Skip to content

Commit b0e7443

Browse files
committed
fix: [torrust#1543] return always in API the downloads number from tracker-core
The tracker-core always has the metric alhoutght it can be persisted or not. When it's not persisted, it contains the number of downloads during the session. On the other hand, the `torrent-repository` metri uses labels, so you have to sum all values for all labels to get the total. ``` torrent_repository_torrents_downloads_total{peer_role="seeder"} 1 tracker_core_persistent_torrents_downloads_total{} 1 ```
1 parent 8d3a6fe commit b0e7443

File tree

7 files changed

+26
-59
lines changed

7 files changed

+26
-59
lines changed

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use bittorrent_udp_tracker_core::services::banning::BanService;
1010
use serde::Deserialize;
1111
use tokio::sync::RwLock;
1212
use torrust_rest_tracker_api_core::statistics::services::{get_labeled_metrics, get_metrics};
13-
use torrust_tracker_configuration::Core;
1413

1514
use super::responses::{labeled_metrics_response, labeled_stats_response, metrics_response, stats_response};
1615

@@ -41,10 +40,8 @@ pub struct QueryParams {
4140
#[allow(clippy::type_complexity)]
4241
pub async fn get_stats_handler(
4342
State(state): State<(
44-
Arc<Core>,
4543
Arc<InMemoryTorrentRepository>,
4644
Arc<RwLock<BanService>>,
47-
Arc<torrust_tracker_torrent_repository::statistics::repository::Repository>,
4845
Arc<bittorrent_tracker_core::statistics::repository::Repository>,
4946
Arc<bittorrent_http_tracker_core::statistics::repository::Repository>,
5047
Arc<torrust_udp_tracker_server::statistics::repository::Repository>,
@@ -57,8 +54,6 @@ pub async fn get_stats_handler(
5754
state.2.clone(),
5855
state.3.clone(),
5956
state.4.clone(),
60-
state.5.clone(),
61-
state.6.clone(),
6257
)
6358
.await;
6459

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ pub fn add(prefix: &str, router: Router, http_api_container: &Arc<TrackerHttpApi
1717
.route(
1818
&format!("{prefix}/stats"),
1919
get(get_stats_handler).with_state((
20-
http_api_container.tracker_core_container.core_config.clone(),
2120
http_api_container.tracker_core_container.in_memory_torrent_repository.clone(),
2221
http_api_container.ban_service.clone(),
23-
http_api_container.torrent_repository_container.stats_repository.clone(),
2422
http_api_container.tracker_core_container.stats_repository.clone(),
2523
http_api_container.http_stats_repository.clone(),
2624
http_api_container.udp_server_stats_repository.clone(),

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

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
use std::sync::Arc;
22

3-
use bittorrent_tracker_core::statistics::TRACKER_CORE_PERSISTENT_TORRENTS_DOWNLOADS_TOTAL;
43
use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository;
54
use bittorrent_udp_tracker_core::services::banning::BanService;
65
use bittorrent_udp_tracker_core::{self};
76
use tokio::sync::RwLock;
8-
use torrust_tracker_configuration::Core;
9-
use torrust_tracker_metrics::label::LabelSet;
107
use torrust_tracker_metrics::metric_collection::MetricCollection;
11-
use torrust_tracker_metrics::metric_name;
12-
use torrust_tracker_torrent_repository::statistics::TORRENT_REPOSITORY_TORRENTS_DOWNLOADS_TOTAL;
138
use torrust_udp_tracker_server::statistics as udp_server_statistics;
149

1510
use super::metrics::TorrentsMetrics;
@@ -31,64 +26,27 @@ pub struct TrackerMetrics {
3126

3227
/// It returns all the [`TrackerMetrics`]
3328
pub async fn get_metrics(
34-
core_config: Arc<Core>,
3529
in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
3630
ban_service: Arc<RwLock<BanService>>,
37-
torrent_repository_stats_repository: Arc<torrust_tracker_torrent_repository::statistics::repository::Repository>,
3831
tracker_core_stats_repository: Arc<bittorrent_tracker_core::statistics::repository::Repository>,
3932
http_stats_repository: Arc<bittorrent_http_tracker_core::statistics::repository::Repository>,
4033
udp_server_stats_repository: Arc<udp_server_statistics::repository::Repository>,
4134
) -> TrackerMetrics {
4235
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,
36+
torrents_metrics: get_torrents_metrics(in_memory_torrent_repository, tracker_core_stats_repository).await,
5037
protocol_metrics: get_protocol_metrics(ban_service, http_stats_repository, udp_server_stats_repository).await,
5138
}
5239
}
5340

5441
async fn get_torrents_metrics(
55-
core_config: Arc<Core>,
5642
in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
57-
torrent_repository_stats_repository: Arc<torrust_tracker_torrent_repository::statistics::repository::Repository>,
43+
5844
tracker_core_stats_repository: Arc<bittorrent_tracker_core::statistics::repository::Repository>,
5945
) -> TorrentsMetrics {
6046
let aggregate_active_swarm_metadata = in_memory_torrent_repository.get_aggregate_swarm_metadata().await;
6147

62-
let total_downloaded = if core_config.tracker_policy.persistent_torrent_completed_stat {
63-
let metrics = tracker_core_stats_repository.get_metrics().await;
64-
65-
let downloads = metrics.metric_collection.get_counter_value(
66-
&metric_name!(TRACKER_CORE_PERSISTENT_TORRENTS_DOWNLOADS_TOTAL),
67-
&LabelSet::default(),
68-
);
69-
70-
if let Some(downloads) = downloads {
71-
downloads.value()
72-
} else {
73-
0
74-
}
75-
} else {
76-
let metrics = torrent_repository_stats_repository.get_metrics().await;
77-
78-
let downloads = metrics.metric_collection.get_counter_value(
79-
&metric_name!(TORRENT_REPOSITORY_TORRENTS_DOWNLOADS_TOTAL),
80-
&LabelSet::default(),
81-
);
82-
83-
if let Some(downloads) = downloads {
84-
downloads.value()
85-
} else {
86-
0
87-
}
88-
};
89-
9048
let mut torrents_metrics: TorrentsMetrics = aggregate_active_swarm_metadata.into();
91-
torrents_metrics.total_downloaded = total_downloaded;
49+
torrents_metrics.total_downloaded = tracker_core_stats_repository.get_torrents_downloads_total().await;
9250

9351
torrents_metrics
9452
}
@@ -152,7 +110,6 @@ pub struct TrackerLabeledMetrics {
152110
///
153111
/// Will panic if the metrics cannot be merged. This could happen if the
154112
/// packages are producing duplicate metric names, for example.
155-
#[allow(deprecated)]
156113
pub async fn get_labeled_metrics(
157114
in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
158115
ban_service: Arc<RwLock<BanService>>,
@@ -245,10 +202,8 @@ mod tests {
245202
let udp_server_stats_repository = Arc::new(torrust_udp_tracker_server::statistics::repository::Repository::new());
246203

247204
let tracker_metrics = get_metrics(
248-
core_config,
249205
tracker_core_container.in_memory_torrent_repository.clone(),
250206
ban_service.clone(),
251-
torrent_repository_container.stats_repository.clone(),
252207
tracker_core_container.stats_repository.clone(),
253208
http_stats_repository.clone(),
254209
udp_server_stats_repository.clone(),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const TORRENT_REPOSITORY_TORRENTS_ADDED_TOTAL: &str = "torrent_repository_torren
1414
const TORRENT_REPOSITORY_TORRENTS_REMOVED_TOTAL: &str = "torrent_repository_torrents_removed_total";
1515

1616
const TORRENT_REPOSITORY_TORRENTS_TOTAL: &str = "torrent_repository_torrents_total";
17-
pub const TORRENT_REPOSITORY_TORRENTS_DOWNLOADS_TOTAL: &str = "torrent_repository_torrents_downloads_total";
17+
const TORRENT_REPOSITORY_TORRENTS_DOWNLOADS_TOTAL: &str = "torrent_repository_torrents_downloads_total";
1818
const TORRENT_REPOSITORY_TORRENTS_INACTIVE_TOTAL: &str = "torrent_repository_torrents_inactive_total";
1919

2020
// Peers metrics

packages/tracker-client/src/http/client/requests/announce.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl QueryBuilder {
102102
peer_id: PeerId(*b"-qB00000000000000001").0,
103103
port: 17548,
104104
left: 0,
105-
event: Some(Event::Completed),
105+
event: Some(Event::Started),
106106
compact: Some(Compact::NotAccepted),
107107
};
108108
Self {

packages/tracker-core/src/statistics/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use torrust_tracker_metrics::unit::Unit;
1010

1111
// Torrent metrics
1212

13-
pub const TRACKER_CORE_PERSISTENT_TORRENTS_DOWNLOADS_TOTAL: &str = "tracker_core_persistent_torrents_downloads_total";
13+
const TRACKER_CORE_PERSISTENT_TORRENTS_DOWNLOADS_TOTAL: &str = "tracker_core_persistent_torrents_downloads_total";
1414

1515
#[must_use]
1616
pub fn describe_metrics() -> Metrics {

packages/tracker-core/src/statistics/repository.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ use tokio::sync::{RwLock, RwLockReadGuard};
44
use torrust_tracker_metrics::label::LabelSet;
55
use torrust_tracker_metrics::metric::MetricName;
66
use torrust_tracker_metrics::metric_collection::Error;
7+
use torrust_tracker_metrics::metric_name;
78
use torrust_tracker_primitives::DurationSinceUnixEpoch;
89

9-
use super::describe_metrics;
1010
use super::metrics::Metrics;
11+
use super::{describe_metrics, TRACKER_CORE_PERSISTENT_TORRENTS_DOWNLOADS_TOTAL};
1112

1213
/// A repository for the torrent repository metrics.
1314
#[derive(Clone)]
@@ -154,4 +155,22 @@ impl Repository {
154155

155156
result
156157
}
158+
159+
/// Get the total number of torrent downloads.
160+
///
161+
/// The value is persisted in database if persistence for downloads metrics is enabled.
162+
pub async fn get_torrents_downloads_total(&self) -> u64 {
163+
let metrics = self.get_metrics().await;
164+
165+
let downloads = metrics.metric_collection.get_counter_value(
166+
&metric_name!(TRACKER_CORE_PERSISTENT_TORRENTS_DOWNLOADS_TOTAL),
167+
&LabelSet::default(),
168+
);
169+
170+
if let Some(downloads) = downloads {
171+
downloads.value()
172+
} else {
173+
0
174+
}
175+
}
157176
}

0 commit comments

Comments
 (0)