runnerd-core 0.1.5

Config, domain logic and API types shared by runnerd and its clients
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//! What insights and the digest mean as every client shows them: each insight's labels and tone, how many warn,
//! the window they come from, what the page says without any, and the digest's lines. Plain derivations of
//! [`Insights`] and [`Digest`]; the dashboard, `runnerctl`, the TUI and notifications only lay them out.

use super::{
    Digest, HostHazard, HostSpan, Insight, InsightKind, Insights, LongWait, ProfileQueue,
    RepositoryWaits, Severity, WaitSpread,
};
use crate::api::LabelledFigure;
use crate::api::units::short_date;
use crate::api::{HostMemory, Tone, duration};
use crate::config::ByteSize;
use std::fmt::Write as _;

impl InsightKind {
    /// Its name as a label: `Slower`, `Flaky`, `Near its cap`, `Cold cache`, `Memory pressure`, `CPU cap`,
    /// `Host memory`, `Host low`, `Waits`, or `Insight` for a kind from a later release.
    pub fn label(&self) -> &'static str {
        match self {
            InsightKind::Slower { .. } => "Slower",
            InsightKind::Flaky { .. } => "Flaky",
            InsightKind::NearCap { .. } => "Near its cap",
            InsightKind::ColdCache { .. } => "Cold cache",
            InsightKind::Throttled { .. } => "Memory pressure",
            InsightKind::CpuHeld { .. } => "CPU cap",
            InsightKind::HostHeld { .. } => "Host memory",
            InsightKind::HostLow { .. } => "Host low",
            InsightKind::Waits { .. } => "Waits",
            InsightKind::Unknown => "Insight",
        }
    }

    /// Its name as one word, the API's `kind`: `slower`, `flaky`, `near-cap`, `cold-cache`, `throttled`,
    /// `cpu-held`, `host-held`, `host-low` or `waits`; `unknown` for a kind from a later release.
    pub fn word(&self) -> &'static str {
        match self {
            InsightKind::Slower { .. } => "slower",
            InsightKind::Flaky { .. } => "flaky",
            InsightKind::NearCap { .. } => "near-cap",
            InsightKind::ColdCache { .. } => "cold-cache",
            InsightKind::Throttled { .. } => "throttled",
            InsightKind::CpuHeld { .. } => "cpu-held",
            InsightKind::HostHeld { .. } => "host-held",
            InsightKind::HostLow { .. } => "host-low",
            InsightKind::Waits { .. } => "waits",
            InsightKind::Unknown => "unknown",
        }
    }
}

impl Insight {
    /// What a [`HostHeld`](InsightKind::HostHeld) insight says: `3 jobs wait for the host's memory, the longest for
    /// 42m 10s (halyard/api: CI / build); programs outside CI hold 30G, most java 9.3G and claude 5.1G. Closing programs
    /// frees only part of that.` `longest` names the job that waited longest; `memory` is the
    /// reading that names who holds the memory. The part the programs named leave is the kernel, compressed swap and
    /// the many small programs, so the sentence never says closing them frees the whole figure.
    pub fn host_held_sentence(
        jobs: u32,
        waited: u64,
        longest: &str,
        memory: &HostMemory,
    ) -> String {
        let who = if jobs == 1 {
            "1 job waits".to_owned()
        } else {
            format!("{jobs} jobs wait")
        };
        format!(
            "{who} for the host's memory, the longest for {} ({longest}); {}. Closing programs frees only part of \
             that.",
            duration(waited),
            memory.outside_line()
        )
    }
}

/// The number an insight is about, short, for a figure beside its sentence: `6m 10s` over `from 4m 40s`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InsightFigure {
    /// The number: `6m 10s`, `2 of 31`, `8.1G`, `4%`.
    pub value: String,
    /// What it is, short and lower case: `from 4m 40s`, `runs passed on a re-run`.
    pub label: String,
}

impl Insight {
    /// The number it is about, with what it is; `None` for a kind from a later release. Its sentence says the rest.
    pub fn figure(&self) -> Option<InsightFigure> {
        let figure = |value: String, label: String| Some(InsightFigure { value, label });
        let of = |part: u32, whole: u32| format!("{part} of {whole}");
        match &self.kind {
            InsightKind::Slower { before, now, .. } => {
                figure(duration(*now), format!("from {}", duration(*before)))
            }
            InsightKind::Flaky { flaky, total } => {
                figure(of(*flaky, *total), "runs passed on a re-run".to_owned())
            }
            InsightKind::NearCap {
                profile, peak, cap, ..
            } => figure(
                ByteSize(*peak).to_string(),
                format!("p95 peak, of {profile}'s {}", ByteSize(*cap)),
            ),
            InsightKind::ColdCache { hits, lookups, .. } => {
                let percent = (u128::from(*hits) * 100 + u128::from(*lookups) / 2)
                    / u128::from((*lookups).max(1));
                figure(
                    format!("{}%", percent.min(100)),
                    format!("of {lookups} lookups hit"),
                )
            }
            InsightKind::Throttled { throttled, total } => {
                figure(of(*throttled, *total), "runs throttled".to_owned())
            }
            InsightKind::Waits { before, now } => figure(
                duration(*now),
                format!("p95 wait, from {}", duration(*before)),
            ),
            InsightKind::CpuHeld {
                profile,
                cpus,
                held,
                total,
                ..
            } => figure(
                of(*held, *total),
                format!("runs held back by {profile}'s {cpus} CPUs"),
            ),
            InsightKind::HostHeld { jobs, waited } => figure(
                duration(*waited),
                if *jobs == 1 {
                    "1 job waits for the host's memory".to_owned()
                } else {
                    format!("longest of {jobs} waits for the host's memory")
                },
            ),
            InsightKind::HostLow { lowest, .. } => figure(
                ByteSize(*lowest).to_string(),
                "least memory available".to_owned(),
            ),
            InsightKind::Unknown => None,
        }
    }
}

impl Severity {
    /// `warning` or `notice`.
    pub fn label(self) -> &'static str {
        match self {
            Severity::Warning => "warning",
            Severity::Notice => "notice",
        }
    }
}

impl Insight {
    /// What it means for the reader, by its severity alone, whatever its kind: a warning is worth a look, a notice
    /// a plain fact.
    pub fn tone(&self) -> Tone {
        match self.severity {
            Severity::Warning => Tone::Warn,
            Severity::Notice => Tone::Neutral,
        }
    }
}

impl Insights {
    /// How many insights warn.
    pub fn warning_count(&self) -> usize {
        self.insights
            .iter()
            .filter(|insight| insight.severity == Severity::Warning)
            .count()
    }

    /// What the insights page says over its list: how many insights it lists, how many of them warn, and the window
    /// they come from.
    pub fn heading(&self) -> InsightsHeading {
        let count = self.insights.len();
        let figure = match count {
            0 => "no insights".to_owned(),
            1 => "1 insight".to_owned(),
            count => format!("{count} insights"),
        };
        let note = match self.warning_count() {
            0 => format!(" {}", self.window_line()),
            1 => format!(", 1 warning, {}", self.window_line()),
            warnings => format!(", {warnings} warnings, {}", self.window_line()),
        };
        InsightsHeading { figure, note }
    }

    /// The whole days its window covers.
    pub fn days(&self) -> u64 {
        self.until.saturating_sub(self.since) / 86_400
    }

    /// Where its insights come from: `from the last 30 days`.
    pub fn window_line(&self) -> String {
        format!("from the last {} days", self.days())
    }

    /// What it says without any insight: `nothing stands out from the last 30 days`.
    pub fn empty_line(&self) -> String {
        format!("nothing stands out {}", self.window_line())
    }
}

/// What the insights page says over its list, as [`Insights::heading`] words it: `5 insights, 4 warnings, from the
/// last 14 days`, `1 insight from the last 14 days`, `no insights from the last 14 days`. A client may set the figure
/// apart; together, as its `Display` writes them, they read as one line.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InsightsHeading {
    /// How many it lists: `5 insights`, `1 insight`, `no insights`.
    pub figure: String,
    /// The rest, starting with what joins it to the figure: `, 4 warnings, from the last 14 days`, or ` from the last
    /// 14 days` when none warns.
    pub note: String,
}

impl std::fmt::Display for InsightsHeading {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(formatter, "{}{}", self.figure, self.note)
    }
}

impl WaitSpread {
    /// `value`, one of its percentiles, when it has jobs to take it over; `None` to show none.
    pub fn reading(&self, value: Option<u64>) -> Option<u64> {
        value.filter(|_| self.jobs > 0)
    }
}

impl Insight {
    /// What a [`HostLow`](InsightKind::HostLow) insight says: `On Nov 6 the host had under 6.2G available for 34s while
    /// a host credit stood, down to 4.1G. The credit lets runners start on memory the running ones may still grow
    /// into; [host] pooled_count = false makes the budgets count every reservation again.`
    pub fn host_low_sentence(day: u64, seconds: u64, lowest: u64, line: u64) -> String {
        format!(
            "On {} the host had under {} available for {} while a host credit stood, down to {}. The credit lets \
             runners start on memory the running ones may still grow into; [host] pooled_count = false makes the \
             budgets count every reservation again.",
            short_date(day),
            ByteSize(line),
            duration(seconds),
            ByteSize(lowest)
        )
    }
}

impl HostHazard {
    /// What the host's memory while CI ran is called where it shows, as a heading or before its line.
    pub const TITLE: &'static str = "Host memory while CI ran";

    /// The window in a line: `CI ran 9h 12m, 6h 10m of it on a host credit; under 19.4G available for 1h 04m (41m on
    /// a credit); under 6.2G available for 34s (34s on a credit); lowest 4.1G (4.5G on a credit); swap free at least
    /// 8.2G`.
    /// Without a host credit in it, nothing is said of one.
    pub fn line(&self) -> String {
        let credited = self.credited > 0;
        let share = |seconds: u64| {
            if credited {
                format!(" ({} on a credit)", duration(seconds))
            } else {
                String::new()
            }
        };
        let under = |line: u64, span: HostSpan| match span.seconds {
            0 => format!("never under {} available", ByteSize(line)),
            seconds => format!(
                "under {} available for {}{}",
                ByteSize(line),
                duration(seconds),
                share(span.credited)
            ),
        };
        let mut line = format!("CI ran {}", duration(self.running));
        if credited {
            let _ = write!(line, ", {} of it on a host credit", duration(self.credited));
        }
        let _ = write!(
            line,
            "; {}; {}; lowest {}",
            under(self.low_line, self.low),
            under(self.critical_line, self.critical),
            ByteSize(self.lowest)
        );
        if let Some(lowest) = self.lowest_credited {
            let _ = write!(line, " ({} on a credit)", ByteSize(lowest));
        }
        if let Some(swap) = self.lowest_swap_free {
            let _ = write!(line, "; swap free at least {}", ByteSize(swap));
        }
        line
    }

    /// The window's UTC day and its line, as `runnerctl insights` and the dashboard list the days: `Nov 6: CI ran …`.
    pub fn dated_line(&self) -> String {
        format!("{}: {}", short_date(self.since), self.line())
    }
}

impl Digest {
    /// Its day in a line: `12 jobs finished, 2 failed`, then `, 1 new insight` when some are new.
    pub fn summary_line(&self) -> String {
        let jobs = if self.finished == 1 { "job" } else { "jobs" };
        let line = format!("{} {jobs} finished, {} failed", self.finished, self.failed);
        match self.new.len() {
            0 => line,
            1 => format!("{line}, 1 new insight"),
            new => format!("{line}, {new} new insights"),
        }
    }

    /// The host's memory over its day in a line, `Host memory while CI ran: CI ran …`, or nothing when CI did not
    /// run.
    pub fn host_line(&self) -> Option<String> {
        self.host
            .as_ref()
            .map(|host| format!("{}: {}.", HostHazard::TITLE, host.line()))
    }
}

impl LongWait {
    /// It in a line: `halyard/api CI / test waited 1m 30s`, or `halyard/api job 7 waited 1m 30s` until the daemon
    /// knows the job's name.
    pub fn line(&self) -> String {
        format!(
            "{} {} waited {}",
            self.repository,
            crate::api::job_title(self.workflow.as_deref(), &self.name, Some(self.job)),
            duration(self.waited)
        )
    }
}

/// One of a repository's wait figures: its label, such as `Warm median`, and its value, `–` without jobs to take it
/// over.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WaitFigure {
    /// `Jobs`, `Median`, `p95`, `Warm median`, `Cold median` or `p95 the week before`.
    pub label: &'static str,
    /// The count of jobs, or a wait such as `4m 10s`.
    pub value: String,
}

impl RepositoryWaits {
    /// What a repository's waits are called where they show.
    pub const TITLE: &'static str = "Waits this week";

    /// Its figures, in the order they show: the week's jobs, their median and p95 wait, the warm and the cold
    /// median, and the week before's p95.
    pub fn figures(&self) -> Vec<WaitFigure> {
        let wait = |spread: &WaitSpread, value: Option<u64>| {
            spread
                .reading(value)
                .map_or_else(|| "–".to_owned(), duration)
        };
        let now = &self.now;
        let before = &self.before.all;
        [
            (crate::api::JOBS_HEADING, now.all.jobs.to_string()),
            ("Median", wait(&now.all, now.all.p50)),
            ("p95", wait(&now.all, now.all.p95)),
            ("Warm median", wait(&now.warm, now.warm.p50)),
            ("Cold median", wait(&now.cold, now.cold.p50)),
            ("p95 the week before", wait(before, before.p95)),
        ]
        .into_iter()
        .map(|(label, value)| WaitFigure { label, value })
        .collect()
    }
}

impl ProfileQueue {
    /// What the split of the profiles' queue time is called where it shows.
    pub const TITLE: &'static str = "Why jobs queued";
    /// The label of the wait before runnerd first listed a job.
    pub const BEFORE_LISTED: &'static str = "Before runnerd listed it";
    /// The label of the wait no plan held a job for.
    pub const UNCOVERED: &'static str = "No plan covered";

    /// It in a line: `jvm: 42 jobs waited 12h 30m in all`, then `; 3 jobs from before runnerd counted why are left
    /// out` when some are, and `; 2 jobs' causes are estimates, scaled into their waits` when some are;
    /// `jvm: 3 jobs ended from before runnerd counted why jobs wait` when none is counted.
    pub fn line(&self) -> String {
        let jobs = |count: u32| {
            if count == 1 {
                "1 job".to_owned()
            } else {
                format!("{count} jobs")
            }
        };
        if self.jobs == 0 {
            return format!(
                "{}: {} ended from before runnerd counted why jobs wait",
                self.profile,
                jobs(self.unrecorded)
            );
        }
        let mut line = format!(
            "{}: {} waited {} in all",
            self.profile,
            jobs(self.jobs),
            duration(seconds(self.waited_ms))
        );
        if self.unrecorded > 0 {
            let _ = write!(
                line,
                "; {} from before runnerd counted why {} left out",
                jobs(self.unrecorded),
                if self.unrecorded == 1 { "is" } else { "are" }
            );
        }
        match self.estimated {
            0 => {}
            1 => line.push_str("; 1 job's causes are estimates, scaled into its wait"),
            estimated => {
                let _ = write!(
                    line,
                    "; {estimated} jobs' causes are estimates, scaled into their waits"
                );
            }
        }
        line
    }

    /// What held the jobs, longest first, each with its share of their waits, taken from the milliseconds:
    /// `Host memory` `6h 10m` `49%`; then the wait before runnerd listed them and the wait no plan covered, when there
    /// was some. Empty when no job was counted.
    pub fn figures(&self) -> Vec<LabelledFigure> {
        let share = |ms: u64| {
            let percent = (u128::from(ms) * 100 + u128::from(self.waited_ms) / 2)
                / u128::from(self.waited_ms.max(1));
            format!("{}%", percent.min(100))
        };
        let figure =
            |label: &str, ms: u64| LabelledFigure::new(label, duration(seconds(ms))).of(share(ms));
        let mut figures: Vec<LabelledFigure> = self
            .causes
            .iter()
            .map(|cause| figure(cause.cause.label(), cause.waited_ms))
            .collect();
        for (label, ms) in [
            (Self::BEFORE_LISTED, self.before_listed_ms),
            (Self::UNCOVERED, self.uncovered_ms),
        ] {
            if ms > 0 {
                figures.push(figure(label, ms));
            }
        }
        figures
    }
}

/// `ms` in whole seconds, rounded, as durations show.
fn seconds(ms: u64) -> u64 {
    ms.saturating_add(500) / 1000
}

#[cfg(test)]
mod tests;