runnerd_core/api/insights/
shown.rs1use super::{Digest, Insight, InsightKind, Insights, LongWait, Severity, WaitSpread};
6use crate::api::Tone;
7
8impl InsightKind {
9 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 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 pub fn label(self) -> &'static str {
43 match self {
44 Severity::Warning => "warning",
45 Severity::Notice => "notice",
46 }
47 }
48}
49
50impl Insight {
51 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 pub fn warning_count(&self) -> usize {
64 self.insights
65 .iter()
66 .filter(|insight| insight.severity == Severity::Warning)
67 .count()
68 }
69
70 pub fn days(&self) -> u64 {
72 self.until.saturating_sub(self.since) / 86_400
73 }
74
75 pub fn window_line(&self) -> String {
77 format!("from the last {} days", self.days())
78 }
79
80 pub fn empty_line(&self) -> String {
82 format!("nothing stands out {}", self.window_line())
83 }
84}
85
86impl WaitSpread {
87 pub fn reading(&self, value: Option<u64>) -> Option<u64> {
89 value.filter(|_| self.jobs > 0)
90 }
91}
92
93impl Digest {
94 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 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;