import { useEffect, useState } from "react"; import { Link } from "react-router-dom"; import { api, type AgentLatest, type DockhandContainer, type UpdatesServer } from "../api/client"; import { agentState, agentVersionWords } from "../utils/agent"; import SortableTh from "../components/SortableTh"; import TableToolbar from "../components/TableToolbar"; import TimeAgo from "../components/TimeAgo"; import UpdatesBadge from "../components/UpdatesBadge"; import { BlockSkeleton } from "../components/Skeleton"; import { ErrorNotice } from "../components/Notice"; import { useSortable } from "../hooks/useSortable"; import { useTableSearch } from "../hooks/useTableSearch"; import { downloadCsv } from "../utils/csv"; import { readableError } from "../utils/errors"; import ExportTableButton from "../components/ExportTableButton"; interface Row { id: number; name: string; os: string; manager: string; /** Null: the agent hasn't said. */ count: number | null; security: number | null; restart: boolean; /** ISO — when updates were first seen waiting. */ since: string | null; reportedAt: string | null; agentVersion: number | null; server: UpdatesServer; } function toRow(s: UpdatesServer): Row { return { id: s.id, name: s.name, os: s.osType, manager: s.updates?.manager ?? "", count: s.updates ? s.updates.count : null, security: s.updates ? s.updates.security : null, restart: s.updates?.rebootRequired ?? false, since: s.pendingSince, reportedAt: s.updates?.reportedAt || null, agentVersion: s.agentVersion, server: s, }; } const DAY_MS = 86_400_000; /** * What is waiting to be installed, in one place: the updates each server's agent says are pending (how many, how many are security fixes, * whether a restart is due, and how long they have been waiting) and the Docker containers that have a newer image. It only looks — * nothing here installs anything. */ export default function Updates() { const [servers, setServers] = useState(null); const [agentLatest, setAgentLatest] = useState(null); const [containers, setContainers] = useState<(DockhandContainer & { integrationName: string })[] | null>(null); const [error, setError] = useState(null); const [onlyWaiting, setOnlyWaiting] = useState(false); useEffect(() => { let alive = true; api.servers .updates() .then((res) => { if (!alive) return; setServers(res.servers); setAgentLatest(res.agentLatest); }) .catch((err) => alive && setError(readableError(err))); api.integrations .list() .then(async (res) => { const docker = res.integrations.filter((i) => i.type === "dockhand" && i.enabled); const all = await Promise.all( docker.map((i) => api.integrations.dockhand .containers(i.id) .then((r) => r.containers.map((c) => ({ ...c, integrationName: i.name }))) .catch(() => []), ), ); if (alive) setContainers(docker.length === 0 ? null : all.flat().filter((c) => c.updateAvailable === true)); }) .catch(() => {}); return () => { alive = false; }; }, []); const rows = (servers ?? []).map(toRow); const waiting = rows.filter((r) => (r.count ?? 0) > 0); const tableSearch = useTableSearch(onlyWaiting ? waiting : rows); const { sorted, sortKey, sortDir, requestSort } = useSortable(tableSearch.filtered, "security", "desc", { url: true }); const reporting = rows.filter((r) => r.count !== null).length; const securityTotal = rows.reduce((n, r) => n + (r.security ?? 0), 0); const restarts = rows.filter((r) => r.restart).length; const oldest = waiting.map((r) => r.since).filter((s): s is string => !!s).sort()[0]; const notReporting = rows.length - reporting; const behind = rows.filter((r) => agentState(r.server, agentLatest) === "behind").length; function exportCsv() { if (!sorted) return; downloadCsv( "updates.csv", ["Server", "Package manager", "Waiting", "Security", "Restart needed", "Waiting since", "Reported"], sorted.map((r) => [r.name, r.manager, r.count ?? "", r.security ?? "", r.restart ? "yes" : "no", r.since ?? "", r.reportedAt ?? ""]), ); } const cards: { label: string; value: string | number; note?: string; color: string }[] = [ { label: "Servers with updates waiting", value: servers ? `${waiting.length} of ${reporting}` : "…", note: notReporting > 0 ? `${notReporting} haven't reported updates` : undefined, color: waiting.length > 0 ? "text-yellow" : "text-green" }, { label: "Security updates waiting", value: servers ? securityTotal : "…", color: securityTotal > 0 ? "text-red" : "text-green" }, { label: "Need a restart", value: servers ? restarts : "…", color: restarts > 0 ? "text-orange" : "text-green" }, { label: "Agents to update", value: servers ? behind : "…", note: behind > 0 ? "run their install command again" : undefined, color: behind > 0 ? "text-yellow" : "text-green" }, { label: "Longest wait", value: oldest ? `${Math.max(1, Math.floor((Date.now() - Date.parse(oldest)) / DAY_MS))} d` : servers ? "—" : "…", color: "text-secondary" }, ]; return ( <>

Updates

What the agents say is waiting to be installed on each server, and the Docker containers that have a newer image. This page only looks — it installs nothing. To apply updates, run a playbook from the server's page.

{cards.map((c) => (
{c.value}
{c.label}
{c.note &&
{c.note}
}
))}
setOnlyWaiting(e.target.checked)} /> Only those with updates } shown={sorted?.length} total={onlyWaiting ? waiting.length : rows.length} onExport={exportCsv} exportDisabled={!sorted || sorted.length === 0} />
label="Server" sortKeyName="name" activeKey={sortKey} direction={sortDir} onSort={requestSort} /> label="Updates" sortKeyName="count" activeKey={sortKey} direction={sortDir} onSort={requestSort} /> label="Security" sortKeyName="security" activeKey={sortKey} direction={sortDir} onSort={requestSort} /> label="Waiting since" sortKeyName="since" activeKey={sortKey} direction={sortDir} onSort={requestSort} /> label="Reported" sortKeyName="reportedAt" activeKey={sortKey} direction={sortDir} onSort={requestSort} /> label="Agent" sortKeyName="agentVersion" activeKey={sortKey} direction={sortDir} onSort={requestSort} /> {servers === null && !error && ( )} {sorted?.map((r) => ( ))} {servers !== null && sorted?.length === 0 && ( )}
{r.name} {r.manager} {r.security === null ? — : r.security} {r.since ? : "—"} {r.reportedAt ? : "—"} {agentVersionWords(r.agentVersion)}{" "} {agentState(r.server, agentLatest) === "behind" && ( behind )}
{tableSearch.active || onlyWaiting ? "Nothing matches that." : "No server has reported yet."}
{notReporting > 0 && (
“Not reported” means that server's agent hasn't said — it may be an older agent (run its install command again, from Servers → Manage servers, to update it), or its package manager isn't apt, dnf, zypper or Windows Update.
)}
{containers !== null && (

Docker containers with a newer image

{containers.map((c) => ( ))} {containers.length === 0 && ( )}
Container Image Newer version Where Checked
{c.name} {c.image} {c.newerVersion ?? "—"} {c.environmentName}
Every container is on its latest image, as far as the last check could tell.
)} ); }