use serde::Deserialize;
use std::path::Path;
use super::market_config::{
DataTypesSection, ExchangeSection, LogsSection, MarketSnapshotConfig,
OutputSection, PipelineSection, SymbolSection, SyncMode, UpdateFrequency,
};
#[derive(Debug, Clone, Deserialize)]
pub struct WorkerManifest {
pub defaults: DefaultsSection,
pub workers: Vec<WorkerEntry>,
pub output: ManifestOutputSection,
#[serde(default)]
pub session: SessionSection,
}
#[derive(Debug, Clone, Deserialize)]
pub struct DefaultsSection {
pub sync_mode: SyncMode,
pub flush_threshold: usize,
pub update_frequency: UpdateFrequency,
pub datatypes: DataTypesSection,
pub logs: LogsSection,
}
#[derive(Debug, Clone, Deserialize)]
pub struct WorkerEntry {
pub exchange: String,
pub symbol: String,
pub sync_mode: Option<SyncMode>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct ManifestOutputSection {
pub base_dir: String,
}
#[derive(Debug, Clone, Deserialize, Default)]
pub struct SessionSection {
pub duration_hours: Option<f64>,
}
impl WorkerManifest {
pub fn from_toml(path: &Path) -> anyhow::Result<Self> {
let contents = std::fs::read_to_string(path)
.map_err(|e| anyhow::anyhow!("failed to read {:?}: {}", path, e))?;
let manifest: Self = toml::from_str(&contents)
.map_err(|e| anyhow::anyhow!("failed to parse {:?}: {}", path, e))?;
Ok(manifest)
}
pub fn resolve_all(&self) -> Vec<MarketSnapshotConfig> {
self.workers
.iter()
.map(|w| w.resolve(&self.defaults, &self.output))
.collect()
}
pub fn duration_secs(&self) -> Option<f64> {
self.session.duration_hours.map(|h| h * 3600.0)
}
}
impl WorkerEntry {
pub fn resolve(
&self,
defaults: &DefaultsSection,
output: &ManifestOutputSection,
) -> MarketSnapshotConfig {
MarketSnapshotConfig {
exchange: ExchangeSection {
name: self.exchange.clone(),
},
symbol: SymbolSection {
name: self.symbol.clone(),
sync_mode: self.sync_mode.unwrap_or(defaults.sync_mode),
},
update_frequency: defaults.update_frequency.clone(),
pipeline: PipelineSection {
flush_threshold: defaults.flush_threshold,
},
datatypes: defaults.datatypes.clone(),
output: OutputSection {
dir: format!(
"{}/{}/{}",
output.base_dir, self.exchange, self.symbol,
),
},
logs: defaults.logs.clone(),
}
}
}