mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-24 22:59:58 -07:00
global: snapshot
This commit is contained in:
@@ -70,7 +70,9 @@ where
|
||||
}};
|
||||
}
|
||||
|
||||
let index = validate_vec!(first, last, min, max, average, sum, cumulative, median, pct10, pct25, pct75, pct90);
|
||||
let index = validate_vec!(
|
||||
first, last, min, max, average, sum, cumulative, median, pct10, pct25, pct75, pct90
|
||||
);
|
||||
|
||||
let needs_first = first.is_some();
|
||||
let needs_last = last.is_some();
|
||||
@@ -298,7 +300,9 @@ where
|
||||
};
|
||||
}
|
||||
|
||||
write_vec!(first, last, min, max, average, sum, cumulative, median, pct10, pct25, pct75, pct90);
|
||||
write_vec!(
|
||||
first, last, min, max, average, sum, cumulative, median, pct10, pct25, pct75, pct90
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -306,7 +310,7 @@ where
|
||||
/// Compute cumulative extension from a source vec.
|
||||
///
|
||||
/// Used when only cumulative needs to be extended from an existing source.
|
||||
pub fn compute_cumulative_extend<I, T>(
|
||||
pub fn compute_cumulative<I, T>(
|
||||
max_from: I,
|
||||
source: &impl IterableVec<I, T>,
|
||||
cumulative: &mut EagerVec<PcoVec<I, T>>,
|
||||
@@ -340,6 +344,47 @@ where
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Compute cumulative from binary transform of two source vecs.
|
||||
pub fn compute_cumulative_transform2<I, T, S1, S2, F>(
|
||||
max_from: I,
|
||||
source1: &impl IterableVec<I, S1>,
|
||||
source2: &impl IterableVec<I, S2>,
|
||||
cumulative: &mut EagerVec<PcoVec<I, T>>,
|
||||
transform: F,
|
||||
exit: &Exit,
|
||||
) -> Result<()>
|
||||
where
|
||||
I: VecIndex,
|
||||
T: ComputedVecValue + JsonSchema,
|
||||
S1: VecValue,
|
||||
S2: VecValue,
|
||||
F: Fn(S1, S2) -> T,
|
||||
{
|
||||
let combined_version = source1.version() + source2.version();
|
||||
cumulative.validate_computed_version_or_reset(combined_version)?;
|
||||
|
||||
let index = max_from.min(I::from(cumulative.len()));
|
||||
let target_len = source1.len().min(source2.len());
|
||||
|
||||
let mut cumulative_val = index
|
||||
.decremented()
|
||||
.map_or(T::from(0_usize), |idx| cumulative.read_unwrap_once(idx));
|
||||
|
||||
let mut iter1 = source1.iter();
|
||||
let mut iter2 = source2.iter();
|
||||
|
||||
for i in index.to_usize()..target_len {
|
||||
let idx = I::from(i);
|
||||
cumulative_val += transform(iter1.get_unwrap(idx), iter2.get_unwrap(idx));
|
||||
cumulative.truncate_push_at(i, cumulative_val)?;
|
||||
}
|
||||
|
||||
let _lock = exit.lock();
|
||||
cumulative.write()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Compute coarser aggregations from already-aggregated source data.
|
||||
///
|
||||
/// This is used for dateindex → weekindex, monthindex, etc. where we derive
|
||||
|
||||
@@ -12,15 +12,14 @@ use brk_types::{
|
||||
};
|
||||
use derive_more::{Deref, DerefMut};
|
||||
use vecdb::{
|
||||
AnyStoredVec, AnyVec, Database, EagerVec, Exit, GenericStoredVec, ImportableVec,
|
||||
IterableBoxedVec, IterableCloneableVec, IterableVec, LazyVecFrom3,
|
||||
Database, EagerVec, Exit, ImportableVec, IterableBoxedVec, IterableCloneableVec, LazyVecFrom3,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
ComputeIndexes, indexes,
|
||||
internal::{
|
||||
CumulativeVec, Full, LazyBinaryTransformFull, LazyDateDerivedFull, LazyFull,
|
||||
SatsTimesClosePrice, Stats,
|
||||
SatsTimesClosePrice, Stats, compute_cumulative,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -137,7 +136,12 @@ impl ValueDollarsFromTxFull {
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
// Compute height cumulative by summing lazy height.sum values
|
||||
self.compute_height_cumulative(starting_indexes.height, exit)?;
|
||||
compute_cumulative(
|
||||
starting_indexes.height,
|
||||
&self.height.sum,
|
||||
&mut self.height_cumulative.0,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// Compute dateindex stats by aggregating lazy height stats
|
||||
self.dateindex.compute(
|
||||
@@ -150,30 +154,6 @@ impl ValueDollarsFromTxFull {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Compute cumulative USD by summing `sum_sats[h] * price[h]` for all heights.
|
||||
fn compute_height_cumulative(&mut self, max_from: Height, exit: &Exit) -> Result<()> {
|
||||
let starting_height = max_from.min(Height::from(self.height_cumulative.0.len()));
|
||||
|
||||
let mut cumulative = starting_height.decremented().map_or(Dollars::ZERO, |h| {
|
||||
self.height_cumulative.0.iter().get_unwrap(h)
|
||||
});
|
||||
|
||||
let mut sum_iter = self.height.sum.iter();
|
||||
let start_idx = *starting_height as usize;
|
||||
let end_idx = sum_iter.len();
|
||||
|
||||
for h in start_idx..end_idx {
|
||||
let sum_usd = sum_iter.get_unwrap(Height::from(h));
|
||||
cumulative += sum_usd;
|
||||
self.height_cumulative.0.truncate_push_at(h, cumulative)?;
|
||||
}
|
||||
|
||||
let _lock = exit.lock();
|
||||
self.height_cumulative.0.write()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn create_lazy_txindex(
|
||||
|
||||
@@ -11,8 +11,8 @@ use vecdb::{Database, Exit, IterableBoxedVec, IterableCloneableVec, IterableVec}
|
||||
use crate::{
|
||||
ComputeIndexes, indexes,
|
||||
internal::{
|
||||
ComputedVecValue, CumulativeVec, LazyDateDerivedFull, Full, LazyFull, NumericValue,
|
||||
compute_cumulative_extend,
|
||||
ComputedVecValue, CumulativeVec, Full, LazyDateDerivedFull, LazyFull, NumericValue,
|
||||
compute_cumulative,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -102,6 +102,6 @@ where
|
||||
height_source: &impl IterableVec<Height, T>,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
compute_cumulative_extend(max_from, height_source, &mut self.height_cumulative.0, exit)
|
||||
compute_cumulative(max_from, height_source, &mut self.height_cumulative.0, exit)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ use crate::{
|
||||
ComputeIndexes, indexes,
|
||||
internal::{
|
||||
ComputedVecValue, CumulativeVec, LazyDateDerivedSumCum, LazySumCum, NumericValue, SumCum,
|
||||
compute_cumulative_extend,
|
||||
compute_cumulative,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -99,7 +99,7 @@ where
|
||||
source: &impl IterableVec<Height, T>,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
compute_cumulative_extend(max_from, source, &mut self.height_cumulative.0, exit)
|
||||
compute_cumulative(max_from, source, &mut self.height_cumulative.0, exit)
|
||||
}
|
||||
|
||||
fn compute_dateindex_sum_cum(
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use schemars::JsonSchema;
|
||||
use vecdb::{AnyVec, Database, Exit, IterableBoxedVec, IterableCloneableVec, IterableVec, VecIndex, VecValue, Version};
|
||||
use vecdb::{
|
||||
AnyVec, Database, Exit, IterableBoxedVec, IterableCloneableVec, IterableVec, VecIndex,
|
||||
VecValue, Version,
|
||||
};
|
||||
|
||||
use crate::internal::{ComputedVecValue, CumulativeVec, SumVec};
|
||||
|
||||
@@ -48,7 +51,7 @@ impl<I: VecIndex, T: ComputedVecValue + JsonSchema> SumCum<I, T> {
|
||||
first_indexes,
|
||||
count_indexes,
|
||||
exit,
|
||||
0, // min_skip_count
|
||||
0, // min_skip_count
|
||||
None, // first
|
||||
None, // last
|
||||
None, // min
|
||||
@@ -64,16 +67,6 @@ impl<I: VecIndex, T: ComputedVecValue + JsonSchema> SumCum<I, T> {
|
||||
)
|
||||
}
|
||||
|
||||
/// Extend cumulative from an existing source vec.
|
||||
pub fn extend_cumulative(
|
||||
&mut self,
|
||||
max_from: I,
|
||||
source: &impl IterableVec<I, T>,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
crate::internal::compute_cumulative_extend(max_from, source, &mut self.cumulative.0, exit)
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.sum.0.len().min(self.cumulative.0.len())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user