apalis_sqlite/queries/
metrics.rs

1use apalis_core::backend::{BackendExt, Metrics, Statistic};
2use ulid::Ulid;
3
4use crate::{CompactType, SqlContext, 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:
20        BackendExt<Context = SqlContext, Compact = CompactType, IdType = Ulid, Error = sqlx::Error>,
21{
22    fn global(&self) -> impl Future<Output = Result<Vec<Statistic>, Self::Error>> + Send {
23        let pool = self.pool.clone();
24        async move {
25            let rec = sqlx::query_file_as!(StatisticRow, "queries/backend/overview.sql")
26                .fetch_all(&pool)
27                .await?
28                .into_iter()
29                .map(|r| Statistic {
30                    priority: Some(r.priority as u64),
31                    stat_type: super::stat_type_from_string(&r.r#type),
32                    title: r.statistic,
33                    value: r.value.unwrap_or_default().to_string(),
34                })
35                .collect();
36            Ok(rec)
37        }
38    }
39    fn fetch_by_queue(
40        &self,
41        queue_id: &str,
42    ) -> impl Future<Output = Result<Vec<Statistic>, Self::Error>> + Send {
43        let pool = self.pool.clone();
44        let queue_id = queue_id.to_owned();
45        async move {
46            let rec = sqlx::query_file_as!(
47                StatisticRow,
48                "queries/backend/overview_by_queue.sql",
49                queue_id
50            )
51            .fetch_all(&pool)
52            .await?
53            .into_iter()
54            .map(|r| Statistic {
55                priority: Some(r.priority as u64),
56                stat_type: super::stat_type_from_string(&r.r#type),
57                title: r.statistic,
58                value: r.value.unwrap_or_default().to_string(),
59            })
60            .collect();
61            Ok(rec)
62        }
63    }
64}