use std::time::{SystemTime, UNIX_EPOCH};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthStatus {
pub status: String,
pub timestamp: u64,
pub uptime_seconds: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReadinessStatus {
pub ready: bool,
pub database_connected: bool,
pub cache_available: bool,
pub reason: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LivenessStatus {
pub alive: bool,
pub pid: u32,
pub response_time_ms: u32,
}
pub fn health_check(uptime_seconds: u64) -> HealthStatus {
HealthStatus {
status: "healthy".to_string(),
timestamp: SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_secs(),
uptime_seconds,
}
}
pub fn readiness_check(database_connected: bool, cache_available: bool) -> ReadinessStatus {
let ready = database_connected && cache_available;
let reason = if !database_connected {
Some("Database unavailable".to_string())
} else if !cache_available {
Some("Cache unavailable".to_string())
} else {
None
};
ReadinessStatus {
ready,
database_connected,
cache_available,
reason,
}
}
pub fn liveness_check() -> LivenessStatus {
LivenessStatus {
alive: true,
pid: std::process::id(),
response_time_ms: 5,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_health_check() {
let status = health_check(3600);
assert_eq!(status.status, "healthy");
assert_eq!(status.uptime_seconds, 3600);
}
#[test]
fn test_readiness_check() {
let status = readiness_check(true, true);
assert!(status.ready);
}
#[test]
fn test_liveness_check() {
let status = liveness_check();
assert!(status.alive);
assert!(status.pid > 0);
}
}