Skip to main content

runnerd_core/api/
insights.rs

1//! Insights as clients read them: what got slower, what is flaky, what nears its memory cap, what memory pressure
2//! throttled, what its CPU cap held back and how long jobs wait, computed from the job history (ADR 0051); the daily digest; and what the
3//! daemon's notifications carry. Times are seconds since the Unix epoch, durations seconds, sizes bytes.
4
5use serde::{Deserialize, Serialize};
6
7use super::RunnerFailure;
8use super::jobs::JobSummary;
9
10mod shown;
11
12/// The window `GET /v1/insights` reads by default, in days.
13pub const INSIGHT_DAYS: u32 = 14;
14
15/// The longest window `GET /v1/insights` reads, in days.
16pub const INSIGHT_DAYS_MAX: u32 = 90;
17
18/// `GET /v1/insights` query parameters.
19#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
20pub struct InsightsQuery {
21    /// Days of history to read; default [`INSIGHT_DAYS`], at most [`INSIGHT_DAYS_MAX`].
22    pub days: Option<u32>,
23}
24
25/// `GET /v1/insights`: what the history says about the window.
26#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
27pub struct Insights {
28    /// The start of the window.
29    pub since: u64,
30    /// When it was read: the end of the window.
31    pub until: u64,
32    /// Warnings first, then by repository, workflow and job.
33    pub insights: Vec<Insight>,
34    /// Each repository's waits this week and the week before, by repository. Always the last 14 days, whatever the
35    /// window.
36    pub waits: Vec<RepositoryWaits>,
37}
38
39/// How much an insight matters.
40#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
41#[serde(rename_all = "kebab-case")]
42pub enum Severity {
43    /// Worth acting on: notifications send these.
44    Warning,
45    /// Worth knowing.
46    Notice,
47}
48
49/// One thing the history says, about one job or one repository.
50#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
51pub struct Insight {
52    /// The same while it holds, such as `slower:halyard/api/CI/test`.
53    pub key: String,
54    /// How much it matters.
55    pub severity: Severity,
56    /// `owner/name`.
57    pub repository: String,
58    /// The workflow's name; `None` for a repository's waits, or when unknown.
59    pub workflow: Option<String>,
60    /// The job's name; `None` for a repository's waits.
61    pub job: Option<String>,
62    /// What it is, in one line.
63    pub sentence: String,
64    /// What it is, with its figures.
65    #[serde(flatten)]
66    pub kind: InsightKind,
67    /// The runs that show it, newest first, at most 5.
68    pub runs: Vec<RunLink>,
69}
70
71/// What an insight is, tagged by `kind`.
72#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
73#[serde(rename_all = "kebab-case", tag = "kind")]
74pub enum InsightKind {
75    /// A job's successful runs on its busiest branch take longer than they did, and stayed so.
76    Slower {
77        /// The branch.
78        branch: Option<String>,
79        /// The median run time before, in seconds.
80        before: u64,
81        /// The median run time of the last runs, in seconds.
82        now: u64,
83        /// The first slow run after which no two runs in a row were fast.
84        since: Option<RunLink>,
85    },
86    /// A job failed and then passed on a re-run of the same run.
87    Flaky {
88        /// Runs in which it did.
89        flaky: u32,
90        /// The job's runs in the window.
91        total: u32,
92    },
93    /// A job's memory peaks near its profile's cap.
94    NearCap {
95        /// The profile.
96        profile: String,
97        /// The 95th percentile of its last runs' memory peaks.
98        peak: u64,
99        /// The profile's `memory`.
100        cap: u64,
101        /// What auto sizing does about it.
102        auto: AutoNote,
103    },
104    /// A job's runs that write to the build cache find almost nothing in it: its cache keys differ from run to run.
105    ColdCache {
106        /// The branch those runs are on.
107        branch: Option<String>,
108        /// Lookups that found an entry, over those runs.
109        hits: u64,
110        /// Lookups over those runs.
111        lookups: u64,
112    },
113    /// The kernel throttled a repository's jobs' downloads for memory pressure (`JobMetrics::sock_throttled`):
114    /// the host, or a job at its cap, was short of memory.
115    Throttled {
116        /// Runs it throttled.
117        throttled: u32,
118        /// The repository's runs in the window.
119        total: u32,
120    },
121    /// A repository's jobs wait longer this week than the week before.
122    Waits {
123        /// The 95th percentile of waits the week before, in seconds.
124        before: u64,
125        /// The 95th percentile of waits this week, in seconds.
126        now: u64,
127    },
128    /// A job's CPU cap held back its runs: their quota throttled them in 15% of their run or more (ADR 0079).
129    CpuHeld {
130        /// The profile its runs ran in.
131        profile: String,
132        /// The profile's `cpus`, the cap those runs had.
133        cpus: u32,
134        /// Runs the cap held back.
135        held: u32,
136        /// The job's last runs at that cap that measured their throttling.
137        total: u32,
138        /// Under `measured` admission with auto sizing, the `cpus` the profile was raised from when that raise did
139        /// not make this job faster or less throttled: its runs ask for no further raise. What auto sizing does
140        /// next for the profile, on the other jobs' runs or on the averages, is `runnerctl sizing`'s to say.
141        raised_from: Option<u32>,
142        /// The `cpus` to set by hand; `None` while auto sizing raises them.
143        set: Option<u32>,
144    },
145    /// A kind from a later release, which this one reads only by its sentence. The daemon never sends it.
146    #[serde(other)]
147    Unknown,
148}
149
150/// What auto sizing does about a job near its cap.
151#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
152#[serde(rename_all = "kebab-case")]
153pub enum AutoNote {
154    /// It raises the profile's `memory` when a run meets the cap.
155    Raises,
156    /// The profile is at its `max_memory`: it cannot.
157    AtMax,
158    /// It is off.
159    Off,
160}
161
162/// One run of a job, to open in the history or on GitHub.
163#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
164pub struct RunLink {
165    /// GitHub's job ID, the history's too.
166    pub job: u64,
167    /// The workflow run.
168    pub run_id: u64,
169    /// The run's attempt.
170    pub run_attempt: u32,
171    /// The branch.
172    pub branch: Option<String>,
173    /// The job's page on GitHub.
174    pub url: Option<String>,
175    /// When it finished.
176    pub at: u64,
177}
178
179/// A repository's waits this week against the week before.
180#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
181pub struct RepositoryWaits {
182    /// `owner/name`.
183    pub repository: String,
184    /// The jobs that started in the last 7 days.
185    pub now: WeekWaits,
186    /// The jobs that started in the 7 days before.
187    pub before: WeekWaits,
188}
189
190/// The waits of the jobs that started in a week, from queued to started, leaving out the time before the history's
191/// start and the time no runnerd ran (ADR 0081).
192#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
193pub struct WeekWaits {
194    /// Every job.
195    pub all: WaitSpread,
196    /// The jobs that found a runner already started: a warm runner (ADR 0050).
197    pub warm: WaitSpread,
198    /// The jobs whose runner started after they were queued.
199    pub cold: WaitSpread,
200}
201
202/// How long some jobs waited.
203#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
204pub struct WaitSpread {
205    /// How many.
206    pub jobs: u32,
207    /// The median, in seconds; `None` without jobs.
208    pub p50: Option<u64>,
209    /// The 95th percentile by nearest rank, in seconds; `None` without jobs.
210    pub p95: Option<u64>,
211}
212
213/// `GET /v1/digest`: the last 24 hours.
214#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
215pub struct Digest {
216    /// 24 hours before `until`.
217    pub since: u64,
218    /// When it was read.
219    pub until: u64,
220    /// Jobs that finished: completed, or lost with their runner.
221    pub finished: u64,
222    /// Of those, the ones that failed, timed out or were lost.
223    pub failed: u64,
224    /// The insights that hold now and did not 24 hours ago.
225    pub new: Vec<Insight>,
226    /// The three longest waits of the jobs that started, longest first.
227    pub waits: Vec<LongWait>,
228    /// What the month's job minutes would have cost on GitHub's runners (ADR 0072); `None` when the history could
229    /// not say.
230    #[serde(default, skip_serializing_if = "Option::is_none")]
231    pub savings: Option<super::SavedWindow>,
232}
233
234/// A job that waited.
235#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
236pub struct LongWait {
237    /// GitHub's job ID.
238    pub job: u64,
239    /// `owner/name`.
240    pub repository: String,
241    /// The workflow's name.
242    pub workflow: Option<String>,
243    /// The job's name.
244    pub name: String,
245    /// Seconds from queued to started, leaving out the time before the history's start and the time no runnerd
246    /// ran (ADR 0081).
247    pub waited: u64,
248    /// The job's page on GitHub.
249    pub url: Option<String>,
250}
251
252/// What a notification target is sent, and the events `[notify]` targets choose from.
253#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
254#[serde(rename_all = "kebab-case")]
255pub enum NotifyEvent {
256    /// A job failed or timed out.
257    Failed,
258    /// A job's runner vanished before GitHub said the job completed.
259    Lost,
260    /// A runner could not be registered or started, and its repository is held back.
261    Runner,
262    /// A new warning insight.
263    Insights,
264    /// The daily digest.
265    Digest,
266    /// `runnerctl notify test`; not one a target chooses.
267    Test,
268}
269
270/// How urgent a notification is, as ntfy ranks it.
271#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
272#[serde(rename_all = "kebab-case")]
273pub enum Priority {
274    /// ntfy's 2: no sound.
275    Low,
276    /// ntfy's 3.
277    Default,
278    /// ntfy's 4: a longer vibration.
279    High,
280}
281
282/// What a webhook target is posted, as JSON; an ntfy target gets its title, message, priority and link.
283#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
284pub struct Notification {
285    /// What it is about.
286    pub event: NotifyEvent,
287    /// One short line.
288    pub title: String,
289    /// A few lines.
290    pub message: String,
291    /// What to open: a job's page on GitHub, or nothing.
292    pub url: Option<String>,
293    /// How urgent.
294    pub priority: Priority,
295    /// The jobs it is about: failed or lost.
296    #[serde(default, skip_serializing_if = "Vec::is_empty")]
297    pub jobs: Vec<JobSummary>,
298    /// The runners that could not start.
299    #[serde(default, skip_serializing_if = "Vec::is_empty")]
300    pub runners: Vec<RunnerFailure>,
301    /// The insights it is about.
302    #[serde(default, skip_serializing_if = "Vec::is_empty")]
303    pub insights: Vec<Insight>,
304    /// The digest.
305    #[serde(default, skip_serializing_if = "Option::is_none")]
306    pub digest: Option<Digest>,
307}
308
309/// `POST /v1/notify/test` query parameters.
310#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
311pub struct NotifyTestQuery {
312    /// Only the target with this name; default every one.
313    pub target: Option<String>,
314}
315
316/// `POST /v1/notify/test`: what each target did with a test message.
317#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
318pub struct NotifyTest {
319    /// In the config's order.
320    pub targets: Vec<Delivery>,
321}
322
323/// What one target did with a message.
324#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
325pub struct Delivery {
326    /// The target's name.
327    pub target: String,
328    /// Why it was not delivered: an HTTP status or a connection error, never the URL. `None` when it was.
329    pub error: Option<String>,
330}