Files
brk/crates/brk_computer/src/internal/rolling/full.rs
2026-03-04 14:02:00 +01:00

65 lines
1.8 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! RollingFull - Sum + Distribution per rolling window.
//!
//! 36 stored height vecs per metric (4 sum + 32 distribution), each with 17 index views.
use std::ops::SubAssign;
use brk_error::Result;
use brk_traversable::Traversable;
use brk_types::{Height, Version};
use schemars::JsonSchema;
use vecdb::{Database, Exit, ReadableVec, Rw, StorageMode};
use crate::{
indexes,
internal::{ComputedVecValue, NumericValue, RollingDistribution, RollingWindows, WindowStarts},
};
/// Sum (4 windows) + Distribution (8 stats × 4 windows) = 36 stored height vecs.
#[derive(Traversable)]
pub struct RollingFull<T, M: StorageMode = Rw>
where
T: ComputedVecValue + PartialOrd + JsonSchema,
{
pub sum: RollingWindows<T, M>,
#[traversable(flatten)]
pub distribution: RollingDistribution<T, M>,
}
impl<T> RollingFull<T>
where
T: NumericValue + JsonSchema,
{
pub(crate) fn forced_import(
db: &Database,
name: &str,
version: Version,
indexes: &indexes::Vecs,
) -> Result<Self> {
Ok(Self {
sum: RollingWindows::forced_import(db, &format!("{name}_sum"), version, indexes)?,
distribution: RollingDistribution::forced_import(db, name, version, indexes)?,
})
}
/// Compute rolling sum + all 8 distribution stats across all 4 windows.
pub(crate) fn compute(
&mut self,
max_from: Height,
windows: &WindowStarts<'_>,
source: &impl ReadableVec<Height, T>,
exit: &Exit,
) -> Result<()>
where
T: From<f64> + Default + SubAssign + Copy + Ord,
f64: From<T>,
{
self.sum
.compute_rolling_sum(max_from, windows, source, exit)?;
self.distribution
.compute_distribution(max_from, windows, source, exit)?;
Ok(())
}
}