Skip to content

Commit 4f51ffd

Browse files
committed
refactor: [torrust#1228] move statistics back from tracker-core to main lib
The statistics are only used at the higher levels: UDP and HTTP tracker. We will move them to new packages.
1 parent 0ad88b6 commit 4f51ffd

File tree

29 files changed

+118
-111
lines changed

29 files changed

+118
-111
lines changed

packages/tracker-core/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@
346346
//!
347347
//! Services are domain services on top of the core tracker domain. Right now there are two types of service:
348348
//!
349-
//! - For statistics: [`crate::core::statistics::services`]
349+
//! - For statistics: [`crate::packages::statistics::services`]
350350
//! - For torrents: [`crate::core::torrent::services`]
351351
//!
352352
//! Services usually format the data inside the tracker to make it easier to consume by other parts.
@@ -442,7 +442,6 @@ pub mod authentication;
442442
pub mod databases;
443443
pub mod error;
444444
pub mod scrape_handler;
445-
pub mod statistics;
446445
pub mod torrent;
447446
pub mod whitelist;
448447

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

-31
This file was deleted.

src/bootstrap/app.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,26 @@ use bittorrent_tracker_core::authentication::key::repository::persisted::Databas
2020
use bittorrent_tracker_core::authentication::service;
2121
use bittorrent_tracker_core::databases::setup::initialize_database;
2222
use bittorrent_tracker_core::scrape_handler::ScrapeHandler;
23-
use bittorrent_tracker_core::statistics;
2423
use bittorrent_tracker_core::torrent::manager::TorrentsManager;
2524
use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository;
2625
use bittorrent_tracker_core::torrent::repository::persisted::DatabasePersistentTorrentRepository;
2726
use bittorrent_tracker_core::whitelist::authorization::WhitelistAuthorization;
2827
use bittorrent_tracker_core::whitelist::repository::in_memory::InMemoryWhitelist;
2928
use bittorrent_tracker_core::whitelist::setup::initialize_whitelist_manager;
29+
use packages::statistics;
3030
use tokio::sync::RwLock;
3131
use torrust_tracker_clock::static_time;
3232
use torrust_tracker_configuration::validator::Validator;
3333
use torrust_tracker_configuration::Configuration;
3434
use tracing::instrument;
3535

3636
use super::config::initialize_configuration;
37-
use crate::bootstrap;
3837
use crate::container::AppContainer;
3938
use crate::servers::udp::server::banning::BanService;
4039
use crate::servers::udp::server::launcher::MAX_CONNECTION_ID_ERRORS_PER_IP;
4140
use crate::shared::crypto::ephemeral_instance_keys;
4241
use crate::shared::crypto::keys::{self, Keeper as _};
42+
use crate::{bootstrap, packages};
4343

4444
/// It loads the configuration from the environment and builds app container.
4545
///

src/container.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@ use bittorrent_tracker_core::authentication::handler::KeysHandler;
55
use bittorrent_tracker_core::authentication::service::AuthenticationService;
66
use bittorrent_tracker_core::databases::Database;
77
use bittorrent_tracker_core::scrape_handler::ScrapeHandler;
8-
use bittorrent_tracker_core::statistics::event::sender::Sender;
9-
use bittorrent_tracker_core::statistics::repository::Repository;
108
use bittorrent_tracker_core::torrent::manager::TorrentsManager;
119
use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository;
1210
use bittorrent_tracker_core::torrent::repository::persisted::DatabasePersistentTorrentRepository;
1311
use bittorrent_tracker_core::whitelist;
1412
use bittorrent_tracker_core::whitelist::manager::WhitelistManager;
13+
use packages::statistics::event::sender::Sender;
14+
use packages::statistics::repository::Repository;
1515
use tokio::sync::RwLock;
1616
use torrust_tracker_configuration::{Core, HttpApi, HttpTracker, UdpTracker};
1717

18+
use crate::packages;
1819
use crate::servers::udp::server::banning::BanService;
1920

2021
pub struct AppContainer {

src/core/statistics/mod.rs

-1
This file was deleted.

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ pub mod app;
494494
pub mod bootstrap;
495495
pub mod console;
496496
pub mod container;
497-
pub mod core;
497+
pub mod packages;
498498
pub mod servers;
499499
pub mod shared;
500500

File renamed without changes.

packages/tracker-core/src/statistics/event/handler.rs src/packages/statistics/event/handler.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::statistics::event::{Event, UdpResponseKind};
2-
use crate::statistics::repository::Repository;
1+
use crate::packages::statistics::event::{Event, UdpResponseKind};
2+
use crate::packages::statistics::repository::Repository;
33

44
pub async fn handle_event(event: Event, stats_repository: &Repository) {
55
match event {
@@ -102,9 +102,9 @@ pub async fn handle_event(event: Event, stats_repository: &Repository) {
102102

103103
#[cfg(test)]
104104
mod tests {
105-
use crate::statistics::event::handler::handle_event;
106-
use crate::statistics::event::Event;
107-
use crate::statistics::repository::Repository;
105+
use crate::packages::statistics::event::handler::handle_event;
106+
use crate::packages::statistics::event::Event;
107+
use crate::packages::statistics::repository::Repository;
108108

109109
#[tokio::test]
110110
async fn should_increase_the_tcp4_announces_counter_when_it_receives_a_tcp4_announce_event() {

packages/tracker-core/src/statistics/event/listener.rs src/packages/statistics/event/listener.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use tokio::sync::mpsc;
22

33
use super::handler::handle_event;
44
use super::Event;
5-
use crate::statistics::repository::Repository;
5+
use crate::packages::statistics::repository::Repository;
66

77
pub async fn dispatch_events(mut receiver: mpsc::Receiver<Event>, stats_repository: Repository) {
88
while let Some(event) = receiver.recv().await {

packages/tracker-core/src/statistics/event/sender.rs src/packages/statistics/event/sender.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ pub trait Sender: Sync + Send {
1313
fn send_event(&self, event: Event) -> BoxFuture<'_, Option<Result<(), SendError<Event>>>>;
1414
}
1515

16-
/// An [`statistics::EventSender`](crate::core::statistics::event::sender::Sender) implementation.
16+
/// An [`statistics::EventSender`](crate::packages::statistics::event::sender::Sender) implementation.
1717
///
1818
/// It uses a channel sender to send the statistic events. The channel is created by a
19-
/// [`statistics::Keeper`](crate::core::statistics::keeper::Keeper)
19+
/// [`statistics::Keeper`](crate::packages::statistics::keeper::Keeper)
2020
#[allow(clippy::module_name_repetitions)]
2121
pub struct ChannelSender {
2222
pub(crate) sender: mpsc::Sender<Event>,

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ impl Keeper {
5151

5252
#[cfg(test)]
5353
mod tests {
54-
use crate::statistics::event::Event;
55-
use crate::statistics::keeper::Keeper;
56-
use crate::statistics::metrics::Metrics;
54+
use crate::packages::statistics::event::Event;
55+
use crate::packages::statistics::keeper::Keeper;
56+
use crate::packages::statistics::metrics::Metrics;
5757

5858
#[tokio::test]
5959
async fn should_contain_the_tracker_statistics() {

src/packages/statistics/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pub mod event;
2+
pub mod keeper;
3+
pub mod metrics;
4+
pub mod repository;
5+
pub mod services;
6+
pub mod setup;

src/core/statistics/services.rs src/packages/statistics/services.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
//!
33
//! It includes:
44
//!
5-
//! - A [`factory`](crate::statistics::setup::factory) function to build the structs needed to collect the tracker metrics.
6-
//! - A [`get_metrics`] service to get the tracker [`metrics`](crate::core::statistics::metrics::Metrics).
5+
//! - A [`factory`](crate::packages::statistics::setup::factory) function to build the structs needed to collect the tracker metrics.
6+
//! - A [`get_metrics`] service to get the tracker [`metrics`](crate::packages::statistics::metrics::Metrics).
77
//!
88
//! Tracker metrics are collected using a Publisher-Subscribe pattern.
99
//!
1010
//! The factory function builds two structs:
1111
//!
12-
//! - An statistics event [`Sender`](crate::core::statistics::event::sender::Sender)
12+
//! - An statistics event [`Sender`](crate::packages::statistics::event::sender::Sender)
1313
//! - An statistics [`Repository`]
1414
//!
1515
//! ```text
@@ -21,7 +21,7 @@
2121
//! There is an event listener that is receiving all the events and processing them with an event handler.
2222
//! Then, the event handler updates the metrics depending on the received event.
2323
//!
24-
//! For example, if you send the event [`Event::Udp4Connect`](crate::core::statistics::event::Event::Udp4Connect):
24+
//! For example, if you send the event [`Event::Udp4Connect`](crate::packages::statistics::event::Event::Udp4Connect):
2525
//!
2626
//! ```text
2727
//! let result = event_sender.send_event(Event::Udp4Connect).await;
@@ -38,12 +38,13 @@
3838
//! ```
3939
use std::sync::Arc;
4040

41-
use bittorrent_tracker_core::statistics::metrics::Metrics;
42-
use bittorrent_tracker_core::statistics::repository::Repository;
4341
use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository;
42+
use packages::statistics::metrics::Metrics;
43+
use packages::statistics::repository::Repository;
4444
use tokio::sync::RwLock;
4545
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
4646

47+
use crate::packages;
4748
use crate::servers::udp::server::banning::BanService;
4849

4950
/// All the metrics collected by the tracker.
@@ -111,13 +112,14 @@ mod tests {
111112
use std::sync::Arc;
112113

113114
use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository;
114-
use bittorrent_tracker_core::{self, statistics};
115+
use bittorrent_tracker_core::{self};
115116
use tokio::sync::RwLock;
116117
use torrust_tracker_configuration::Configuration;
117118
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
118119
use torrust_tracker_test_helpers::configuration;
119120

120-
use crate::core::statistics::services::{get_metrics, TrackerMetrics};
121+
use crate::packages::statistics;
122+
use crate::packages::statistics::services::{get_metrics, TrackerMetrics};
121123
use crate::servers::udp::server::banning::BanService;
122124
use crate::servers::udp::server::launcher::MAX_CONNECTION_ID_ERRORS_PER_IP;
123125

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
//! Setup for the tracker statistics.
22
//!
33
//! The [`factory`] function builds the structs needed for handling the tracker metrics.
4-
use crate::statistics;
4+
use crate::packages::statistics;
55

66
/// It builds the structs needed for handling the tracker metrics.
77
///
88
/// It returns:
99
///
10-
/// - An statistics event [`Sender`](crate::core::statistics::event::sender::Sender) that allows you to send events related to statistics.
11-
/// - An statistics [`Repository`](crate::core::statistics::repository::Repository) which is an in-memory repository for the tracker metrics.
10+
/// - An statistics event [`Sender`](crate::packages::statistics::event::sender::Sender) that allows you to send events related to statistics.
11+
/// - An statistics [`Repository`](crate::packages::statistics::repository::Repository) which is an in-memory repository for the tracker metrics.
1212
///
1313
/// When the input argument `tracker_usage_statistics`is false the setup does not run the event listeners, consequently the statistics
1414
/// events are sent are received but not dispatched to the handler.

src/servers/apis/v1/context/stats/handlers.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ use std::sync::Arc;
55
use axum::extract::State;
66
use axum::response::Response;
77
use axum_extra::extract::Query;
8-
use bittorrent_tracker_core::statistics::repository::Repository;
98
use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository;
9+
use packages::statistics::repository::Repository;
1010
use serde::Deserialize;
1111
use tokio::sync::RwLock;
1212

1313
use super::responses::{metrics_response, stats_response};
14-
use crate::core::statistics::services::get_metrics;
14+
use crate::packages;
15+
use crate::packages::statistics::services::get_metrics;
1516
use crate::servers::udp::server::banning::BanService;
1617

1718
#[derive(Deserialize, Debug, Default)]

src/servers/apis/v1/context/stats/resources.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! API context.
33
use serde::{Deserialize, Serialize};
44

5-
use crate::core::statistics::services::TrackerMetrics;
5+
use crate::packages::statistics::services::TrackerMetrics;
66

77
/// It contains all the statistics generated by the tracker.
88
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
@@ -118,11 +118,12 @@ impl From<TrackerMetrics> for Stats {
118118

119119
#[cfg(test)]
120120
mod tests {
121-
use bittorrent_tracker_core::statistics::metrics::Metrics;
121+
use packages::statistics::metrics::Metrics;
122122
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
123123

124124
use super::Stats;
125-
use crate::core::statistics::services::TrackerMetrics;
125+
use crate::packages::statistics::services::TrackerMetrics;
126+
use crate::packages::{self};
126127

127128
#[test]
128129
fn stats_resource_should_be_converted_from_tracker_metrics() {

src/servers/apis/v1/context/stats/responses.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use axum::response::{IntoResponse, Json, Response};
44

55
use super::resources::Stats;
6-
use crate::core::statistics::services::TrackerMetrics;
6+
use crate::packages::statistics::services::TrackerMetrics;
77

88
/// `200` response that contains the [`Stats`] resource as json.
99
#[must_use]

src/servers/http/v1/handlers/announce.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ use bittorrent_http_protocol::v1::services::peer_ip_resolver::ClientIpSources;
1919
use bittorrent_tracker_core::announce_handler::{AnnounceHandler, PeersWanted};
2020
use bittorrent_tracker_core::authentication::service::AuthenticationService;
2121
use bittorrent_tracker_core::authentication::Key;
22-
use bittorrent_tracker_core::statistics::event::sender::Sender;
2322
use bittorrent_tracker_core::whitelist;
2423
use hyper::StatusCode;
24+
use packages::statistics::event::sender::Sender;
2525
use torrust_tracker_clock::clock::Time;
2626
use torrust_tracker_configuration::Core;
2727
use torrust_tracker_primitives::core::AnnounceData;
@@ -33,7 +33,7 @@ use crate::servers::http::v1::extractors::authentication_key::Extract as Extract
3333
use crate::servers::http::v1::extractors::client_ip_sources::Extract as ExtractClientIpSources;
3434
use crate::servers::http::v1::handlers::common::auth;
3535
use crate::servers::http::v1::services::{self};
36-
use crate::CurrentClock;
36+
use crate::{packages, CurrentClock};
3737

3838
/// It handles the `announce` request when the HTTP tracker does not require
3939
/// authentication (no PATH `key` parameter required).
@@ -256,15 +256,17 @@ mod tests {
256256
use bittorrent_tracker_core::authentication::service::AuthenticationService;
257257
use bittorrent_tracker_core::core_tests::sample_info_hash;
258258
use bittorrent_tracker_core::databases::setup::initialize_database;
259-
use bittorrent_tracker_core::statistics;
260-
use bittorrent_tracker_core::statistics::event::sender::Sender;
261259
use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository;
262260
use bittorrent_tracker_core::torrent::repository::persisted::DatabasePersistentTorrentRepository;
263261
use bittorrent_tracker_core::whitelist::authorization::WhitelistAuthorization;
264262
use bittorrent_tracker_core::whitelist::repository::in_memory::InMemoryWhitelist;
263+
use packages::statistics;
264+
use packages::statistics::event::sender::Sender;
265265
use torrust_tracker_configuration::{Configuration, Core};
266266
use torrust_tracker_test_helpers::configuration;
267267

268+
use crate::packages;
269+
268270
struct CoreTrackerServices {
269271
pub core_config: Arc<Core>,
270272
pub announce_handler: Arc<AnnounceHandler>,

src/servers/http/v1/handlers/scrape.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ use bittorrent_http_protocol::v1::services::peer_ip_resolver::{self, ClientIpSou
1515
use bittorrent_tracker_core::authentication::service::AuthenticationService;
1616
use bittorrent_tracker_core::authentication::Key;
1717
use bittorrent_tracker_core::scrape_handler::ScrapeHandler;
18-
use bittorrent_tracker_core::statistics::event::sender::Sender;
1918
use hyper::StatusCode;
19+
use packages::statistics::event::sender::Sender;
2020
use torrust_tracker_configuration::Core;
2121
use torrust_tracker_primitives::core::ScrapeData;
2222

23+
use crate::packages;
2324
use crate::servers::http::v1::extractors::authentication_key::Extract as ExtractKey;
2425
use crate::servers::http::v1::extractors::client_ip_sources::Extract as ExtractClientIpSources;
2526
use crate::servers::http::v1::extractors::scrape_request::ExtractRequest;
@@ -174,13 +175,15 @@ mod tests {
174175
use bittorrent_tracker_core::authentication::key::repository::in_memory::InMemoryKeyRepository;
175176
use bittorrent_tracker_core::authentication::service::AuthenticationService;
176177
use bittorrent_tracker_core::scrape_handler::ScrapeHandler;
177-
use bittorrent_tracker_core::statistics;
178178
use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository;
179179
use bittorrent_tracker_core::whitelist::authorization::WhitelistAuthorization;
180180
use bittorrent_tracker_core::whitelist::repository::in_memory::InMemoryWhitelist;
181+
use packages::statistics;
181182
use torrust_tracker_configuration::{Configuration, Core};
182183
use torrust_tracker_test_helpers::configuration;
183184

185+
use crate::packages;
186+
184187
struct CoreTrackerServices {
185188
pub core_config: Arc<Core>,
186189
pub scrape_handler: Arc<ScrapeHandler>,

0 commit comments

Comments
 (0)