mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-07-14 12:38:13 -07:00
global: snapshot
This commit is contained in:
@@ -60,3 +60,127 @@ where
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Single 24h rolling window (1 stored vec).
|
||||
#[derive(Traversable)]
|
||||
pub struct RollingWindow24h<T, M: StorageMode = Rw>
|
||||
where
|
||||
T: ComputedVecValue + PartialOrd + JsonSchema,
|
||||
{
|
||||
#[traversable(rename = "24h")]
|
||||
pub _24h: ComputedFromHeight<T, M>,
|
||||
}
|
||||
|
||||
impl<T> RollingWindow24h<T>
|
||||
where
|
||||
T: NumericValue + JsonSchema,
|
||||
{
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
name: &str,
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
Ok(Self {
|
||||
_24h: ComputedFromHeight::forced_import(
|
||||
db,
|
||||
&format!("{name}_24h"),
|
||||
version,
|
||||
indexes,
|
||||
)?,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn compute_rolling_sum(
|
||||
&mut self,
|
||||
max_from: Height,
|
||||
height_24h_ago: &impl ReadableVec<Height, Height>,
|
||||
source: &impl ReadableVec<Height, T>,
|
||||
exit: &Exit,
|
||||
) -> Result<()>
|
||||
where
|
||||
T: Default + SubAssign,
|
||||
{
|
||||
self._24h
|
||||
.height
|
||||
.compute_rolling_sum(max_from, height_24h_ago, source, exit)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Extended rolling windows: 1w + 1m + 1y (3 stored vecs).
|
||||
#[derive(Traversable)]
|
||||
pub struct RollingWindowsFrom1w<T, M: StorageMode = Rw>
|
||||
where
|
||||
T: ComputedVecValue + PartialOrd + JsonSchema,
|
||||
{
|
||||
#[traversable(rename = "1w")]
|
||||
pub _1w: ComputedFromHeight<T, M>,
|
||||
#[traversable(rename = "1m")]
|
||||
pub _1m: ComputedFromHeight<T, M>,
|
||||
#[traversable(rename = "1y")]
|
||||
pub _1y: ComputedFromHeight<T, M>,
|
||||
}
|
||||
|
||||
impl<T> RollingWindowsFrom1w<T>
|
||||
where
|
||||
T: NumericValue + JsonSchema,
|
||||
{
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
name: &str,
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
Ok(Self {
|
||||
_1w: ComputedFromHeight::forced_import(
|
||||
db,
|
||||
&format!("{name}_1w"),
|
||||
version,
|
||||
indexes,
|
||||
)?,
|
||||
_1m: ComputedFromHeight::forced_import(
|
||||
db,
|
||||
&format!("{name}_1m"),
|
||||
version,
|
||||
indexes,
|
||||
)?,
|
||||
_1y: ComputedFromHeight::forced_import(
|
||||
db,
|
||||
&format!("{name}_1y"),
|
||||
version,
|
||||
indexes,
|
||||
)?,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn as_array(&self) -> [&ComputedFromHeight<T>; 3] {
|
||||
[&self._1w, &self._1m, &self._1y]
|
||||
}
|
||||
|
||||
pub fn as_mut_array(&mut self) -> [&mut ComputedFromHeight<T>; 3] {
|
||||
[&mut self._1w, &mut self._1m, &mut self._1y]
|
||||
}
|
||||
|
||||
pub(crate) fn compute_rolling_sum(
|
||||
&mut self,
|
||||
max_from: Height,
|
||||
windows: &WindowStarts<'_>,
|
||||
source: &impl ReadableVec<Height, T>,
|
||||
exit: &Exit,
|
||||
) -> Result<()>
|
||||
where
|
||||
T: Default + SubAssign,
|
||||
{
|
||||
self._1w
|
||||
.height
|
||||
.compute_rolling_sum(max_from, windows._1w, source, exit)?;
|
||||
self._1m
|
||||
.height
|
||||
.compute_rolling_sum(max_from, windows._1m, source, exit)?;
|
||||
self._1y
|
||||
.height
|
||||
.compute_rolling_sum(max_from, windows._1y, source, exit)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user