use crate::{
error::{Error, ErrorKind},
types::Value,
utils,
};
use bytes_utils::Str;
use std::collections::HashMap;
#[cfg_attr(docsrs, doc(cfg(feature = "i-time-series")))]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Encoding {
Compressed,
Uncompressed,
}
impl Encoding {
pub(crate) fn to_str(&self) -> Str {
utils::static_str(match *self {
Encoding::Compressed => "COMPRESSED",
Encoding::Uncompressed => "UNCOMPRESSED",
})
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "i-time-series")))]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum DuplicatePolicy {
Block,
First,
Last,
Min,
Max,
Sum,
}
impl DuplicatePolicy {
pub(crate) fn to_str(&self) -> Str {
utils::static_str(match *self {
DuplicatePolicy::Block => "BLOCK",
DuplicatePolicy::First => "FIRST",
DuplicatePolicy::Last => "LAST",
DuplicatePolicy::Min => "MIN",
DuplicatePolicy::Max => "MAX",
DuplicatePolicy::Sum => "SUM",
})
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "i-time-series")))]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Timestamp {
Custom(i64),
Now,
}
impl Default for Timestamp {
fn default() -> Self {
Timestamp::Now
}
}
impl Timestamp {
pub(crate) fn to_value(&self) -> Value {
match *self {
Timestamp::Now => Value::String(utils::static_str("*")),
Timestamp::Custom(v) => Value::Integer(v),
}
}
pub(crate) fn from_str(value: &str) -> Result<Self, Error> {
match value {
"*" => Ok(Timestamp::Now),
_ => Ok(Timestamp::Custom(value.parse::<i64>()?)),
}
}
}
impl From<i64> for Timestamp {
fn from(value: i64) -> Self {
Timestamp::Custom(value)
}
}
impl TryFrom<&str> for Timestamp {
type Error = Error;
fn try_from(value: &str) -> Result<Self, Self::Error> {
Self::from_str(value)
}
}
impl TryFrom<Str> for Timestamp {
type Error = Error;
fn try_from(value: Str) -> Result<Self, Self::Error> {
Self::from_str(&value)
}
}
impl TryFrom<String> for Timestamp {
type Error = Error;
fn try_from(value: String) -> Result<Self, Self::Error> {
Self::from_str(&value)
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "i-time-series")))]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Aggregator {
Avg,
Sum,
Min,
Max,
Range,
Count,
First,
Last,
StdP,
StdS,
VarP,
VarS,
TWA,
}
impl Aggregator {
pub(crate) fn to_str(&self) -> Str {
utils::static_str(match *self {
Aggregator::Avg => "avg",
Aggregator::Sum => "sum",
Aggregator::Min => "min",
Aggregator::Max => "max",
Aggregator::Range => "range",
Aggregator::Count => "count",
Aggregator::First => "first",
Aggregator::Last => "last",
Aggregator::StdP => "std.p",
Aggregator::StdS => "std.s",
Aggregator::VarP => "var.p",
Aggregator::VarS => "var.s",
Aggregator::TWA => "twa",
})
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "i-time-series")))]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum GetLabels {
WithLabels,
SelectedLabels(Vec<Str>),
}
impl GetLabels {
pub(crate) fn args_len(&self) -> usize {
match *self {
GetLabels::WithLabels => 1,
GetLabels::SelectedLabels(ref s) => 1 + s.len(),
}
}
}
impl<S> FromIterator<S> for GetLabels
where
S: Into<Str>,
{
fn from_iter<I: IntoIterator<Item = S>>(iter: I) -> Self {
GetLabels::SelectedLabels(iter.into_iter().map(|v| v.into()).collect())
}
}
impl<S, const N: usize> From<[S; N]> for GetLabels
where
S: Into<Str>,
{
fn from(value: [S; N]) -> Self {
GetLabels::SelectedLabels(value.into_iter().map(|v| v.into()).collect())
}
}
impl<S> From<Vec<S>> for GetLabels
where
S: Into<Str>,
{
fn from(value: Vec<S>) -> Self {
GetLabels::SelectedLabels(value.into_iter().map(|v| v.into()).collect())
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "i-time-series")))]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum GetTimestamp {
Earliest,
Latest,
Custom(i64),
}
impl GetTimestamp {
pub(crate) fn to_value(&self) -> Value {
match *self {
GetTimestamp::Earliest => static_val!("-"),
GetTimestamp::Latest => static_val!("+"),
GetTimestamp::Custom(i) => i.into(),
}
}
}
impl TryFrom<&str> for GetTimestamp {
type Error = Error;
fn try_from(value: &str) -> Result<Self, Self::Error> {
Ok(match value {
"-" => GetTimestamp::Earliest,
"+" => GetTimestamp::Latest,
_ => GetTimestamp::Custom(value.parse::<i64>()?),
})
}
}
impl From<i64> for GetTimestamp {
fn from(value: i64) -> Self {
GetTimestamp::Custom(value)
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "i-time-series")))]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RangeAggregation {
pub align: Option<GetTimestamp>,
pub aggregation: Aggregator,
pub bucket_duration: u64,
pub bucket_timestamp: Option<BucketTimestamp>,
pub empty: bool,
}
impl From<(Aggregator, u64)> for RangeAggregation {
fn from((aggregation, duration): (Aggregator, u64)) -> Self {
RangeAggregation {
aggregation,
bucket_duration: duration,
align: None,
bucket_timestamp: None,
empty: false,
}
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "i-time-series")))]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Reducer {
Avg,
Sum,
Min,
Max,
Range,
Count,
StdP,
StdS,
VarP,
VarS,
}
impl Reducer {
pub(crate) fn to_str(&self) -> Str {
utils::static_str(match *self {
Reducer::Avg => "avg",
Reducer::Sum => "sum",
Reducer::Min => "min",
Reducer::Max => "max",
Reducer::Range => "range",
Reducer::Count => "count",
Reducer::StdP => "std.p",
Reducer::StdS => "std.s",
Reducer::VarP => "var.p",
Reducer::VarS => "var.s",
})
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "i-time-series")))]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct GroupBy {
pub groupby: Str,
pub reduce: Reducer,
}
impl<S: Into<Str>> From<(S, Reducer)> for GroupBy {
fn from((groupby, reduce): (S, Reducer)) -> Self {
GroupBy {
groupby: groupby.into(),
reduce,
}
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "i-time-series")))]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum BucketTimestamp {
Start,
End,
Mid,
}
impl TryFrom<&str> for BucketTimestamp {
type Error = Error;
fn try_from(value: &str) -> Result<Self, Self::Error> {
Ok(match value {
"-" | "start" => BucketTimestamp::Start,
"+" | "end" => BucketTimestamp::End,
"~" | "mid" => BucketTimestamp::Mid,
_ => return Err(Error::new(ErrorKind::InvalidArgument, "Invalid bucket timestamp.")),
})
}
}
impl BucketTimestamp {
pub(crate) fn to_str(&self) -> Str {
utils::static_str(match *self {
BucketTimestamp::Start => "-",
BucketTimestamp::End => "+",
BucketTimestamp::Mid => "~",
})
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "i-time-series")))]
pub type Resp2TimeSeriesValues<K, Lk, Lv> = Vec<(K, Vec<(Lk, Lv)>, Vec<(i64, f64)>)>;
#[cfg_attr(docsrs, doc(cfg(feature = "i-time-series")))]
pub type Resp3TimeSeriesValues<K, Lk, Lv> = HashMap<K, (Vec<(Lk, Lv)>, Vec<(i64, f64)>)>;