enya_client/types.rs
1//! Shared API types for metrics query responses.
2//!
3//! These types support both JSON (serde) and bitcode serialization for
4//! high-performance binary encoding between Rust applications.
5
6use serde::{Deserialize, Serialize};
7
8/// Nanosecond timestamp (same as metrics-store Timestamp)
9pub type Timestamp = u128;
10
11/// Prometheus query result type.
12///
13/// Indicates the shape of data returned by a Prometheus query.
14/// See: <https://prometheus.io/docs/prometheus/latest/querying/api/>
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
16#[serde(rename_all = "lowercase")]
17pub enum ResultType {
18 /// Range vector - time series data with multiple data points per series.
19 /// Returned by range queries (`/api/v1/query_range`).
20 #[default]
21 Matrix,
22 /// Instant vector - single data point per series.
23 /// Returned by instant queries (`/api/v1/query`).
24 Vector,
25 /// Single numeric value (e.g., from `scalar()` function).
26 Scalar,
27 /// String value (e.g., from `label_replace()` function).
28 String,
29}
30
31/// A single time bucket in a metrics query response.
32#[derive(Debug, Clone, Serialize, Deserialize)]
33pub struct MetricsBucket {
34 /// Start timestamp (nanoseconds)
35 pub start: Timestamp,
36 /// End timestamp (nanoseconds)
37 pub end: Timestamp,
38 /// Aggregated value for this bucket
39 pub value: f64,
40 /// Number of samples in this bucket
41 pub count: usize,
42}
43
44/// A group of time buckets, typically representing a unique tag combination.
45#[derive(Debug, Clone, Serialize, Deserialize)]
46pub struct MetricsGroup {
47 /// Group identifier (e.g., "host:server1,env:prod")
48 pub group: String,
49 /// Time series buckets for this group
50 pub buckets: Vec<MetricsBucket>,
51}
52
53/// Response from a metrics query.
54#[derive(Debug, Clone, Serialize, Deserialize)]
55pub struct QueryResponse {
56 /// The metric name that was queried
57 pub metric: String,
58 /// The original query string
59 pub query: String,
60 /// Parsed aggregation function (e.g., "sum", "avg")
61 pub parsed_agg: Option<String>,
62 /// Parsed filter expression (e.g., "env:prod AND host:server1")
63 pub parsed_filter: String,
64 /// Parsed grouping clause (e.g., "by (host)")
65 pub parsed_grouping: Option<String>,
66 /// Parsed time range (e.g., "5m")
67 pub parsed_time_range: Option<String>,
68 /// Query start timestamp (nanoseconds)
69 pub start: Option<Timestamp>,
70 /// Query end timestamp (nanoseconds)
71 pub end: Option<Timestamp>,
72 /// Bucket granularity (nanoseconds)
73 pub granularity_ns: u128,
74 /// Result groups
75 pub groups: Vec<MetricsGroup>,
76 /// The Prometheus result type (matrix, vector, scalar, string).
77 /// Used to suggest appropriate visualization types.
78 #[serde(default)]
79 pub result_type: ResultType,
80}