global: snapshot

This commit is contained in:
nym21
2026-03-01 11:39:02 +01:00
parent a6664bbb93
commit e10013fd2c
23 changed files with 367 additions and 467 deletions
@@ -58,6 +58,20 @@ where
f64: From<T>,
{
compute_height(&mut self.height)?;
self.compute_rest(max_from, windows, exit)
}
/// Compute rolling distribution from already-populated height data.
pub(crate) fn compute_rest(
&mut self,
max_from: Height,
windows: &WindowStarts<'_>,
exit: &Exit,
) -> Result<()>
where
T: Copy + Ord + From<f64> + Default,
f64: From<T>,
{
self.rolling
.compute_distribution(max_from, windows, &self.height, exit)?;
Ok(())
@@ -1,69 +0,0 @@
//! LazyComputedFromHeightCumulativeFull - block full with lazy height transform + cumulative + rolling.
use std::ops::SubAssign;
use brk_error::Result;
use brk_traversable::Traversable;
use brk_types::{Height, Version};
use derive_more::{Deref, DerefMut};
use schemars::JsonSchema;
use vecdb::{Database, Exit, LazyVecFrom1, ReadableCloneableVec, Rw, StorageMode, UnaryTransform};
use crate::{
indexes,
internal::{ComputedHeightDerivedCumulativeFull, ComputedVecValue, NumericValue, WindowStarts},
};
const VERSION: Version = Version::ZERO;
/// Block full aggregation with lazy height transform + cumulative + rolling windows.
#[derive(Deref, DerefMut, Traversable)]
pub struct LazyComputedFromHeightFull<T, S = T, M: StorageMode = Rw>
where
T: NumericValue + JsonSchema,
S: ComputedVecValue,
{
#[traversable(rename = "base")]
pub height: LazyVecFrom1<Height, T, Height, S>,
#[deref]
#[deref_mut]
pub rest: Box<ComputedHeightDerivedCumulativeFull<T, M>>,
}
impl<T, S> LazyComputedFromHeightFull<T, S>
where
T: NumericValue + JsonSchema,
S: ComputedVecValue + JsonSchema,
{
pub(crate) fn forced_import<F: UnaryTransform<S, T>>(
db: &Database,
name: &str,
version: Version,
source: &impl ReadableCloneableVec<Height, S>,
indexes: &indexes::Vecs,
) -> Result<Self> {
let v = version + VERSION;
let height = LazyVecFrom1::transformed::<F>(name, v, source.read_only_boxed_clone());
let rest = ComputedHeightDerivedCumulativeFull::forced_import(db, name, v, indexes)?;
Ok(Self {
height,
rest: Box::new(rest),
})
}
pub(crate) fn compute(
&mut self,
max_from: Height,
windows: &WindowStarts<'_>,
exit: &Exit,
) -> Result<()>
where
T: From<f64> + Default + SubAssign + Copy + Ord,
f64: From<T>,
{
self.rest.compute(max_from, windows, &self.height, exit)
}
}
@@ -6,17 +6,14 @@ mod cumulative_sum;
mod distribution;
mod full;
mod last;
mod lazy_computed_full;
mod lazy_computed_value_cumulative;
mod value_cumulative;
mod lazy_last;
mod lazy_value_last;
mod percentiles;
mod price;
mod ratio;
mod stddev;
mod stored_value_last;
mod value_change;
mod value_ema;
mod value_full;
mod value_last;
mod value_last_rolling;
@@ -30,17 +27,14 @@ pub use cumulative_sum::*;
pub use distribution::*;
pub use full::*;
pub use last::*;
pub use lazy_computed_full::*;
pub use lazy_computed_value_cumulative::*;
pub use value_cumulative::*;
pub use lazy_last::*;
pub use lazy_value_last::*;
pub use percentiles::*;
pub use price::*;
pub use ratio::*;
pub use stddev::*;
pub use stored_value_last::*;
pub use value_change::*;
pub use value_ema::*;
pub use value_full::*;
pub use value_last::*;
pub use value_last_rolling::*;
@@ -1,53 +0,0 @@
use brk_error::Result;
use brk_traversable::Traversable;
use brk_types::{Dollars, Height, Sats, Version};
use derive_more::{Deref, DerefMut};
use vecdb::{Database, Exit, ReadableVec, Rw, StorageMode};
use crate::{
indexes,
internal::ByUnit,
};
const VERSION: Version = Version::ZERO;
#[derive(Deref, DerefMut, Traversable)]
#[traversable(transparent)]
pub struct StoredValueFromHeightLast<M: StorageMode = Rw> {
#[deref]
#[deref_mut]
pub base: ByUnit<M>,
}
impl StoredValueFromHeightLast {
pub(crate) fn forced_import(
db: &Database,
name: &str,
version: Version,
indexes: &indexes::Vecs,
) -> Result<Self> {
let v = version + VERSION;
Ok(Self {
base: ByUnit::forced_import(db, name, v, indexes)?,
})
}
pub(crate) fn compute_rolling_sum(
&mut self,
max_from: Height,
window_starts: &impl ReadableVec<Height, Height>,
sats_source: &impl ReadableVec<Height, Sats>,
usd_source: &impl ReadableVec<Height, Dollars>,
exit: &Exit,
) -> Result<()> {
self.base
.sats
.height
.compute_rolling_sum(max_from, window_starts, sats_source, exit)?;
self.base
.usd
.height
.compute_rolling_sum(max_from, window_starts, usd_source, exit)?;
Ok(())
}
}
@@ -14,13 +14,13 @@ const VERSION: Version = Version::ZERO;
/// Change values indexed by height - sats (stored), btc (lazy), usd (stored).
#[derive(Traversable)]
pub struct ValueChangeFromHeight<M: StorageMode = Rw> {
pub struct ValueFromHeightChange<M: StorageMode = Rw> {
pub sats: ComputedFromHeightLast<SatsSigned, M>,
pub btc: LazyFromHeightLast<Bitcoin, SatsSigned>,
pub usd: ComputedFromHeightLast<Dollars, M>,
}
impl ValueChangeFromHeight {
impl ValueFromHeightChange {
pub(crate) fn forced_import(
db: &Database,
name: &str,
@@ -10,14 +10,14 @@ use crate::{
};
#[derive(Traversable)]
pub struct LazyComputedValueFromHeightCumulative<M: StorageMode = Rw> {
pub struct ValueFromHeightCumulative<M: StorageMode = Rw> {
pub base: ByUnit<M>,
pub cumulative: ByUnit<M>,
}
const VERSION: Version = Version::ONE;
impl LazyComputedValueFromHeightCumulative {
impl ValueFromHeightCumulative {
pub(crate) fn forced_import(
db: &Database,
name: &str,
@@ -1,53 +0,0 @@
use brk_error::Result;
use brk_traversable::Traversable;
use brk_types::{Dollars, Height, Sats, Version};
use derive_more::{Deref, DerefMut};
use vecdb::{Database, Exit, ReadableVec, Rw, StorageMode};
use crate::{
indexes,
internal::ByUnit,
};
const VERSION: Version = Version::ZERO;
#[derive(Deref, DerefMut, Traversable)]
#[traversable(transparent)]
pub struct ValueEmaFromHeight<M: StorageMode = Rw> {
#[deref]
#[deref_mut]
pub base: ByUnit<M>,
}
impl ValueEmaFromHeight {
pub(crate) fn forced_import(
db: &Database,
name: &str,
version: Version,
indexes: &indexes::Vecs,
) -> Result<Self> {
let v = version + VERSION;
Ok(Self {
base: ByUnit::forced_import(db, name, v, indexes)?,
})
}
pub(crate) fn compute_rolling_average(
&mut self,
starting_height: Height,
window_starts: &impl ReadableVec<Height, Height>,
sats_source: &impl ReadableVec<Height, Sats>,
dollars_source: &(impl ReadableVec<Height, Dollars> + Sync),
exit: &Exit,
) -> Result<()> {
self.base
.sats
.height
.compute_rolling_average(starting_height, window_starts, sats_source, exit)?;
self.base
.usd
.height
.compute_rolling_average(starting_height, window_starts, dollars_source, exit)?;
Ok(())
}
}
@@ -2,7 +2,7 @@ use brk_error::Result;
use brk_traversable::Traversable;
use brk_types::{Dollars, Height, Sats, Version};
use derive_more::{Deref, DerefMut};
use vecdb::{Database, Exit, Rw, StorageMode};
use vecdb::{Database, Exit, ReadableVec, Rw, StorageMode};
use crate::{
indexes, prices,
@@ -46,4 +46,42 @@ impl ValueFromHeightLast {
)?;
Ok(())
}
pub(crate) fn compute_rolling_sum(
&mut self,
max_from: Height,
window_starts: &impl ReadableVec<Height, Height>,
sats_source: &impl ReadableVec<Height, Sats>,
usd_source: &impl ReadableVec<Height, Dollars>,
exit: &Exit,
) -> Result<()> {
self.base
.sats
.height
.compute_rolling_sum(max_from, window_starts, sats_source, exit)?;
self.base
.usd
.height
.compute_rolling_sum(max_from, window_starts, usd_source, exit)?;
Ok(())
}
pub(crate) fn compute_ema(
&mut self,
starting_height: Height,
window_starts: &impl ReadableVec<Height, Height>,
sats_source: &impl ReadableVec<Height, Sats>,
dollars_source: &(impl ReadableVec<Height, Dollars> + Sync),
exit: &Exit,
) -> Result<()> {
self.base
.sats
.height
.compute_rolling_ema(starting_height, window_starts, sats_source, exit)?;
self.base
.usd
.height
.compute_rolling_ema(starting_height, window_starts, dollars_source, exit)?;
Ok(())
}
}
@@ -1,7 +1,7 @@
//! Value type for Height + Rolling pattern.
//!
//! Combines ValueFromHeight (sats/btc/usd per height, no period views) with
//! StoredValueRollingWindows (rolling sums across 4 windows).
//! ValueFromHeightLastWindows (rolling sums across 4 windows).
use brk_error::Result;
use brk_traversable::Traversable;
@@ -11,7 +11,7 @@ use vecdb::{Database, EagerVec, Exit, PcoVec, Rw, StorageMode};
use crate::{
indexes,
internal::{StoredValueRollingWindows, ValueFromHeight, WindowStarts},
internal::{ValueFromHeightLastWindows, ValueFromHeight, WindowStarts},
prices,
};
@@ -22,7 +22,7 @@ pub struct ValueFromHeightLastRolling<M: StorageMode = Rw> {
#[traversable(flatten)]
pub value: ValueFromHeight<M>,
#[traversable(flatten)]
pub rolling: StoredValueRollingWindows<M>,
pub rolling: ValueFromHeightLastWindows<M>,
}
const VERSION: Version = Version::ZERO;
@@ -37,7 +37,7 @@ impl ValueFromHeightLastRolling {
let v = version + VERSION;
Ok(Self {
value: ValueFromHeight::forced_import(db, name, v)?,
rolling: StoredValueRollingWindows::forced_import(db, name, v, indexes)?,
rolling: ValueFromHeightLastWindows::forced_import(db, name, v, indexes)?,
})
}
@@ -1,4 +1,4 @@
//! StoredValueRollingWindows - window-first ordering.
//! ValueFromHeightLastWindows - window-first ordering.
//!
//! Access pattern: `coinbase_sum._24h.sats.height`
//! Each window (24h, 7d, 30d, 1y) contains sats (stored) + btc (lazy) + usd (stored).
@@ -14,21 +14,21 @@ use brk_types::{Dollars, Sats};
use crate::{
indexes,
internal::{StoredValueFromHeightLast, WindowStarts, Windows},
internal::{ValueFromHeightLast, WindowStarts, Windows},
};
const VERSION: Version = Version::ZERO;
/// Stored value rolling windows — window-first, currency-last.
/// Value rolling windows — window-first, currency-last.
///
/// Each window contains `StoredValueFromHeightLast` (sats + btc lazy + usd).
/// Each window contains `ValueFromHeightLast` (sats + btc lazy + usd).
#[derive(Deref, DerefMut, Traversable)]
#[traversable(transparent)]
pub struct StoredValueRollingWindows<M: StorageMode = Rw>(
pub Windows<StoredValueFromHeightLast<M>>,
pub struct ValueFromHeightLastWindows<M: StorageMode = Rw>(
pub Windows<ValueFromHeightLast<M>>,
);
impl StoredValueRollingWindows {
impl ValueFromHeightLastWindows {
pub(crate) fn forced_import(
db: &Database,
name: &str,
@@ -37,25 +37,25 @@ impl StoredValueRollingWindows {
) -> Result<Self> {
let v = version + VERSION;
Ok(Self(Windows {
_24h: StoredValueFromHeightLast::forced_import(
_24h: ValueFromHeightLast::forced_import(
db,
&format!("{name}_24h"),
v,
indexes,
)?,
_7d: StoredValueFromHeightLast::forced_import(
_7d: ValueFromHeightLast::forced_import(
db,
&format!("{name}_7d"),
v,
indexes,
)?,
_30d: StoredValueFromHeightLast::forced_import(
_30d: ValueFromHeightLast::forced_import(
db,
&format!("{name}_30d"),
v,
indexes,
)?,
_1y: StoredValueFromHeightLast::forced_import(
_1y: ValueFromHeightLast::forced_import(
db,
&format!("{name}_1y"),
v,