use super::{Digest, Insight, InsightKind, Insights, LongWait, Severity, WaitSpread};
use crate::api::{HostMemory, Tone, duration};
impl InsightKind {
pub fn label(&self) -> &'static str {
match self {
InsightKind::Slower { .. } => "Slower",
InsightKind::Flaky { .. } => "Flaky",
InsightKind::NearCap { .. } => "Near its cap",
InsightKind::ColdCache { .. } => "Cold cache",
InsightKind::Throttled { .. } => "Memory pressure",
InsightKind::CpuHeld { .. } => "CPU cap",
InsightKind::HostHeld { .. } => "Host memory",
InsightKind::Waits { .. } => "Waits",
InsightKind::Unknown => "Insight",
}
}
pub fn word(&self) -> &'static str {
match self {
InsightKind::Slower { .. } => "slower",
InsightKind::Flaky { .. } => "flaky",
InsightKind::NearCap { .. } => "near-cap",
InsightKind::ColdCache { .. } => "cold-cache",
InsightKind::Throttled { .. } => "throttled",
InsightKind::CpuHeld { .. } => "cpu-held",
InsightKind::HostHeld { .. } => "host-held",
InsightKind::Waits { .. } => "waits",
InsightKind::Unknown => "unknown",
}
}
}
impl Insight {
pub fn host_held_sentence(
jobs: u32,
waited: u64,
longest: &str,
memory: &HostMemory,
) -> String {
let who = if jobs == 1 {
"1 job waits".to_owned()
} else {
format!("{jobs} jobs wait")
};
format!(
"{who} for the host's memory, the longest for {} ({longest}); {}. Closing programs frees only part of \
that.",
duration(waited),
memory.outside_line()
)
}
}
impl Severity {
pub fn label(self) -> &'static str {
match self {
Severity::Warning => "warning",
Severity::Notice => "notice",
}
}
}
impl Insight {
pub fn tone(&self) -> Tone {
match self.severity {
Severity::Warning => Tone::Warn,
Severity::Notice => Tone::Neutral,
}
}
}
impl Insights {
pub fn warning_count(&self) -> usize {
self.insights
.iter()
.filter(|insight| insight.severity == Severity::Warning)
.count()
}
pub fn warning_line(&self) -> Option<String> {
match self.warning_count() {
0 => None,
1 => Some("1 insight".to_owned()),
count => Some(format!("{count} insights")),
}
}
pub fn days(&self) -> u64 {
self.until.saturating_sub(self.since) / 86_400
}
pub fn window_line(&self) -> String {
format!("from the last {} days", self.days())
}
pub fn empty_line(&self) -> String {
format!("nothing stands out {}", self.window_line())
}
}
impl WaitSpread {
pub fn reading(&self, value: Option<u64>) -> Option<u64> {
value.filter(|_| self.jobs > 0)
}
}
impl Digest {
pub fn summary_line(&self) -> String {
let jobs = if self.finished == 1 { "job" } else { "jobs" };
let line = format!("{} {jobs} finished, {} failed", self.finished, self.failed);
match self.new.len() {
0 => line,
1 => format!("{line}, 1 new insight"),
new => format!("{line}, {new} new insights"),
}
}
}
impl LongWait {
pub fn line(&self) -> String {
format!(
"{} {} waited {}",
self.repository,
crate::api::job_title(self.workflow.as_deref(), &self.name, Some(self.job)),
duration(self.waited)
)
}
}
#[cfg(test)]
mod tests;