use crate::output::ObservationData;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ObserverId(pub String);
impl ObserverId {
pub fn new(name: impl Into<String>) -> Self {
ObserverId(name.into())
}
}
impl std::fmt::Display for ObserverId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
pub trait Observer: Send + Sync {
fn id(&self) -> &ObserverId;
fn observe(&self, data: &ObservationData);
}
pub struct VaultOracleObserver {
pub id: ObserverId,
pub captured: std::sync::Mutex<Vec<ObservationData>>,
}
impl VaultOracleObserver {
pub fn new() -> Self {
Self {
id: ObserverId::new("vault_oracle"),
captured: std::sync::Mutex::new(Vec::new()),
}
}
pub fn drain(&self) -> Vec<ObservationData> {
self.captured.lock().unwrap().drain(..).collect()
}
}
impl Default for VaultOracleObserver {
fn default() -> Self {
Self::new()
}
}
impl Observer for VaultOracleObserver {
fn id(&self) -> &ObserverId {
&self.id
}
fn observe(&self, data: &ObservationData) {
self.captured.lock().unwrap().push(data.clone());
}
}
pub struct CandleCompareObserver {
pub id: ObserverId,
pub logits: std::sync::Mutex<Option<Vec<f32>>>,
}
impl CandleCompareObserver {
pub fn new() -> Self {
Self {
id: ObserverId::new("candle_compare"),
logits: std::sync::Mutex::new(None),
}
}
pub fn take_logits(&self) -> Option<Vec<f32>> {
self.logits.lock().unwrap().take()
}
}
impl Default for CandleCompareObserver {
fn default() -> Self {
Self::new()
}
}
impl Observer for CandleCompareObserver {
fn id(&self) -> &ObserverId {
&self.id
}
fn observe(&self, data: &ObservationData) {
if let ObservationData::FinalLogits { values, .. } = data {
*self.logits.lock().unwrap() = Some(values.clone());
}
}
}