forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
59 lines (50 loc) · 1.71 KB
/
Copy pathmod.rs
File metadata and controls
59 lines (50 loc) · 1.71 KB
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
50
51
52
53
54
55
56
57
58
59
pub mod downloads;
use std::sync::Arc;
use thiserror::Error;
use torrust_clock::DurationSinceUnixEpoch;
use torrust_metrics::label::LabelSet;
use torrust_metrics::{metric_collection, metric_name};
use super::TRACKER_CORE_PERSISTENT_TORRENTS_DOWNLOADS_TOTAL;
use super::repository::Repository;
use crate::databases;
use crate::statistics::persisted::downloads::DatabaseDownloadsMetricRepository;
/// Loads persisted metrics from the database and sets them in the stats repository.
///
/// # Errors
///
/// This function will return an error if the database query fails or if the
/// metric collection fails to set the initial metric values.
pub async fn load_persisted_metrics(
stats_repository: &Arc<Repository>,
db_downloads_metric_repository: &Arc<DatabaseDownloadsMetricRepository>,
now: DurationSinceUnixEpoch,
) -> Result<(), Error> {
if let Some(downloads) = db_downloads_metric_repository.load_global_downloads().await? {
stats_repository
.set_counter(
&metric_name!(TRACKER_CORE_PERSISTENT_TORRENTS_DOWNLOADS_TOTAL),
&LabelSet::default(),
u64::from(downloads),
now,
)
.await?;
}
Ok(())
}
#[derive(Error, Debug, Clone)]
pub enum Error {
#[error("Database error: {err}")]
DatabaseError { err: databases::error::Error },
#[error("Metrics error: {err}")]
MetricsError { err: metric_collection::Error },
}
impl From<databases::error::Error> for Error {
fn from(err: databases::error::Error) -> Self {
Self::DatabaseError { err }
}
}
impl From<metric_collection::Error> for Error {
fn from(err: metric_collection::Error) -> Self {
Self::MetricsError { err }
}
}