mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-07-15 13:08:12 -07:00
global: snapshot part 7
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{Bitcoin, Cents, Dollars, Height, Sats, Version};
|
||||
use vecdb::{AnyVec, Database, Exit, ReadableCloneableVec, Rw, StorageMode};
|
||||
use vecdb::{Database, Exit, ReadableCloneableVec, Rw, StorageMode};
|
||||
|
||||
use crate::{
|
||||
indexes,
|
||||
@@ -54,10 +54,6 @@ impl AmountPerBlock {
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn min_stateful_len(&self) -> usize {
|
||||
self.sats.height.len()
|
||||
}
|
||||
|
||||
pub(crate) fn compute(
|
||||
&mut self,
|
||||
prices: &prices::Vecs,
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{Bitcoin, Cents, Dollars, Height, Sats, Version};
|
||||
use vecdb::{Database, EagerVec, Exit, ImportableVec, LazyVecFrom1, PcoVec, ReadableCloneableVec, Rw, StorageMode};
|
||||
|
||||
use crate::{
|
||||
internal::{CentsUnsignedToDollars, SatsToBitcoin, SatsToCents},
|
||||
prices,
|
||||
};
|
||||
|
||||
/// Raw per-block amount data: sats + cents (stored), btc + usd (lazy), no resolutions.
|
||||
#[derive(Traversable)]
|
||||
pub struct AmountBlock<M: StorageMode = Rw> {
|
||||
pub btc: LazyVecFrom1<Height, Bitcoin, Height, Sats>,
|
||||
pub sats: M::Stored<EagerVec<PcoVec<Height, Sats>>>,
|
||||
pub usd: LazyVecFrom1<Height, Dollars, Height, Cents>,
|
||||
pub cents: M::Stored<EagerVec<PcoVec<Height, Cents>>>,
|
||||
}
|
||||
|
||||
impl AmountBlock {
|
||||
pub(crate) fn forced_import(db: &Database, name: &str, version: Version) -> Result<Self> {
|
||||
let sats: EagerVec<PcoVec<Height, Sats>> =
|
||||
EagerVec::forced_import(db, &format!("{name}_sats"), version)?;
|
||||
let btc = LazyVecFrom1::transformed::<SatsToBitcoin>(
|
||||
name,
|
||||
version,
|
||||
sats.read_only_boxed_clone(),
|
||||
);
|
||||
let cents: EagerVec<PcoVec<Height, Cents>> =
|
||||
EagerVec::forced_import(db, &format!("{name}_cents"), version)?;
|
||||
let usd = LazyVecFrom1::transformed::<CentsUnsignedToDollars>(
|
||||
&format!("{name}_usd"),
|
||||
version,
|
||||
cents.read_only_boxed_clone(),
|
||||
);
|
||||
Ok(Self { btc, sats, usd, cents })
|
||||
}
|
||||
|
||||
pub(crate) fn compute_cents(
|
||||
&mut self,
|
||||
max_from: Height,
|
||||
prices: &prices::Vecs,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.cents.compute_binary::<Sats, Cents, SatsToCents>(
|
||||
max_from,
|
||||
&self.sats,
|
||||
&prices.spot.cents.height,
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,17 @@
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{Cents, Height, Sats, Version};
|
||||
use brk_types::{Height, Version};
|
||||
use vecdb::{Database, Exit, Rw, StorageMode};
|
||||
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{AmountPerBlock, SatsToCents},
|
||||
internal::{AmountBlock, AmountPerBlock},
|
||||
prices,
|
||||
};
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct AmountPerBlockCumulative<M: StorageMode = Rw> {
|
||||
pub block: AmountPerBlock<M>,
|
||||
pub block: AmountBlock<M>,
|
||||
pub cumulative: AmountPerBlock<M>,
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ impl AmountPerBlockCumulative {
|
||||
let v = version + VERSION;
|
||||
|
||||
Ok(Self {
|
||||
block: AmountPerBlock::forced_import(db, name, v, indexes)?,
|
||||
block: AmountBlock::forced_import(db, name, v)?,
|
||||
cumulative: AmountPerBlock::forced_import(
|
||||
db,
|
||||
&format!("{name}_cumulative"),
|
||||
@@ -46,19 +46,14 @@ impl AmountPerBlockCumulative {
|
||||
self.cumulative
|
||||
.sats
|
||||
.height
|
||||
.compute_cumulative(max_from, &self.block.sats.height, exit)?;
|
||||
.compute_cumulative(max_from, &self.block.sats, exit)?;
|
||||
|
||||
self.block.cents.compute_binary::<Sats, Cents, SatsToCents>(
|
||||
max_from,
|
||||
&self.block.sats.height,
|
||||
&prices.spot.cents.height,
|
||||
exit,
|
||||
)?;
|
||||
self.block.compute_cents(max_from, prices, exit)?;
|
||||
|
||||
self.cumulative
|
||||
.cents
|
||||
.height
|
||||
.compute_cumulative(max_from, &self.block.cents.height, exit)?;
|
||||
.compute_cumulative(max_from, &self.block.cents, exit)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ impl AmountPerBlockCumulativeRolling {
|
||||
exit: &Exit,
|
||||
compute_sats: impl FnOnce(&mut EagerVec<PcoVec<Height, Sats>>) -> Result<()>,
|
||||
) -> Result<()> {
|
||||
compute_sats(&mut self.block.sats.height)?;
|
||||
compute_sats(&mut self.block.sats)?;
|
||||
self.compute_rest(max_from, prices, exit)
|
||||
}
|
||||
|
||||
|
||||
@@ -54,15 +54,15 @@ impl AmountPerBlockFull {
|
||||
exit: &Exit,
|
||||
compute_sats: impl FnOnce(&mut EagerVec<PcoVec<Height, Sats>>) -> Result<()>,
|
||||
) -> Result<()> {
|
||||
compute_sats(&mut self.inner.block.sats.height)?;
|
||||
compute_sats(&mut self.inner.block.sats)?;
|
||||
|
||||
self.inner.compute_rest(max_from, prices, exit)?;
|
||||
|
||||
self.distribution.compute(
|
||||
max_from,
|
||||
windows,
|
||||
&self.inner.block.sats.height,
|
||||
&self.inner.block.cents.height,
|
||||
&self.inner.block.sats,
|
||||
&self.inner.block.cents,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
mod base;
|
||||
mod block;
|
||||
mod cumulative;
|
||||
mod cumulative_rolling;
|
||||
mod full;
|
||||
@@ -10,6 +11,7 @@ mod rolling_distribution;
|
||||
mod with_deltas;
|
||||
|
||||
pub use base::*;
|
||||
pub use block::*;
|
||||
pub use cumulative::*;
|
||||
pub use cumulative_rolling::*;
|
||||
pub use full::*;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//! PerBlockCumulativeRolling - base PerBlock + cumulative PerBlock + lazy rolling sums.
|
||||
//! PerBlockCumulativeRolling - base EagerVec + cumulative PerBlock + lazy rolling sums.
|
||||
//!
|
||||
//! Rolling sums are derived lazily from the cumulative vec via LazyDeltaVec.
|
||||
//! No rolling sum vecs are stored on disk.
|
||||
@@ -13,7 +13,7 @@ use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{Height, Version};
|
||||
use schemars::JsonSchema;
|
||||
use vecdb::{Database, EagerVec, Exit, PcoVec, Rw, StorageMode};
|
||||
use vecdb::{Database, EagerVec, Exit, ImportableVec, PcoVec, Rw, StorageMode};
|
||||
|
||||
use crate::{
|
||||
indexes,
|
||||
@@ -29,7 +29,8 @@ where
|
||||
T: NumericValue + JsonSchema,
|
||||
C: NumericValue + JsonSchema,
|
||||
{
|
||||
pub block: PerBlock<T, M>,
|
||||
#[traversable(hidden)]
|
||||
pub block: M::Stored<EagerVec<PcoVec<Height, T>>>,
|
||||
pub cumulative: PerBlock<C, M>,
|
||||
pub sum: LazyRollingSumsFromHeight<C>,
|
||||
pub average: LazyRollingAvgsFromHeight<C>,
|
||||
@@ -47,7 +48,7 @@ where
|
||||
indexes: &indexes::Vecs,
|
||||
cached_starts: &CachedWindowStarts,
|
||||
) -> Result<Self> {
|
||||
let block = PerBlock::forced_import(db, name, version, indexes)?;
|
||||
let block = EagerVec::forced_import(db, name, version)?;
|
||||
let cumulative =
|
||||
PerBlock::forced_import(db, &format!("{name}_cumulative"), version, indexes)?;
|
||||
let sum = LazyRollingSumsFromHeight::new(
|
||||
@@ -83,7 +84,7 @@ where
|
||||
where
|
||||
C: Default,
|
||||
{
|
||||
compute_base(&mut self.block.height)?;
|
||||
compute_base(&mut self.block)?;
|
||||
self.compute_rest(max_from, exit)
|
||||
}
|
||||
|
||||
@@ -94,7 +95,7 @@ where
|
||||
{
|
||||
self.cumulative
|
||||
.height
|
||||
.compute_cumulative(max_from, &self.block.height, exit)?;
|
||||
.compute_cumulative(max_from, &self.block, exit)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//! PerBlockFull - base PerBlock + cumulative PerBlock + RollingComplete.
|
||||
//! PerBlockFull - base EagerVec + cumulative PerBlock + RollingComplete.
|
||||
//!
|
||||
//! For metrics with stored per-block data, cumulative sums, and rolling windows.
|
||||
|
||||
@@ -6,7 +6,7 @@ use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{Height, Version};
|
||||
use schemars::JsonSchema;
|
||||
use vecdb::{Database, EagerVec, Exit, PcoVec, Rw, StorageMode};
|
||||
use vecdb::{Database, EagerVec, Exit, ImportableVec, PcoVec, Rw, StorageMode};
|
||||
|
||||
use crate::{
|
||||
indexes,
|
||||
@@ -18,7 +18,8 @@ pub struct PerBlockFull<T, M: StorageMode = Rw>
|
||||
where
|
||||
T: NumericValue + JsonSchema,
|
||||
{
|
||||
pub block: PerBlock<T, M>,
|
||||
#[traversable(hidden)]
|
||||
pub block: M::Stored<EagerVec<PcoVec<Height, T>>>,
|
||||
pub cumulative: PerBlock<T, M>,
|
||||
#[traversable(flatten)]
|
||||
pub rolling: RollingComplete<T, M>,
|
||||
@@ -35,7 +36,7 @@ where
|
||||
indexes: &indexes::Vecs,
|
||||
cached_starts: &CachedWindowStarts,
|
||||
) -> Result<Self> {
|
||||
let block = PerBlock::forced_import(db, name, version, indexes)?;
|
||||
let block = EagerVec::forced_import(db, name, version)?;
|
||||
let cumulative =
|
||||
PerBlock::forced_import(db, &format!("{name}_cumulative"), version, indexes)?;
|
||||
let rolling = RollingComplete::forced_import(
|
||||
@@ -66,12 +67,12 @@ where
|
||||
T: From<f64> + Default + Copy + Ord,
|
||||
f64: From<T>,
|
||||
{
|
||||
compute_base(&mut self.block.height)?;
|
||||
compute_base(&mut self.block)?;
|
||||
self.cumulative
|
||||
.height
|
||||
.compute_cumulative(max_from, &self.block.height, exit)?;
|
||||
.compute_cumulative(max_from, &self.block, exit)?;
|
||||
self.rolling
|
||||
.compute(max_from, windows, &self.block.height, exit)?;
|
||||
.compute(max_from, windows, &self.block, exit)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ pub struct PerBlockRollingAverage<T, M: StorageMode = Rw>
|
||||
where
|
||||
T: NumericValue + JsonSchema,
|
||||
{
|
||||
#[traversable(hidden)]
|
||||
pub block: M::Stored<EagerVec<PcoVec<Height, T>>>,
|
||||
#[traversable(hidden)]
|
||||
pub cumulative: M::Stored<EagerVec<PcoVec<Height, T>>>,
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{Dollars, Height, Version};
|
||||
use vecdb::{Database, EagerVec, ImportableVec, LazyVecFrom1, PcoVec, ReadableCloneableVec, Rw, StorageMode};
|
||||
|
||||
use super::CentsType;
|
||||
|
||||
/// Raw per-block fiat data: cents (stored) + usd (lazy), no resolutions.
|
||||
#[derive(Traversable)]
|
||||
pub struct FiatBlock<C: CentsType, M: StorageMode = Rw> {
|
||||
pub usd: LazyVecFrom1<Height, Dollars, Height, C>,
|
||||
pub cents: M::Stored<EagerVec<PcoVec<Height, C>>>,
|
||||
}
|
||||
|
||||
impl<C: CentsType> FiatBlock<C> {
|
||||
pub(crate) fn forced_import(db: &Database, name: &str, version: Version) -> Result<Self> {
|
||||
let cents: EagerVec<PcoVec<Height, C>> =
|
||||
EagerVec::forced_import(db, &format!("{name}_cents"), version)?;
|
||||
let usd = LazyVecFrom1::transformed::<C::ToDollars>(
|
||||
name,
|
||||
version,
|
||||
cents.read_only_boxed_clone(),
|
||||
);
|
||||
Ok(Self { usd, cents })
|
||||
}
|
||||
}
|
||||
@@ -5,12 +5,12 @@ use vecdb::{Database, Exit, Rw, StorageMode};
|
||||
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{CachedWindowStarts, CentsType, FiatPerBlock, LazyRollingSumsFiatFromHeight},
|
||||
internal::{CachedWindowStarts, CentsType, FiatBlock, FiatPerBlock, LazyRollingSumsFiatFromHeight},
|
||||
};
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct FiatPerBlockCumulativeWithSums<C: CentsType, M: StorageMode = Rw> {
|
||||
pub block: FiatPerBlock<C, M>,
|
||||
pub block: FiatBlock<C, M>,
|
||||
pub cumulative: FiatPerBlock<C, M>,
|
||||
pub sum: LazyRollingSumsFiatFromHeight<C>,
|
||||
}
|
||||
@@ -23,7 +23,7 @@ impl<C: CentsType> FiatPerBlockCumulativeWithSums<C> {
|
||||
indexes: &indexes::Vecs,
|
||||
cached_starts: &CachedWindowStarts,
|
||||
) -> Result<Self> {
|
||||
let block = FiatPerBlock::forced_import(db, name, version, indexes)?;
|
||||
let block = FiatBlock::forced_import(db, name, version)?;
|
||||
let cumulative = FiatPerBlock::forced_import(
|
||||
db,
|
||||
&format!("{name}_cumulative"),
|
||||
@@ -47,7 +47,7 @@ impl<C: CentsType> FiatPerBlockCumulativeWithSums<C> {
|
||||
self.cumulative
|
||||
.cents
|
||||
.height
|
||||
.compute_cumulative(max_from, &self.block.cents.height, exit)?;
|
||||
.compute_cumulative(max_from, &self.block.cents, exit)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
mod base;
|
||||
mod block;
|
||||
mod cumulative_sum;
|
||||
mod cumulative_sum_with_deltas;
|
||||
mod lazy;
|
||||
mod lazy_rolling_sum;
|
||||
mod with_deltas;
|
||||
pub use base::*;
|
||||
pub use block::*;
|
||||
pub use cumulative_sum::*;
|
||||
pub use cumulative_sum_with_deltas::*;
|
||||
pub use lazy::*;
|
||||
|
||||
Reference in New Issue
Block a user