use std::fmt;
use serde::{Deserialize, Serialize};
use crate::api::AdmissionPolicy;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RunningJob {
pub repository: String,
pub runner: String,
pub job: Option<u64>,
}
impl fmt::Display for RunningJob {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.job {
Some(job) => write!(f, "job {job} on {}", self.runner),
None => write!(f, "{}", self.runner),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Busy {
pub message: String,
pub jobs: Vec<RunningJob>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct PauseQuery {
#[serde(default)]
pub force: bool,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Paused {
pub stopped: Vec<RunningJob>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ConfigFile {
pub path: String,
pub text: String,
pub revision: String,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct ReplaceQuery {
pub revision: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ConfigCheck {
pub valid: bool,
pub message: String,
pub warnings: Vec<String>,
pub admission: AdmissionPolicy,
pub memory_caps: Option<u64>,
pub memory_needed: Option<u64>,
pub memory_max: Option<u64>,
pub revision: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Doctor {
pub checks: Vec<Check>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub state: Option<DoctorState>,
}
impl Doctor {
pub const CHECKING: &'static str = "Checking the host";
pub fn summary(&self, now: u64) -> DoctorSummary<'_> {
let not_current = self.state.as_ref().and_then(|state| state.line(now));
let current = not_current.is_none();
let (problems, rest): (Vec<&Check>, Vec<&Check>) = self.checks.iter().partition(|check| {
current && matches!(check.status.tone(), super::Tone::Bad | super::Tone::Warn)
});
let mut counts: Vec<CheckCount> = Vec::new();
for check in &rest {
let label = check.status.label();
match counts.iter_mut().find(|count| count.label == label) {
Some(count) => count.count += 1,
None => counts.push(CheckCount { label, count: 1 }),
}
}
DoctorSummary {
not_current,
problems,
rest,
counts,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DoctorSummary<'doctor> {
pub not_current: Option<String>,
pub problems: Vec<&'doctor Check>,
pub rest: Vec<&'doctor Check>,
pub counts: Vec<CheckCount>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CheckCount {
pub label: &'static str,
pub count: usize,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct DoctorQuery {
#[serde(default)]
pub recent: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case", tag = "kind")]
pub enum DoctorState {
NotRun,
Ready {
at: u64,
},
Stale {
at: u64,
},
Failed {
at: u64,
error: String,
},
#[serde(other)]
Unknown,
}
impl DoctorState {
pub fn checked(&self, now: u64) -> Option<String> {
match self {
DoctorState::Ready { at } => Some(format!("Checked {}", super::ago(*at, now))),
DoctorState::NotRun
| DoctorState::Stale { .. }
| DoctorState::Failed { .. }
| DoctorState::Unknown => None,
}
}
pub fn line(&self, now: u64) -> Option<String> {
match self {
DoctorState::Ready { .. } => None,
DoctorState::NotRun => Some("Host checks have not finished yet".to_owned()),
DoctorState::Stale { at } => Some(format!(
"Host checks are from {} and may no longer hold; a new run is due",
super::ago(*at, now)
)),
DoctorState::Failed { error, .. } => {
Some(format!("Host checks could not finish: {error}"))
}
DoctorState::Unknown => {
Some("Host checks are in a state this client does not know".to_owned())
}
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Check {
pub name: String,
pub status: CheckStatus,
pub detail: String,
pub fix: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum CheckStatus {
Pass,
Fail,
Warn,
Skip,
Info,
}
impl CheckStatus {
pub fn label(self) -> &'static str {
match self {
CheckStatus::Pass => "ok",
CheckStatus::Fail => "FAIL",
CheckStatus::Warn => "WARN",
CheckStatus::Skip => "skip",
CheckStatus::Info => "info",
}
}
pub fn tone(self) -> super::Tone {
match self {
CheckStatus::Pass => super::Tone::Good,
CheckStatus::Fail => super::Tone::Bad,
CheckStatus::Warn => super::Tone::Warn,
CheckStatus::Skip => super::Tone::Quiet,
CheckStatus::Info => super::Tone::Neutral,
}
}
}
impl Doctor {
pub fn passed(&self) -> bool {
matches!(self.state, None | Some(DoctorState::Ready { .. }))
&& Doctor::all_passed(&self.checks)
}
pub fn all_passed(checks: &[Check]) -> bool {
!checks.iter().any(|check| check.status == CheckStatus::Fail)
}
}
impl fmt::Display for Check {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let status = self.status.label();
write!(f, "{status:<5} {}: {}", self.name, self.detail)?;
if let Some(fix) = &self.fix {
for (i, line) in fix.lines().enumerate() {
let label = if i == 0 { "fix:" } else { "" };
write!(f, "\n {label:<4} {line}")?;
}
}
Ok(())
}
}
#[cfg(test)]
mod tests;