1use serde::{Deserialize, Deserializer, Serialize};
8
9use super::{
10 CheckStatus, Doctor, DoctorState, Insight, InsightKind, Insights, QueuedJob, Severity, Status,
11 Tone, View, Waiting,
12};
13
14pub const ATTENTION_MAX: usize = 50;
16
17#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
19pub struct Attention {
20 pub instance: String,
23 pub revision: u64,
27 pub items: Vec<AttentionItem>,
29 #[serde(default, skip_serializing_if = "is_zero")]
31 pub omitted: u64,
32}
33
34#[expect(
35 clippy::trivially_copy_pass_by_ref,
36 reason = "serde's skip_serializing_if passes a reference"
37)]
38fn is_zero(count: &u64) -> bool {
39 *count == 0
40}
41
42#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
44pub struct AttentionItem {
45 pub key: String,
47 pub kind: AttentionKind,
49 #[serde(deserialize_with = "tone_or_neutral")]
51 pub tone: Tone,
52 pub title: String,
54 pub subject: Subject,
56 #[serde(default, skip_serializing_if = "Option::is_none")]
58 pub since: Option<u64>,
59 #[serde(
61 default,
62 skip_serializing_if = "Option::is_none",
63 deserialize_with = "lenient"
64 )]
65 pub fix: Option<Fix>,
66}
67
68#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
70#[serde(rename_all = "kebab-case")]
71pub enum AttentionKind {
72 CannotStart,
75 HostMemory,
77 Doctor,
79 DoctorUnavailable,
81 Insight,
83 GithubListing,
85 GithubAuth,
87 RateLimited,
89 #[serde(other)]
91 Unknown,
92}
93
94#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
96#[serde(rename_all = "kebab-case", tag = "kind")]
97pub enum Subject {
98 Job {
100 id: u64,
102 repository: String,
104 name: String,
106 },
107 Repository {
109 name: String,
111 },
112 Check {
114 name: String,
116 },
117 Insight {
119 key: String,
121 },
122 Host,
124 Github,
126 #[serde(other)]
128 Unknown,
129}
130
131#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
134#[serde(rename_all = "kebab-case", tag = "kind")]
135pub enum Fix {
136 Command {
138 command: String,
140 },
141 Config {
143 key: String,
145 },
146 Open {
148 view: View,
150 },
151 Text {
153 text: String,
155 },
156}
157
158impl Fix {
159 pub fn line(&self) -> String {
162 match self {
163 Fix::Command { command } => command.clone(),
164 Fix::Config { key } => format!("Change {key} in the config"),
165 Fix::Open { view } => format!("Open {}", view.title()),
166 Fix::Text { text } => text.clone(),
167 }
168 }
169}
170
171impl Attention {
172 pub fn new(instance: String, revision: u64, mut items: Vec<AttentionItem>) -> Attention {
175 let omitted = items.len().saturating_sub(ATTENTION_MAX);
176 items.truncate(ATTENTION_MAX);
177 Attention {
178 instance,
179 revision,
180 items,
181 omitted: omitted as u64,
182 }
183 }
184
185 pub fn same_content(&self, other: &Attention) -> bool {
187 self.items == other.items && self.omitted == other.omitted
188 }
189
190 pub fn more(&self, shown: usize) -> Option<String> {
192 let rest = self.items.len().saturating_sub(shown) as u64 + self.omitted;
193 (rest > 0).then(|| format!("and {rest} more"))
194 }
195}
196
197pub fn attention_items(
201 status: &Status,
202 doctor: &Doctor,
203 insights: Option<&Insights>,
204 now: u64,
205) -> Vec<AttentionItem> {
206 let mut items = cannot_start(status);
207 items.extend(host_memory(status, insights));
208 items.extend(doctor_items(doctor, now));
209 items.extend(
210 insights
211 .into_iter()
212 .flat_map(|insights| &insights.insights)
213 .filter_map(insight_item),
214 );
215 items.extend(github(status, now));
216 items.sort_by(|a, b| {
217 rank(a.tone)
218 .cmp(&rank(b.tone))
219 .then_with(|| b.since.is_some().cmp(&a.since.is_some()))
220 .then_with(|| b.since.cmp(&a.since))
221 .then_with(|| a.key.cmp(&b.key))
222 });
223 items
224}
225
226fn rank(tone: Tone) -> u8 {
228 match tone {
229 Tone::Bad => 0,
230 Tone::Warn => 1,
231 Tone::Neutral => 2,
232 Tone::Quiet => 3,
233 Tone::Good => 4,
234 }
235}
236
237fn cannot_start(status: &Status) -> Vec<AttentionItem> {
239 let mut groups: Vec<(String, Vec<&QueuedJob>)> = Vec::new();
240 for job in &status.queued {
241 let Some(waiting) = job
242 .waiting
243 .as_ref()
244 .filter(|waiting| waiting.cannot_start())
245 else {
246 continue;
247 };
248 let key = match waiting {
249 Waiting::NoPool { labels } => format!(
250 "cannot-start:no-pool:{}:{}",
251 job.repository,
252 labels.join(",")
253 ),
254 _ => format!("cannot-start:paused:{}", job.repository),
255 };
256 match groups.iter_mut().find(|(group, _)| *group == key) {
257 Some((_, jobs)) => jobs.push(job),
258 None => groups.push((key, vec![job])),
259 }
260 }
261 groups
262 .into_iter()
263 .filter_map(|(key, jobs)| {
264 let first = *jobs.first()?;
265 let repository = &first.repository;
266 let (what, subject) = match jobs.as_slice() {
267 [job] => (
268 format!("{} of {repository}", job.name),
269 Subject::Job {
270 id: job.id,
271 repository: repository.clone(),
272 name: job.name.clone(),
273 },
274 ),
275 _ => (
276 format!("{} queued jobs of {repository}", jobs.len()),
277 Subject::Repository {
278 name: repository.clone(),
279 },
280 ),
281 };
282 let (why, fix) = match first.waiting.as_ref()? {
283 Waiting::NoPool { labels } if labels.is_empty() => (
284 "it asks for no labels, so no profile serves it".to_owned(),
285 Fix::Config {
286 key: "repositories.labels".to_owned(),
287 },
288 ),
289 Waiting::NoPool { labels } => (
290 format!(
291 "no profile of {repository} serves the {} {}",
292 if labels.len() == 1 { "label" } else { "labels" },
293 labels.join(", ")
294 ),
295 Fix::Config {
296 key: "repositories.labels".to_owned(),
297 },
298 ),
299 _ => (
300 format!("{repository} is paused"),
301 Fix::Command {
302 command: format!("runnerctl resume {repository}"),
303 },
304 ),
305 };
306 Some(AttentionItem {
307 key,
308 kind: AttentionKind::CannotStart,
309 tone: Tone::Warn,
310 title: format!("runnerd cannot start {what} with the current configuration: {why}"),
311 subject,
312 since: jobs
313 .iter()
314 .map(|job| job.queued_at)
315 .filter(|at| *at > 0)
316 .min(),
317 fix: Some(fix),
318 })
319 })
320 .collect()
321}
322
323fn host_memory(status: &Status, insights: Option<&Insights>) -> Option<AttentionItem> {
328 let held = insights?
329 .insights
330 .iter()
331 .find(|insight| matches!(insight.kind, InsightKind::HostHeld { .. }))?;
332 status
333 .queued
334 .iter()
335 .any(|job| matches!(job.waiting, Some(Waiting::HostMemory { .. })))
336 .then(|| AttentionItem {
337 key: "host-memory".to_owned(),
338 kind: AttentionKind::HostMemory,
339 tone: Tone::Warn,
340 title: held.sentence.clone(),
341 subject: Subject::Host,
342 since: None,
343 fix: Some(Fix::Open {
344 view: View::Overview,
345 }),
346 })
347}
348
349fn doctor_items(doctor: &Doctor, now: u64) -> Vec<AttentionItem> {
351 let at = match &doctor.state {
352 None => None,
354 Some(DoctorState::Ready { at }) => Some(*at),
355 Some(
356 state @ (DoctorState::NotRun
357 | DoctorState::Stale { .. }
358 | DoctorState::Failed { .. }
359 | DoctorState::Unknown),
360 ) => {
361 let (tone, since) = match state {
362 DoctorState::Failed { at, .. } => (Tone::Warn, Some(*at)),
363 DoctorState::Stale { at } => (Tone::Quiet, Some(*at)),
364 _ => (Tone::Quiet, None),
365 };
366 return state
367 .line(now)
368 .map(|title| AttentionItem {
369 key: "doctor-unavailable".to_owned(),
370 kind: AttentionKind::DoctorUnavailable,
371 tone,
372 title,
373 subject: Subject::Host,
374 since,
375 fix: Some(Fix::Open {
376 view: View::Settings,
377 }),
378 })
379 .into_iter()
380 .collect();
381 }
382 };
383 let shown: Vec<_> = doctor
384 .checks
385 .iter()
386 .filter(|check| matches!(check.status, CheckStatus::Fail | CheckStatus::Warn))
387 .filter(|check| check.name != super::JOB_ACTIONS_CHECK)
389 .collect();
390 let mut keys: Vec<String> = Vec::new();
391 shown
392 .iter()
393 .map(|check| {
394 let repeated = shown
397 .iter()
398 .filter(|other| other.name == check.name)
399 .count()
400 > 1;
401 let mut key = if repeated {
402 format!("doctor:{}:{}", check.name, check.detail)
403 } else {
404 format!("doctor:{}", check.name)
405 };
406 let twins = keys.iter().filter(|seen| **seen == key).count();
407 keys.push(key.clone());
408 if twins > 0 {
409 key = format!("{key}#{}", twins + 1);
410 }
411 AttentionItem {
412 key,
413 kind: AttentionKind::Doctor,
414 tone: check.status.tone(),
415 title: format!("{}: {}", check.name, check.detail),
416 subject: Subject::Check {
417 name: check.name.clone(),
418 },
419 since: at,
420 fix: Some(match &check.fix {
421 Some(text) => Fix::Text { text: text.clone() },
422 None => Fix::Open {
423 view: View::Settings,
424 },
425 }),
426 }
427 })
428 .collect()
429}
430
431fn insight_item(insight: &Insight) -> Option<AttentionItem> {
434 let current = !matches!(
435 insight.kind,
436 InsightKind::HostHeld { .. } | InsightKind::HostLow { .. } | InsightKind::Unknown
437 );
438 (insight.severity == Severity::Warning && current).then(|| AttentionItem {
439 key: format!("insight:{}", insight.key),
440 kind: AttentionKind::Insight,
441 tone: Tone::Warn,
442 title: insight.sentence.clone(),
443 subject: Subject::Insight {
444 key: insight.key.clone(),
445 },
446 since: insight.runs.first().map(|run| run.at),
447 fix: Some(Fix::Open {
448 view: View::Insights,
449 }),
450 })
451}
452
453fn github(status: &Status, now: u64) -> Vec<AttentionItem> {
455 let failing: Vec<_> = status
456 .repositories
457 .iter()
458 .filter_map(|repository| Some((repository.name.as_str(), repository.listing.as_ref()?)))
459 .collect();
460 let token = failing
461 .iter()
462 .filter(|(_, failure)| failure.problem.is_token())
463 .min_by_key(|(_, failure)| failure.since);
464 let mut items: Vec<AttentionItem> = token
465 .map(|(_, failure)| AttentionItem {
466 key: "github-auth".to_owned(),
467 kind: AttentionKind::GithubAuth,
468 tone: Tone::Bad,
469 title: match failure.problem {
470 super::ListingProblem::NoToken => {
471 "runnerd has no GitHub token, so it lists no repository's queued jobs"
472 .to_owned()
473 }
474 _ => {
475 "GitHub refuses runnerd's token (401), so it lists no repository's queued jobs"
476 .to_owned()
477 }
478 },
479 subject: Subject::Github,
480 since: Some(failure.since),
481 fix: Some(Fix::Open {
482 view: View::Settings,
483 }),
484 })
485 .into_iter()
486 .collect();
487 items.extend(
488 failing
489 .iter()
490 .filter(|(_, failure)| !failure.problem.is_token())
491 .map(|(name, failure)| AttentionItem {
492 key: format!("github-listing:{name}"),
493 kind: AttentionKind::GithubListing,
494 tone: Tone::Warn,
495 title: failure.line(name),
496 subject: Subject::Repository {
497 name: (*name).to_owned(),
498 },
499 since: Some(failure.since),
500 fix: matches!(failure.problem, super::ListingProblem::Refused { .. }).then_some(
501 Fix::Open {
502 view: View::Settings,
503 },
504 ),
505 }),
506 );
507 let limited = status.github_limited_until.is_some_and(|until| until > now);
509 items.extend(
510 status
511 .github_line()
512 .filter(|_| limited)
513 .map(|line| AttentionItem {
514 key: "rate-limited".to_owned(),
515 kind: AttentionKind::RateLimited,
516 tone: Tone::Warn,
517 title: line.to_owned(),
518 subject: Subject::Github,
519 since: None,
520 fix: None,
521 }),
522 );
523 items
524}
525
526fn lenient<'de, D: Deserializer<'de>, T: Deserialize<'de>>(
529 deserializer: D,
530) -> Result<Option<T>, D::Error> {
531 #[derive(Deserialize)]
532 #[serde(untagged)]
533 enum Read<T> {
534 Known(T),
535 Other(serde::de::IgnoredAny),
536 }
537 Ok(match Option::<Read<T>>::deserialize(deserializer)? {
538 Some(Read::Known(value)) => Some(value),
539 Some(Read::Other(_)) | None => None,
540 })
541}
542
543fn tone_or_neutral<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Tone, D::Error> {
544 Ok(lenient(deserializer)?.unwrap_or(Tone::Neutral))
545}
546
547#[cfg(all(test, any(feature = "host", feature = "client")))]
549mod tests;