apalis_sqlite/queries/
metrics.rs

1use apalis_core::backend::{BackendExt, Metrics, Statistic};
2use ulid::Ulid;
3
4use crate::{CompactType, SqliteContext, SqliteStorage};
5
6struct StatisticRow {
7    /// The priority of the statistic (lower number means higher priority)
8    pub priority: i64,
9    /// The statistics type
10    pub r#type: String,
11    /// Overall statistics of the backend
12    pub statistic: String,
13    /// The value of the statistic
14    pub value: Option<f64>,
15}
16
17impl<Args, D, F> Metrics for SqliteStorage<Args, D, F>
18where
19    Self: BackendExt<
20            Context = SqliteContext,
21            Compact = CompactType,
22            IdType = Ulid,
23            Error = sqlx::Error,
24        >,
25{
26    fn global(&self) -> impl Future<Output = Result<Vec<Statistic>, Self::Error>> + Send {
27        let pool = self.pool.clone();
28        async move {
29            let rec = sqlx::query_file_as!(StatisticRow, "queries/backend/overview.sql")
30                .fetch_all(&pool)
31                .await?
32                .into_iter()
33                .map(|r| Statistic {
34                    priority: Some(r.priority as u64),
35                    stat_type: super::stat_type_from_string(&r.r#type),
36                    title: r.statistic,
37                    value: r.value.unwrap_or_default().to_string(),
38                })
39                .collect();
40            Ok(rec)
41        }
42    }
43    fn fetch_by_queue(
44        &self,
45        queue_id: &str,
46    ) -> impl Future<Output = Result<Vec<Statistic>, Self::Error>> + Send {
47        let pool = self.pool.clone();
48        let queue_id = queue_id.to_owned();
49        async move {
50            let rec = sqlx::query_file_as!(
51                StatisticRow,
52                "queries/backend/overview_by_queue.sql",
53                queue_id
54            )
55            .fetch_all(&pool)
56            .await?
57            .into_iter()
58            .map(|r| Statistic {
59                priority: Some(r.priority as u64),
60                stat_type: super::stat_type_from_string(&r.r#type),
61                title: r.statistic,
62                value: r.value.unwrap_or_default().to_string(),
63            })
64            .collect();
65            Ok(rec)
66        }
67    }
68}