darpan 0.2.2

Linux developer service monitoring utility with auto-detection, real-time health checks, and interactive TUI for databases, APIs, Docker containers, and more
Documentation
use crate::models::Service;
use std::collections::HashMap;

pub struct ServiceRegistry {
    services: HashMap<String, Service>,
}

impl ServiceRegistry {
    pub fn new() -> Self {
        Self {
            services: HashMap::new(),
        }
    }

    pub fn add_service(&mut self, service: Service) {
        self.services.insert(service.id.clone(), service);
    }

    pub fn get_service(&self, id: &str) -> Option<&Service> {
        self.services.get(id)
    }

    pub fn get_service_mut(&mut self, id: &str) -> Option<&mut Service> {
        self.services.get_mut(id)
    }

    pub fn get_service_by_name(&self, name: &str) -> Option<&Service> {
        self.services.values().find(|s| s.name == name)
    }

    pub fn all_services(&self) -> Vec<&Service> {
        self.services.values().collect()
    }

    pub fn all_services_mut(&mut self) -> Vec<&mut Service> {
        self.services.values_mut().collect()
    }

    pub fn remove_service(&mut self, id: &str) -> Option<Service> {
        self.services.remove(id)
    }

    pub fn clear(&mut self) {
        self.services.clear();
    }

    pub fn count(&self) -> usize {
        self.services.len()
    }

    pub fn merge_or_update(&mut self, new_service: Service) {
        if let Some(existing) = self.services.get_mut(&new_service.id) {
            // Update existing service with new information
            existing.pid = new_service.pid;
            existing.process_name = new_service.process_name.clone();
            existing.command_line = new_service.command_line.clone();
            existing.last_seen = new_service.last_seen;
            
            // Preserve health status if new service doesn't have it
            if new_service.health_status.is_some() {
                existing.health_status = new_service.health_status;
            }
        } else {
            // Add new service
            self.services.insert(new_service.id.clone(), new_service);
        }
    }

    pub fn services_by_tag(&self, tag: &str) -> Vec<&Service> {
        self.services
            .values()
            .filter(|s| s.tags.contains(&tag.to_string()))
            .collect()
    }
}

impl Default for ServiceRegistry {
    fn default() -> Self {
        Self::new()
    }
}