1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright 2024 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! Sample and aggregation interpolation.

use std::mem;

use crate::experimental::series::statistic::Statistic;

/// A type constructor for an interpolation over the samples or aggregations of a [`Statistic`].
///
/// [`Statistic`]: crate::experimental::series::statistic::Statistic
pub trait Interpolation {
    /// The type of fill (interpolated) samples computed by the interpolation.
    type FillSample<F>: Clone
    where
        F: Statistic;
    /// The state (output type) of the interpolation type constructor.
    type State<F>: InterpolationState<F::Aggregation, FillSample = Self::FillSample<F>>
    where
        F: Statistic;
}

/// An interpolation type constructor with the same sample type as the given [`Statistic`] type.
///
/// [`Statistic`]: crate::experimental::series::statistic::Statistic
pub trait InterpolationFor<F>: Interpolation<FillSample<F> = F::Sample>
where
    F: Statistic,
{
}

impl<P, F> InterpolationFor<F> for P
where
    F: Statistic,
    P: Interpolation<FillSample<F> = F::Sample>,
{
}

/// A type constructor for an interpolation that can be suspended.
///
/// A suspended interpolation can produce different fill samples while the system is in a power
/// saving mode of operation, etc.
pub trait Suspend: Interpolation {
    type Suspended<F>: InterpolationState<F::Aggregation, FillSample = Self::FillSample<F>>
    where
        F: Statistic;
}

/// An interpolation over samples and statistical aggregations.
pub trait InterpolationState<A>: Clone {
    /// The type of fill (interpolated) samples computed by the interpolation.
    type FillSample: Clone;

    /// Gets the fill (interpolated) sample of the interpolation.
    fn sample(&self) -> Self::FillSample;

    /// Folds a sample into the interpolation.
    fn fold_sample(&mut self, _sample: Self::FillSample) {}

    /// Folds an aggregation into the interpolation.
    fn fold_aggregation(&mut self, _aggregation: A) {}
}

/// An interpolation over a constant sample.
#[derive(Debug)]
pub enum Constant {}

impl Constant {
    pub fn default<T>() -> ConstantState<T>
    where
        T: Default,
    {
        ConstantState::default()
    }

    pub fn new<T>(sample: T) -> ConstantState<T> {
        ConstantState(sample)
    }
}

impl Interpolation for Constant {
    type FillSample<F> = F::Sample
    where
        F: Statistic;
    type State<F> = ConstantState<F::Sample>
    where
        F: Statistic;
}

#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ConstantState<T>(pub T);

impl<T, A> InterpolationState<A> for ConstantState<T>
where
    T: Clone,
{
    type FillSample = T;

    fn sample(&self) -> Self::FillSample {
        self.0.clone()
    }
}

/// An interpolation over the last observed sample.
#[derive(Debug)]
pub enum LastSample {}

impl LastSample {
    pub fn or<T>(sample: T) -> LastSampleState<T> {
        LastSampleState::or(sample)
    }
}

impl Interpolation for LastSample {
    type FillSample<F> = F::Sample
    where
        F: Statistic;
    type State<F> = LastSampleState<F::Sample>
    where
        F: Statistic;
}

#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct LastSampleState<T>(T);

impl<T> LastSampleState<T> {
    pub fn or(sample: T) -> Self {
        LastSampleState(sample)
    }
}

impl<T, A> InterpolationState<A> for LastSampleState<T>
where
    T: Clone,
{
    type FillSample = T;

    fn sample(&self) -> Self::FillSample {
        self.0.clone()
    }

    fn fold_sample(&mut self, sample: Self::FillSample) {
        let _prev = mem::replace(&mut self.0, sample);
    }
}

/// An interpolation over the last interval aggregation.
#[derive(Debug)]
pub enum LastAggregation {}

impl LastAggregation {
    pub fn or<A>(aggregation: A) -> LastAggregationState<A> {
        LastAggregationState::or(aggregation)
    }
}

impl Interpolation for LastAggregation {
    type FillSample<F> = F::Aggregation
    where
        F: Statistic;
    type State<F> = LastAggregationState<F::Aggregation>
    where
        F: Statistic;
}

#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct LastAggregationState<A>(A);

impl<A> LastAggregationState<A> {
    pub fn or(aggregation: A) -> Self {
        LastAggregationState(aggregation)
    }
}

impl<A> InterpolationState<A> for LastAggregationState<A>
where
    A: Clone,
{
    type FillSample = A;

    fn sample(&self) -> Self::FillSample {
        self.0.clone()
    }

    fn fold_aggregation(&mut self, aggregation: A) {
        let _prev = mem::replace(&mut self.0, aggregation);
    }
}

#[cfg(test)]
mod tests {
    use crate::experimental::series::interpolation::{
        Constant, InterpolationState, LastAggregation, LastSample,
    };

    /// Erases type information such that the `InterpolationState` returned by `f` only supports
    /// `u64` samples and aggregations.
    ///
    /// This avoids the need for verbose type annotations in tests.
    fn erase<P, F>(mut f: F) -> impl InterpolationState<u64, FillSample = u64>
    where
        F: FnMut() -> P,
        P: InterpolationState<u64, FillSample = u64>,
    {
        f()
    }

    #[test]
    fn interpolate_constant() {
        let mut interpolation = erase(|| Constant::new(1u64));
        assert_eq!(interpolation.sample(), 1);

        interpolation.fold_sample(7);
        interpolation.fold_aggregation(7);
        assert_eq!(interpolation.sample(), 1); // Both samples and aggregations are ignored.
    }

    #[test]
    fn interpolate_last_aggregation() {
        let mut interpolation = erase(|| LastAggregation::or(1u64));
        assert_eq!(interpolation.sample(), 1);

        interpolation.fold_sample(7);
        assert_eq!(interpolation.sample(), 1);

        interpolation.fold_aggregation(7); // Last aggregation is cached.
        assert_eq!(interpolation.sample(), 7);
    }

    #[test]
    fn interpolate_last_sample() {
        let mut interpolation = erase(|| LastSample::or(1u64));
        assert_eq!(interpolation.sample(), 1);

        interpolation.fold_aggregation(7);
        assert_eq!(interpolation.sample(), 1);

        interpolation.fold_sample(7); // Last sample is cached.
        assert_eq!(interpolation.sample(), 7);
    }
}