Skip to main content

BasicAggregator

Struct BasicAggregator 

Source
pub struct BasicAggregator<G, I> { /* private fields */ }
Expand description

Decayed aggregate sum, count and average over a stream of items.

§Examples

§Basic Aggregation

use std::time::{Duration, Instant};
use fermentation::{ForwardDecay, g};
use fermentation::aggregate::{BasicAggregator, Aggregator};

let decay = ForwardDecay::new(Instant::now(), g::Polynomial::new(2));
let landmark = decay.landmark();
let now = landmark + Duration::from_secs(10);
let stream = vec![
    (landmark + Duration::from_secs(5), 4.0),
    (landmark + Duration::from_secs(7), 8.0),
    (landmark + Duration::from_secs(3), 3.0),
    (landmark + Duration::from_secs(8), 6.0),
    (landmark + Duration::from_secs(4), 4.0),
];

let mut aggregator = BasicAggregator::new(decay);

for item in stream {
    aggregator.update(item);
}

let epsilon = 0.01;

assert_eq!(aggregator.sum(now), 9.67);
assert_eq!(aggregator.count(now), 1.63);
assert!(aggregator.average() >= (5.93 - epsilon) && aggregator.average() <= (5.93 + epsilon));

§Update Landmark

use std::time::{Duration, Instant};
use fermentation::{ForwardDecay, g};
use fermentation::aggregate::{BasicAggregator, Aggregator};

let decay = ForwardDecay::new(Instant::now(), g::Exponential::new(0.2));
let landmark = decay.landmark();
let new_landmark = landmark + Duration::from_secs(1);
let now = landmark + Duration::from_secs(10);
let stream = vec![
    (landmark + Duration::from_secs(5), 4.0),
    (landmark + Duration::from_secs(7), 8.0),
    (landmark + Duration::from_secs(3), 3.0),
    (landmark + Duration::from_secs(8), 6.0),
    (landmark + Duration::from_secs(4), 4.0),
];

let mut aggregator = BasicAggregator::new(decay);
let mut clone = aggregator.clone();

clone.reset(new_landmark);

for item in stream {
    aggregator.update(item);
    clone.update(item);
}

aggregator.update_landmark(new_landmark);

let epsilon = 0.0001;

assert!((aggregator.sum(now) - clone.sum(now)).abs() < epsilon);
assert!((aggregator.count(now) - clone.count(now)).abs() < epsilon);
assert!((aggregator.average() - clone.average()).abs() < epsilon);

Implementations§

Source§

impl<I> BasicAggregator<Exponential, I>
where I: Item,

Source

pub fn update_landmark(&mut self, landmark: Instant)

Source§

impl<G, I> BasicAggregator<G, I>
where G: Function, I: Item,

Source

pub fn new(decay: ForwardDecay<G>) -> Self

Source

pub fn sum(&self, timestamp: Instant) -> f64

Source

pub fn static_sum(&self) -> f64

Source

pub fn count(&self, timestamp: Instant) -> f64

Source

pub fn static_count(&self) -> f64

Source

pub fn average(&self) -> f64

Source

pub fn decay(&mut self) -> &ForwardDecay<G>

Trait Implementations§

Source§

impl<G, I> Aggregator for BasicAggregator<G, I>
where G: Function, I: Item,

Source§

type Item = I

Source§

fn update(&mut self, item: I)

Update the aggregation with the given item.
Source§

fn reset(&mut self, landmark: Instant)

Reset the aggregation to the initial state. This is equivalent to creating a new aggregator with the same decay model and the given landmark.
Source§

impl<G: Clone, I: Clone> Clone for BasicAggregator<G, I>

Source§

fn clone(&self) -> BasicAggregator<G, I>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<G: Copy, I: Copy> Copy for BasicAggregator<G, I>

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.