Skip to content

Commit f113d2c

Browse files
committedDec 17, 2024··
Merge #1135: Refactor: reorganize statistics mod
7cf08a6 refactor: reorganize statistics mod (Jose Celano) Pull request description: Relates to: #1134 Preparing to introduce new changes in the repository: use Atomics in the repository instead of RwLocks. ACKs for top commit: josecelano: ACK 7cf08a6 Tree-SHA512: f87cb96a1324f6cfab278826bc89fdea8120324a001b2dd03dc9bbf4707f3f8a5d860d5df601957fccf6992f35fb56334de4a4eedb632a07c2eb43f394f9d889
2 parents 2bea7ec + 7cf08a6 commit f113d2c

File tree

18 files changed

+721
-658
lines changed

18 files changed

+721
-658
lines changed
 

‎src/core/mod.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@
422422
//! For example, the HTTP tracker would send an event like the following when it handles an `announce` request received from a peer using IP version 4.
423423
//!
424424
//! ```text
425-
//! tracker.send_stats_event(statistics::Event::Tcp4Announce).await
425+
//! tracker.send_stats_event(statistics::event::Event::Tcp4Announce).await
426426
//! ```
427427
//!
428428
//! Refer to [`statistics`] module for more information about statistics.
@@ -505,10 +505,10 @@ pub struct Tracker {
505505
torrents: Arc<Torrents>,
506506

507507
/// Service to send stats events.
508-
stats_event_sender: Option<Box<dyn statistics::EventSender>>,
508+
stats_event_sender: Option<Box<dyn statistics::event::sender::Sender>>,
509509

510510
/// The in-memory stats repo.
511-
stats_repository: statistics::Repo,
511+
stats_repository: statistics::repository::Repository,
512512
}
513513

514514
/// Structure that holds the data returned by the `announce` request.
@@ -624,8 +624,8 @@ impl Tracker {
624624
/// Will return a `databases::error::Error` if unable to connect to database. The `Tracker` is responsible for the persistence.
625625
pub fn new(
626626
config: &Core,
627-
stats_event_sender: Option<Box<dyn statistics::EventSender>>,
628-
stats_repository: statistics::Repo,
627+
stats_event_sender: Option<Box<dyn statistics::event::sender::Sender>>,
628+
stats_repository: statistics::repository::Repository,
629629
) -> Result<Tracker, databases::error::Error> {
630630
let driver = match config.database.driver {
631631
database::Driver::Sqlite3 => Driver::Sqlite3,
@@ -1207,17 +1207,20 @@ impl Tracker {
12071207
Ok(())
12081208
}
12091209

1210-
/// It return the `Tracker` [`statistics::Metrics`].
1210+
/// It return the `Tracker` [`statistics::metrics::Metrics`].
12111211
///
12121212
/// # Context: Statistics
1213-
pub async fn get_stats(&self) -> tokio::sync::RwLockReadGuard<'_, statistics::Metrics> {
1213+
pub async fn get_stats(&self) -> tokio::sync::RwLockReadGuard<'_, statistics::metrics::Metrics> {
12141214
self.stats_repository.get_stats().await
12151215
}
12161216

1217-
/// It allows to send a statistic events which eventually will be used to update [`statistics::Metrics`].
1217+
/// It allows to send a statistic events which eventually will be used to update [`statistics::metrics::Metrics`].
12181218
///
12191219
/// # Context: Statistics
1220-
pub async fn send_stats_event(&self, event: statistics::Event) -> Option<Result<(), SendError<statistics::Event>>> {
1220+
pub async fn send_stats_event(
1221+
&self,
1222+
event: statistics::event::Event,
1223+
) -> Option<Result<(), SendError<statistics::event::Event>>> {
12211224
match &self.stats_event_sender {
12221225
None => None,
12231226
Some(stats_event_sender) => stats_event_sender.send_event(event).await,

‎src/core/services/statistics/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
//! It includes:
44
//!
55
//! - A [`factory`](crate::core::services::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).
6+
//! - A [`get_metrics`] service to get the tracker [`metrics`](crate::core::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 [`EventSender`](crate::core::statistics::EventSender)
13-
//! - An statistics [`Repo`](crate::core::statistics::Repo)
12+
//! - An statistics event [`Sender`](crate::core::statistics::event::sender::Sender)
13+
//! - An statistics [`Repository`](crate::core::statistics::repository::Repository)
1414
//!
1515
//! ```text
1616
//! let (stats_event_sender, stats_repository) = factory(tracker_usage_statistics);
@@ -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::Udp4Connect):
24+
//! For example, if you send the event [`Event::Udp4Connect`](crate::core::statistics::event::Event::Udp4Connect):
2525
//!
2626
//! ```text
2727
//! let result = event_sender.send_event(Event::Udp4Connect).await;
@@ -42,7 +42,7 @@ use std::sync::Arc;
4242

4343
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
4444

45-
use crate::core::statistics::Metrics;
45+
use crate::core::statistics::metrics::Metrics;
4646
use crate::core::Tracker;
4747

4848
/// All the metrics collected by the tracker.
@@ -118,7 +118,7 @@ mod tests {
118118
tracker_metrics,
119119
TrackerMetrics {
120120
torrents_metrics: TorrentsMetrics::default(),
121-
protocol_metrics: core::statistics::Metrics::default(),
121+
protocol_metrics: core::statistics::metrics::Metrics::default(),
122122
}
123123
);
124124
}

‎src/core/services/statistics/setup.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,21 @@ use crate::core::statistics;
77
///
88
/// It returns:
99
///
10-
/// - An statistics [`EventSender`](crate::core::statistics::EventSender) that allows you to send events related to statistics.
11-
/// - An statistics [`Repo`](crate::core::statistics::Repo) which is an in-memory repository for the tracker metrics.
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.
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.
1515
#[must_use]
16-
pub fn factory(tracker_usage_statistics: bool) -> (Option<Box<dyn statistics::EventSender>>, statistics::Repo) {
16+
pub fn factory(
17+
tracker_usage_statistics: bool,
18+
) -> (
19+
Option<Box<dyn statistics::event::sender::Sender>>,
20+
statistics::repository::Repository,
21+
) {
1722
let mut stats_event_sender = None;
1823

19-
let mut stats_tracker = statistics::Keeper::new();
24+
let mut stats_tracker = statistics::keeper::Keeper::new();
2025

2126
if tracker_usage_statistics {
2227
stats_event_sender = Some(stats_tracker.run_event_listener());

0 commit comments

Comments
 (0)
Please sign in to comment.