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 {
11 match self {
12 InsightKind::Slower { .. } => "Slower",
13 InsightKind::Flaky { .. } => "Flaky",
14 InsightKind::NearCap { .. } => "Near its cap",
15 InsightKind::ColdCache { .. } => "Cold cache",
16 InsightKind::Throttled { .. } => "Memory pressure",
17 InsightKind::Waits { .. } => "Waits",
18 }
19 }
20
21 pub fn word(&self) -> &'static str {
24 match self {
25 InsightKind::Slower { .. } => "slower",
26 InsightKind::Flaky { .. } => "flaky",
27 InsightKind::NearCap { .. } => "near-cap",
28 InsightKind::ColdCache { .. } => "cold-cache",
29 InsightKind::Throttled { .. } => "throttled",
30 InsightKind::Waits { .. } => "waits",
31 }
32 }
33}
34
35impl Severity {
36 pub fn label(self) -> &'static str {
38 match self {
39 Severity::Warning => "warning",
40 Severity::Notice => "notice",
41 }
42 }
43}
44
45impl Insight {
46 pub fn tone(&self) -> Tone {
49 match self.severity {
50 Severity::Warning => Tone::Warn,
51 Severity::Notice => Tone::Neutral,
52 }
53 }
54}
55
56impl Insights {
57 pub fn warning_count(&self) -> usize {
59 self.insights
60 .iter()
61 .filter(|insight| insight.severity == Severity::Warning)
62 .count()
63 }
64
65 pub fn days(&self) -> u64 {
67 self.until.saturating_sub(self.since) / 86_400
68 }
69
70 pub fn window_line(&self) -> String {
72 format!("from the last {} days", self.days())
73 }
74
75 pub fn empty_line(&self) -> String {
77 format!("nothing stands out {}", self.window_line())
78 }
79}
80
81impl WaitSpread {
82 pub fn reading(&self, value: Option<u64>) -> Option<u64> {
84 value.filter(|_| self.jobs > 0)
85 }
86}
87
88impl Digest {
89 pub fn summary_line(&self) -> String {
91 let jobs = if self.finished == 1 { "job" } else { "jobs" };
92 let line = format!("{} {jobs} finished, {} failed", self.finished, self.failed);
93 match self.new.len() {
94 0 => line,
95 1 => format!("{line}, 1 new insight"),
96 new => format!("{line}, {new} new insights"),
97 }
98 }
99}
100
101impl LongWait {
102 pub fn line(&self) -> String {
105 format!(
106 "{} {} waited {}",
107 self.repository,
108 crate::api::job_title(self.workflow.as_deref(), &self.name, Some(self.job)),
109 crate::api::duration(self.waited)
110 )
111 }
112}
113
114#[cfg(test)]
115mod tests;