import m, {MithrilComponent} from '@garage44/mithril' import {Button, Card} from '@garage44/mithril/components' import {Lines, SERIES} from '@/components/charts' import {runnersCard} from '@/components/runners' import {api} from '@/lib/api' import type {Runners, Watch, WatchTarget} from '@/lib/api' import {bytes} from '@/lib/format' import {local} from '@/lib/reactive' /* * Workspace ▸ Services (watch.rs): every deploy target the hub watches — is it * up, what it serves, its memory, CPU and answer time — each chart crossed by a * line at every deploy, which says the landings it carried. Asked for every * minute, which is how often the hub samples. */ const RANGES: {label: string; s: number}[] = [ {label: '6 h', s: 6 * 3600}, {label: '24 h', s: 86400}, {label: '7 d', s: 7 * 86400}, ] const STATE_WORD: Record = {ok: 'up', trouble: 'in trouble', down: 'down', unknown: 'not sampled'} const sha8 = (s: string | null | undefined) => (s ? s.slice(0, 8) : '—') const clock = (t: number) => new Date(t * 1000).toLocaleTimeString(undefined, {hour: '2-digit', minute: '2-digit'}) const dayClock = (t: number) => new Date(t * 1000).toLocaleString(undefined, {weekday: 'short', hour: '2-digit'}) const mb = (v: number) => (v >= 1024 ? `${(v / 1024).toFixed(1)} GB` : `${Math.round(v)} MB`) const pct = (v: number) => `${Math.round(v)} %` const ms = (v: number) => `${Math.round(v)} ms` /** What a target's last sample says, in one line. */ function nowLine(t: WatchTarget): string { const n = t.now if (!n) return t.why const parts = [`serves ${sha8(t.serves)}`] if (n.mem != null) parts.push(bytes(n.mem)) if (n.cpu != null) parts.push(`CPU ${pct(n.cpu)}`) const answer = n.probes?.map((p) => p.ms).filter((x): x is number => x != null) if (answer?.length) parts.push(`answers in ${ms(Math.max(...answer))}`) if (t.restarts_h) parts.push(`${t.restarts_h} restarts this hour`) return parts.join(' · ') } /** The buckets a target was down in, joined into spans. */ function downSpans(t: WatchTarget, step: number): [number, number][] { const out: [number, number][] = [] for (const p of t.points) { if (p[4]) continue const last = out[out.length - 1] if (last && last[1] >= p[0]) last[1] = p[0] + step else out.push([p[0], p[0] + step]) } return out } export class Services extends MithrilComponent { s = local({data: null as Watch | null, runners: null as Runners | null, failed: false, range: RANGES[0]!.s, open: null as string | null}) timer = 0 oninit() { void this.load() this.timer = window.setInterval(() => void this.load(), 60_000) } onremove() { window.clearInterval(this.timer) } async load() { try { // A hub from before runners.rs has no runners to say: the services all the same. const [data, runners] = await Promise.all([api.watch(this.s.range), api.runners(this.s.range).catch(() => null)]) this.s.data = data this.s.runners = runners this.s.failed = false } catch { this.s.failed = true } } chart(w: Watch, t: WatchTarget, title: string, k: 1 | 2 | 3, format: (v: number) => string) { const points = t.points.filter((p) => p[k] != null).map((p) => [p[0], p[k]!] as [number, number]) if (!points.length) return null const label = w.to - w.from > 2 * 86400 ? dayClock : clock return [

{title}

, ({ t: d.t, lines: [ sha8(d.sha), `deployed ${clock(d.t)}${d.by ? ` by ${d.by}` : ''}`, ...(d.landings.length ? d.landings.map((l) => `${l.session}${l.branch ? ` · ${l.branch}` : ''}`) : ['no landing of its own']), ], }))} />, ] } detail(w: Watch, t: WatchTarget) { return (
{this.chart(w, t, 'Memory', 1, mb)} {this.chart(w, t, 'CPU, of one core', 2, pct)} {this.chart(w, t, 'Answer time', 3, ms)}

{t.unit ? {t.unit} : null} {t.probes.map((p) => {p})} · a dashed line is a deploy (tap it for the landings it carried), red is down. {t.memory_mb ? ` In trouble above ${mb(t.memory_mb)}` : ''} {t.restarts_per_h != null ? `${t.memory_mb ? ' or' : ' In trouble at'} more than ${t.restarts_per_h} restarts an hour.` : t.memory_mb ? '.' : ''}

) } view() { const w = this.s.data if (!w) return {this.s.failed ?

The hub did not answer; trying again.

:

Asking the hub…

}
if (!w.targets.length) { return (
{runnersCard(this.s.runners)}

Services

No deploy target is watched yet. Give one a watch in hub.conf — its systemd unit and a health address — and the hub samples it every minute.

) } const open = w.targets.find((t) => t.name === this.s.open) ?? w.targets.find((t) => t.state !== 'ok') ?? w.targets[0]! const mn = w.machine.now return (

Services

{RANGES.map((r) => ( ))}
    {w.targets.map((t) => (
  • (this.s.open = t.name)} > {t.name} {STATE_WORD[t.state]} {t.state === 'ok' || !t.now ? nowLine(t) : `${t.why} · ${nowLine(t)}`}
  • ))}
{this.detail(w, open)}
{runnersCard(this.s.runners)} {mn ? (

This machine

{mn.mem_used != null && mn.mem_total ? `${bytes(mn.mem_used)} of ${bytes(mn.mem_total)} memory in use` : ''} {mn.load != null ? ` · load ${mn.load.toFixed(1)}` : ''}

    {mn.disks.map((d) => (
  • {d.p} {bytes(d.free)} free of {bytes(d.size)} ({Math.round((100 * (d.size - d.free)) / d.size)} % used)
  • ))}

Sampled every minute; kept two weeks.

) : null}
) } }