runnerd_core/api/insights/
shown.rs1use super::{Digest, Insight, InsightKind, Insights, LongWait, Severity, WaitSpread};
6use crate::api::{HostMemory, Tone, duration};
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::HostHeld { .. } => "Host memory",
20 InsightKind::Waits { .. } => "Waits",
21 InsightKind::Unknown => "Insight",
22 }
23 }
24
25 pub fn word(&self) -> &'static str {
28 match self {
29 InsightKind::Slower { .. } => "slower",
30 InsightKind::Flaky { .. } => "flaky",
31 InsightKind::NearCap { .. } => "near-cap",
32 InsightKind::ColdCache { .. } => "cold-cache",
33 InsightKind::Throttled { .. } => "throttled",
34 InsightKind::CpuHeld { .. } => "cpu-held",
35 InsightKind::HostHeld { .. } => "host-held",
36 InsightKind::Waits { .. } => "waits",
37 InsightKind::Unknown => "unknown",
38 }
39 }
40}
41
42impl Insight {
43 pub fn host_held_sentence(
49 jobs: u32,
50 waited: u64,
51 longest: &str,
52 memory: &HostMemory,
53 ) -> String {
54 let who = if jobs == 1 {
55 "1 job waits".to_owned()
56 } else {
57 format!("{jobs} jobs wait")
58 };
59 format!(
60 "{who} for the host's memory, the longest for {} ({longest}); {}. Closing programs frees only part of \
61 that.",
62 duration(waited),
63 memory.outside_line()
64 )
65 }
66}
67
68impl Severity {
69 pub fn label(self) -> &'static str {
71 match self {
72 Severity::Warning => "warning",
73 Severity::Notice => "notice",
74 }
75 }
76}
77
78impl Insight {
79 pub fn tone(&self) -> Tone {
82 match self.severity {
83 Severity::Warning => Tone::Warn,
84 Severity::Notice => Tone::Neutral,
85 }
86 }
87}
88
89impl Insights {
90 pub fn warning_count(&self) -> usize {
92 self.insights
93 .iter()
94 .filter(|insight| insight.severity == Severity::Warning)
95 .count()
96 }
97
98 pub fn warning_line(&self) -> Option<String> {
100 match self.warning_count() {
101 0 => None,
102 1 => Some("1 insight".to_owned()),
103 count => Some(format!("{count} insights")),
104 }
105 }
106
107 pub fn days(&self) -> u64 {
109 self.until.saturating_sub(self.since) / 86_400
110 }
111
112 pub fn window_line(&self) -> String {
114 format!("from the last {} days", self.days())
115 }
116
117 pub fn empty_line(&self) -> String {
119 format!("nothing stands out {}", self.window_line())
120 }
121}
122
123impl WaitSpread {
124 pub fn reading(&self, value: Option<u64>) -> Option<u64> {
126 value.filter(|_| self.jobs > 0)
127 }
128}
129
130impl Digest {
131 pub fn summary_line(&self) -> String {
133 let jobs = if self.finished == 1 { "job" } else { "jobs" };
134 let line = format!("{} {jobs} finished, {} failed", self.finished, self.failed);
135 match self.new.len() {
136 0 => line,
137 1 => format!("{line}, 1 new insight"),
138 new => format!("{line}, {new} new insights"),
139 }
140 }
141}
142
143impl LongWait {
144 pub fn line(&self) -> String {
147 format!(
148 "{} {} waited {}",
149 self.repository,
150 crate::api::job_title(self.workflow.as_deref(), &self.name, Some(self.job)),
151 duration(self.waited)
152 )
153 }
154}
155
156#[cfg(test)]
157mod tests;