Skip to main content

agentmail/types/
metrics.rs

1use std::collections::BTreeMap;
2
3use crate::util::QueryBuilder;
4use serde::Deserialize;
5
6/// One time-bucket of an event metric.
7#[derive(Clone, Debug, Deserialize)]
8pub struct MetricBucket {
9    /// Start of the bucket (RFC 3339).
10    pub timestamp: String,
11    /// Event count in the bucket.
12    pub count: i64,
13}
14
15/// One time-bucket of a usage metric.
16#[derive(Clone, Debug, Deserialize)]
17pub struct UsagePoint {
18    /// Start of the bucket (RFC 3339).
19    pub timestamp: String,
20    /// Usage value in the bucket.
21    pub value: i64,
22}
23
24/// Event metrics keyed by event type (e.g. `message.received`).
25pub type MetricsEvents = BTreeMap<String, Vec<MetricBucket>>;
26
27/// Usage metrics keyed by usage type.
28pub type MetricsUsage = BTreeMap<String, Vec<UsagePoint>>;
29
30/// Query parameters for [`Client::get_metrics_events`] and
31/// [`Client::get_metrics_usage`]. `types` filters by event/usage type; leave it
32/// empty for all.
33#[derive(Clone, Debug, Default)]
34pub struct MetricsQuery {
35    /// Event or usage types to include; empty means all.
36    pub types: Vec<String>,
37    /// Window start (RFC 3339).
38    pub start: Option<String>,
39    /// Window end (RFC 3339).
40    pub end: Option<String>,
41    /// Bucket size in seconds (1 to 86400).
42    pub period: Option<u32>,
43    /// Maximum buckets to return.
44    pub limit: Option<u32>,
45    /// Return newest bucket first.
46    pub descending: Option<bool>,
47}
48
49impl MetricsQuery {
50    pub(crate) fn query(&self, types_key: &'static str) -> Vec<(&'static str, String)> {
51        QueryBuilder::new()
52            .many(types_key, &self.types)
53            .opt("start", self.start.as_ref())
54            .opt("end", self.end.as_ref())
55            .opt("period", self.period.as_ref())
56            .opt("limit", self.limit.as_ref())
57            .opt("descending", self.descending.as_ref())
58            .build()
59    }
60}