floxide_core/distributed/
metrics_store.rs

1//! Metrics store for distributed workflow runs.
2//!
3//! This module defines the MetricsStore trait for tracking workflow run metrics (e.g., completed, failed, retries),
4//! and provides an in-memory implementation for testing and local development.
5
6use 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/// Trait for a distributed workflow metrics store.
23///
24/// Implementations track per-run metrics such as completed, failed, and retried work items.
25#[async_trait]
26pub trait MetricsStore {
27    /// Update the metrics for a workflow run.
28    async fn update_metrics(&self, run_id: &str, metrics: RunMetrics) -> Result<(), MetricsError>;
29    /// Get the metrics for a workflow run.
30    async fn get_metrics(&self, run_id: &str) -> Result<Option<RunMetrics>, MetricsError>;
31}
32
33/// In-memory implementation of MetricsStore for testing and local development.
34#[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}