Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: reorganize statistics mod #1135

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@
//! 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.
//!
//! ```text
//! tracker.send_stats_event(statistics::Event::Tcp4Announce).await
//! tracker.send_stats_event(statistics::event::Event::Tcp4Announce).await
//! ```
//!
//! Refer to [`statistics`] module for more information about statistics.
Expand Down Expand Up @@ -505,10 +505,10 @@ pub struct Tracker {
torrents: Arc<Torrents>,

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

/// The in-memory stats repo.
stats_repository: statistics::Repo,
stats_repository: statistics::repository::Repository,
}

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

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

/// It allows to send a statistic events which eventually will be used to update [`statistics::Metrics`].
/// It allows to send a statistic events which eventually will be used to update [`statistics::metrics::Metrics`].
///
/// # Context: Statistics
pub async fn send_stats_event(&self, event: statistics::Event) -> Option<Result<(), SendError<statistics::Event>>> {
pub async fn send_stats_event(
&self,
event: statistics::event::Event,
) -> Option<Result<(), SendError<statistics::event::Event>>> {
match &self.stats_event_sender {
None => None,
Some(stats_event_sender) => stats_event_sender.send_event(event).await,
Expand Down
12 changes: 6 additions & 6 deletions src/core/services/statistics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
//! It includes:
//!
//! - A [`factory`](crate::core::services::statistics::setup::factory) function to build the structs needed to collect the tracker metrics.
//! - A [`get_metrics`] service to get the [`tracker metrics`](crate::core::statistics::Metrics).
//! - A [`get_metrics`] service to get the tracker [`metrics`](crate::core::statistics::metrics::Metrics).
//!
//! Tracker metrics are collected using a Publisher-Subscribe pattern.
//!
//! The factory function builds two structs:
//!
//! - An statistics [`EventSender`](crate::core::statistics::EventSender)
//! - An statistics [`Repo`](crate::core::statistics::Repo)
//! - An statistics event [`Sender`](crate::core::statistics::event::sender::Sender)
//! - An statistics [`Repository`](crate::core::statistics::repository::Repository)
//!
//! ```text
//! let (stats_event_sender, stats_repository) = factory(tracker_usage_statistics);
Expand All @@ -21,7 +21,7 @@
//! There is an event listener that is receiving all the events and processing them with an event handler.
//! Then, the event handler updates the metrics depending on the received event.
//!
//! For example, if you send the event [`Event::Udp4Connect`](crate::core::statistics::Event::Udp4Connect):
//! For example, if you send the event [`Event::Udp4Connect`](crate::core::statistics::event::Event::Udp4Connect):
//!
//! ```text
//! let result = event_sender.send_event(Event::Udp4Connect).await;
Expand All @@ -42,7 +42,7 @@ use std::sync::Arc;

use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;

use crate::core::statistics::Metrics;
use crate::core::statistics::metrics::Metrics;
use crate::core::Tracker;

/// All the metrics collected by the tracker.
Expand Down Expand Up @@ -118,7 +118,7 @@ mod tests {
tracker_metrics,
TrackerMetrics {
torrents_metrics: TorrentsMetrics::default(),
protocol_metrics: core::statistics::Metrics::default(),
protocol_metrics: core::statistics::metrics::Metrics::default(),
}
);
}
Expand Down
13 changes: 9 additions & 4 deletions src/core/services/statistics/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@ use crate::core::statistics;
///
/// It returns:
///
/// - An statistics [`EventSender`](crate::core::statistics::EventSender) that allows you to send events related to statistics.
/// - An statistics [`Repo`](crate::core::statistics::Repo) which is an in-memory repository for the tracker metrics.
/// - An statistics event [`Sender`](crate::core::statistics::event::sender::Sender) that allows you to send events related to statistics.
/// - An statistics [`Repository`](crate::core::statistics::repository::Repository) which is an in-memory repository for the tracker metrics.
///
/// When the input argument `tracker_usage_statistics`is false the setup does not run the event listeners, consequently the statistics
/// events are sent are received but not dispatched to the handler.
#[must_use]
pub fn factory(tracker_usage_statistics: bool) -> (Option<Box<dyn statistics::EventSender>>, statistics::Repo) {
pub fn factory(
tracker_usage_statistics: bool,
) -> (
Option<Box<dyn statistics::event::sender::Sender>>,
statistics::repository::Repository,
) {
let mut stats_event_sender = None;

let mut stats_tracker = statistics::Keeper::new();
let mut stats_tracker = statistics::keeper::Keeper::new();

if tracker_usage_statistics {
stats_event_sender = Some(stats_tracker.run_event_listener());
Expand Down
Loading
Loading