git clone https://garage44.eu/jeroen/midas.git.. /web/src/components/runners.tsx
import m from '@garage44/mithril'
import {Alert, Card} from '@garage44/mithril/components'
import {Bars} from '@/components/charts'
import type {RunnerStop, Runners} from '@/lib/api'
import {dur, now} from '@/lib/format'
import {dayKey} from '@/lib/stats'
import {hosted, runners} from '@/lib/stream'
/*
* The sessions runners (runners.rs; docs/mockups/runner-health): a line over the
* Sessions list while one is away, its row in Workspace ▸ Services with every stop
* and why, and its card in Activity ▸ How it flows.
*/
const clock = (t: number) => new Date(t * 1000).toLocaleTimeString([], {hour: '2-digit', minute: '2-digit'})
const when = (t: number) =>
now() - t < 86400 ? clock(t) : new Date(t * 1000).toLocaleString([], {weekday: 'short', hour: '2-digit', minute: '2-digit'})
/** Over the Sessions list while a runner is away: nothing it ran is working, and what happens next. */
export function runnerAwayLine(): m.Vnode | null {
const away = runners().filter((r) => r.away)
if (!away.length) return null
const since = Math.min(...away.map((r) => r.away!))
const paused = hosted().filter((h) => h.runner_away).length
return (
<Alert tone="danger">
<b>The sessions runner stopped asking for work at {clock(since)}</b>, {dur(now() - since)} ago.{' '}
{paused ? `The ${paused} sessions it ran are paused until it is back. ` : ''}
Its supervisor or the watch on its machine starts it again; each session then carries on where its turn was cut off.
</Alert>
)
}
const REASON: Record<RunnerStop['reason'], string> = {
build: 'New build',
signal: 'Stopped',
panic: 'Crashed',
killed: 'Died',
exit: 'Ended by itself',
unexplained: 'Gone without a word',
}
/** Red: a stop nobody asked for. A new build is the runner's own doing. */
const bad = (s: RunnerStop) => s.reason !== 'build'
function stopLine(s: RunnerStop): string {
switch (s.reason) {
case 'build':
return `${s.version ? `from ${s.version}, ` : ''}${s.sessions ?? 0} sessions taken back`
case 'signal': {
const who = s.by_cmd ? `pid ${s.by_pid}, ${s.by_cmd}` : s.by_pid ? `pid ${s.by_pid} (${s.by_note ?? 'gone'})` : 'an unknown sender'
return `${s.signal} from ${who}${s.by_owner ? `, run by ${s.by_owner}` : ''}${s.supervisor ? ', to its supervisor' : ''}`
}
case 'panic':
return `${s.where}: ${s.message}`
case 'killed':
return `after ${dur(s.lived_s ?? 0)}, by a signal it could not write down: its supervisor saw it`
case 'exit':
return `after ${dur(s.lived_s ?? 0)}, without an error`
case 'unexplained':
return 'the runner and its supervisor both went without a record: the machine, the container, or SIGKILL to both'
}
}
/** How long each absence that began at or after this stop lasted, if one did. */
function gapAfter(r: Runners, s: RunnerStop): number | null {
const a = r.away.find((x) => x.runner === s.runner && x.from >= s.at - 120 && x.from <= s.at + 120)
return a ? (a.to ?? r.now) - a.from : null
}
/** The share of the range each runner was there: its absences taken off. */
function upShare(r: Runners, name: string): number {
const span = r.now - r.since
const away = r.away.filter((a) => a.runner === name).reduce((n, a) => n + ((a.to ?? r.now) - Math.max(a.from, r.since)), 0)
return span > 0 ? Math.max(0, 1 - away / span) : 1
}
/** A band over the range: green while there, red while away. */
function upBand(r: Runners, name: string): m.Children {
const span = r.now - r.since
const x = (t: number) => `${(100 * (Math.max(t, r.since) - r.since)) / span}%`
const w = (a: number, b: number) => `${(100 * (Math.min(b, r.now) - Math.max(a, r.since))) / span}%`
return (
<div class="runner__band" title="green: asking the hub for work; red: away">
{r.away
.filter((a) => a.runner === name)
.map((a) => (
<i style={{left: x(a.from), width: w(a.from, a.to ?? r.now)}} title={`away ${dur((a.to ?? r.now) - a.from)} from ${when(a.from)}`} />
))}
{r.stops
.filter((s) => s.runner === name && s.reason === 'build')
.map((s) => (
<b style={{left: x(s.at)}} title={`new build at ${when(s.at)}`} />
))}
</div>
)
}
/** Workspace ▸ Services: each runner, up or away, and every stop in the range with its reason. */
export function runnersCard(r: Runners | null): m.Children {
if (!r || (!r.runners.length && !r.stops.length)) return null
const today = dayKey(r.now)
return (
<Card>
<h3 class="card__title">Sessions runner</h3>
<ul class="services">
{r.runners.map((x) => {
const stops = r.stops.filter((s) => s.runner === x.name && s.at >= today)
const reasons = Object.entries(
stops.reduce<Record<string, number>>((o, s) => ({...o, [REASON[s.reason].toLowerCase()]: (o[REASON[s.reason].toLowerCase()] ?? 0) + 1}), {}),
)
const state = x.away ? 'down' : stops.some(bad) ? 'trouble' : 'ok'
return (
<li class={`services__row services__row--open${x.away ? ' services__row--down' : ''}`}>
<span class={`services__dot services__dot--${state}`} />
<b>{x.name}</b>
<span class="hint services__state">{x.away ? `away since ${when(x.away)}` : 'up'}</span>
<span class="hint">
{x.away ? `away ${dur(r.now - x.away)}` : x.up_since ? `up ${dur(r.now - x.up_since)}` : 'up'} · {x.sessions} sessions
{stops.length
? ` · ${stops.length} ${stops.length === 1 ? 'stop' : 'stops'} today: ${reasons.map(([k, n]) => `${n} ${k}`).join(', ')}`
: ' · no stop today'}
</span>
{upBand(r, x.name)}
</li>
)
})}
</ul>
{r.stops.length ? (
<ul class="runner__stops">
{[...r.stops].reverse().map((s) => {
const gap = gapAfter(r, s)
return (
<li class={`runner__stop${bad(s) ? ' runner__stop--bad' : ''}`}>
<div class="runner__stop-head">
<b>
{REASON[s.reason]}
{s.signal && s.reason === 'killed' ? `: ${s.signal}` : ''}
</b>
<span class="hint">{when(s.at)}</span>
</div>
<span class="hint">
{stopLine(s)}
{s.reason !== 'build' && s.sessions != null ? ` · ${s.sessions} sessions running` : ''}
{gap != null ? ` · away ${dur(gap)}` : ''}
</span>
</li>
)
})}
</ul>
) : (
<p class="hint">No stop in this range.</p>
)}
</Card>
)
}
/** Activity ▸ How it flows: how often the runner stopped, why, and how long sessions sat without it. */
export function runnersHistory(r: Runners | null, dayKeys: number[]): m.Children {
if (!r) return null
const names = [...new Set([...r.runners.map((x) => x.name), ...r.stops.map((s) => s.runner)])]
const stops = r.stops
const builds = stops.filter((s) => s.reason === 'build').length
const crashes = stops.filter((s) => s.reason === 'panic' || s.reason === 'killed' || s.reason === 'exit').length
const others = stops.length - builds - crashes
const gaps = r.away.map((a) => ({...a, len: (a.to ?? r.now) - a.from}))
const longest = gaps.reduce<(typeof gaps)[number] | null>((l, a) => (!l || a.len > l.len ? a : l), null)
const awayTotal = gaps.reduce((n, a) => n + a.len, 0)
const cut = r.away.reduce((n, a) => n + (a.sessions ?? 0), 0)
const up = names.length ? Math.min(...names.map((n) => upShare(r, n))) : 1
const perDay = (pick: (s: RunnerStop) => boolean) => (d: number) => stops.filter((s) => dayKey(s.at) === d && pick(s)).length
const awayOn = (d: number) =>
gaps.reduce((n, a) => {
const from = Math.max(a.from, d)
const to = Math.min(a.to ?? r.now, d + 86400)
return n + Math.max(0, to - from)
}, 0) / 60
return [
<h2 class="history__heading">Sessions runner</h2>,
<div class="tiles">
<div class="tile">
<div class="tile__label">up</div>
<div class="tile__value">{`${(100 * up).toFixed(1)} %`}</div>
<div class="tile__sub">{awayTotal ? `${dur(awayTotal)} away in all` : 'never away'}</div>
</div>
<div class="tile">
<div class="tile__label">restarts</div>
<div class="tile__value">{String(stops.length)}</div>
<div class="tile__sub">{`${builds} new build · ${crashes} crash · ${others} stopped or unexplained`}</div>
</div>
<div class="tile">
<div class="tile__label">longest gap</div>
<div class="tile__value">{longest ? dur(longest.len) : '–'}</div>
<div class="tile__sub">{longest ? `from ${when(longest.from)}` : 'none in this range'}</div>
</div>
<div class="tile">
<div class="tile__label">sessions paused</div>
<div class="tile__value">{String(cut)}</div>
<div class="tile__sub">by its absences, each taken back after</div>
</div>
</div>,
<div class="grid2">
<Card title="Runner restarts" subtitle="per day, by reason">
<Bars
days={dayKeys}
series={[
{label: 'new build', color: 'var(--series-1)'},
{label: 'crash', color: 'var(--m-warning)'},
{label: 'stopped or unexplained', color: 'var(--m-danger)'},
]}
value={(d) => [
perDay((s) => s.reason === 'build')(d),
perDay((s) => s.reason === 'panic' || s.reason === 'killed' || s.reason === 'exit')(d),
perDay((s) => s.reason === 'signal' || s.reason === 'unexplained')(d),
]}
format={(v) => String(v)}
/>
</Card>
<Card title="Runner away" subtitle="minutes per day the sessions sat paused without it">
<Bars days={dayKeys} series={[{label: 'away', color: 'var(--m-danger)'}]} value={(d) => [Math.round(awayOn(d))]} format={(v) => `${v} min`} />
</Card>
</div>,
]
}