1use crate::ast::{Grouping, LogQuery, Unwrap};
10use std::time::Duration;
11
12#[derive(Debug, Clone, PartialEq)]
14#[non_exhaustive]
15pub enum MetricQuery {
16 Range(Box<RangeAggregation>),
18 Vector(Box<VectorAggregation>),
21 Binary(Box<BinaryExpr>),
23 Literal(f64),
25 VectorLiteral(f64),
28 LabelReplace(Box<LabelReplace>),
31}
32
33#[derive(Debug, Clone, PartialEq)]
35pub struct LabelReplace {
36 pub inner: MetricQuery,
38 pub dst_label: String,
40 pub replacement: String,
42 pub src_label: String,
44 pub regex: String,
46}
47
48#[derive(Debug, Clone, PartialEq)]
50pub struct RangeAggregation {
51 pub function: RangeFunction,
52 pub log_query: LogQuery,
54 pub range: Duration,
56 pub offset: Option<Duration>,
58 pub unwrap: Option<Unwrap>,
61 pub param: Option<f64>,
63 pub grouping: Option<Grouping>,
65}
66
67#[derive(Debug, Clone, Copy, PartialEq, Eq)]
69#[non_exhaustive]
70pub enum RangeFunction {
71 Rate,
72 RateCounter,
73 CountOverTime,
74 BytesRate,
75 BytesOverTime,
76 AvgOverTime,
77 SumOverTime,
78 MinOverTime,
79 MaxOverTime,
80 StdvarOverTime,
81 StddevOverTime,
82 QuantileOverTime,
83 FirstOverTime,
84 LastOverTime,
85 AbsentOverTime,
86}
87
88impl RangeFunction {
89 pub fn from_name(name: &str) -> Option<Self> {
91 Some(match name {
92 "rate" => RangeFunction::Rate,
93 "rate_counter" => RangeFunction::RateCounter,
94 "count_over_time" => RangeFunction::CountOverTime,
95 "bytes_rate" => RangeFunction::BytesRate,
96 "bytes_over_time" => RangeFunction::BytesOverTime,
97 "avg_over_time" => RangeFunction::AvgOverTime,
98 "sum_over_time" => RangeFunction::SumOverTime,
99 "min_over_time" => RangeFunction::MinOverTime,
100 "max_over_time" => RangeFunction::MaxOverTime,
101 "stdvar_over_time" => RangeFunction::StdvarOverTime,
102 "stddev_over_time" => RangeFunction::StddevOverTime,
103 "quantile_over_time" => RangeFunction::QuantileOverTime,
104 "first_over_time" => RangeFunction::FirstOverTime,
105 "last_over_time" => RangeFunction::LastOverTime,
106 "absent_over_time" => RangeFunction::AbsentOverTime,
107 _ => return None,
108 })
109 }
110
111 pub fn takes_param(self) -> bool {
114 matches!(self, RangeFunction::QuantileOverTime)
115 }
116}
117
118#[derive(Debug, Clone, PartialEq)]
120pub struct VectorAggregation {
121 pub function: AggregationFunction,
122 pub param: Option<f64>,
124 pub grouping: Option<Grouping>,
126 pub inner: MetricQuery,
128}
129
130#[derive(Debug, Clone, Copy, PartialEq, Eq)]
132#[non_exhaustive]
133pub enum AggregationFunction {
134 Sum,
135 Avg,
136 Min,
137 Max,
138 Stddev,
139 Stdvar,
140 Count,
141 Topk,
142 Bottomk,
143 Sort,
144 SortDesc,
145}
146
147impl AggregationFunction {
148 pub fn from_name(name: &str) -> Option<Self> {
150 Some(match name {
151 "sum" => AggregationFunction::Sum,
152 "avg" => AggregationFunction::Avg,
153 "min" => AggregationFunction::Min,
154 "max" => AggregationFunction::Max,
155 "stddev" => AggregationFunction::Stddev,
156 "stdvar" => AggregationFunction::Stdvar,
157 "count" => AggregationFunction::Count,
158 "topk" => AggregationFunction::Topk,
159 "bottomk" => AggregationFunction::Bottomk,
160 "sort" => AggregationFunction::Sort,
161 "sort_desc" => AggregationFunction::SortDesc,
162 _ => return None,
163 })
164 }
165
166 pub fn takes_param(self) -> bool {
169 matches!(
170 self,
171 AggregationFunction::Topk | AggregationFunction::Bottomk
172 )
173 }
174}
175
176#[derive(Debug, Clone, PartialEq)]
178pub struct BinaryExpr {
179 pub op: BinOp,
180 pub left: MetricQuery,
181 pub right: MetricQuery,
182 pub bool_modifier: bool,
185 pub matching: Option<VectorMatch>,
188}
189
190#[derive(Debug, Clone, PartialEq, Eq)]
194pub struct VectorMatch {
195 pub on: bool,
198 pub labels: Vec<String>,
200 pub grouped: bool,
202}
203
204#[derive(Debug, Clone, Copy, PartialEq, Eq)]
206#[non_exhaustive]
207pub enum BinOp {
208 Add,
210 Sub,
211 Mul,
212 Div,
213 Mod,
214 Pow,
215 Eq,
217 Neq,
218 Gt,
219 Gte,
220 Lt,
221 Lte,
222 And,
224 Or,
225 Unless,
226}
227
228#[cfg(test)]
229mod tests {
230 use super::*;
231
232 #[test]
236 fn range_functions_round_trip_and_declare_their_parameter() {
237 let cases: &[(&str, RangeFunction, bool)] = &[
238 ("rate", RangeFunction::Rate, false),
239 ("count_over_time", RangeFunction::CountOverTime, false),
240 ("bytes_rate", RangeFunction::BytesRate, false),
241 ("bytes_over_time", RangeFunction::BytesOverTime, false),
242 ("sum_over_time", RangeFunction::SumOverTime, false),
243 ("avg_over_time", RangeFunction::AvgOverTime, false),
244 ("min_over_time", RangeFunction::MinOverTime, false),
245 ("max_over_time", RangeFunction::MaxOverTime, false),
246 ("stdvar_over_time", RangeFunction::StdvarOverTime, false),
247 ("stddev_over_time", RangeFunction::StddevOverTime, false),
248 ("first_over_time", RangeFunction::FirstOverTime, false),
249 ("last_over_time", RangeFunction::LastOverTime, false),
250 ("absent_over_time", RangeFunction::AbsentOverTime, false),
251 ("quantile_over_time", RangeFunction::QuantileOverTime, true),
252 ];
253 for (name, expected, takes_param) in cases {
254 assert_eq!(
255 RangeFunction::from_name(name),
256 Some(*expected),
257 "from_name({name:?})"
258 );
259 assert_eq!(expected.takes_param(), *takes_param, "{name} takes_param");
260 }
261 }
262
263 #[test]
264 fn unknown_range_functions_are_rejected() {
265 for name in ["rate_over_time", "histogram_quantile", "sum", ""] {
266 assert_eq!(RangeFunction::from_name(name), None, "{name:?}");
267 }
268 }
269
270 #[test]
272 fn aggregation_functions_round_trip_and_declare_their_parameter() {
273 let cases: &[(&str, AggregationFunction, bool)] = &[
274 ("sum", AggregationFunction::Sum, false),
275 ("avg", AggregationFunction::Avg, false),
276 ("min", AggregationFunction::Min, false),
277 ("max", AggregationFunction::Max, false),
278 ("count", AggregationFunction::Count, false),
279 ("stddev", AggregationFunction::Stddev, false),
280 ("stdvar", AggregationFunction::Stdvar, false),
281 ("topk", AggregationFunction::Topk, true),
282 ("bottomk", AggregationFunction::Bottomk, true),
283 ];
284 for (name, expected, takes_param) in cases {
285 assert_eq!(
286 AggregationFunction::from_name(name),
287 Some(*expected),
288 "from_name({name:?})"
289 );
290 assert_eq!(expected.takes_param(), *takes_param, "{name} takes_param");
291 }
292 }
293
294 #[test]
295 fn unknown_aggregation_functions_are_rejected() {
296 for name in ["rate", "quantile", "median", ""] {
297 assert_eq!(AggregationFunction::from_name(name), None, "{name:?}");
298 }
299 }
300}