import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query" import { createFileRoute } from "@tanstack/react-router" import { Cpu, Pencil, Plus, Server, Trash2 } from "lucide-react" import { useState } from "react" import { type ApiError, type FlavorPublic, FlavorsService, WorkersService, } from "@/client" import { Badge } from "@/components/ui/badge" import { Button } from "@/components/ui/button" import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import useAuth from "@/hooks/useAuth" import useCustomToast from "@/hooks/useCustomToast" import { ago } from "@/lib/utils" import { handleError } from "@/utils" export const Route = createFileRoute("/_layout/workers")({ component: Workers, head: () => ({ meta: [{ title: "Workers - Fluksio" }], }), }) /** The node panel's dropdown reads this too, so an edit refreshes it. */ const flavorsKey = ["flavors"] const emptyFlavor = { name: "", cpus: 1, gpus: 0, ram: 2048, description: "" } /** Fresh enough to watch a node take a machine, slow enough to be free. */ const REFETCH_MS = 5000 type Level = { total: number; free: number } /** "cpu 2/18" — what is in use, out of what there is. */ function Used({ label, level }: { label: string; level?: Level | null }) { if (!level?.total) return null return ( {label} {level.total - level.free}/{level.total} ) } function Machines() { const { data, isError } = useQuery({ queryKey: ["workers", "resources"], queryFn: () => WorkersService.readResources(), refetchInterval: REFETCH_MS, }) if (isError || !data) { return (

This engine does not account for resources.

) } return (
{(data.targets ?? []).map((target) => (
{target.target} {(target.labels ?? []).length > 0 ? ( {(target.labels ?? []).join(", ")} ) : null}
))} {(data.waiting ?? []).length > 0 ? (
{(data.waiting ?? []).map((node) => (

{node.node} — {node.reason} ·{" "} {node.seconds.toFixed(0)}s

))}
) : null} {(data.provisioners ?? []).map((provisioner) => { const outstanding = (provisioner.outstanding ?? []) as { profile: string job: string seconds: number }[] return (

{String(provisioner.name)} can start{" "} {(provisioner.profiles as string[]).join(", ")} {outstanding.length > 0 ? `. Asked for ${outstanding .map((job) => `${job.profile} (job ${job.job})`) .join(", ")}` : ""} {provisioner.last_error ? `. Last failed: ${String(provisioner.last_error)}` : ""}

) })}
) } function Attached() { const { data } = useQuery({ queryKey: ["workers"], queryFn: () => WorkersService.readWorkers(), refetchInterval: REFETCH_MS, }) const workers = data ?? [] if (workers.length === 0) { return (

Nothing attached. A machine runs{" "} pip install fluksio-worker and dials in; nodes go to it by label, or because it has room and this engine does not.

) } return (
{workers.map((worker) => (
{worker.name} {(worker.labels ?? []).length > 0 ? ( {(worker.labels ?? []).join(", ")} ) : null} {worker.behind ? ( Refused for an older protocol. Upgrade it with{" "} pip install -U fluksio-worker ) : ( {worker.cpus} cpu {worker.gpus ? ` · ${worker.gpus} gpu` : ""} {worker.ram_mb ? ` · ${Math.round(worker.ram_mb / 1024)} GB` : ""}{" "} · {worker.in_flight}/{worker.max_parallel} in flight )} {worker.behind ? ( Needs upgrade ) : null} {worker.version ? `v${worker.version} · ` : ""}protocol{" "} {worker.protocol} {worker.behind ? "refused" : "seen"} {ago(worker.last_seen)}
))}
) } function Flavors() { const { user } = useAuth() const { data } = useQuery({ queryKey: flavorsKey, queryFn: () => FlavorsService.readFlavors(), }) const queryClient = useQueryClient() const { showSuccessToast, showErrorToast } = useCustomToast() const [editing, setEditing] = useState(null) const [isNew, setIsNew] = useState(false) const [pendingDelete, setPendingDelete] = useState(null) const refresh = () => queryClient.invalidateQueries({ queryKey: flavorsKey }) const save = useMutation({ mutationFn: (flavor: typeof emptyFlavor) => isNew ? FlavorsService.createFlavor({ requestBody: flavor }) : FlavorsService.updateFlavor({ name: flavor.name, requestBody: { cpus: flavor.cpus, gpus: flavor.gpus, ram: flavor.ram, description: flavor.description, }, }), onSuccess: (_result, flavor) => { showSuccessToast(`Saved '${flavor.name}'`) setEditing(null) }, onError: handleError.bind(showErrorToast), onSettled: refresh, }) const remove = useMutation({ mutationFn: (name: string) => FlavorsService.deleteFlavor({ name }), onSuccess: (_result, name) => { showSuccessToast(`Deleted '${name}'`) setPendingDelete(null) }, onError: (error: ApiError) => { handleError.call(showErrorToast, error) setPendingDelete(null) }, onSettled: refresh, }) const flavors = data?.data ?? [] const mayEdit = Boolean(user?.is_superuser) return (
{flavors.map((flavor: FlavorPublic) => (
{flavor.name} {flavor.cpus} cpu · {Math.round((flavor.ram ?? 0) / 1024)} GB {flavor.gpus ? ` · ${flavor.gpus} gpu` : ""} {flavor.description ? ` · ${flavor.description}` : ""} {mayEdit ? ( ) : null}
))} {mayEdit ? ( ) : null} !open && setEditing(null)} > {isNew ? "New size" : `Edit '${editing?.name}'`} Every node that names this gets what it says here, from its next run. {editing ? (
{isNew ? (
setEditing({ ...editing, name: event.target.value }) } />
) : null}
{( [ ["cpus", "Cores", 1], ["gpus", "GPUs", 0], ["ram", "MB", 1], ] as const ).map(([field, label, floor]) => (
setEditing({ ...editing, [field]: Math.max( floor, Number(event.target.value) || 0, ), }) } />
))}
setEditing({ ...editing, description: event.target.value }) } />
) : null}
!open && setPendingDelete(null)} > Delete '{pendingDelete}'? A node still asking for it keeps this from being deleted, and it will say which. Nothing is seeded back afterwards.
) } function Workers() { return (

Workers

Every machine this engine can run a node on, what is free of each, and the sizes a node can ask for by name.

Machines

Attached workers

Sizes

) }