mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-07-12 19:48:15 -07:00
computer: snapshot
This commit is contained in:
+7
-10
@@ -1,4 +1,4 @@
|
||||
//! Value type with height-level data only (no period-derived views).
|
||||
//! Amount type with height-level data only (no period-derived views).
|
||||
//!
|
||||
//! Stores sats and cents per index, plus lazy btc and usd transforms.
|
||||
//! Use when period views are unnecessary (e.g., rolling windows provide windowed data).
|
||||
@@ -16,26 +16,23 @@ use crate::{
|
||||
prices,
|
||||
};
|
||||
|
||||
const VERSION: Version = Version::TWO; // Match ValueFromHeight versioning
|
||||
const VERSION: Version = Version::TWO; // Match AmountFromHeight versioning
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct Value<I: VecIndex, M: StorageMode = Rw> {
|
||||
pub struct Amount<I: VecIndex, M: StorageMode = Rw> {
|
||||
pub sats: M::Stored<EagerVec<PcoVec<I, Sats>>>,
|
||||
pub btc: LazyVecFrom1<I, Bitcoin, I, Sats>,
|
||||
pub cents: M::Stored<EagerVec<PcoVec<I, Cents>>>,
|
||||
pub usd: LazyVecFrom1<I, Dollars, I, Cents>,
|
||||
}
|
||||
|
||||
impl Value<Height> {
|
||||
impl Amount<Height> {
|
||||
pub(crate) fn forced_import(db: &Database, name: &str, version: Version) -> Result<Self> {
|
||||
let v = version + VERSION;
|
||||
|
||||
let sats: EagerVec<PcoVec<Height, Sats>> = EagerVec::forced_import(db, name, v)?;
|
||||
let btc = LazyVecFrom1::transformed::<SatsToBitcoin>(
|
||||
&format!("{name}_btc"),
|
||||
v,
|
||||
sats.read_only_boxed_clone(),
|
||||
);
|
||||
let sats: EagerVec<PcoVec<Height, Sats>> =
|
||||
EagerVec::forced_import(db, &format!("{name}_sats"), v)?;
|
||||
let btc = LazyVecFrom1::transformed::<SatsToBitcoin>(name, v, sats.read_only_boxed_clone());
|
||||
let cents: EagerVec<PcoVec<Height, Cents>> =
|
||||
EagerVec::forced_import(db, &format!("{name}_cents"), v)?;
|
||||
let usd = LazyVecFrom1::transformed::<CentsUnsignedToDollars>(
|
||||
+6
-6
@@ -2,20 +2,20 @@ use brk_traversable::Traversable;
|
||||
use brk_types::{Bitcoin, Cents, Dollars, Height, Sats, Version};
|
||||
use vecdb::{LazyVecFrom1, ReadableCloneableVec, UnaryTransform, VecIndex};
|
||||
|
||||
use crate::internal::ValueFromHeight;
|
||||
use crate::internal::AmountFromHeight;
|
||||
|
||||
/// Fully lazy value type at height level.
|
||||
///
|
||||
/// All fields are lazy transforms from existing sources - no storage.
|
||||
#[derive(Clone, Traversable)]
|
||||
pub struct LazyValue<I: VecIndex> {
|
||||
pub struct LazyAmount<I: VecIndex> {
|
||||
pub sats: LazyVecFrom1<I, Sats, I, Sats>,
|
||||
pub btc: LazyVecFrom1<I, Bitcoin, I, Sats>,
|
||||
pub cents: LazyVecFrom1<I, Cents, I, Cents>,
|
||||
pub usd: LazyVecFrom1<I, Dollars, I, Dollars>,
|
||||
}
|
||||
|
||||
impl LazyValue<Height> {
|
||||
impl LazyAmount<Height> {
|
||||
pub(crate) fn from_block_source<
|
||||
SatsTransform,
|
||||
BitcoinTransform,
|
||||
@@ -23,7 +23,7 @@ impl LazyValue<Height> {
|
||||
DollarsTransform,
|
||||
>(
|
||||
name: &str,
|
||||
source: &ValueFromHeight,
|
||||
source: &AmountFromHeight,
|
||||
version: Version,
|
||||
) -> Self
|
||||
where
|
||||
@@ -33,13 +33,13 @@ impl LazyValue<Height> {
|
||||
DollarsTransform: UnaryTransform<Dollars, Dollars>,
|
||||
{
|
||||
let sats = LazyVecFrom1::transformed::<SatsTransform>(
|
||||
name,
|
||||
&format!("{name}_sats"),
|
||||
version,
|
||||
source.sats.height.read_only_boxed_clone(),
|
||||
);
|
||||
|
||||
let btc = LazyVecFrom1::transformed::<BitcoinTransform>(
|
||||
&format!("{name}_btc"),
|
||||
name,
|
||||
version,
|
||||
source.sats.height.read_only_boxed_clone(),
|
||||
);
|
||||
@@ -14,7 +14,7 @@ pub struct DistributionStats<A> {
|
||||
|
||||
impl<A> DistributionStats<A> {
|
||||
pub const SUFFIXES: [&'static str; 8] = [
|
||||
"average", "min", "max", "p10", "p25", "median", "p75", "p90",
|
||||
"average", "min", "max", "pct10", "pct25", "median", "pct75", "pct90",
|
||||
];
|
||||
|
||||
pub fn try_from_fn<E>(
|
||||
|
||||
+6
-6
@@ -2,17 +2,17 @@ use brk_traversable::Traversable;
|
||||
use brk_types::{Bitcoin, Cents, Dollars, Sats, Version};
|
||||
use vecdb::UnaryTransform;
|
||||
|
||||
use crate::internal::{LazyHeightDerived, ValueFromHeight};
|
||||
use crate::internal::{AmountFromHeight, LazyHeightDerived};
|
||||
|
||||
#[derive(Clone, Traversable)]
|
||||
pub struct LazyValueHeightDerived {
|
||||
pub struct LazyAmountHeightDerived {
|
||||
pub sats: LazyHeightDerived<Sats, Sats>,
|
||||
pub btc: LazyHeightDerived<Bitcoin, Sats>,
|
||||
pub cents: LazyHeightDerived<Cents, Cents>,
|
||||
pub usd: LazyHeightDerived<Dollars, Dollars>,
|
||||
}
|
||||
|
||||
impl LazyValueHeightDerived {
|
||||
impl LazyAmountHeightDerived {
|
||||
pub(crate) fn from_block_source<
|
||||
SatsTransform,
|
||||
BitcoinTransform,
|
||||
@@ -20,7 +20,7 @@ impl LazyValueHeightDerived {
|
||||
DollarsTransform,
|
||||
>(
|
||||
name: &str,
|
||||
source: &ValueFromHeight,
|
||||
source: &AmountFromHeight,
|
||||
version: Version,
|
||||
) -> Self
|
||||
where
|
||||
@@ -30,13 +30,13 @@ impl LazyValueHeightDerived {
|
||||
DollarsTransform: UnaryTransform<Dollars, Dollars>,
|
||||
{
|
||||
let sats = LazyHeightDerived::from_derived_computed::<SatsTransform>(
|
||||
name,
|
||||
&format!("{name}_sats"),
|
||||
version,
|
||||
&source.sats.rest,
|
||||
);
|
||||
|
||||
let btc = LazyHeightDerived::from_derived_computed::<BitcoinTransform>(
|
||||
&format!("{name}_btc"),
|
||||
name,
|
||||
version,
|
||||
&source.sats.rest,
|
||||
);
|
||||
@@ -1,13 +1,13 @@
|
||||
mod full;
|
||||
mod last;
|
||||
mod lazy_last;
|
||||
mod lazy_value;
|
||||
mod lazy_amount;
|
||||
mod map_option;
|
||||
mod transform_last;
|
||||
|
||||
pub use full::*;
|
||||
pub use last::*;
|
||||
pub use lazy_last::*;
|
||||
pub use lazy_value::*;
|
||||
pub use lazy_amount::*;
|
||||
pub use map_option::*;
|
||||
pub use transform_last::*;
|
||||
|
||||
+12
-7
@@ -5,19 +5,19 @@ use vecdb::{Database, EagerVec, Exit, PcoVec, Rw, StorageMode};
|
||||
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{ByUnit, SatsToCents},
|
||||
internal::{AmountFromHeight, SatsToCents},
|
||||
prices,
|
||||
};
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct ValueFromHeightCumulative<M: StorageMode = Rw> {
|
||||
pub base: ByUnit<M>,
|
||||
pub cumulative: ByUnit<M>,
|
||||
pub struct AmountFromHeightCumulative<M: StorageMode = Rw> {
|
||||
pub base: AmountFromHeight<M>,
|
||||
pub cumulative: AmountFromHeight<M>,
|
||||
}
|
||||
|
||||
const VERSION: Version = Version::ONE;
|
||||
|
||||
impl ValueFromHeightCumulative {
|
||||
impl AmountFromHeightCumulative {
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
name: &str,
|
||||
@@ -27,8 +27,13 @@ impl ValueFromHeightCumulative {
|
||||
let v = version + VERSION;
|
||||
|
||||
Ok(Self {
|
||||
base: ByUnit::forced_import(db, name, v, indexes)?,
|
||||
cumulative: ByUnit::forced_import(db, &format!("{name}_cumulative"), v, indexes)?,
|
||||
base: AmountFromHeight::forced_import(db, name, v, indexes)?,
|
||||
cumulative: AmountFromHeight::forced_import(
|
||||
db,
|
||||
&format!("{name}_cumulative"),
|
||||
v,
|
||||
indexes,
|
||||
)?,
|
||||
})
|
||||
}
|
||||
|
||||
+14
-9
@@ -5,20 +5,20 @@ use vecdb::{Database, EagerVec, Exit, PcoVec, Rw, StorageMode};
|
||||
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{ByUnit, RollingSumByUnit, SatsToCents, WindowStarts},
|
||||
internal::{AmountFromHeight, RollingSumAmountFromHeight, SatsToCents, WindowStarts},
|
||||
prices,
|
||||
};
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct ValueFromHeightCumulativeSum<M: StorageMode = Rw> {
|
||||
pub base: ByUnit<M>,
|
||||
pub cumulative: ByUnit<M>,
|
||||
pub sum: RollingSumByUnit<M>,
|
||||
pub struct AmountFromHeightCumulativeSum<M: StorageMode = Rw> {
|
||||
pub base: AmountFromHeight<M>,
|
||||
pub cumulative: AmountFromHeight<M>,
|
||||
pub sum: RollingSumAmountFromHeight<M>,
|
||||
}
|
||||
|
||||
const VERSION: Version = Version::TWO;
|
||||
|
||||
impl ValueFromHeightCumulativeSum {
|
||||
impl AmountFromHeightCumulativeSum {
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
name: &str,
|
||||
@@ -28,9 +28,14 @@ impl ValueFromHeightCumulativeSum {
|
||||
let v = version + VERSION;
|
||||
|
||||
Ok(Self {
|
||||
base: ByUnit::forced_import(db, name, v, indexes)?,
|
||||
cumulative: ByUnit::forced_import(db, &format!("{name}_cumulative"), v, indexes)?,
|
||||
sum: RollingSumByUnit::forced_import(db, name, v, indexes)?,
|
||||
base: AmountFromHeight::forced_import(db, name, v, indexes)?,
|
||||
cumulative: AmountFromHeight::forced_import(
|
||||
db,
|
||||
&format!("{name}_cumulative"),
|
||||
v,
|
||||
indexes,
|
||||
)?,
|
||||
sum: RollingSumAmountFromHeight::forced_import(db, name, v, indexes)?,
|
||||
})
|
||||
}
|
||||
|
||||
+14
-9
@@ -5,21 +5,21 @@ use vecdb::{Database, EagerVec, Exit, PcoVec, Rw, StorageMode};
|
||||
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{ByUnit, RollingFullByUnit, SatsToCents, WindowStarts},
|
||||
internal::{AmountFromHeight, RollingFullAmountFromHeight, SatsToCents, WindowStarts},
|
||||
prices,
|
||||
};
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct ValueFromHeightFull<M: StorageMode = Rw> {
|
||||
pub base: ByUnit<M>,
|
||||
pub cumulative: ByUnit<M>,
|
||||
pub struct AmountFromHeightFull<M: StorageMode = Rw> {
|
||||
pub base: AmountFromHeight<M>,
|
||||
pub cumulative: AmountFromHeight<M>,
|
||||
#[traversable(flatten)]
|
||||
pub rolling: RollingFullByUnit<M>,
|
||||
pub rolling: RollingFullAmountFromHeight<M>,
|
||||
}
|
||||
|
||||
const VERSION: Version = Version::TWO;
|
||||
|
||||
impl ValueFromHeightFull {
|
||||
impl AmountFromHeightFull {
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
name: &str,
|
||||
@@ -29,9 +29,14 @@ impl ValueFromHeightFull {
|
||||
let v = version + VERSION;
|
||||
|
||||
Ok(Self {
|
||||
base: ByUnit::forced_import(db, name, v, indexes)?,
|
||||
cumulative: ByUnit::forced_import(db, &format!("{name}_cumulative"), v, indexes)?,
|
||||
rolling: RollingFullByUnit::forced_import(db, name, v, indexes)?,
|
||||
base: AmountFromHeight::forced_import(db, name, v, indexes)?,
|
||||
cumulative: AmountFromHeight::forced_import(
|
||||
db,
|
||||
&format!("{name}_cumulative"),
|
||||
v,
|
||||
indexes,
|
||||
)?,
|
||||
rolling: RollingFullAmountFromHeight::forced_import(db, name, v, indexes)?,
|
||||
})
|
||||
}
|
||||
|
||||
+10
-10
@@ -1,25 +1,25 @@
|
||||
//! Lazy value wrapper for ValueFromHeight - all transforms are lazy.
|
||||
//! Lazy value wrapper for AmountFromHeight - all transforms are lazy.
|
||||
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{Bitcoin, Cents, Dollars, Height, Sats, Version};
|
||||
use derive_more::{Deref, DerefMut};
|
||||
use vecdb::UnaryTransform;
|
||||
|
||||
use crate::internal::{LazyValue, LazyValueHeightDerived, ValueFromHeight};
|
||||
use crate::internal::{AmountFromHeight, LazyAmount, LazyAmountHeightDerived};
|
||||
|
||||
/// Lazy value wrapper with height + all derived last transforms from ValueFromHeight.
|
||||
/// Lazy value wrapper with height + all derived last transforms from AmountFromHeight.
|
||||
#[derive(Clone, Deref, DerefMut, Traversable)]
|
||||
#[traversable(merge)]
|
||||
pub struct LazyValueFromHeight {
|
||||
pub struct LazyAmountFromHeight {
|
||||
#[traversable(flatten)]
|
||||
pub height: LazyValue<Height>,
|
||||
pub height: LazyAmount<Height>,
|
||||
#[deref]
|
||||
#[deref_mut]
|
||||
#[traversable(flatten)]
|
||||
pub rest: Box<LazyValueHeightDerived>,
|
||||
pub rest: Box<LazyAmountHeightDerived>,
|
||||
}
|
||||
|
||||
impl LazyValueFromHeight {
|
||||
impl LazyAmountFromHeight {
|
||||
pub(crate) fn from_block_source<
|
||||
SatsTransform,
|
||||
BitcoinTransform,
|
||||
@@ -27,7 +27,7 @@ impl LazyValueFromHeight {
|
||||
DollarsTransform,
|
||||
>(
|
||||
name: &str,
|
||||
source: &ValueFromHeight,
|
||||
source: &AmountFromHeight,
|
||||
version: Version,
|
||||
) -> Self
|
||||
where
|
||||
@@ -36,14 +36,14 @@ impl LazyValueFromHeight {
|
||||
CentsTransform: UnaryTransform<Cents, Cents>,
|
||||
DollarsTransform: UnaryTransform<Dollars, Dollars>,
|
||||
{
|
||||
let height = LazyValue::from_block_source::<
|
||||
let height = LazyAmount::from_block_source::<
|
||||
SatsTransform,
|
||||
BitcoinTransform,
|
||||
CentsTransform,
|
||||
DollarsTransform,
|
||||
>(name, source, version);
|
||||
|
||||
let rest = LazyValueHeightDerived::from_block_source::<
|
||||
let rest = LazyAmountHeightDerived::from_block_source::<
|
||||
SatsTransform,
|
||||
BitcoinTransform,
|
||||
CentsTransform,
|
||||
@@ -0,0 +1,122 @@
|
||||
mod cumulative;
|
||||
mod cumulative_sum;
|
||||
mod full;
|
||||
mod lazy;
|
||||
mod rolling;
|
||||
mod rolling_full;
|
||||
mod rolling_sum;
|
||||
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{Bitcoin, Cents, Dollars, Height, Sats, Version};
|
||||
use vecdb::{AnyVec, Database, Exit, ReadableCloneableVec, ReadableVec, Rw, StorageMode};
|
||||
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{
|
||||
CentsUnsignedToDollars, ComputedFromHeight, LazyFromHeight, SatsToBitcoin, SatsToCents,
|
||||
Windows,
|
||||
},
|
||||
prices,
|
||||
};
|
||||
|
||||
pub use cumulative::*;
|
||||
pub use cumulative_sum::*;
|
||||
pub use full::*;
|
||||
pub use lazy::*;
|
||||
pub use rolling::*;
|
||||
pub use rolling_full::*;
|
||||
pub use rolling_sum::*;
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct AmountFromHeight<M: StorageMode = Rw> {
|
||||
pub sats: ComputedFromHeight<Sats, M>,
|
||||
pub btc: LazyFromHeight<Bitcoin, Sats>,
|
||||
pub cents: ComputedFromHeight<Cents, M>,
|
||||
pub usd: LazyFromHeight<Dollars, Cents>,
|
||||
}
|
||||
|
||||
impl AmountFromHeight {
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
name: &str,
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
let sats =
|
||||
ComputedFromHeight::forced_import(db, &format!("{name}_sats"), version, indexes)?;
|
||||
|
||||
let btc = LazyFromHeight::from_computed::<SatsToBitcoin>(
|
||||
name,
|
||||
version,
|
||||
sats.height.read_only_boxed_clone(),
|
||||
&sats,
|
||||
);
|
||||
|
||||
let cents =
|
||||
ComputedFromHeight::forced_import(db, &format!("{name}_cents"), version, indexes)?;
|
||||
|
||||
let usd = LazyFromHeight::from_computed::<CentsUnsignedToDollars>(
|
||||
&format!("{name}_usd"),
|
||||
version,
|
||||
cents.height.read_only_boxed_clone(),
|
||||
¢s,
|
||||
);
|
||||
|
||||
Ok(Self {
|
||||
sats,
|
||||
btc,
|
||||
cents,
|
||||
usd,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn min_stateful_len(&self) -> usize {
|
||||
self.sats.height.len()
|
||||
}
|
||||
|
||||
pub(crate) fn compute(
|
||||
&mut self,
|
||||
prices: &prices::Vecs,
|
||||
max_from: Height,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.cents.compute_binary::<Sats, Cents, SatsToCents>(
|
||||
max_from,
|
||||
&self.sats.height,
|
||||
&prices.price.cents.height,
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn compute_rolling_sum(
|
||||
&mut self,
|
||||
max_from: Height,
|
||||
window_starts: &impl ReadableVec<Height, Height>,
|
||||
sats_source: &impl ReadableVec<Height, Sats>,
|
||||
cents_source: &impl ReadableVec<Height, Cents>,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.sats
|
||||
.height
|
||||
.compute_rolling_sum(max_from, window_starts, sats_source, exit)?;
|
||||
self.cents
|
||||
.height
|
||||
.compute_rolling_sum(max_from, window_starts, cents_source, exit)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Windows<AmountFromHeight> {
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
name: &str,
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
Windows::try_from_fn(|suffix| {
|
||||
AmountFromHeight::forced_import(db, &format!("{name}_{suffix}"), version, indexes)
|
||||
})
|
||||
}
|
||||
}
|
||||
+12
-12
@@ -1,7 +1,7 @@
|
||||
//! Value type for Height + Rolling pattern.
|
||||
//!
|
||||
//! Combines Value (sats/btc/usd per height, no period views) with
|
||||
//! ValueFromHeightWindows (rolling sums across 4 windows).
|
||||
//! AmountFromHeightWindows (rolling sums across 4 windows).
|
||||
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
@@ -11,21 +11,21 @@ use vecdb::{Database, EagerVec, Exit, PcoVec, Rw, StorageMode};
|
||||
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{Value, ValueFromHeightWindows, WindowStarts},
|
||||
internal::{Amount, AmountFromHeightWindows, WindowStarts},
|
||||
prices,
|
||||
};
|
||||
|
||||
#[derive(Deref, DerefMut, Traversable)]
|
||||
pub struct ValueFromHeightRolling<M: StorageMode = Rw> {
|
||||
pub struct AmountFromHeightRolling<M: StorageMode = Rw> {
|
||||
#[deref]
|
||||
#[deref_mut]
|
||||
#[traversable(flatten)]
|
||||
pub value: Value<Height, M>,
|
||||
pub amount: Amount<Height, M>,
|
||||
#[traversable(flatten)]
|
||||
pub rolling: ValueFromHeightWindows<M>,
|
||||
pub rolling: AmountFromHeightWindows<M>,
|
||||
}
|
||||
|
||||
impl ValueFromHeightRolling {
|
||||
impl AmountFromHeightRolling {
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
name: &str,
|
||||
@@ -33,8 +33,8 @@ impl ValueFromHeightRolling {
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
Ok(Self {
|
||||
value: Value::forced_import(db, name, version)?,
|
||||
rolling: ValueFromHeightWindows::forced_import(db, name, version, indexes)?,
|
||||
amount: Amount::forced_import(db, name, version)?,
|
||||
rolling: AmountFromHeightWindows::forced_import(db, name, version, indexes)?,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -47,13 +47,13 @@ impl ValueFromHeightRolling {
|
||||
exit: &Exit,
|
||||
compute_sats: impl FnOnce(&mut EagerVec<PcoVec<Height, Sats>>) -> Result<()>,
|
||||
) -> Result<()> {
|
||||
compute_sats(&mut self.value.sats)?;
|
||||
self.value.compute_cents(prices, max_from, exit)?;
|
||||
compute_sats(&mut self.amount.sats)?;
|
||||
self.amount.compute_cents(prices, max_from, exit)?;
|
||||
self.rolling.compute_rolling_sum(
|
||||
max_from,
|
||||
windows,
|
||||
&self.value.sats,
|
||||
&self.value.cents,
|
||||
&self.amount.sats,
|
||||
&self.amount.cents,
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
+8
-8
@@ -7,18 +7,18 @@ use vecdb::{Database, Exit, ReadableVec, Rw, StorageMode};
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{
|
||||
ByUnit, DistributionStats, WindowStarts, Windows, compute_rolling_distribution_from_starts,
|
||||
AmountFromHeight, DistributionStats, WindowStarts, Windows, compute_rolling_distribution_from_starts,
|
||||
},
|
||||
};
|
||||
|
||||
/// One window slot: sum + 8 distribution stats, each a ByUnit.
|
||||
/// One window slot: sum + 8 distribution stats, each a AmountFromHeight.
|
||||
///
|
||||
/// Tree: `sum.sats.height`, `average.sats.height`, etc.
|
||||
#[derive(Traversable)]
|
||||
pub struct RollingFullSlot<M: StorageMode = Rw> {
|
||||
pub sum: ByUnit<M>,
|
||||
pub sum: AmountFromHeight<M>,
|
||||
#[traversable(flatten)]
|
||||
pub distribution: DistributionStats<ByUnit<M>>,
|
||||
pub distribution: DistributionStats<AmountFromHeight<M>>,
|
||||
}
|
||||
|
||||
impl RollingFullSlot {
|
||||
@@ -29,9 +29,9 @@ impl RollingFullSlot {
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
Ok(Self {
|
||||
sum: ByUnit::forced_import(db, &format!("{name}_sum"), version, indexes)?,
|
||||
sum: AmountFromHeight::forced_import(db, &format!("{name}_sum"), version, indexes)?,
|
||||
distribution: DistributionStats::try_from_fn(|suffix| {
|
||||
ByUnit::forced_import(db, &format!("{name}_{suffix}"), version, indexes)
|
||||
AmountFromHeight::forced_import(db, &format!("{name}_{suffix}"), version, indexes)
|
||||
})?,
|
||||
})
|
||||
}
|
||||
@@ -85,9 +85,9 @@ impl RollingFullSlot {
|
||||
/// Tree: `_24h.sum.sats.height`, `_24h.average.sats.height`, etc.
|
||||
#[derive(Deref, DerefMut, Traversable)]
|
||||
#[traversable(transparent)]
|
||||
pub struct RollingFullByUnit<M: StorageMode = Rw>(pub Windows<RollingFullSlot<M>>);
|
||||
pub struct RollingFullAmountFromHeight<M: StorageMode = Rw>(pub Windows<RollingFullSlot<M>>);
|
||||
|
||||
impl RollingFullByUnit {
|
||||
impl RollingFullAmountFromHeight {
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
name: &str,
|
||||
+4
-4
@@ -6,7 +6,7 @@ use vecdb::{Database, Exit, ReadableVec, Rw, StorageMode};
|
||||
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{ByUnit, WindowStarts, Windows},
|
||||
internal::{AmountFromHeight, WindowStarts, Windows},
|
||||
};
|
||||
|
||||
/// Rolling sum only, window-first then unit.
|
||||
@@ -14,16 +14,16 @@ use crate::{
|
||||
/// Tree: `_24h.sats.height`, `_24h.btc.height`, etc.
|
||||
#[derive(Deref, DerefMut, Traversable)]
|
||||
#[traversable(transparent)]
|
||||
pub struct RollingSumByUnit<M: StorageMode = Rw>(pub Windows<ByUnit<M>>);
|
||||
pub struct RollingSumAmountFromHeight<M: StorageMode = Rw>(pub Windows<AmountFromHeight<M>>);
|
||||
|
||||
impl RollingSumByUnit {
|
||||
impl RollingSumAmountFromHeight {
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
name: &str,
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
Ok(Self(Windows::<ByUnit>::forced_import(
|
||||
Ok(Self(Windows::<AmountFromHeight>::forced_import(
|
||||
db,
|
||||
&format!("{name}_sum"),
|
||||
version,
|
||||
@@ -1,77 +0,0 @@
|
||||
mod rolling_full;
|
||||
mod rolling_sum;
|
||||
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{Bitcoin, Cents, Dollars, Sats, Version};
|
||||
use vecdb::{AnyVec, Database, ReadableCloneableVec, Rw, StorageMode};
|
||||
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{
|
||||
CentsUnsignedToDollars, ComputedFromHeight, LazyFromHeight, SatsToBitcoin, Windows,
|
||||
},
|
||||
};
|
||||
|
||||
pub use rolling_full::*;
|
||||
pub use rolling_sum::*;
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct ByUnit<M: StorageMode = Rw> {
|
||||
pub sats: ComputedFromHeight<Sats, M>,
|
||||
pub btc: LazyFromHeight<Bitcoin, Sats>,
|
||||
pub cents: ComputedFromHeight<Cents, M>,
|
||||
pub usd: LazyFromHeight<Dollars, Cents>,
|
||||
}
|
||||
|
||||
impl ByUnit {
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
name: &str,
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
let sats = ComputedFromHeight::forced_import(db, name, version, indexes)?;
|
||||
|
||||
let btc = LazyFromHeight::from_computed::<SatsToBitcoin>(
|
||||
&format!("{name}_btc"),
|
||||
version,
|
||||
sats.height.read_only_boxed_clone(),
|
||||
&sats,
|
||||
);
|
||||
|
||||
let cents =
|
||||
ComputedFromHeight::forced_import(db, &format!("{name}_cents"), version, indexes)?;
|
||||
|
||||
let usd = LazyFromHeight::from_computed::<CentsUnsignedToDollars>(
|
||||
&format!("{name}_usd"),
|
||||
version,
|
||||
cents.height.read_only_boxed_clone(),
|
||||
¢s,
|
||||
);
|
||||
|
||||
Ok(Self {
|
||||
sats,
|
||||
btc,
|
||||
cents,
|
||||
usd,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn min_stateful_len(&self) -> usize {
|
||||
self.sats.height.len()
|
||||
}
|
||||
}
|
||||
|
||||
impl Windows<ByUnit> {
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
name: &str,
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
Windows::try_from_fn(|suffix| {
|
||||
ByUnit::forced_import(db, &format!("{name}_{suffix}"), version, indexes)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -41,7 +41,7 @@ impl<C: CentsType> FiatFromHeight<C> {
|
||||
let cents =
|
||||
ComputedFromHeight::forced_import(db, &format!("{name}_cents"), version, indexes)?;
|
||||
let usd = LazyFromHeight::from_computed::<C::ToDollars>(
|
||||
&format!("{name}_usd"),
|
||||
name,
|
||||
version,
|
||||
cents.height.read_only_boxed_clone(),
|
||||
¢s,
|
||||
|
||||
@@ -31,7 +31,7 @@ impl<C: CentsType> LazyFiatFromHeight<C> {
|
||||
source,
|
||||
);
|
||||
let usd = LazyFromHeight::from_computed::<C::ToDollars>(
|
||||
&format!("{name}_usd"),
|
||||
name,
|
||||
version,
|
||||
source.height.read_only_boxed_clone(),
|
||||
source,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
mod base;
|
||||
mod by_unit;
|
||||
mod amount;
|
||||
mod computed;
|
||||
mod constant;
|
||||
mod fiat;
|
||||
@@ -10,10 +10,9 @@ mod percentiles;
|
||||
mod price;
|
||||
mod ratio;
|
||||
mod stddev;
|
||||
mod value;
|
||||
|
||||
pub use base::*;
|
||||
pub use by_unit::*;
|
||||
pub use amount::*;
|
||||
pub use computed::*;
|
||||
pub use constant::*;
|
||||
pub use fiat::*;
|
||||
@@ -24,4 +23,3 @@ pub use percentiles::*;
|
||||
pub use price::*;
|
||||
pub use ratio::*;
|
||||
pub use stddev::*;
|
||||
pub use value::*;
|
||||
|
||||
@@ -91,14 +91,14 @@ impl ComputedFromHeightRatioPercentiles {
|
||||
) -> Result<()> {
|
||||
self.ratio_sma_1w.bps.height.compute_rolling_average(
|
||||
starting_indexes.height,
|
||||
&blocks.count.height_1w_ago,
|
||||
&blocks.lookback.height_1w_ago,
|
||||
ratio_source,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.ratio_sma_1m.bps.height.compute_rolling_average(
|
||||
starting_indexes.height,
|
||||
&blocks.count.height_1m_ago,
|
||||
&blocks.lookback.height_1m_ago,
|
||||
ratio_source,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
@@ -65,7 +65,7 @@ impl ComputedFromHeightStdDev {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let window_starts = blocks.count.start_vec(self.days);
|
||||
let window_starts = blocks.lookback.start_vec(self.days);
|
||||
|
||||
self.sma.height.compute_rolling_average(
|
||||
starting_indexes.height,
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{Cents, Height, Sats, Version};
|
||||
use derive_more::{Deref, DerefMut};
|
||||
use vecdb::{Database, Exit, ReadableVec, Rw, StorageMode};
|
||||
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{ByUnit, SatsToCents},
|
||||
prices,
|
||||
};
|
||||
|
||||
#[derive(Deref, DerefMut, Traversable)]
|
||||
#[traversable(transparent)]
|
||||
pub struct ValueFromHeight<M: StorageMode = Rw> {
|
||||
#[deref]
|
||||
#[deref_mut]
|
||||
pub base: ByUnit<M>,
|
||||
}
|
||||
|
||||
const VERSION: Version = Version::TWO;
|
||||
|
||||
impl ValueFromHeight {
|
||||
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(
|
||||
&mut self,
|
||||
prices: &prices::Vecs,
|
||||
max_from: Height,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.base.cents.compute_binary::<Sats, Cents, SatsToCents>(
|
||||
max_from,
|
||||
&self.base.sats.height,
|
||||
&prices.price.cents.height,
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn compute_rolling_sum(
|
||||
&mut self,
|
||||
max_from: Height,
|
||||
window_starts: &impl ReadableVec<Height, Height>,
|
||||
sats_source: &impl ReadableVec<Height, Sats>,
|
||||
cents_source: &impl ReadableVec<Height, Cents>,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.base
|
||||
.sats
|
||||
.height
|
||||
.compute_rolling_sum(max_from, window_starts, sats_source, exit)?;
|
||||
self.base
|
||||
.cents
|
||||
.height
|
||||
.compute_rolling_sum(max_from, window_starts, cents_source, exit)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
mod base;
|
||||
mod cumulative;
|
||||
mod cumulative_sum;
|
||||
mod full;
|
||||
mod lazy;
|
||||
mod rolling;
|
||||
|
||||
pub use base::*;
|
||||
pub use cumulative::*;
|
||||
pub use cumulative_sum::*;
|
||||
pub use full::*;
|
||||
pub use lazy::*;
|
||||
pub use rolling::*;
|
||||
@@ -9,7 +9,7 @@ mod indexes;
|
||||
mod rolling;
|
||||
mod traits;
|
||||
mod transform;
|
||||
mod value;
|
||||
mod amount;
|
||||
|
||||
pub(crate) use aggregate::*;
|
||||
pub(crate) use algo::*;
|
||||
@@ -22,4 +22,4 @@ pub(crate) use indexes::*;
|
||||
pub(crate) use rolling::*;
|
||||
pub(crate) use traits::*;
|
||||
pub use transform::*;
|
||||
pub(crate) use value::*;
|
||||
pub(crate) use amount::*;
|
||||
|
||||
+6
-6
@@ -1,4 +1,4 @@
|
||||
//! ValueFromHeightWindows - window-first ordering.
|
||||
//! AmountFromHeightWindows - window-first ordering.
|
||||
//!
|
||||
//! Access pattern: `coinbase_sum._24h.sats.height`
|
||||
//! Each window (24h, 7d, 30d, 1y) contains sats (stored) + btc (lazy) + usd (stored).
|
||||
@@ -14,17 +14,17 @@ use brk_types::{Cents, Sats};
|
||||
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{ValueFromHeight, WindowStarts, Windows},
|
||||
internal::{AmountFromHeight, WindowStarts, Windows},
|
||||
};
|
||||
|
||||
/// Value rolling windows — window-first, currency-last.
|
||||
///
|
||||
/// Each window contains `ValueFromHeight` (sats + btc lazy + usd).
|
||||
/// Each window contains `AmountFromHeight` (sats + btc lazy + usd).
|
||||
#[derive(Deref, DerefMut, Traversable)]
|
||||
#[traversable(transparent)]
|
||||
pub struct ValueFromHeightWindows<M: StorageMode = Rw>(pub Windows<ValueFromHeight<M>>);
|
||||
pub struct AmountFromHeightWindows<M: StorageMode = Rw>(pub Windows<AmountFromHeight<M>>);
|
||||
|
||||
impl ValueFromHeightWindows {
|
||||
impl AmountFromHeightWindows {
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
name: &str,
|
||||
@@ -32,7 +32,7 @@ impl ValueFromHeightWindows {
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
Ok(Self(Windows::try_from_fn(|suffix| {
|
||||
ValueFromHeight::forced_import(db, &format!("{name}_{suffix}"), version, indexes)
|
||||
AmountFromHeight::forced_import(db, &format!("{name}_{suffix}"), version, indexes)
|
||||
})?))
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
mod distribution;
|
||||
mod full;
|
||||
mod percent_windows;
|
||||
mod value_windows;
|
||||
mod amount_windows;
|
||||
mod windows;
|
||||
|
||||
pub use distribution::*;
|
||||
pub use full::*;
|
||||
pub use percent_windows::*;
|
||||
pub use value_windows::*;
|
||||
pub use amount_windows::*;
|
||||
pub use windows::*;
|
||||
|
||||
Reference in New Issue
Block a user