Skip to main content

platform/monitoring/
service_registry.rs

1//! Service registry for managing service statuses
2
3use super::ServiceInfo;
4use actrix_proto::ServiceStatus;
5use std::collections::HashMap;
6use std::sync::Arc;
7use tokio::sync::RwLock;
8
9/// Service registry that provides service statuses
10///
11/// This registry stores ServiceInfo entries and can convert them to ServiceStatus.
12#[derive(Clone, Debug)]
13pub struct ServiceCollector {
14    inner: Arc<RwLock<HashMap<String, ServiceInfo>>>,
15}
16
17impl Default for ServiceCollector {
18    fn default() -> Self {
19        Self {
20            inner: Arc::new(RwLock::new(HashMap::new())),
21        }
22    }
23}
24
25impl ServiceCollector {
26    /// Create a new service registry
27    pub fn new() -> Self {
28        Self::default()
29    }
30
31    /// Get all service info values
32    pub async fn values(&self) -> Vec<ServiceInfo> {
33        self.inner
34            .read()
35            .await
36            .values()
37            .cloned()
38            .collect::<Vec<_>>()
39    }
40
41    /// Insert a service info entry
42    pub async fn insert(&self, key: String, value: ServiceInfo) {
43        self.inner.write().await.insert(key, value);
44    }
45
46    /// Get a service info entry by key
47    pub async fn get(&self, key: &str) -> Option<ServiceInfo> {
48        self.inner.read().await.get(key).cloned()
49    }
50
51    /// Remove a service info entry by key
52    pub async fn remove(&self, key: &str) -> Option<ServiceInfo> {
53        self.inner.write().await.remove(key)
54    }
55
56    /// Get all service statuses as proto ServiceStatus
57    ///
58    /// Converts all ServiceInfo entries to ServiceStatus using the From trait.
59    pub async fn all_statuses(&self) -> Vec<ServiceStatus> {
60        let map = self.inner.read().await;
61        map.values()
62            .map(|info| ServiceStatus::from(info.clone()))
63            .collect()
64    }
65
66    /// Get all values directly (returns ServiceInfo)
67    pub async fn all_values(&self) -> Vec<ServiceInfo> {
68        self.values().await
69    }
70}