1use super::{Activity, ProfileStatus, RunnerStatus, SlotStatus, Status, Tone, duration};
5
6#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum SlotState {
9 Idle,
11 Reseeding,
13 Starting {
15 repository: String,
17 },
18 Warm {
20 repository: String,
22 },
23 Running {
25 repository: String,
27 },
28}
29
30impl SlotState {
31 pub fn of_runner(runner: &RunnerStatus) -> SlotState {
33 let repository = runner.repository.clone();
34 if runner.busy {
35 SlotState::Running { repository }
36 } else if runner.warm {
37 SlotState::Warm { repository }
38 } else {
39 SlotState::Starting { repository }
40 }
41 }
42
43 pub fn word(&self) -> &'static str {
45 match self {
46 SlotState::Idle => "idle",
47 SlotState::Reseeding => "reseeding",
48 SlotState::Starting { .. } => "starting",
49 SlotState::Warm { .. } => "warm",
50 SlotState::Running { .. } => "running",
51 }
52 }
53
54 pub fn line(&self) -> String {
57 match self {
58 SlotState::Idle => "idle".to_owned(),
59 SlotState::Reseeding => "reseeding the snapshot".to_owned(),
60 SlotState::Starting { repository } => {
61 format!("a runner for {repository} waiting for its job")
62 }
63 SlotState::Warm { repository } => {
64 format!("a warm runner for {repository}, waiting for a job")
65 }
66 SlotState::Running { repository } => format!("running a job of {repository}"),
67 }
68 }
69
70 pub fn is_running(&self) -> bool {
72 matches!(self, SlotState::Running { .. })
73 }
74
75 pub fn tone(&self) -> Tone {
77 match self {
78 SlotState::Running { .. } => Tone::Neutral,
79 _ => Tone::Quiet,
80 }
81 }
82}
83
84impl Status {
85 pub fn slot_state(profile: &ProfileStatus, slot: &SlotStatus) -> SlotState {
88 match &slot.runner {
89 Some(runner) => SlotState::of_runner(runner),
90 None if profile.reseeding && slot.slot == 1 => SlotState::Reseeding,
91 None => SlotState::Idle,
92 }
93 }
94
95 pub fn health(&self, now: u64) -> Health {
97 let mut health = Health {
98 slots: 0,
99 running: 0,
100 starting: 0,
101 warm: 0,
102 waiting: self.queued.len(),
103 oldest_wait: self
104 .queued
105 .iter()
106 .filter(|job| job.queued_at > 0)
107 .map(|job| now.saturating_sub(job.queued_at))
108 .max(),
109 };
110 for profile in &self.profiles {
111 for slot in &profile.slots {
112 health.slots += 1;
113 match Status::slot_state(profile, slot) {
114 SlotState::Running { .. } => health.running += 1,
115 SlotState::Starting { .. } => health.starting += 1,
116 SlotState::Warm { .. } => health.warm += 1,
117 SlotState::Idle | SlotState::Reseeding => {}
118 }
119 }
120 }
121 health
122 }
123}
124
125#[derive(Debug, Clone, Copy, PartialEq, Eq)]
127pub struct Health {
128 pub slots: usize,
130 pub running: usize,
132 pub starting: usize,
134 pub warm: usize,
136 pub waiting: usize,
138 pub oldest_wait: Option<u64>,
140}
141
142impl Health {
143 pub fn waiting_tone(&self) -> Tone {
145 if self.waiting > 0 {
146 Tone::Warn
147 } else {
148 Tone::Neutral
149 }
150 }
151}
152
153#[derive(Debug, Clone, PartialEq, Eq)]
156pub struct Figure {
157 pub kind: HealthKind,
159 pub label: &'static str,
161 pub value: String,
163 pub of: Option<String>,
165 pub notes: Vec<String>,
167 pub tone: Tone,
169}
170
171#[derive(Debug, Clone, Copy, PartialEq, Eq)]
173pub enum HealthKind {
174 Running,
176 Waiting,
178 Wait,
180 Failed,
182}
183
184impl Health {
185 pub fn figures(&self, activity: Option<&Activity>) -> Vec<Figure> {
189 let mut running = Vec::new();
190 if self.starting > 0 {
191 let s = if self.starting == 1 { "" } else { "s" };
192 running.push(format!("{} runner{s} starting", self.starting));
193 }
194 if self.warm > 0 {
195 running.push(format!("{} warm, waiting for jobs", self.warm));
196 }
197 let waiting = match self.oldest_wait {
198 Some(oldest) => vec![format!("oldest {}", duration(oldest))],
199 None => Vec::new(),
200 };
201 let mut figures = vec![
202 Figure {
203 kind: HealthKind::Running,
204 label: "Running",
205 value: self.running.to_string(),
206 of: Some(format!("of {} slots", self.slots)),
207 notes: running,
208 tone: Tone::Neutral,
209 },
210 Figure {
211 kind: HealthKind::Waiting,
212 label: "Waiting",
213 value: self.waiting.to_string(),
214 of: None,
215 notes: waiting,
216 tone: self.waiting_tone(),
217 },
218 ];
219 if let Some(day) = activity {
220 let started = day.started.len();
221 figures.push(Figure {
222 kind: HealthKind::Wait,
223 label: "Wait p95, 24 h",
224 value: day.wait_p95().map_or_else(|| "–".to_owned(), duration),
225 of: None,
226 notes: vec![format!(
227 "{started} job{} started",
228 if started == 1 { "" } else { "s" }
229 )],
230 tone: Tone::Neutral,
231 });
232 let runner =
233 (day.runner_failed > 0).then(|| format!("{} by their runner", day.runner_failed));
234 figures.push(Figure {
235 kind: HealthKind::Failed,
236 label: "Failed, 24 h",
237 value: day.failed.to_string(),
238 of: Some(format!("of {} finished", day.finished)),
239 notes: runner.into_iter().collect(),
240 tone: day.failed_tone(),
241 });
242 }
243 figures
244 }
245}
246
247impl Activity {
248 pub fn failed_tone(&self) -> Tone {
251 if self.runner_failed > 0 {
252 Tone::Bad
253 } else {
254 Tone::Neutral
255 }
256 }
257}
258
259#[cfg(test)]
260mod tests;