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 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 pub fn days(&self) -> u64 {
81 self.until.saturating_sub(self.since) / 86_400
82 }
83
84 pub fn window_line(&self) -> String {
86 format!("from the last {} days", self.days())
87 }
88
89 pub fn empty_line(&self) -> String {
91 format!("nothing stands out {}", self.window_line())
92 }
93}
94
95impl WaitSpread {
96 pub fn reading(&self, value: Option<u64>) -> Option<u64> {
98 value.filter(|_| self.jobs > 0)
99 }
100}
101
102impl Digest {
103 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 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;