use smol_str::SmolStr;
use std::collections::HashMap;
use std::sync::{Arc, OnceLock};
use std::time::Duration;
use crate::data::ChannelData;
#[derive(Debug, Clone)]
pub struct Acquisition {
pub data: Arc<Vec<ChannelData>>,
pub wait_time: Duration,
pub download_time: Duration,
by_symbol: Arc<OnceLock<HashMap<SmolStr, usize>>>,
}
impl Acquisition {
pub fn new(data: Vec<ChannelData>, wait_time: Duration, download_time: Duration) -> Self {
Self {
data: Arc::new(data),
wait_time,
download_time,
by_symbol: Arc::new(OnceLock::new()),
}
}
pub fn get_by_symbol(&self, symbol: &str) -> Option<&ChannelData> {
let map = self.by_symbol.get_or_init(|| {
self.data
.iter()
.enumerate()
.map(|(idx, channel)| (channel.symbol().clone(), idx))
.collect()
});
map.get(symbol).and_then(|&idx| self.data.get(idx))
}
}