global: snapshot

This commit is contained in:
nym21
2026-03-06 21:46:44 +01:00
parent 8c32ad2483
commit 9a2ee0273f
26 changed files with 955 additions and 578 deletions
@@ -0,0 +1,62 @@
use brk_traversable::Traversable;
#[derive(Clone, Traversable)]
pub struct DistributionStats<A> {
pub average: A,
pub min: A,
pub max: A,
pub pct10: A,
pub pct25: A,
pub median: A,
pub pct75: A,
pub pct90: A,
}
impl<A> DistributionStats<A> {
pub const SUFFIXES: [&'static str; 8] = [
"average", "min", "max", "p10", "p25", "median", "p75", "p90",
];
pub fn try_from_fn<E>(
mut f: impl FnMut(&str) -> std::result::Result<A, E>,
) -> std::result::Result<Self, E> {
Ok(Self {
average: f(Self::SUFFIXES[0])?,
min: f(Self::SUFFIXES[1])?,
max: f(Self::SUFFIXES[2])?,
pct10: f(Self::SUFFIXES[3])?,
pct25: f(Self::SUFFIXES[4])?,
median: f(Self::SUFFIXES[5])?,
pct75: f(Self::SUFFIXES[6])?,
pct90: f(Self::SUFFIXES[7])?,
})
}
/// Apply a fallible operation to each of the 8 fields.
pub fn try_for_each_mut(
&mut self,
mut f: impl FnMut(&mut A) -> brk_error::Result<()>,
) -> brk_error::Result<()> {
f(&mut self.average)?;
f(&mut self.min)?;
f(&mut self.max)?;
f(&mut self.pct10)?;
f(&mut self.pct25)?;
f(&mut self.median)?;
f(&mut self.pct75)?;
f(&mut self.pct90)?;
Ok(())
}
/// Get minimum value by applying a function to each field.
pub fn min_by(&self, mut f: impl FnMut(&A) -> usize) -> usize {
f(&self.average)
.min(f(&self.min))
.min(f(&self.max))
.min(f(&self.pct10))
.min(f(&self.pct25))
.min(f(&self.median))
.min(f(&self.pct75))
.min(f(&self.pct90))
}
}
@@ -0,0 +1,30 @@
use brk_traversable::Traversable;
#[derive(Clone, Traversable)]
pub struct Emas1w1m<A> {
#[traversable(rename = "1w")]
pub _1w: A,
#[traversable(rename = "1m")]
pub _1m: A,
}
impl<A> Emas1w1m<A> {
pub const SUFFIXES: [&'static str; 2] = ["ema_1w", "ema_1m"];
pub fn try_from_fn<E>(
mut f: impl FnMut(&str) -> std::result::Result<A, E>,
) -> std::result::Result<Self, E> {
Ok(Self {
_1w: f(Self::SUFFIXES[0])?,
_1m: f(Self::SUFFIXES[1])?,
})
}
pub fn as_array(&self) -> [&A; 2] {
[&self._1w, &self._1m]
}
pub fn as_mut_array(&mut self) -> [&mut A; 2] {
[&mut self._1w, &mut self._1m]
}
}
@@ -0,0 +1,9 @@
mod distribution_stats;
mod emas;
mod per_period;
mod windows;
pub use distribution_stats::*;
pub use emas::*;
pub use per_period::*;
pub use windows::*;
@@ -0,0 +1,21 @@
use brk_traversable::Traversable;
#[derive(Clone, Traversable)]
#[traversable(merge)]
pub struct PerPeriod<M10, M30, H1, H4, H12, D1, D3, W1, Mo1, Mo3, Mo6, Y1, Y10, HE, DE> {
pub minute10: M10,
pub minute30: M30,
pub hour1: H1,
pub hour4: H4,
pub hour12: H12,
pub day1: D1,
pub day3: D3,
pub week1: W1,
pub month1: Mo1,
pub month3: Mo3,
pub month6: Mo6,
pub year1: Y1,
pub year10: Y10,
pub halvingepoch: HE,
pub difficultyepoch: DE,
}
@@ -0,0 +1,36 @@
use brk_traversable::Traversable;
#[derive(Clone, Traversable)]
pub struct Windows<A> {
#[traversable(rename = "24h")]
pub _24h: A,
#[traversable(rename = "1w")]
pub _1w: A,
#[traversable(rename = "1m")]
pub _1m: A,
#[traversable(rename = "1y")]
pub _1y: A,
}
impl<A> Windows<A> {
pub const SUFFIXES: [&'static str; 4] = ["24h", "1w", "1m", "1y"];
pub fn try_from_fn<E>(
mut f: impl FnMut(&str) -> std::result::Result<A, E>,
) -> std::result::Result<Self, E> {
Ok(Self {
_24h: f(Self::SUFFIXES[0])?,
_1w: f(Self::SUFFIXES[1])?,
_1m: f(Self::SUFFIXES[2])?,
_1y: f(Self::SUFFIXES[3])?,
})
}
pub fn as_array(&self) -> [&A; 4] {
[&self._24h, &self._1w, &self._1m, &self._1y]
}
pub fn as_mut_array(&mut self) -> [&mut A; 4] {
[&mut self._24h, &mut self._1w, &mut self._1m, &mut self._1y]
}
}