git clone https://garage44.eu/jeroen/midas.git.. /web/src/lib/nodes.ts
import type {WorkspaceState, Lease, Node} from '@/lib/types'
export type NodeState = 'asleep' | 'offline' | 'drained' | 'bot' | 'busy' | 'free'
/**
* What a node is doing, in the order that decides it: off the network beats
* drained beats held. Asleep is off the network on purpose: its VM was shut
* down while nobody used it, and starts when somebody waits for a node.
*/
export function nodeState(n: Node, lease: Lease | undefined): NodeState {
if (n.asleep && !lease) return 'asleep'
if (!n.enabled) return 'offline'
if (n.drained) return 'drained'
if (lease) return lease.mode === 'bot' ? 'bot' : 'busy'
return 'free'
}
export const STATE_LABEL: Record<NodeState, string> = {
asleep: 'asleep',
offline: 'offline',
drained: 'drained',
bot: 'bot',
busy: 'in use',
free: 'free',
}
/** A status keeps its icon and label beside its colour, never colour alone. */
export const STATE_ICON: Record<NodeState, string> = {
asleep: '☾',
offline: '✕',
drained: '‖',
bot: '◆',
busy: '●',
free: '○',
}
/** The machines, in a fixed order, each with its nodes: the order colours follow. */
export function machines(workspace: WorkspaceState): {machine: string; address: string; nodes: Node[]}[] {
const out = new Map<string, {machine: string; address: string; nodes: Node[]}>()
for (const n of workspace.nodes) {
const m = out.get(n.machine) ?? {machine: n.machine, address: n.address, nodes: []}
m.nodes.push(n)
out.set(n.machine, m)
}
return [...out.values()].sort((a, b) => a.machine.localeCompare(b.machine))
}