import m from '@garage44/mithril'
import {confirmButton} from '@/components/confirm'
import {act} from '@/lib/actions'
import {api} from '@/lib/api'
import {age, now, shortName, span} from '@/lib/format'
import {highlight} from '@/lib/highlight'
import {nameOf} from '@/lib/names'
import {nodeLine, nodeStepWords, placeWords, wantsWords} from '@/lib/nodeline'
import type {NodeStep} from '@/lib/nodeline'
import {$s, steers} from '@/lib/store'
import {cards, hosted, nodes} from '@/lib/stream'
import type {BoardCard, Hosted, LogItem, NodeHeld} from '@/lib/stream'
import type {Lease, Node} from '@/lib/types'
/**
* How a session gets a node, drawn as its way to live is (landing.tsx): one
* line under its conversation, "Waiting for a mac, #2 in line · mac-1 held by …"
* or "On mac-1 · building now", and its `midas take`, `wait` and `release` as
* short lines where they ran rather than raw commands. A long wait gets who
* holds the node and the nudge, take-back and leaving the line, as Home's row does.
*/
/** Waiting this long for a node, a session is shown who holds it, and the nudge and take-back. */
export const STUCK_WAIT_S = 10 * 60
/** Who holds what a waiting card wants: the node held longest without a command, as the one to ask. */
function blocker(c: BoardCard): {waiter: string; node: Node; lease: Lease; idle_s: number} | null {
const ws = $s.workspace
if (!ws) return null
const waiter = [c.session, `gate-${c.session}`].find((o) => ws.waiters[o])
const w = waiter ? ws.waiters[waiter] : undefined
if (!waiter || !w) return null
const machine = w.machine ?? 'any'
const fits = (n: Node) => (machine === 'any' || (machine === 'gpu' ? n.gpu : n.machine === machine)) && (!w.node || w.node === n.id)
const held = ws.nodes
.filter((n) => fits(n) && ws.leases[n.id])
.map((n) => {
const lease = ws.leases[n.id]!
// A command running on it (an ssh session open) is use, however long since it was renewed.
const idle_s = n.reading?.sessions ? 0 : Math.max(0, now() - lease.renewed_at)
return {waiter, node: n, lease, idle_s}
})
.sort((a, b) => b.idle_s - a.idle_s)
return held[0] ?? null
}
/** A long wait for a node: who holds it, and a nudge, a take-back and leaving the line, each a second tap. */
export function stuck(c: BoardCard, holderLine = true): m.Children {
if (c.stage !== 'waiting_for_node' || now() - (c.since ?? now()) < STUCK_WAIT_S) return null
const b = blocker(c)
const waiter = b?.waiter ?? c.session
const me = shortName(nameOf(c.session).title, 40)
const holder = b ? shortName(nameOf(b.lease.owner).title, 40) : ''
return (
e.stopPropagation()}>
{b && holderLine ? (
{b.node.id} is held by {holder}
{b.idle_s > 120 ? `, idle ${span(b.idle_s)}` : ', in use now'}
) : null}
{$s.hasKey ? (
{b
? [
confirmButton(`nudge-${c.session}`, {
label: 'Nudge',
armedLabel: 'Tap again to tell them',
title: `tell ${holder} that ${me} is waiting for ${b.node.id}`,
disabled: !steers(),
run: () => act(`told ${holder}`, () => api.say(b.lease.owner, `${me} is waiting for ${b.node.id}`)),
}),
confirmButton(`takeback-${c.session}`, {
label: 'Take back',
armedLabel: 'Tap again to take it',
title: `end ${holder}'s hold on ${b.node.id}; it goes to the first in line`,
run: () => act(`${b.node.id} taken back`, () => api.release(b.node.id)),
}),
]
: null}
{confirmButton(`unqueue-${c.session}`, {
label: 'Leave the line',
armedLabel: 'Tap again to leave',
title: 'take this session out of the line for a node',
run: () => act('taken out of the line', () => api.unqueue(waiter)),
})}
) : null}
)
}
/** A session holding a node, by name, linked where midas runs it. */
function holderName(owner: string): m.Children {
const h = hosted().find((x) => x.owner === owner && !x.archived && !x.imported)
const title = shortName(h?.title ?? nameOf(owner).title, 40)
return h ? {title} : title
}
/** Who holds the nodes it may take: the first in full, the rest short, as one says it. */
function holders(held: NodeHeld[]): m.Children {
return held.flatMap((h, i) => [
i ? ', ' : ' · ',
h.offline ? (
`${h.node} offline`
) : (
<>
{h.node} {i ? 'by' : 'held by'} {h.by ? holderName(h.by) : 'someone'}
{h.since ? ` (${age(h.since)})` : ''}
{h.yielding ? ', giving it back after its build' : ''}
>
),
])
}
/**
* The session's node line, under its conversation: in line for one and who
* holds those it may take, or the node it is on and whether it builds there now.
* Nothing while it neither holds nor waits, or has ended.
*/
export function nodeFoot(s: Hosted): m.Vnode | null {
if (s.status === 'stopped' || s.status === 'failed' || s.finished || s.imported) return null
const c = cards().find((x) => x.session === s.owner)
const n = nodeLine(c, $s.workspace?.leases, now())
if (!c || !n) return null
if (n.kind === 'on') {
if (n.quiet) {
return (
Node
On {n.node} · its command has done nothing for {span(n.quiet.quiet_s)}: no output, no CPU. midas stops it at 30 minutes, which tells the session.
)
}
const what = n.busy ? 'building now' : n.idle_s == null || n.idle_s < 60 ? 'idle' : `idle ${span(n.idle_s)}`
return (
Node
On {n.node} · {what}
)
}
const long = now() - n.since >= STUCK_WAIT_S
return (
Node
Waiting for {wantsWords(n.wants, nodes())}, {placeWords(n.position)}
{holders(n.held)}
{age(n.since)}
{long ? stuck(c, false) : null}
)
}
/**
* A node step where it ran, as a short line, opening to the command and what it
* printed: nothing it said is lost, only the raw command is out of the way.
*/
export function nodeStepLine(t: LogItem, step: NodeStep, open: boolean, toggle: (e: Event) => void): m.Vnode {
const command = typeof t.input?.command === 'string' ? t.input.command : null
const tone = step.state === 'failed' ? 'bad' : step.state === 'took' ? 'ok' : step.state === 'running' || step.state === 'background' || step.state === 'queued' ? 'wait' : ''
return (
Node
{nodeStepWords(step)}
{open ? (
{command ?
{highlight(command, 'sh')} : null}
{t.output ?
{t.output} : null}
) : null}
)
}