global: snapshot

This commit is contained in:
nym21
2026-03-10 11:22:17 +01:00
parent 64ef63a056
commit 5ede3dc416
40 changed files with 408 additions and 259 deletions
@@ -1,14 +1,13 @@
//! ComputedPerBlockCumulative - stored height + LazyAggVec + cumulative (from height).
//! ComputedPerBlockCumulative - raw ComputedPerBlock + cumulative ComputedPerBlock.
//!
//! Like ComputedPerBlockCumulativeSum but without RollingWindows.
//! Used for distribution metrics where rolling is optional per cohort.
//! Cumulative gets its own ComputedPerBlock so it has LazyAggVec index views.
use brk_error::Result;
use brk_traversable::Traversable;
use brk_types::{Height, Version};
use schemars::JsonSchema;
use vecdb::{Database, EagerVec, Exit, ImportableVec, PcoVec, Rw, StorageMode};
use vecdb::{Database, EagerVec, Exit, PcoVec, Rw, StorageMode};
use crate::{
indexes,
@@ -20,7 +19,7 @@ pub struct ComputedPerBlockCumulative<T, M: StorageMode = Rw>
where
T: NumericValue + JsonSchema,
{
pub height: M::Stored<EagerVec<PcoVec<Height, T>>>,
pub raw: ComputedPerBlock<T, M>,
pub cumulative: ComputedPerBlock<T, M>,
}
@@ -34,35 +33,35 @@ where
version: Version,
indexes: &indexes::Vecs,
) -> Result<Self> {
let height: EagerVec<PcoVec<Height, T>> = EagerVec::forced_import(db, name, version)?;
let raw = ComputedPerBlock::forced_import(db, name, version, indexes)?;
let cumulative =
ComputedPerBlock::forced_import(db, &format!("{name}_cumulative"), version, indexes)?;
Ok(Self { height, cumulative })
Ok(Self { raw, cumulative })
}
/// Compute height data via closure, then cumulative only (no rolling).
/// Compute raw data via closure, then cumulative only (no rolling).
pub(crate) fn compute(
&mut self,
max_from: Height,
exit: &Exit,
compute_height: impl FnOnce(&mut EagerVec<PcoVec<Height, T>>) -> Result<()>,
compute_raw: impl FnOnce(&mut EagerVec<PcoVec<Height, T>>) -> Result<()>,
) -> Result<()>
where
T: Default,
{
compute_height(&mut self.height)?;
compute_raw(&mut self.raw.height)?;
self.compute_rest(max_from, exit)
}
/// Compute cumulative from already-filled height vec.
/// Compute cumulative from already-filled raw vec.
pub(crate) fn compute_rest(&mut self, max_from: Height, exit: &Exit) -> Result<()>
where
T: Default,
{
self.cumulative
.height
.compute_cumulative(max_from, &self.height, exit)?;
.compute_cumulative(max_from, &self.raw.height, exit)?;
Ok(())
}
}
@@ -1,8 +1,7 @@
//! ComputedPerBlockCumulativeSum - stored height + LazyAggVec + cumulative (from height) + RollingWindows (sum).
//! ComputedPerBlockCumulativeSum - raw ComputedPerBlock + cumulative ComputedPerBlock + RollingWindows (sum).
//!
//! Like ComputedPerBlockFull but with rolling sum only (no distribution).
//! Used for count metrics where distribution stats aren't meaningful.
//! Cumulative gets its own ComputedPerBlock so it has LazyAggVec index views too.
use std::ops::SubAssign;
@@ -10,7 +9,7 @@ use brk_error::Result;
use brk_traversable::Traversable;
use brk_types::{Height, Version};
use schemars::JsonSchema;
use vecdb::{Database, EagerVec, Exit, ImportableVec, PcoVec, Rw, StorageMode};
use vecdb::{Database, EagerVec, Exit, PcoVec, Rw, StorageMode};
use crate::{
indexes,
@@ -22,7 +21,7 @@ pub struct ComputedPerBlockCumulativeSum<T, M: StorageMode = Rw>
where
T: NumericValue + JsonSchema,
{
pub height: M::Stored<EagerVec<PcoVec<Height, T>>>,
pub raw: ComputedPerBlock<T, M>,
pub cumulative: ComputedPerBlock<T, M>,
pub sum: RollingWindows<T, M>,
}
@@ -37,34 +36,34 @@ where
version: Version,
indexes: &indexes::Vecs,
) -> Result<Self> {
let height: EagerVec<PcoVec<Height, T>> = EagerVec::forced_import(db, name, version)?;
let raw = ComputedPerBlock::forced_import(db, name, version, indexes)?;
let cumulative =
ComputedPerBlock::forced_import(db, &format!("{name}_cumulative"), version, indexes)?;
let rolling = RollingWindows::forced_import(db, name, version, indexes)?;
let sum = RollingWindows::forced_import(db, name, version, indexes)?;
Ok(Self {
height,
raw,
cumulative,
sum: rolling,
sum,
})
}
/// Compute height data via closure, then cumulative + rolling sum.
/// Compute raw data via closure, then cumulative + rolling sum.
pub(crate) fn compute(
&mut self,
max_from: Height,
windows: &WindowStarts<'_>,
exit: &Exit,
compute_height: impl FnOnce(&mut EagerVec<PcoVec<Height, T>>) -> Result<()>,
compute_raw: impl FnOnce(&mut EagerVec<PcoVec<Height, T>>) -> Result<()>,
) -> Result<()>
where
T: Default + SubAssign,
{
compute_height(&mut self.height)?;
compute_raw(&mut self.raw.height)?;
self.compute_rest(max_from, windows, exit)
}
/// Compute cumulative + rolling sum from already-populated height data.
/// Compute cumulative + rolling sum from already-populated raw data.
pub(crate) fn compute_rest(
&mut self,
max_from: Height,
@@ -76,9 +75,9 @@ where
{
self.cumulative
.height
.compute_cumulative(max_from, &self.height, exit)?;
.compute_cumulative(max_from, &self.raw.height, exit)?;
self.sum
.compute_rolling_sum(max_from, windows, &self.height, exit)?;
.compute_rolling_sum(max_from, windows, &self.raw.height, exit)?;
Ok(())
}
}
@@ -1,7 +1,6 @@
//! ComputedPerBlockFull - stored height + LazyAggVec + cumulative (from height) + RollingFull.
//! ComputedPerBlockFull - raw ComputedPerBlock + cumulative ComputedPerBlock + RollingFull.
//!
//! For metrics with stored per-block data, cumulative sums, and rolling windows.
//! Cumulative gets its own ComputedPerBlock so it has LazyAggVec index views too.
use std::ops::SubAssign;
@@ -9,7 +8,7 @@ use brk_error::Result;
use brk_traversable::Traversable;
use brk_types::{Height, Version};
use schemars::JsonSchema;
use vecdb::{Database, EagerVec, Exit, ImportableVec, PcoVec, Rw, StorageMode};
use vecdb::{Database, EagerVec, Exit, PcoVec, Rw, StorageMode};
use crate::{
indexes,
@@ -21,7 +20,7 @@ pub struct ComputedPerBlockFull<T, M: StorageMode = Rw>
where
T: NumericValue + JsonSchema,
{
pub height: M::Stored<EagerVec<PcoVec<Height, T>>>,
pub raw: ComputedPerBlock<T, M>,
pub cumulative: ComputedPerBlock<T, M>,
#[traversable(flatten)]
pub rolling: RollingFull<T, M>,
@@ -37,36 +36,36 @@ where
version: Version,
indexes: &indexes::Vecs,
) -> Result<Self> {
let height: EagerVec<PcoVec<Height, T>> = EagerVec::forced_import(db, name, version)?;
let raw = ComputedPerBlock::forced_import(db, name, version, indexes)?;
let cumulative =
ComputedPerBlock::forced_import(db, &format!("{name}_cumulative"), version, indexes)?;
let rolling = RollingFull::forced_import(db, name, version, indexes)?;
Ok(Self {
height,
raw,
cumulative,
rolling,
})
}
/// Compute height data via closure, then cumulative + rolling.
/// Compute raw data via closure, then cumulative + rolling.
pub(crate) fn compute(
&mut self,
max_from: Height,
windows: &WindowStarts<'_>,
exit: &Exit,
compute_height: impl FnOnce(&mut EagerVec<PcoVec<Height, T>>) -> Result<()>,
compute_raw: impl FnOnce(&mut EagerVec<PcoVec<Height, T>>) -> Result<()>,
) -> Result<()>
where
T: From<f64> + Default + SubAssign + Copy + Ord,
f64: From<T>,
{
compute_height(&mut self.height)?;
compute_raw(&mut self.raw.height)?;
self.cumulative
.height
.compute_cumulative(max_from, &self.height, exit)?;
.compute_cumulative(max_from, &self.raw.height, exit)?;
self.rolling
.compute(max_from, windows, &self.height, exit)?;
.compute(max_from, windows, &self.raw.height, exit)?;
Ok(())
}
}
@@ -1,4 +1,4 @@
//! ComputedPerBlockSum - stored height + RollingWindows (sum only).
//! ComputedPerBlockSum - raw ComputedPerBlock + RollingWindows (sum only).
//!
//! Like ComputedPerBlockCumulativeSum but without the cumulative vec.
@@ -8,11 +8,11 @@ use brk_error::Result;
use brk_traversable::Traversable;
use brk_types::{Height, Version};
use schemars::JsonSchema;
use vecdb::{Database, EagerVec, Exit, ImportableVec, PcoVec, Rw, StorageMode};
use vecdb::{Database, EagerVec, Exit, PcoVec, Rw, StorageMode};
use crate::{
indexes,
internal::{NumericValue, RollingWindows, WindowStarts},
internal::{ComputedPerBlock, NumericValue, RollingWindows, WindowStarts},
};
#[derive(Traversable)]
@@ -20,7 +20,7 @@ pub struct ComputedPerBlockSum<T, M: StorageMode = Rw>
where
T: NumericValue + JsonSchema,
{
pub height: M::Stored<EagerVec<PcoVec<Height, T>>>,
pub raw: ComputedPerBlock<T, M>,
pub sum: RollingWindows<T, M>,
}
@@ -34,26 +34,26 @@ where
version: Version,
indexes: &indexes::Vecs,
) -> Result<Self> {
let height: EagerVec<PcoVec<Height, T>> = EagerVec::forced_import(db, name, version)?;
let raw = ComputedPerBlock::forced_import(db, name, version, indexes)?;
let sum = RollingWindows::forced_import(db, &format!("{name}_sum"), version, indexes)?;
Ok(Self { height, sum })
Ok(Self { raw, sum })
}
/// Compute height data via closure, then rolling sum.
/// Compute raw data via closure, then rolling sum.
pub(crate) fn compute(
&mut self,
max_from: Height,
windows: &WindowStarts<'_>,
exit: &Exit,
compute_height: impl FnOnce(&mut EagerVec<PcoVec<Height, T>>) -> Result<()>,
compute_raw: impl FnOnce(&mut EagerVec<PcoVec<Height, T>>) -> Result<()>,
) -> Result<()>
where
T: Default + SubAssign,
{
compute_height(&mut self.height)?;
compute_raw(&mut self.raw.height)?;
self.sum
.compute_rolling_sum(max_from, windows, &self.height, exit)?;
.compute_rolling_sum(max_from, windows, &self.raw.height, exit)?;
Ok(())
}
}