//! ComputedFromHeightFull - Full (distribution + sum + cumulative) + RollingFull. //! //! For metrics aggregated per-block from finer-grained sources (e.g., per-tx data), //! where we want full per-block stats plus rolling window stats. use std::ops::SubAssign; use brk_error::Result; use brk_traversable::Traversable; use brk_types::{Height, Version}; use schemars::JsonSchema; use vecdb::{Database, Exit, Rw, StorageMode}; use crate::{ indexes, internal::{Full, NumericValue, RollingFull, WindowStarts}, }; #[derive(Traversable)] pub struct ComputedFromHeightFull where T: NumericValue + JsonSchema, { #[traversable(flatten)] pub full: Full, pub rolling: RollingFull, } const VERSION: Version = Version::ZERO; impl ComputedFromHeightFull where T: NumericValue + JsonSchema, { pub(crate) fn forced_import( db: &Database, name: &str, version: Version, indexes: &indexes::Vecs, ) -> Result { let v = version + VERSION; let height = Full::forced_import(db, name, v)?; let rolling = RollingFull::forced_import(db, name, v, indexes)?; Ok(Self { full: height, rolling, }) } /// Compute Full stats via closure, then rolling windows from the per-block sum. pub(crate) fn compute( &mut self, max_from: Height, windows: &WindowStarts<'_>, exit: &Exit, compute_full: impl FnOnce(&mut Full) -> Result<()>, ) -> Result<()> where T: From + Default + SubAssign + Copy + Ord, f64: From, { compute_full(&mut self.full)?; self.rolling.compute( max_from, windows, self.full.sum_cumulative.sum.inner(), exit, )?; Ok(()) } }