runnerd-web 0.0.4

The web dashboard runnerd serves on loopback: a client of the daemon's API
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
//! `/`: the memory budget as blocks, every slot and the job it runs, the slice's CPU and memory over the last half
//! hour, the repositories with pause and resume, and the latest jobs.

use std::collections::BTreeMap;

use maud::{Markup, html};
use runnerd_core::api::{
    HostSample, HostSamples, Job, JobPage, JobQuery, JobState, JobSummary, ProfileStatus,
    RepositoryStatus, Sample, SampleSeries, SlotStatus, Status,
};

use super::{Page, badge, page, profile_class, slug, time_ago};
use crate::chart::{Base, Series, TimeChart, nice_top, sparkline};
use crate::daemon::{Daemon, Error};
use crate::format;

/// How far back the host charts reach.
pub(crate) const HOST_WINDOW: u64 = 30 * 60;
/// How far back a slot's sparkline reaches.
const SPARK_WINDOW: u64 = 5 * 60;
/// Latest jobs listed.
const RECENT: u32 = 8;

/// Everything the overview shows.
#[derive(Debug, Clone)]
pub(crate) struct Overview {
    pub(crate) now: u64,
    pub(crate) status: Status,
    /// Running jobs with their steps.
    pub(crate) running: Vec<Job>,
    /// Samples of each running job, by job ID.
    pub(crate) samples: BTreeMap<u64, Vec<Sample>>,
    pub(crate) host: HostSamples,
    pub(crate) recent: Vec<JobSummary>,
}

impl Overview {
    pub(crate) async fn load(daemon: &Daemon, now: u64) -> Result<Overview, Error> {
        let status: Status = daemon.get("/v1/status").await?;
        let host = daemon
            .get(&format!(
                "/v1/host/samples?since={}",
                now.saturating_sub(HOST_WINDOW)
            ))
            .await?;
        let recent: JobPage = daemon
            .get_query(
                "/v1/jobs",
                &JobQuery {
                    limit: Some(RECENT),
                    ..JobQuery::default()
                },
            )
            .await?;
        let mut overview = Overview {
            now,
            status,
            running: Vec::new(),
            samples: BTreeMap::new(),
            host,
            recent: recent.jobs,
        };
        overview.reload_running(daemon).await?;
        Ok(overview)
    }

    /// Reads the running jobs again, and the samples of those not seen before.
    pub(crate) async fn reload_running(&mut self, daemon: &Daemon) -> Result<(), Error> {
        let page: JobPage = daemon
            .get_query(
                "/v1/jobs",
                &JobQuery {
                    state: Some(JobState::Running),
                    limit: Some(100),
                    ..JobQuery::default()
                },
            )
            .await?;
        let mut running = Vec::with_capacity(page.jobs.len());
        for summary in page.jobs {
            running.push(
                daemon
                    .get::<Job>(&format!("/v1/jobs/{}", summary.id))
                    .await?,
            );
        }
        self.samples
            .retain(|id, _| running.iter().any(|job| job.summary.id == *id));
        for job in &running {
            if let std::collections::btree_map::Entry::Vacant(entry) =
                self.samples.entry(job.summary.id)
            {
                let series: SampleSeries = daemon
                    .get(&format!("/v1/jobs/{}/samples", job.summary.id))
                    .await?;
                entry.insert(series.samples);
            }
        }
        self.running = running;
        Ok(())
    }

    /// Adds a live sample of a running job.
    pub(crate) fn add_job_sample(&mut self, job: u64, sample: Sample) {
        if let Some(samples) = self.samples.get_mut(&job) {
            samples.push(sample);
        }
    }

    /// Adds a live sample of the slice, dropping those older than the window.
    pub(crate) fn add_host_sample(&mut self, sample: HostSample) {
        let since = sample.at.saturating_sub(HOST_WINDOW);
        self.host.samples.push(sample);
        self.host.samples.retain(|sample| sample.at >= since);
    }

    fn job_in(&self, profile: &str, slot: usize) -> Option<&Job> {
        self.running.iter().find(|job| {
            job.summary
                .runner
                .as_ref()
                .is_some_and(|runner| runner.profile == profile && runner.slot == slot)
        })
    }
}

/// The whole page.
pub(crate) fn render(overview: &Overview) -> Markup {
    let main = html! {
        h1.visually-hidden { "Overview" }
        (budget(overview))
        (slots(overview))
        (host(overview))
        div.split {
            (repositories(&overview.status))
            (recent(overview))
        }
    };
    page(
        "Overview",
        Some(Page::Overview),
        Some("/live/overview"),
        &main,
    )
}

#[allow(
    clippy::cast_precision_loss,
    reason = "sizes as flex weights in GiB; precision far beyond what a pixel shows"
)]
fn gib(bytes: u64) -> f64 {
    bytes as f64 / f64::from(1u32 << 30)
}

/// The budget: the slice as the ground, a block per slot sized by its memory cap, filled as high as the job's last
/// sampled memory; whatever else holds memory (the mirror, a seed container); and what is free.
pub(crate) fn budget(overview: &Overview) -> Markup {
    let status = &overview.status;
    let budget = &status.budget;
    let reserved: u64 = status
        .profiles
        .iter()
        .map(|profile| profile.memory * profile.slots.len() as u64)
        .sum();
    let running_caps: u64 = status
        .profiles
        .iter()
        .map(|profile| {
            profile.memory
                * profile
                    .slots
                    .iter()
                    .filter(|slot| slot.runner.is_some())
                    .count() as u64
        })
        .sum();
    let other = budget.memory_in_use.saturating_sub(running_caps);
    let free = budget
        .memory_max
        .map(|max| max.saturating_sub(reserved + other));
    let used_now = overview.host.samples.last().map(|sample| sample.memory);
    let label = format!(
        "Memory budget of {}: {} of {} reserved by running containers{}",
        budget.slice,
        format::bytes(budget.memory_in_use),
        budget
            .memory_max
            .map_or("no limit".to_owned(), format::bytes),
        used_now.map_or(String::new(), |used| format!(
            ", {} in use",
            format::bytes(used)
        )),
    );
    html! {
        section #budget .budget aria-labelledby="budget-title" {
            div.section-head {
                h2 #budget-title { "Budget" }
                p.figures {
                    span.big { (format::bytes(budget.memory_in_use)) }
                    span { " of " (budget.memory_max.map_or("unlimited".to_owned(), format::bytes)) " reserved in " code { (budget.slice) } }
                    @if let Some(used) = used_now {
                        span.muted { " · " (format::bytes(used)) " in use, sampled" }
                    }
                }
            }
            div.bar role="img" aria-label=(label) {
                @for profile in &status.profiles {
                    @for slot in &profile.slots {
                        (block(overview, profile, slot))
                    }
                }
                @if other > 0 {
                    div.block.other style=(format!("--size: {:.3}", gib(other))) title=(format!("Mirror and seed containers: {}", format::bytes(other))) {
                        span.size { (format::bytes(other)) }
                        span.name { "mirror" }
                    }
                }
                @if let Some(free) = free.filter(|free| *free > 0) {
                    div.free style=(format!("--size: {:.3}", gib(free))) {
                        span.size { (format::bytes(free)) }
                        span.name { "free" }
                    }
                }
            }
            @if status.shutting_down {
                p.warning { "runnerd is shutting down: it starts no new runners and stops once running jobs finish." }
            }
        }
    }
}

fn block(overview: &Overview, profile: &ProfileStatus, slot: &SlotStatus) -> Markup {
    let class = profile_class(&overview.status, &profile.name);
    let job = overview.job_in(&profile.name, slot.slot);
    let memory = job
        .and_then(|job| overview.samples.get(&job.summary.id))
        .and_then(|samples| samples.last())
        .map(|sample| sample.memory);
    #[allow(
        clippy::cast_precision_loss,
        reason = "a ratio of two sizes for a CSS fill"
    )]
    let fill = memory.map(|memory| (memory as f64 / profile.memory.max(1) as f64).min(1.0));
    let state = match (&slot.runner, job) {
        (None, _) => "idle",
        (Some(_), Some(_)) => "busy",
        (Some(_), None) => "starting",
    };
    html! {
        div class=(format!("block {class} {state}")) style=(format!("--size: {:.3}; --fill: {:.3}", gib(profile.memory), fill.unwrap_or(0.0))) {
            @if fill.is_some() { span.fill aria-hidden="true" {} }
            span.size { (format::bytes(profile.memory)) }
            span.name { (profile.name) " " (slot.slot) }
            span.repo {
                @match (&slot.runner, memory) {
                    (Some(_), Some(memory)) => { (format::bytes(memory)) " used" },
                    (Some(runner), None) => { (runner.repository) },
                    (None, _) => { "idle" },
                }
            }
        }
    }
}

/// A card per slot: what runs in it, its current step, and its memory and CPU over the last minutes.
pub(crate) fn slots(overview: &Overview) -> Markup {
    let status = &overview.status;
    html! {
        section #slots .slots aria-labelledby="slots-title" {
            div.section-head {
                h2 #slots-title { "Slots" }
                p.muted {
                    (overview.running.len()) " running · "
                    (status.repositories.iter().map(|repository| repository.queued_jobs).sum::<usize>()) " queued"
                }
            }
            @if status.profiles.is_empty() {
                p.muted { "No profiles in runnerd.toml, so no slots." }
            }
            ul.slot-grid {
                @for profile in &status.profiles {
                    @for slot in &profile.slots {
                        (slot_card(overview, profile, slot))
                    }
                }
            }
        }
    }
}

fn slot_card(overview: &Overview, profile: &ProfileStatus, slot: &SlotStatus) -> Markup {
    let class = profile_class(&overview.status, &profile.name);
    let job = overview.job_in(&profile.name, slot.slot);
    html! {
        li class=(format!("slot {class}")) {
            div.slot-head {
                span.swatch aria-hidden="true" {}
                span.slot-name { (profile.name) " · slot " (slot.slot) }
                span.slot-caps { (format::bytes(profile.memory)) " · " (profile.cpus) " CPUs" }
            }
            @match (&slot.runner, job) {
                (None, _) => {
                    p.slot-idle { @if profile.reseeding && slot.slot == 1 { "Reseeding the profile's snapshot" } @else { "Idle" } }
                }
                (Some(runner), None) => {
                    p.slot-idle { "Runner for " strong { (runner.repository) } " waiting for its job" }
                    p.muted { "started " (time_ago(runner.started_at, overview.now)) }
                }
                (Some(_), Some(job)) => { (running_job(overview, profile, job)) }
            }
        }
    }
}

fn running_job(overview: &Overview, profile: &ProfileStatus, job: &Job) -> Markup {
    let summary = &job.summary;
    let now = overview.now;
    let started = summary
        .timeline
        .started_at
        .unwrap_or(summary.timeline.queued_at);
    let current = job
        .steps
        .iter()
        .find(|step| step.status == "in_progress")
        .or_else(|| {
            job.steps
                .iter()
                .rev()
                .find(|step| step.status == "completed")
        });
    let done = job
        .steps
        .iter()
        .filter(|step| step.status == "completed")
        .count();
    let samples = overview
        .samples
        .get(&summary.id)
        .map_or(&[][..], Vec::as_slice);
    let from = now.saturating_sub(SPARK_WINDOW).max(started.min(now));
    let memory: Vec<(u64, u64)> = samples
        .iter()
        .filter(|sample| sample.at >= from)
        .map(|sample| (sample.at, sample.memory))
        .collect();
    let cpu: Vec<(u64, u64)> = samples
        .iter()
        .filter(|sample| sample.at >= from)
        .map(|sample| (sample.at, sample.cpu))
        .collect();
    let last = samples.last();
    html! {
        a.slot-job href=(format!("/jobs/{}", summary.id)) {
            span.repo { (summary.repository) }
            span.job-name { (summary.name) }
        }
        p.step {
            @if let Some(step) = current {
                span.step-count { (done) "/" (job.steps.len()) }
                " " (step.name)
            } @else {
                "Setting up"
            }
        }
        dl.slot-figures {
            div { dt { "Running" } dd { (format::duration(now.saturating_sub(started))) } }
            div.memory {
                dt { "Memory" }
                dd { (last.map_or("–".to_owned(), |sample| format::bytes(sample.memory))) }
                (sparkline(&memory, from, now, profile.memory, "memory"))
            }
            div.cpu {
                dt { "CPU" }
                dd { (last.map_or("–".to_owned(), |sample| format::cores(sample.cpu))) }
                (sparkline(&cpu, from, now, u64::from(profile.cpus) * 1000, "cpu"))
            }
        }
    }
}

/// The slice's memory and CPU over the last half hour, against `MemoryMax` and the CPUs the slots may use.
pub(crate) fn host(overview: &Overview) -> Markup {
    let status = &overview.status;
    let now = overview.now;
    let start = now.saturating_sub(HOST_WINDOW);
    let samples = &overview.host.samples;
    let memory: Vec<(u64, u64)> = samples.iter().map(|s| (s.at, s.memory)).collect();
    let cpu: Vec<(u64, u64)> = samples.iter().map(|s| (s.at, s.cpu)).collect();
    let cpus: u64 = status
        .profiles
        .iter()
        .map(|profile| u64::from(profile.cpus) * profile.slots.len() as u64)
        .sum();
    let gap = overview.host.interval.max(1) * 3;
    let memory_peak = memory.iter().map(|point| point.1).max().unwrap_or(0);
    let cpu_peak = cpu.iter().map(|point| point.1).max().unwrap_or(0);
    let memory_chart = TimeChart {
        title: format!("Memory of {} over the last 30 minutes", status.budget.slice),
        start,
        end: now,
        series: vec![Series {
            label: "memory".to_owned(),
            class: "memory",
            points: memory,
            area: true,
        }],
        limit: status
            .budget
            .memory_max
            .map(|max| (max, format!("MemoryMax {}", format::bytes(max)))),
        value: format::bytes,
        time: relative_clock,
        gap,
    };
    let memory_top = nice_top(
        memory_peak.max(status.budget.memory_max.unwrap_or(0)),
        Base::Binary,
    );
    let cpu_chart = TimeChart {
        title: format!("CPU of {} over the last 30 minutes", status.budget.slice),
        start,
        end: now,
        series: vec![Series {
            label: "CPU".to_owned(),
            class: "cpu",
            points: cpu,
            area: true,
        }],
        limit: (cpus > 0).then(|| (cpus * 1000, format!("{cpus} CPUs of all slots"))),
        value: |millicores| format!("{} cores", format::cores(millicores)),
        time: relative_clock,
        gap,
    };
    let cpu_top = nice_top(cpu_peak.max(cpus * 1000), Base::Decimal);
    html! {
        section #host .host aria-labelledby="host-title" {
            div.section-head {
                h2 #host-title { "Slice" }
                p.muted { "Every runner container together, sampled every " (overview.host.interval) " s" }
            }
            div.charts {
                div.chart-card {
                    h3 { "Memory" span.reading { (overview.host.samples.last().map_or("–".to_owned(), |s| format::bytes(s.memory))) } }
                    (memory_chart.render(memory_top))
                    p.muted { "Sampled peak " (format::bytes(memory_peak)) }
                }
                div.chart-card {
                    h3 { "CPU" span.reading { (overview.host.samples.last().map_or("–".to_owned(), |s| format!("{} cores", format::cores(s.cpu)))) } }
                    (cpu_chart.render(cpu_top))
                    p.muted { "Sampled peak " (format::cores(cpu_peak)) " cores" }
                }
            }
        }
    }
}

fn relative_clock(at: u64) -> String {
    format!("{} UTC", format::clock(at))
}

/// The configured repositories, with their queue and a pause or resume button.
pub(crate) fn repositories(status: &Status) -> Markup {
    html! {
        section #repositories .repositories aria-labelledby="repositories-title" {
            h2 #repositories-title { "Repositories" }
            @if status.repositories.is_empty() {
                p.muted { "No repositories yet. Add a " code { "[[repositories]]" } " table to runnerd.toml." }
            }
            ul.repo-list {
                @for repository in &status.repositories {
                    (repository_row(status, repository))
                }
            }
        }
    }
}

/// One repository's row; a pause or resume replaces it.
pub(crate) fn repository_row(status: &Status, repository: &RepositoryStatus) -> Markup {
    let class = profile_class(status, &repository.profile);
    let (action, label) = if repository.paused {
        ("resume", "Resume")
    } else {
        ("pause", "Pause")
    };
    html! {
        li #(format!("repo-{}", slug(&repository.name))) class=(format!("repository {class}")) {
            div.repo-name {
                span.swatch aria-hidden="true" {}
                a href=(format!("/history?repository={}", repository.name)) { (repository.name) }
            }
            div.repo-meta {
                span { (repository.profile) }
                span { (repository.queued_jobs) " queued" }
                @if repository.paused { span.badge.neutral { "paused" } }
            }
            // One busy signal per row, so a pending pause greys out only its own button.
            @let busy = format!("_busy_{}", slug(&repository.name).replace('-', "_"));
            button.button-quiet type="button"
                data-indicator=(busy)
                data-attr:aria-busy=(format!("${busy} && 'true'"))
                data-attr:disabled=(format!("${busy}"))
                data-on:click=(format!("@post('/v1/repositories/{}/{action}')", repository.name))
                aria-label=(format!("{label} {}", repository.name)) { (label) }
        }
    }
}

/// The latest jobs, any state.
pub(crate) fn recent(overview: &Overview) -> Markup {
    html! {
        section #recent .recent aria-labelledby="recent-title" {
            div.section-head {
                h2 #recent-title { "Latest jobs" }
                a href="/history" { "All history" }
            }
            @if overview.recent.is_empty() {
                p.muted { "No jobs yet. Jobs show here once a runner of this machine takes one." }
            } @else {
                ul.job-list {
                    @for job in &overview.recent {
                        li {
                            a href=(format!("/jobs/{}", job.id)) {
                                (badge(job))
                                span.job-name { (job.name) }
                                span.repo { (job.repository) }
                                span.when { (time_ago(job.timeline.queued_at, overview.now)) }
                            }
                        }
                    }
                }
            }
        }
    }
}