Skip to main content

harmos_storage/
aggregate.rs

1/* ***
2 *
3 * Harmos
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * ***
8 */
9
10/* === Modules === */
11
12/* === Re-Exports === */
13
14/* === Imports === */
15
16// Standard Library Imports
17
18// Third Party Imports
19use crate::signal::{Series, Summary};
20
21// Local Imports
22use harmos_domain::Aggregate;
23
24/* === Constants === */
25
26/* === Macros === */
27
28/* === Type Aliases === */
29
30/* === Structs === */
31
32/* === Enums === */
33
34/* === Traits === */
35
36/* === Implementations === */
37
38/* === Public Functions === */
39
40// Answers one member of the contract vocabulary from the signal math. The
41// kernels live in the sibling `signal` module; these functions connect them to
42// core reductions and for `signal::Aggregates` alike. Empty input answers
43// `Count` and `Sum` with zero and every other member with `None`.
44pub fn of_series(series: &Series<'_>, kind: Aggregate) -> Option<f64> {
45    match kind {
46        Aggregate::Median => series.median(),
47        Aggregate::Percentile(rank) => series.percentile(f64::from(rank)),
48        mergeable => of_summary(&series.summary(), mergeable),
49    }
50}
51
52pub fn of_summary(summary: &Summary, kind: Aggregate) -> Option<f64> {
53    match kind {
54        Aggregate::Count => Some(summary.count() as f64),
55        Aggregate::Sum => Some(summary.sum()),
56        Aggregate::Mean => summary.mean(),
57        Aggregate::Min => summary.min(),
58        Aggregate::Max => summary.max(),
59        Aggregate::Rms => summary.rms(),
60        Aggregate::Std => summary.std(),
61        // A summary keeps constant state so it can merge, and an order
62        // statistic is not a function of any constant state: the median of a
63        // union depends on where every value falls, not on per-part counts
64        // and moments. Refusing beats an estimate that reads exact.
65        Aggregate::Median | Aggregate::Percentile(_) => None,
66    }
67}
68
69/* === Private Functions === */
70
71/* === Tests === */