drasi_core/evaluation/functions/aggregation/
mod.rs

1// Copyright 2024 The Drasi Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15mod avg;
16mod count;
17mod last;
18pub mod lazy_sorted_set;
19mod linear_gradient;
20mod max;
21mod min;
22mod sum;
23
24use std::sync::Arc;
25
26pub use avg::Avg;
27pub use count::Count;
28pub use last::Last;
29pub use linear_gradient::LinearGradient;
30pub use max::Max;
31pub use min::Min;
32pub use sum::Sum;
33
34use crate::models::ElementValue;
35
36use self::lazy_sorted_set::LazySortedSet;
37
38use super::{Function, FunctionRegistry};
39
40pub trait RegisterAggregationFunctions {
41    fn register_aggregation_functions(&self);
42}
43
44impl RegisterAggregationFunctions for FunctionRegistry {
45    fn register_aggregation_functions(&self) {
46        self.register_function("sum", Function::Aggregating(Arc::new(Sum {})));
47        self.register_function("avg", Function::Aggregating(Arc::new(Avg {})));
48        self.register_function("count", Function::Aggregating(Arc::new(Count {})));
49        self.register_function("min", Function::Aggregating(Arc::new(Min {})));
50        self.register_function("max", Function::Aggregating(Arc::new(Max {})));
51        self.register_function(
52            "drasi.linearGradient",
53            Function::Aggregating(Arc::new(LinearGradient {})),
54        );
55        self.register_function("drasi.last", Function::Aggregating(Arc::new(Last {})));
56    }
57}
58
59#[derive(Debug, Clone)]
60pub enum ValueAccumulator {
61    Sum {
62        value: f64,
63    },
64    Avg {
65        sum: f64,
66        count: i64,
67    },
68    Count {
69        value: i64,
70    },
71    TimeMarker {
72        timestamp: u64,
73    },
74    Signature(u64),
75    LinearGradient {
76        count: i64,
77        mean_x: f64,
78        mean_y: f64,
79        m2: f64,
80        cov: f64,
81    },
82    Value(ElementValue),
83}
84
85#[derive(Clone)]
86pub enum Accumulator {
87    Value(ValueAccumulator),
88    LazySortedSet(LazySortedSet),
89}