Skip to content

Commit 02c33f6

Browse files
committed
fix: [torrust#1543] the downloads counter values returned in the API
It now returns the persisted value when available (stats persistence enabled).
1 parent 762bf69 commit 02c33f6

File tree

7 files changed

+73
-7
lines changed

7 files changed

+73
-7
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ 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;
1314

1415
use super::responses::{labeled_metrics_response, labeled_stats_response, metrics_response, stats_response};
1516

@@ -40,14 +41,26 @@ pub struct QueryParams {
4041
#[allow(clippy::type_complexity)]
4142
pub async fn get_stats_handler(
4243
State(state): State<(
44+
Arc<Core>,
4345
Arc<InMemoryTorrentRepository>,
4446
Arc<RwLock<BanService>>,
47+
Arc<torrust_tracker_torrent_repository::statistics::repository::Repository>,
48+
Arc<bittorrent_tracker_core::statistics::repository::Repository>,
4549
Arc<bittorrent_http_tracker_core::statistics::repository::Repository>,
4650
Arc<torrust_udp_tracker_server::statistics::repository::Repository>,
4751
)>,
4852
params: Query<QueryParams>,
4953
) -> Response {
50-
let metrics = get_metrics(state.0.clone(), state.1.clone(), state.2.clone(), state.3.clone()).await;
54+
let metrics = get_metrics(
55+
state.0.clone(),
56+
state.1.clone(),
57+
state.2.clone(),
58+
state.3.clone(),
59+
state.4.clone(),
60+
state.5.clone(),
61+
state.6.clone(),
62+
)
63+
.await;
5164

5265
match params.0.format {
5366
Some(format) => match format {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ 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(),
2021
http_api_container.tracker_core_container.in_memory_torrent_repository.clone(),
2122
http_api_container.ban_service.clone(),
23+
http_api_container.torrent_repository_container.stats_repository.clone(),
24+
http_api_container.tracker_core_container.stats_repository.clone(),
2225
http_api_container.http_stats_repository.clone(),
2326
http_api_container.udp_server_stats_repository.clone(),
2427
)),

packages/rest-tracker-api-core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ torrust-tracker-torrent-repository = { version = "3.0.0-develop", path = "../tor
2525
torrust-udp-tracker-server = { version = "3.0.0-develop", path = "../udp-tracker-server" }
2626

2727
[dev-dependencies]
28+
torrust-tracker-events = { version = "3.0.0-develop", path = "../events" }
2829
torrust-tracker-test-helpers = { version = "3.0.0-develop", path = "../test-helpers" }

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

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
use std::sync::Arc;
22

3+
use bittorrent_tracker_core::statistics::TRACKER_CORE_PERSISTENT_TORRENTS_DOWNLOADS_TOTAL;
34
use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository;
45
use bittorrent_udp_tracker_core::services::banning::BanService;
56
use bittorrent_udp_tracker_core::{self};
67
use tokio::sync::RwLock;
8+
use torrust_tracker_configuration::Core;
9+
use torrust_tracker_metrics::label::LabelSet;
710
use torrust_tracker_metrics::metric_collection::MetricCollection;
11+
use torrust_tracker_metrics::metric_name;
812
use torrust_tracker_primitives::swarm_metadata::AggregateActiveSwarmMetadata;
13+
use torrust_tracker_torrent_repository::statistics::TORRENT_REPOSITORY_TORRENTS_DOWNLOADS_TOTAL;
914
use torrust_udp_tracker_server::statistics as udp_server_statistics;
1015

1116
use crate::statistics::metrics::Metrics;
@@ -27,16 +32,50 @@ pub struct TrackerMetrics {
2732
/// It returns all the [`TrackerMetrics`]
2833
#[allow(deprecated)]
2934
pub async fn get_metrics(
35+
core_config: Arc<Core>,
3036
in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
3137
ban_service: Arc<RwLock<BanService>>,
38+
torrent_repository_stats_repository: Arc<torrust_tracker_torrent_repository::statistics::repository::Repository>,
39+
tracker_core_stats_repository: Arc<bittorrent_tracker_core::statistics::repository::Repository>,
3240
http_stats_repository: Arc<bittorrent_http_tracker_core::statistics::repository::Repository>,
3341
udp_server_stats_repository: Arc<udp_server_statistics::repository::Repository>,
3442
) -> TrackerMetrics {
35-
let torrents_metrics = in_memory_torrent_repository.get_aggregate_swarm_metadata().await;
43+
let aggregate_active_swarm_metadata = in_memory_torrent_repository.get_aggregate_swarm_metadata().await;
3644
let udp_banned_ips_total = ban_service.read().await.get_banned_ips_total();
3745
let http_stats = http_stats_repository.get_stats().await;
3846
let udp_server_stats = udp_server_stats_repository.get_stats().await;
3947

48+
let total_downloaded = if core_config.tracker_policy.persistent_torrent_completed_stat {
49+
let metrics = tracker_core_stats_repository.get_metrics().await;
50+
51+
let downloads = metrics.metric_collection.get_counter_value(
52+
&metric_name!(TRACKER_CORE_PERSISTENT_TORRENTS_DOWNLOADS_TOTAL),
53+
&LabelSet::default(),
54+
);
55+
56+
if let Some(downloads) = downloads {
57+
downloads.value()
58+
} else {
59+
0
60+
}
61+
} else {
62+
let metrics = torrent_repository_stats_repository.get_metrics().await;
63+
64+
let downloads = metrics.metric_collection.get_counter_value(
65+
&metric_name!(TORRENT_REPOSITORY_TORRENTS_DOWNLOADS_TOTAL),
66+
&LabelSet::default(),
67+
);
68+
69+
if let Some(downloads) = downloads {
70+
downloads.value()
71+
} else {
72+
0
73+
}
74+
};
75+
76+
let mut torrents_metrics = aggregate_active_swarm_metadata;
77+
torrents_metrics.total_downloaded = total_downloaded;
78+
4079
// For backward compatibility we keep the `tcp4_connections_handled` and
4180
// `tcp6_connections_handled` metrics. They don't make sense for the HTTP
4281
// tracker, but we keep them for now. In new major versions we should remove
@@ -138,14 +177,16 @@ mod tests {
138177
use bittorrent_http_tracker_core::event::sender::Broadcaster;
139178
use bittorrent_http_tracker_core::statistics::event::listener::run_event_listener;
140179
use bittorrent_http_tracker_core::statistics::repository::Repository;
141-
use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository;
180+
use bittorrent_tracker_core::container::TrackerCoreContainer;
142181
use bittorrent_tracker_core::{self};
143182
use bittorrent_udp_tracker_core::services::banning::BanService;
144183
use bittorrent_udp_tracker_core::MAX_CONNECTION_ID_ERRORS_PER_IP;
145184
use tokio::sync::RwLock;
146185
use torrust_tracker_configuration::Configuration;
186+
use torrust_tracker_events::bus::SenderStatus;
147187
use torrust_tracker_primitives::swarm_metadata::AggregateActiveSwarmMetadata;
148188
use torrust_tracker_test_helpers::configuration;
189+
use torrust_tracker_torrent_repository::container::TorrentRepositoryContainer;
149190

150191
use crate::statistics::metrics::Metrics;
151192
use crate::statistics::services::{get_metrics, TrackerMetrics};
@@ -157,8 +198,12 @@ mod tests {
157198
#[tokio::test]
158199
async fn the_statistics_service_should_return_the_tracker_metrics() {
159200
let config = tracker_configuration();
201+
let core_config = Arc::new(config.core.clone());
202+
203+
let torrent_repository_container = Arc::new(TorrentRepositoryContainer::initialize(SenderStatus::Enabled));
204+
205+
let tracker_core_container = TrackerCoreContainer::initialize_from(&core_config, &torrent_repository_container.clone());
160206

161-
let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default());
162207
let ban_service = Arc::new(RwLock::new(BanService::new(MAX_CONNECTION_ID_ERRORS_PER_IP)));
163208

164209
// HTTP core stats
@@ -177,8 +222,11 @@ mod tests {
177222
let udp_server_stats_repository = Arc::new(torrust_udp_tracker_server::statistics::repository::Repository::new());
178223

179224
let tracker_metrics = get_metrics(
180-
in_memory_torrent_repository.clone(),
225+
core_config,
226+
tracker_core_container.in_memory_torrent_repository.clone(),
181227
ban_service.clone(),
228+
torrent_repository_container.stats_repository.clone(),
229+
tracker_core_container.stats_repository.clone(),
182230
http_stats_repository.clone(),
183231
udp_server_stats_repository.clone(),
184232
)

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-
const TORRENT_REPOSITORY_TORRENTS_DOWNLOADS_TOTAL: &str = "torrent_repository_torrents_downloads_total";
17+
pub 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-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-
const TRACKER_CORE_PERSISTENT_TORRENTS_DOWNLOADS_TOTAL: &str = "tracker_core_persistent_torrents_downloads_total";
13+
pub const TRACKER_CORE_PERSISTENT_TORRENTS_DOWNLOADS_TOTAL: &str = "tracker_core_persistent_torrents_downloads_total";
1414

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

0 commit comments

Comments
 (0)