Skip to main content

enya_client/
request.rs

1//! Query request types.
2
3use crate::types::Timestamp;
4
5/// A metrics query request.
6#[derive(Debug, Clone)]
7pub struct QueryRequest {
8    /// The metric name (e.g., "cpu_usage", "http_requests_total").
9    pub metric: String,
10
11    /// The PromQL query string (e.g., "sum(cpu_usage{env=\"prod\"}) by (region)").
12    pub query: String,
13
14    /// Start of the query time range (nanoseconds since epoch).
15    /// If None, defaults to backend-specific behavior (e.g., 1 hour ago).
16    pub start: Option<Timestamp>,
17
18    /// End of the query time range (nanoseconds since epoch).
19    /// If None, defaults to now.
20    pub end: Option<Timestamp>,
21
22    /// Query step/granularity in seconds.
23    /// This determines the resolution of the returned data points.
24    pub step_secs: u64,
25}
26
27impl QueryRequest {
28    /// Create a new query request with the given metric and query.
29    #[must_use]
30    pub fn new(metric: impl Into<String>, query: impl Into<String>) -> Self {
31        Self {
32            metric: metric.into(),
33            query: query.into(),
34            start: None,
35            end: None,
36            step_secs: 60, // Default 1 minute resolution
37        }
38    }
39
40    /// Set the time range for the query.
41    #[must_use]
42    pub fn with_range(mut self, start: Timestamp, end: Timestamp) -> Self {
43        self.start = Some(start);
44        self.end = Some(end);
45        self
46    }
47
48    /// Set the query step/granularity in seconds.
49    #[must_use]
50    pub fn with_step(mut self, step_secs: u64) -> Self {
51        self.step_secs = step_secs;
52        self
53    }
54}