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}