use std::sync::Arc;
use std::time::Duration;
use digdigdig3::connector_manager::ExchangeHub;
use digdigdig3::core::types::{AccountType, ExchangeId};
use crate::data::{BarPoint, TradePoint};
#[derive(Debug, Clone, Copy)]
pub struct GapHealConfig {
pub enabled: bool,
pub trade_gap: Duration,
pub kline_intervals: u32,
pub max_records: usize,
}
impl Default for GapHealConfig {
fn default() -> Self {
Self {
enabled: false,
trade_gap: Duration::from_secs(10),
kline_intervals: 3,
max_records: 500,
}
}
}
impl GapHealConfig {
pub fn on() -> Self {
Self { enabled: true, ..Self::default() }
}
pub fn trade_gap(mut self, d: Duration) -> Self { self.trade_gap = d; self }
pub fn kline_intervals(mut self, n: u32) -> Self { self.kline_intervals = n; self }
pub fn max_records(mut self, n: usize) -> Self { self.max_records = n; self }
}
pub fn kline_interval_to_duration(s: &str) -> Option<Duration> {
let (n_str, unit) = s.split_at(s.len().saturating_sub(1));
let n: u64 = n_str.parse().ok()?;
let secs = match unit {
"s" => n,
"m" => n * 60,
"h" => n * 3600,
"d" => n * 86400,
"w" => n * 86400 * 7,
_ => return None,
};
Some(Duration::from_secs(secs))
}
pub async fn heal_trades(
hub: &Arc<ExchangeHub>,
exchange: ExchangeId,
account: AccountType,
symbol: &str,
since_ms: i64,
max_records: usize,
) -> Vec<TradePoint> {
let pulled = crate::backfill::trades_recent(hub, exchange, account, symbol, max_records).await;
pulled.into_iter().filter(|p| p.ts_ms > since_ms).collect()
}
pub async fn heal_klines(
hub: &Arc<ExchangeHub>,
exchange: ExchangeId,
account: AccountType,
symbol: &str,
interval: &str,
since_open_time_ms: i64,
max_records: usize,
) -> Vec<BarPoint> {
let pulled =
crate::backfill::klines_recent(hub, exchange, account, symbol, interval, max_records).await;
pulled.into_iter().filter(|p| p.open_time > since_open_time_ms).collect()
}