Skip to main content

apalis_sqlite/queries/
metrics.rs

1use std::str::FromStr;
2
3use apalis_core::backend::{Backend, Metrics, StatType, Statistic};
4
5use crate::{SqliteStorage, error::Error};
6
7struct StatisticRow {
8    /// The priority of the statistic (lower number means higher priority)
9    pub priority: i64,
10    /// The statistics type
11    pub r#type: String,
12    /// Overall statistics of the backend
13    pub statistic: String,
14    /// The value of the statistic
15    pub value: Option<f64>,
16}
17
18impl<Args> Metrics for SqliteStorage<Args>
19where
20    Self: Backend<Error = Error>,
21{
22    fn global(&self) -> impl Future<Output = Result<Vec<Statistic>, Self::Error>> + Send {
23        let pool = self.persistence.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: FromStr::from_str(&r.r#type).unwrap_or(StatType::Number),
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(&self) -> impl Future<Output = Result<Vec<Statistic>, Self::Error>> + Send {
40        let pool = self.persistence.pool.clone();
41        let queue_id = self.persistence.config.queue.as_ref();
42        async move {
43            let rec = sqlx::query_file_as!(
44                StatisticRow,
45                "queries/backend/overview_by_queue.sql",
46                queue_id
47            )
48            .fetch_all(&pool)
49            .await?
50            .into_iter()
51            .map(|r| Statistic {
52                priority: Some(r.priority as u64),
53                stat_type: FromStr::from_str(&r.r#type).unwrap_or(StatType::Number),
54                title: r.statistic,
55                value: r.value.unwrap_or_default().to_string(),
56            })
57            .collect();
58            Ok(rec)
59        }
60    }
61}