import { useState } from "react"; import { garage, type ClusterLayout, type ClusterStatus, type NodeInfo } from "./api"; import { fmtAgo, fmtSize } from "./format"; import { EditIcon, PlusIcon, RefreshIcon, TrashIcon } from "./icons"; import { Confirm, Loading, Modal, Mono, PageHead, Status, UsageBar, useAct, useLoad } from "./ui"; const GB = 1e9; // Garage raisonne en octets ; on saisit des Go décimaux, comme dans la doc export function Cluster() { const act = useAct(); const { data, error, loading, reload } = useLoad(async () => { const [status, layout] = await Promise.all([garage.get("GetClusterStatus"), garage.get("GetClusterLayout")]); return { status, layout }; }); const [edit, setEdit] = useState(null); const [connect, setConnect] = useState(false); const [preview, setPreview] = useState(null); const [confirmRevert, setConfirmRevert] = useState(false); const [removing, setRemoving] = useState(null); const run = (fn: () => Promise, ok?: string) => act(fn, ok).then((r) => r && reload()); const staged = data?.layout.stagedRoleChanges ?? []; const stagedOf = (id: string) => staged.find((s) => s.id === id); const showPreview = async () => { const r = await garage.post<{ error?: string; message?: string[] }>("PreviewClusterLayoutChanges"); if (r.error) throw new Error(r.error); setPreview(r.message ?? ["Aucun détail."]); }; return (
{data && ( <> {staged.length > 0 && (
{staged.length} changement(s) de layout en attente. Ils ne sont appliqués qu'après validation : Garage redistribue alors les données entre les nœuds.
)}
{data.status.nodes.map((n) => { const s = stagedOf(n.id); const used = n.dataPartition ? n.dataPartition.total - n.dataPartition.available : 0; return ( ); })}
Nœud État Zone Capacité Données Version
{n.hostname || "(sans nom)"}
{n.addr}
{n.isUp ? "en ligne" : "hors ligne"} {!n.isUp && {fmtAgo(n.lastSeenSecsAgo)}} {n.draining && vidange} {n.role ? n.role.zone : sans rôle} {n.role?.tags?.length ? [{n.role.tags.join(", ")}] : null} {s && {s.remove ? "retrait prévu" : "modifié"}} {n.role ? (n.role.capacity != null ? fmtSize(n.role.capacity) : "passerelle") : "-"} {n.dataPartition ? ( <> {fmtSize(used)} / {fmtSize(n.dataPartition.total)} ) : ( - )} {n.garageVersion ?? "-"} {n.role && ( )}

Version du layout : {data.layout.version} {data.layout.partitionSize ? ` · partitions de ${fmtSize(data.layout.partitionSize)}` : ""}

)} {edit && ( setEdit(null)} onSave={async (role) => { if (await act(() => garage.post("UpdateClusterLayout", undefined, { roles: [{ id: edit.id, ...role }] }), "Changement mis en attente")) { setEdit(null); reload(); } }} /> )} {connect && setConnect(false)} onDone={() => (setConnect(false), reload())} />} {preview && ( setPreview(null)}>
{preview.join("\n")}
)} {confirmRevert && ( Les changements de layout en attente seront abandonnés.

} confirmLabel="Annuler les changements" onClose={() => setConfirmRevert(false)} onConfirm={async () => { setConfirmRevert(false); await run(() => garage.post("RevertClusterLayout"), "Changements abandonnés"); }} /> )} {removing && ( {removing.hostname || removing.id.slice(0, 12)} n'aura plus de rôle de stockage. Le changement est mis en attente : il faudra l'appliquer, et Garage redistribuera alors ses données sur les autres nœuds.

} confirmLabel="Mettre en attente" onClose={() => setRemoving(null)} onConfirm={async () => { const id = removing.id; setRemoving(null); await run(() => garage.post("UpdateClusterLayout", undefined, { roles: [{ id, remove: true }] }), "Retrait mis en attente"); }} /> )}
); } function RoleForm({ node, staged, onClose, onSave, }: { node: NodeInfo; staged?: { zone?: string; capacity?: number | null; tags?: string[]; remove?: boolean }; onClose: () => void; onSave: (r: { zone: string; capacity: number | null; tags: string[] }) => Promise; }) { const cur = staged && !staged.remove ? staged : node.role; const [zone, setZone] = useState(cur?.zone ?? "dc1"); const [gateway, setGateway] = useState(cur ? cur.capacity == null : false); const [cap, setCap] = useState(cur?.capacity ? String(cur.capacity / GB) : ""); const [tags, setTags] = useState((cur?.tags ?? []).join(", ")); const valid = zone.trim() !== "" && (gateway || parseFloat(cap) > 0); return (
{ e.preventDefault(); onSave({ zone: zone.trim(), capacity: gateway ? null : Math.round(parseFloat(cap) * GB), tags: tags.split(",").map((t) => t.trim()).filter(Boolean), }); }} > {!gateway && ( )}
); } function ConnectForm({ onClose, onDone }: { onClose: () => void; onDone: () => void }) { const act = useAct(); const [addr, setAddr] = useState(""); return (
{ e.preventDefault(); const r = await garage.post<{ success: boolean; error?: string }[]>("ConnectClusterNodes", undefined, [addr]).catch((er) => { act(async () => { throw er; }); return null; }); if (!r) return; if (r[0]?.success) onDone(); else act(async () => { throw new Error(r[0]?.error || "Connexion refusée"); }); }} >
); }