arch_toolkit/types/
health.rs

1//! Health check types for archlinux.org services.
2
3use std::time::{Duration, Instant};
4
5/// What: Health status for archlinux.org services.
6///
7/// Inputs: None (created by health check operations)
8///
9/// Output: Struct containing service status and latency information
10///
11/// Details:
12/// - Provides detailed health information for connection status UIs
13/// - Includes latency measurements for performance monitoring
14#[derive(Debug, Clone)]
15pub struct HealthStatus {
16    /// Whether the AUR RPC API is reachable.
17    pub aur_api: ServiceStatus,
18    /// Measured latency to the AUR API (if reachable).
19    pub latency: Option<Duration>,
20    /// Timestamp when health check was performed.
21    pub checked_at: Instant,
22}
23
24/// What: Status of a single service endpoint.
25///
26/// Inputs: None (enum variant)
27///
28/// Output: Enum representing service health state
29///
30/// Details:
31/// - Healthy: Service is responding normally
32/// - Degraded: Service is slow but functional
33/// - Unreachable: Service returned an error
34/// - Timeout: Request timed out
35#[derive(Debug, Clone, Copy, PartialEq, Eq)]
36pub enum ServiceStatus {
37    /// Service is healthy and responding.
38    Healthy,
39    /// Service is degraded (slow response, partial functionality).
40    Degraded,
41    /// Service is unreachable.
42    Unreachable,
43    /// Health check timed out.
44    Timeout,
45}
46
47impl ServiceStatus {
48    /// What: Check if the service is operational.
49    ///
50    /// Inputs: None
51    ///
52    /// Output:
53    /// - `true` if the service is operational (Healthy or Degraded), `false` otherwise
54    ///
55    /// Details:
56    /// - Returns `true` for `Healthy` and `Degraded` statuses
57    /// - Returns `false` for `Unreachable` and `Timeout` statuses
58    #[must_use]
59    pub const fn is_operational(&self) -> bool {
60        matches!(self, Self::Healthy | Self::Degraded)
61    }
62}
63
64impl HealthStatus {
65    /// What: Check if all services are healthy.
66    ///
67    /// Inputs: None
68    ///
69    /// Output:
70    /// - `true` if all services are operational, `false` otherwise
71    ///
72    /// Details:
73    /// - Delegates to `ServiceStatus::is_operational()` for the AUR API
74    #[must_use]
75    pub const fn is_healthy(&self) -> bool {
76        self.aur_api.is_operational()
77    }
78}
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83
84    #[test]
85    fn test_service_status_is_operational() {
86        assert!(ServiceStatus::Healthy.is_operational());
87        assert!(ServiceStatus::Degraded.is_operational());
88        assert!(!ServiceStatus::Unreachable.is_operational());
89        assert!(!ServiceStatus::Timeout.is_operational());
90    }
91
92    #[test]
93    fn test_health_status_is_healthy() {
94        let healthy = HealthStatus {
95            aur_api: ServiceStatus::Healthy,
96            latency: Some(Duration::from_millis(100)),
97            checked_at: Instant::now(),
98        };
99        assert!(healthy.is_healthy());
100
101        let degraded = HealthStatus {
102            aur_api: ServiceStatus::Degraded,
103            latency: Some(Duration::from_secs(3)),
104            checked_at: Instant::now(),
105        };
106        assert!(degraded.is_healthy()); // Degraded is still operational
107
108        let unreachable = HealthStatus {
109            aur_api: ServiceStatus::Unreachable,
110            latency: None,
111            checked_at: Instant::now(),
112        };
113        assert!(!unreachable.is_healthy());
114
115        let timeout = HealthStatus {
116            aur_api: ServiceStatus::Timeout,
117            latency: None,
118            checked_at: Instant::now(),
119        };
120        assert!(!timeout.is_healthy());
121    }
122}