global: snapshot

This commit is contained in:
nym21
2026-03-14 18:27:25 +01:00
parent 9d365f4bbb
commit 0d177494d9
55 changed files with 1117 additions and 1006 deletions

View File

@@ -39,8 +39,7 @@ impl Binance {
previous_timestamp: Option<Timestamp>,
) -> Result<OHLCCents> {
// Try live API data first
if self._1mn.is_none()
|| self._1mn.as_ref().unwrap().last_key_value().unwrap().0 <= &timestamp
if self._1mn.as_ref().and_then(|m| m.last_key_value()).is_none_or(|(k, _)| k <= &timestamp)
{
self._1mn.replace(Self::fetch_1mn()?);
}
@@ -80,7 +79,7 @@ impl Binance {
}
pub fn get_from_1d(&mut self, date: &Date) -> Result<OHLCCents> {
if self._1d.is_none() || self._1d.as_ref().unwrap().last_key_value().unwrap().0 <= date {
if self._1d.as_ref().and_then(|m| m.last_key_value()).is_none_or(|(k, _)| k <= date) {
self._1d.replace(Self::fetch_1d()?);
}

View File

@@ -22,8 +22,7 @@ impl Kraken {
timestamp: Timestamp,
previous_timestamp: Option<Timestamp>,
) -> Result<OHLCCents> {
if self._1mn.is_none()
|| self._1mn.as_ref().unwrap().last_key_value().unwrap().0 <= &timestamp
if self._1mn.as_ref().and_then(|m| m.last_key_value()).is_none_or(|(k, _)| k <= &timestamp)
{
self._1mn.replace(Self::fetch_1mn()?);
}
@@ -46,7 +45,7 @@ impl Kraken {
}
fn get_from_1d(&mut self, date: &Date) -> Result<OHLCCents> {
if self._1d.is_none() || self._1d.as_ref().unwrap().last_key_value().unwrap().0 <= date {
if self._1d.as_ref().and_then(|m| m.last_key_value()).is_none_or(|(k, _)| k <= date) {
self._1d.replace(Self::fetch_1d()?);
}
self._1d

View File

@@ -4,7 +4,7 @@ use std::{path::Path, thread::sleep, time::Duration};
use brk_error::{Error, Result};
use brk_types::{Date, Height, OHLCCents, Timestamp};
use tracing::info;
use tracing::{info, warn};
mod binance;
mod brk;
@@ -70,14 +70,20 @@ impl Fetcher {
where
F: FnMut(&mut dyn PriceSource) -> Option<Result<OHLCCents>>,
{
if let Some(Ok(ohlc)) = fetch(&mut self.binance) {
return Some(Ok(ohlc));
match fetch(&mut self.binance) {
Some(Ok(ohlc)) => return Some(Ok(ohlc)),
Some(Err(e)) => warn!("Binance fetch failed: {e}"),
None => {}
}
if let Some(Ok(ohlc)) = fetch(&mut self.kraken) {
return Some(Ok(ohlc));
match fetch(&mut self.kraken) {
Some(Ok(ohlc)) => return Some(Ok(ohlc)),
Some(Err(e)) => warn!("Kraken fetch failed: {e}"),
None => {}
}
if let Some(Ok(ohlc)) = fetch(&mut self.brk) {
return Some(Ok(ohlc));
match fetch(&mut self.brk) {
Some(Ok(ohlc)) => return Some(Ok(ohlc)),
Some(Err(e)) => warn!("Brk fetch failed: {e}"),
None => {}
}
None
}

View File

@@ -5,13 +5,18 @@ use brk_types::{Cents, Close, Date, Dollars, High, Low, OHLCCents, Open, Timesta
/// Parse OHLC value from a JSON array element at given index
pub fn parse_cents(array: &[serde_json::Value], index: usize) -> Cents {
Cents::from(Dollars::from(
array
.get(index)
.and_then(|v| v.as_str())
.and_then(|s| s.parse::<f64>().ok())
.unwrap_or(0.0),
))
let value = array
.get(index)
.and_then(|v| v.as_str())
.and_then(|s| s.parse::<f64>().ok())
.unwrap_or_else(|| {
tracing::warn!(
"Failed to parse price at index {index}: {:?}",
array.get(index)
);
0.0
});
Cents::from(Dollars::from(value))
}
/// Build OHLCCentsUnsigned from array indices 1-4 (open, high, low, close)