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 ( The sessions runner stopped asking for work at {clock(since)}, {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. ) } const REASON: Record = { 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 (
{r.away .filter((a) => a.runner === name) .map((a) => ( ))} {r.stops .filter((s) => s.runner === name && s.reason === 'build') .map((s) => ( ))}
) } /** 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 (

Sessions runner

    {r.runners.map((x) => { const stops = r.stops.filter((s) => s.runner === x.name && s.at >= today) const reasons = Object.entries( stops.reduce>((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 (
  • {x.name} {x.away ? `away since ${when(x.away)}` : 'up'} {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'} {upBand(r, x.name)}
  • ) })}
{r.stops.length ? (
    {[...r.stops].reverse().map((s) => { const gap = gapAfter(r, s) return (
  • {REASON[s.reason]} {s.signal && s.reason === 'killed' ? `: ${s.signal}` : ''} {when(s.at)}
    {stopLine(s)} {s.reason !== 'build' && s.sessions != null ? ` · ${s.sessions} sessions running` : ''} {gap != null ? ` · away ${dur(gap)}` : ''}
  • ) })}
) : (

No stop in this range.

)}
) } /** 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 [

Sessions runner

,
up
{`${(100 * up).toFixed(1)} %`}
{awayTotal ? `${dur(awayTotal)} away in all` : 'never away'}
restarts
{String(stops.length)}
{`${builds} new build · ${crashes} crash · ${others} stopped or unexplained`}
longest gap
{longest ? dur(longest.len) : '–'}
{longest ? `from ${when(longest.from)}` : 'none in this range'}
sessions paused
{String(cut)}
by its absences, each taken back after
,
[ 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)} /> [Math.round(awayOn(d))]} format={(v) => `${v} min`} />
, ] }