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