Skip to main content

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 collect;
17mod count;
18mod last;
19pub mod lazy_sorted_set;
20mod linear_gradient;
21mod max;
22mod min;
23mod sum;
24
25pub use avg::Avg;
26pub use collect::Collect;
27pub use count::Count;
28pub use last::AggregatingLast;
29pub use linear_gradient::LinearGradient;
30pub use max::Max;
31pub use min::Min;
32pub use sum::Sum;
33
34use std::sync::Arc;
35
36use crate::models::{ElementPropertyMap, ElementValue};
37
38use self::lazy_sorted_set::LazySortedSet;
39
40use super::{Function, FunctionRegistry};
41
42pub trait RegisterAggregationFunctions {
43    fn register_aggregation_functions(&self);
44}
45
46impl RegisterAggregationFunctions for FunctionRegistry {
47    fn register_aggregation_functions(&self) {
48        self.register_function("sum", Function::Aggregating(Arc::new(Sum {})));
49        self.register_function("avg", Function::Aggregating(Arc::new(Avg {})));
50        self.register_function("count", Function::Aggregating(Arc::new(Count {})));
51        self.register_function("min", Function::Aggregating(Arc::new(Min {})));
52        self.register_function("max", Function::Aggregating(Arc::new(Max {})));
53        self.register_function("collect", Function::Aggregating(Arc::new(Collect {})));
54        self.register_function(
55            "drasi.linearGradient",
56            Function::Aggregating(Arc::new(LinearGradient {})),
57        );
58        self.register_function(
59            "drasi.last",
60            Function::Aggregating(Arc::new(AggregatingLast {})),
61        );
62    }
63}
64
65#[derive(Debug, Clone)]
66pub enum ValueAccumulator {
67    Sum {
68        value: f64,
69    },
70    Avg {
71        sum: f64,
72        count: i64,
73    },
74    Count {
75        value: i64,
76    },
77    TimeMarker {
78        timestamp: u64,
79    },
80    Signature(u64),
81    LinearGradient {
82        count: i64,
83        mean_x: f64,
84        mean_y: f64,
85        m2: f64,
86        cov: f64,
87    },
88    Value(ElementValue),
89    Map(ElementPropertyMap),
90}
91
92#[derive(Clone)]
93pub enum Accumulator {
94    Value(ValueAccumulator),
95    LazySortedSet(LazySortedSet),
96}