use crate::distributed::RunMetrics;
use async_trait::async_trait;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::Mutex;
#[derive(Debug, thiserror::Error)]
pub enum MetricsError {
#[error("I/O error: {0}")]
Io(String),
#[error("Not found")]
NotFound,
#[error("Other error: {0}")]
Other(String),
}
#[async_trait]
pub trait MetricsStore {
async fn update_metrics(&self, run_id: &str, metrics: RunMetrics) -> Result<(), MetricsError>;
async fn get_metrics(&self, run_id: &str) -> Result<Option<RunMetrics>, MetricsError>;
}
#[derive(Clone, Default)]
pub struct InMemoryMetricsStore {
inner: Arc<Mutex<HashMap<String, RunMetrics>>>,
}
#[async_trait]
impl MetricsStore for InMemoryMetricsStore {
async fn update_metrics(&self, run_id: &str, metrics: RunMetrics) -> Result<(), MetricsError> {
let mut map = self.inner.lock().await;
map.insert(run_id.to_string(), metrics);
Ok(())
}
async fn get_metrics(&self, run_id: &str) -> Result<Option<RunMetrics>, MetricsError> {
let map = self.inner.lock().await;
Ok(map.get(run_id).cloned())
}
}