floxide_core/distributed/
metrics_store.rs1use crate::distributed::RunMetrics;
7use async_trait::async_trait;
8use std::collections::HashMap;
9use std::sync::Arc;
10use tokio::sync::Mutex;
11
12#[derive(Debug, thiserror::Error)]
13pub enum MetricsError {
14 #[error("I/O error: {0}")]
15 Io(String),
16 #[error("Not found")]
17 NotFound,
18 #[error("Other error: {0}")]
19 Other(String),
20}
21
22#[async_trait]
26pub trait MetricsStore {
27 async fn update_metrics(&self, run_id: &str, metrics: RunMetrics) -> Result<(), MetricsError>;
29 async fn get_metrics(&self, run_id: &str) -> Result<Option<RunMetrics>, MetricsError>;
31}
32
33#[derive(Clone, Default)]
35pub struct InMemoryMetricsStore {
36 inner: Arc<Mutex<HashMap<String, RunMetrics>>>,
37}
38
39#[async_trait]
40impl MetricsStore for InMemoryMetricsStore {
41 async fn update_metrics(&self, run_id: &str, metrics: RunMetrics) -> Result<(), MetricsError> {
42 let mut map = self.inner.lock().await;
43 map.insert(run_id.to_string(), metrics);
44 Ok(())
45 }
46 async fn get_metrics(&self, run_id: &str) -> Result<Option<RunMetrics>, MetricsError> {
47 let map = self.inner.lock().await;
48 Ok(map.get(run_id).cloned())
49 }
50}