1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
//! Setup for the tracker statistics.
//!
//! The [`factory`](crate::tracker::services::statistics::setup::factory) function builds the structs needed for handling the tracker metrics.
use crate::tracker::statistics;
/// It builds the structs needed for handling the tracker metrics.
///
/// It returns:
///
/// - An statistics [`EventSender`](crate::tracker::statistics::EventSender) that allows you to send events related to statistics.
/// - An statistics [`Repo`](crate::tracker::statistics::Repo) 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) {
let mut stats_event_sender = None;
let mut stats_tracker = statistics::Keeper::new();
if tracker_usage_statistics {
stats_event_sender = Some(stats_tracker.run_event_listener());
}
(stats_event_sender, stats_tracker.repository)
}
#[cfg(test)]
mod test {
use super::factory;
#[tokio::test]
async fn should_not_send_any_event_when_statistics_are_disabled() {
let tracker_usage_statistics = false;
let (stats_event_sender, _stats_repository) = factory(tracker_usage_statistics);
assert!(stats_event_sender.is_none());
}
#[tokio::test]
async fn should_send_events_when_statistics_are_enabled() {
let tracker_usage_statistics = true;
let (stats_event_sender, _stats_repository) = factory(tracker_usage_statistics);
assert!(stats_event_sender.is_some());
}
}