aidaemon-work 0.26.1

Goals, mandates, tasks and work coordination with a SQLite store; internal to aidaemon, no stability promise
Documentation
//! First-class health probe system with structured metrics, trend analysis, and alerting.

pub mod alerts;
pub mod probes;
pub mod store;
mod tool;
pub mod trends;

use chrono::{DateTime, Utc};
use std::sync::Arc;
use std::time::Duration;
use tracing::{error, info, warn};

use crate::schedule::{compute_next_run, parse_schedule};

pub use alerts::{AlertDelivery, AlertManager};
pub use probes::{HealthProbe, ProbeConfig, ProbeExecutor, ProbeResult, ProbeType};
pub use store::HealthProbeStore;
pub use tool::HealthProbeTool;
pub use trends::TrendAnalyzer;

use serde::Deserialize;

/// The `[health]` settings.
#[derive(Debug, Deserialize, Clone, Default)]
pub struct HealthConfig {
    #[serde(default = "default_health_enabled")]
    pub enabled: bool,
    #[serde(default = "default_health_tick_interval_secs")]
    pub tick_interval_secs: u64,
    #[serde(default = "default_health_result_retention_days")]
    pub result_retention_days: u32,
    #[serde(default)]
    pub probes: Vec<HealthProbeConfig>,
}

fn default_health_enabled() -> bool {
    false
}

fn default_health_tick_interval_secs() -> u64 {
    30
}

fn default_health_result_retention_days() -> u32 {
    7
}

#[derive(Debug, Deserialize, Clone)]
pub struct HealthProbeConfig {
    pub name: String,
    #[serde(rename = "type")]
    pub probe_type: String,
    pub target: String,
    pub schedule: String,
    #[serde(default)]
    pub description: Option<String>,
    #[serde(default)]
    pub config: HealthProbeConfigOptions,
    #[serde(default)]
    pub consecutive_failures_alert: Option<u32>,
    #[serde(default)]
    pub latency_threshold_ms: Option<u32>,
    #[serde(default)]
    pub alert_session_ids: Vec<String>,
}

#[derive(Debug, Deserialize, Clone, Default)]
pub struct HealthProbeConfigOptions {
    #[serde(default)]
    pub timeout_secs: Option<u64>,
    #[serde(default)]
    pub expected_status: Option<u16>,
    #[serde(default)]
    pub expected_body: Option<String>,
    #[serde(default)]
    pub method: Option<String>,
    #[serde(default)]
    pub headers: Option<std::collections::HashMap<String, String>>,
    #[serde(default)]
    pub max_age_secs: Option<u64>,
    #[serde(default)]
    pub expected_exit_code: Option<i32>,
}

/// Manager for health probes with background tick loop.
pub struct HealthProbeManager {
    store: Arc<HealthProbeStore>,
    alerts: Arc<AlertManager>,
    tick_interval: Duration,
}

impl HealthProbeManager {
    /// Create a new health probe manager.
    pub fn new(
        store: Arc<HealthProbeStore>,
        hub: Arc<dyn AlertDelivery>,
        tick_interval_secs: u64,
    ) -> Self {
        let alerts = Arc::new(AlertManager::new(store.clone(), hub));

        Self {
            store,
            alerts,
            tick_interval: Duration::from_secs(tick_interval_secs),
        }
    }

    /// Get a reference to the store for external use.
    pub fn store(&self) -> &Arc<HealthProbeStore> {
        &self.store
    }

    /// Seed probes from configuration.
    ///
    /// Uses upsert by name for config-sourced probes and removes stale entries.
    pub async fn seed_from_config(
        &self,
        probes: &[HealthProbeConfig],
        default_alert_sessions: &[String],
    ) {
        let now = Utc::now();

        for probe_config in probes {
            // Parse schedule to cron
            let cron_expr = match parse_schedule(&probe_config.schedule) {
                Ok(expr) => expr,
                Err(e) => {
                    error!(
                        name = %probe_config.name,
                        schedule = %probe_config.schedule,
                        "Failed to parse health probe schedule: {}",
                        e
                    );
                    continue;
                }
            };

            // Compute next run time
            let next_run = match compute_next_run(&cron_expr) {
                Ok(dt) => dt,
                Err(e) => {
                    error!(
                        name = %probe_config.name,
                        "Failed to compute next run for health probe: {}",
                        e
                    );
                    continue;
                }
            };

            // Build probe configuration
            let config = ProbeConfig {
                timeout_secs: probe_config.config.timeout_secs.unwrap_or(10),
                expected_status: probe_config.config.expected_status,
                expected_body: probe_config.config.expected_body.clone(),
                method: probe_config
                    .config
                    .method
                    .clone()
                    .unwrap_or_else(|| "GET".to_string()),
                headers: probe_config.config.headers.clone().unwrap_or_default(),
                max_age_secs: probe_config.config.max_age_secs,
                expected_exit_code: probe_config.config.expected_exit_code,
            };

            // Determine alert sessions - use probe-specific or fall back to defaults
            let alert_sessions = if probe_config.alert_session_ids.is_empty() {
                default_alert_sessions.to_vec()
            } else {
                probe_config.alert_session_ids.clone()
            };

            let probe = HealthProbe {
                id: uuid::Uuid::new_v4().to_string(),
                name: probe_config.name.clone(),
                description: probe_config.description.clone(),
                probe_type: ProbeType::from_str(&probe_config.probe_type),
                target: probe_config.target.clone(),
                schedule: cron_expr,
                source: "config".to_string(),
                config,
                consecutive_failures_alert: probe_config.consecutive_failures_alert.unwrap_or(3),
                latency_threshold_ms: probe_config.latency_threshold_ms,
                alert_session_ids: alert_sessions,
                is_paused: false,
                last_run_at: None,
                next_run_at: next_run,
                created_at: now,
                updated_at: now,
            };

            match self.store.upsert_probe(&probe).await {
                Ok(_) => info!(
                    name = %probe_config.name,
                    probe_type = %probe_config.probe_type,
                    schedule = %probe_config.schedule,
                    "Seeded health probe from config"
                ),
                Err(e) => error!(
                    name = %probe_config.name,
                    "Failed to seed health probe: {}",
                    e
                ),
            }
        }

        // Remove config-sourced probes that are no longer in config
        let config_names: Vec<&str> = probes.iter().map(|p| p.name.as_str()).collect();
        match self.store.remove_stale_config_probes(&config_names).await {
            Ok(removed) if removed > 0 => {
                info!(count = removed, "Removed stale config health probes");
            }
            Err(e) => {
                warn!("Failed to remove stale config probes: {}", e);
            }
            _ => {}
        }
    }

    /// Spawn the background tick loop.
    pub fn spawn(self: Arc<Self>) {
        tokio::spawn(async move {
            info!(
                interval_secs = self.tick_interval.as_secs(),
                "Health probe manager started"
            );

            loop {
                tokio::time::sleep(self.tick_interval).await;

                if let Err(e) = self.tick().await {
                    error!("Health probe tick error: {}", e);
                }
            }
        });
    }

    /// Check for due probes and execute them.
    async fn tick(&self) -> anyhow::Result<()> {
        let now = Utc::now();
        let due_probes = self.store.get_due_probes(now).await?;

        if due_probes.is_empty() {
            return Ok(());
        }

        info!(count = due_probes.len(), "Running due health probes");

        for probe in due_probes {
            // Execute the probe
            let result = ProbeExecutor::execute(&probe).await;

            info!(
                probe = %probe.name,
                status = %result.status.as_str(),
                latency_ms = ?result.latency_ms,
                "Probe check completed"
            );

            // Store the result
            if let Err(e) = self.store.insert_result(&result).await {
                warn!(probe = %probe.name, "Failed to store probe result: {}", e);
            }

            // Process alerts
            self.alerts.process_result(&probe, &result).await;

            // Update next run time
            let next_run = match compute_next_run(&probe.schedule) {
                Ok(dt) => dt,
                Err(e) => {
                    warn!(
                        probe = %probe.name,
                        "Failed to compute next run, using +1 minute: {}",
                        e
                    );
                    now + chrono::Duration::minutes(1)
                }
            };

            if let Err(e) = self.store.update_probe_run(&probe.id, now, next_run).await {
                warn!(probe = %probe.name, "Failed to update probe run time: {}", e);
            }
        }

        Ok(())
    }

    /// Cleanup old probe results beyond retention period.
    pub async fn cleanup_old_results(&self, retention_days: u32) -> anyhow::Result<u64> {
        let cutoff = Utc::now() - chrono::Duration::days(retention_days as i64);
        let deleted = self.store.delete_old_results(cutoff).await?;

        if deleted > 0 {
            info!(deleted, retention_days, "Cleaned up old probe results");
        }

        Ok(deleted)
    }
}

/// Summary of a probe's current status for API responses.
#[derive(Debug, Clone, serde::Serialize)]
pub struct ProbeStatusSummary {
    pub id: String,
    pub name: String,
    pub probe_type: String,
    pub target: String,
    pub is_paused: bool,
    pub last_status: Option<String>,
    pub last_latency_ms: Option<u32>,
    pub last_checked: Option<DateTime<Utc>>,
    pub next_run: DateTime<Utc>,
    pub consecutive_failures: u32,
    pub has_active_alert: bool,
}

/// Spawn a cleanup task that runs daily.
pub fn spawn_cleanup_task(manager: Arc<HealthProbeManager>, retention_days: u32) {
    tokio::spawn(async move {
        loop {
            // Run at 3:40 AM (after other cleanup tasks)
            let now = chrono::Utc::now();
            let next_340am = {
                let today_340am = now.date_naive().and_hms_opt(3, 40, 0).unwrap();
                let today_340am_utc = chrono::DateTime::<chrono::Utc>::from_naive_utc_and_offset(
                    today_340am,
                    chrono::Utc,
                );
                if now < today_340am_utc {
                    today_340am_utc
                } else {
                    today_340am_utc + chrono::Duration::days(1)
                }
            };

            let sleep_duration = (next_340am - now)
                .to_std()
                .unwrap_or(Duration::from_secs(3600));
            tokio::time::sleep(sleep_duration).await;

            match manager.cleanup_old_results(retention_days).await {
                Ok(deleted) => {
                    if deleted > 0 {
                        info!(deleted, "Health probe result cleanup complete");
                    }
                }
                Err(e) => {
                    error!("Health probe result cleanup failed: {}", e);
                }
            }
        }
    });
}