use crate::ast::{Grouping, LogQuery, Unwrap};
use std::time::Duration;
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum MetricQuery {
Range(Box<RangeAggregation>),
Vector(Box<VectorAggregation>),
Binary(Box<BinaryExpr>),
Literal(f64),
VectorLiteral(f64),
LabelReplace(Box<LabelReplace>),
}
#[derive(Debug, Clone, PartialEq)]
pub struct LabelReplace {
pub inner: MetricQuery,
pub dst_label: String,
pub replacement: String,
pub src_label: String,
pub regex: String,
}
#[derive(Debug, Clone, PartialEq)]
pub struct RangeAggregation {
pub function: RangeFunction,
pub log_query: LogQuery,
pub range: Duration,
pub offset: Option<Duration>,
pub unwrap: Option<Unwrap>,
pub param: Option<f64>,
pub grouping: Option<Grouping>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum RangeFunction {
Rate,
RateCounter,
CountOverTime,
BytesRate,
BytesOverTime,
AvgOverTime,
SumOverTime,
MinOverTime,
MaxOverTime,
StdvarOverTime,
StddevOverTime,
QuantileOverTime,
FirstOverTime,
LastOverTime,
AbsentOverTime,
}
impl RangeFunction {
pub fn from_name(name: &str) -> Option<Self> {
Some(match name {
"rate" => RangeFunction::Rate,
"rate_counter" => RangeFunction::RateCounter,
"count_over_time" => RangeFunction::CountOverTime,
"bytes_rate" => RangeFunction::BytesRate,
"bytes_over_time" => RangeFunction::BytesOverTime,
"avg_over_time" => RangeFunction::AvgOverTime,
"sum_over_time" => RangeFunction::SumOverTime,
"min_over_time" => RangeFunction::MinOverTime,
"max_over_time" => RangeFunction::MaxOverTime,
"stdvar_over_time" => RangeFunction::StdvarOverTime,
"stddev_over_time" => RangeFunction::StddevOverTime,
"quantile_over_time" => RangeFunction::QuantileOverTime,
"first_over_time" => RangeFunction::FirstOverTime,
"last_over_time" => RangeFunction::LastOverTime,
"absent_over_time" => RangeFunction::AbsentOverTime,
_ => return None,
})
}
pub fn takes_param(self) -> bool {
matches!(self, RangeFunction::QuantileOverTime)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct VectorAggregation {
pub function: AggregationFunction,
pub param: Option<f64>,
pub grouping: Option<Grouping>,
pub inner: MetricQuery,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum AggregationFunction {
Sum,
Avg,
Min,
Max,
Stddev,
Stdvar,
Count,
Topk,
Bottomk,
Sort,
SortDesc,
}
impl AggregationFunction {
pub fn from_name(name: &str) -> Option<Self> {
Some(match name {
"sum" => AggregationFunction::Sum,
"avg" => AggregationFunction::Avg,
"min" => AggregationFunction::Min,
"max" => AggregationFunction::Max,
"stddev" => AggregationFunction::Stddev,
"stdvar" => AggregationFunction::Stdvar,
"count" => AggregationFunction::Count,
"topk" => AggregationFunction::Topk,
"bottomk" => AggregationFunction::Bottomk,
"sort" => AggregationFunction::Sort,
"sort_desc" => AggregationFunction::SortDesc,
_ => return None,
})
}
pub fn takes_param(self) -> bool {
matches!(
self,
AggregationFunction::Topk | AggregationFunction::Bottomk
)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct BinaryExpr {
pub op: BinOp,
pub left: MetricQuery,
pub right: MetricQuery,
pub bool_modifier: bool,
pub matching: Option<VectorMatch>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VectorMatch {
pub on: bool,
pub labels: Vec<String>,
pub grouped: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum BinOp {
Add,
Sub,
Mul,
Div,
Mod,
Pow,
Eq,
Neq,
Gt,
Gte,
Lt,
Lte,
And,
Or,
Unless,
}