Skip to main content

runnerd_core/api/insights/
shown.rs

1//! What insights and the digest mean as every client shows them: each insight's labels and tone, how many warn,
2//! the window they come from, what the page says without any, and the digest's lines. Plain derivations of
3//! [`Insights`] and [`Digest`]; the dashboard, `runnerctl`, the TUI and notifications only lay them out.
4
5use super::{Digest, Insight, InsightKind, Insights, LongWait, Severity, WaitSpread};
6use crate::api::Tone;
7
8impl InsightKind {
9    /// Its name as a label: `Slower`, `Flaky`, `Near its cap`, `Cold cache`, `Memory pressure`, `CPU cap`,
10    /// `Waits`, or `Insight` for a kind from a later release.
11    pub fn label(&self) -> &'static str {
12        match self {
13            InsightKind::Slower { .. } => "Slower",
14            InsightKind::Flaky { .. } => "Flaky",
15            InsightKind::NearCap { .. } => "Near its cap",
16            InsightKind::ColdCache { .. } => "Cold cache",
17            InsightKind::Throttled { .. } => "Memory pressure",
18            InsightKind::CpuHeld { .. } => "CPU cap",
19            InsightKind::Waits { .. } => "Waits",
20            InsightKind::Unknown => "Insight",
21        }
22    }
23
24    /// Its name as one word, the API's `kind`: `slower`, `flaky`, `near-cap`, `cold-cache`, `throttled`,
25    /// `cpu-held` or `waits`; `unknown` for a kind from a later release.
26    pub fn word(&self) -> &'static str {
27        match self {
28            InsightKind::Slower { .. } => "slower",
29            InsightKind::Flaky { .. } => "flaky",
30            InsightKind::NearCap { .. } => "near-cap",
31            InsightKind::ColdCache { .. } => "cold-cache",
32            InsightKind::Throttled { .. } => "throttled",
33            InsightKind::CpuHeld { .. } => "cpu-held",
34            InsightKind::Waits { .. } => "waits",
35            InsightKind::Unknown => "unknown",
36        }
37    }
38}
39
40impl Severity {
41    /// `warning` or `notice`.
42    pub fn label(self) -> &'static str {
43        match self {
44            Severity::Warning => "warning",
45            Severity::Notice => "notice",
46        }
47    }
48}
49
50impl Insight {
51    /// What it means for the reader, by its severity alone, whatever its kind: a warning is worth a look, a notice
52    /// a plain fact.
53    pub fn tone(&self) -> Tone {
54        match self.severity {
55            Severity::Warning => Tone::Warn,
56            Severity::Notice => Tone::Neutral,
57        }
58    }
59}
60
61impl Insights {
62    /// How many insights warn.
63    pub fn warning_count(&self) -> usize {
64        self.insights
65            .iter()
66            .filter(|insight| insight.severity == Severity::Warning)
67            .count()
68    }
69
70    /// The whole days its window covers.
71    pub fn days(&self) -> u64 {
72        self.until.saturating_sub(self.since) / 86_400
73    }
74
75    /// Where its insights come from: `from the last 30 days`.
76    pub fn window_line(&self) -> String {
77        format!("from the last {} days", self.days())
78    }
79
80    /// What it says without any insight: `nothing stands out from the last 30 days`.
81    pub fn empty_line(&self) -> String {
82        format!("nothing stands out {}", self.window_line())
83    }
84}
85
86impl WaitSpread {
87    /// `value`, one of its percentiles, when it has jobs to take it over; `None` to show none.
88    pub fn reading(&self, value: Option<u64>) -> Option<u64> {
89        value.filter(|_| self.jobs > 0)
90    }
91}
92
93impl Digest {
94    /// Its day in a line: `12 jobs finished, 2 failed`, then `, 1 new insight` when some are new.
95    pub fn summary_line(&self) -> String {
96        let jobs = if self.finished == 1 { "job" } else { "jobs" };
97        let line = format!("{} {jobs} finished, {} failed", self.finished, self.failed);
98        match self.new.len() {
99            0 => line,
100            1 => format!("{line}, 1 new insight"),
101            new => format!("{line}, {new} new insights"),
102        }
103    }
104}
105
106impl LongWait {
107    /// It in a line: `halyard/api CI / test waited 1m 30s`, or `halyard/api job 7 waited 1m 30s` until the daemon
108    /// knows the job's name.
109    pub fn line(&self) -> String {
110        format!(
111            "{} {} waited {}",
112            self.repository,
113            crate::api::job_title(self.workflow.as_deref(), &self.name, Some(self.job)),
114            crate::api::duration(self.waited)
115        )
116    }
117}
118
119#[cfg(test)]
120mod tests;