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    /// How many insights warn, worded for the overview: `3 insights`; `None` when none does.
71    pub fn warning_line(&self) -> Option<String> {
72        match self.warning_count() {
73            0 => None,
74            1 => Some("1 insight".to_owned()),
75            count => Some(format!("{count} insights")),
76        }
77    }
78
79    /// The whole days its window covers.
80    pub fn days(&self) -> u64 {
81        self.until.saturating_sub(self.since) / 86_400
82    }
83
84    /// Where its insights come from: `from the last 30 days`.
85    pub fn window_line(&self) -> String {
86        format!("from the last {} days", self.days())
87    }
88
89    /// What it says without any insight: `nothing stands out from the last 30 days`.
90    pub fn empty_line(&self) -> String {
91        format!("nothing stands out {}", self.window_line())
92    }
93}
94
95impl WaitSpread {
96    /// `value`, one of its percentiles, when it has jobs to take it over; `None` to show none.
97    pub fn reading(&self, value: Option<u64>) -> Option<u64> {
98        value.filter(|_| self.jobs > 0)
99    }
100}
101
102impl Digest {
103    /// Its day in a line: `12 jobs finished, 2 failed`, then `, 1 new insight` when some are new.
104    pub fn summary_line(&self) -> String {
105        let jobs = if self.finished == 1 { "job" } else { "jobs" };
106        let line = format!("{} {jobs} finished, {} failed", self.finished, self.failed);
107        match self.new.len() {
108            0 => line,
109            1 => format!("{line}, 1 new insight"),
110            new => format!("{line}, {new} new insights"),
111        }
112    }
113}
114
115impl LongWait {
116    /// It in a line: `halyard/api CI / test waited 1m 30s`, or `halyard/api job 7 waited 1m 30s` until the daemon
117    /// knows the job's name.
118    pub fn line(&self) -> String {
119        format!(
120            "{} {} waited {}",
121            self.repository,
122            crate::api::job_title(self.workflow.as_deref(), &self.name, Some(self.job)),
123            crate::api::duration(self.waited)
124        )
125    }
126}
127
128#[cfg(test)]
129mod tests;