use super::ConfigurableCache; use cm_dashboard_shared::{CacheConfig, Metric}; use std::sync::Arc; use tokio::time::{interval, Duration}; use tracing::{debug, info}; /// Manages metric caching with background tasks pub struct MetricCacheManager { cache: Arc, config: CacheConfig, } impl MetricCacheManager { pub fn new(config: CacheConfig) -> Self { let cache = Arc::new(ConfigurableCache::new(config.clone())); Self { cache, config, } } /// Start background cache management tasks pub async fn start_background_tasks(&self) { // Temporarily disabled to isolate CPU usage issue info!("Cache manager background tasks disabled for debugging"); } /// Check if metric should be collected pub async fn should_collect_metric(&self, metric_name: &str) -> bool { self.cache.should_collect(metric_name).await } /// Store metric in cache pub async fn cache_metric(&self, metric: Metric) { self.cache.store_metric(metric).await; } /// Get cached metric if valid pub async fn get_cached_metric(&self, metric_name: &str) -> Option { self.cache.get_cached_metric(metric_name).await } /// Get all valid cached metrics pub async fn get_all_valid_metrics(&self) -> Vec { self.cache.get_all_valid_metrics().await } /// Cache warm-up: collect and cache high-priority metrics pub async fn warm_cache(&self, collector_fn: F) where F: Fn(&str) -> Option, { if !self.config.enabled { return; } let high_priority_patterns = ["cpu_load_*", "memory_usage_*"]; let mut warmed_count = 0; for pattern in &high_priority_patterns { // This is a simplified warm-up - in practice, you'd iterate through // known metric names or use a registry if pattern.starts_with("cpu_load_") { for suffix in &["1min", "5min", "15min"] { let metric_name = format!("cpu_load_{}", suffix); if let Some(metric) = collector_fn(&metric_name) { self.cache_metric(metric).await; warmed_count += 1; } } } } if warmed_count > 0 { info!("Cache warmed with {} metrics", warmed_count); } } /// Get cache configuration pub fn get_config(&self) -> &CacheConfig { &self.config } /// Get cache tier interval for a metric pub fn get_cache_interval(&self, metric_name: &str) -> u64 { self.config.get_cache_interval(metric_name) } }