agentmail/types/
metrics.rs1use std::collections::BTreeMap;
2
3use crate::util::QueryBuilder;
4use serde::Deserialize;
5
6#[derive(Clone, Debug, Deserialize)]
8pub struct MetricBucket {
9 pub timestamp: String,
11 pub count: i64,
13}
14
15#[derive(Clone, Debug, Deserialize)]
17pub struct UsagePoint {
18 pub timestamp: String,
20 pub value: i64,
22}
23
24#[derive(Clone, Debug, Deserialize)]
26pub struct RatePoint {
27 pub timestamp: String,
29 #[serde(default)]
31 pub rate: Option<f64>,
32 #[serde(default)]
34 pub sent: Option<u64>,
35}
36
37pub type MetricsEvents = BTreeMap<String, Vec<MetricBucket>>;
39
40pub type MetricsUsage = BTreeMap<String, Vec<UsagePoint>>;
42
43pub type MetricsRates = BTreeMap<String, Vec<RatePoint>>;
45
46#[derive(Clone, Debug, Default)]
51pub struct MetricsQuery {
52 pub types: Vec<String>,
54 pub start: Option<String>,
56 pub end: Option<String>,
58 pub period: Option<u32>,
60 pub window: Option<u32>,
62 pub limit: Option<u32>,
64 pub descending: Option<bool>,
66}
67
68impl MetricsQuery {
69 pub(crate) fn query(&self, types_key: &'static str) -> Vec<(&'static str, String)> {
70 QueryBuilder::new()
71 .many(types_key, &self.types)
72 .opt("start", self.start.as_ref())
73 .opt("end", self.end.as_ref())
74 .opt("period", self.period.as_ref())
75 .opt("window", self.window.as_ref())
76 .opt("limit", self.limit.as_ref())
77 .opt("descending", self.descending.as_ref())
78 .build()
79 }
80}