Files

203 lines
7.9 KiB
TypeScript

"use client"
import { useEffect, useState } from "react"
/**
* SandboxFleetView — fleet-native Sandboxes-View (ersetzt die tote Upstream-
* "Sandboxes"-View, die den openshell-CLI auf dem Tower-HOST brauchte — unser
* Tower ist ein K8s-Container ohne Host-CLI).
*
* Datenquelle: /api/fleet/sandboxes (kube-state-metrics via Prometheus,
* read-only PromQL; KEIN kubectl, KEINE Host-CLI). Zeigt die 9 Agenten-Sandboxes:
* Phase/Ready, Restarts, Pod-Age, Node, CPU/Mem-Requests, Image-Digest,
* Heartbeat-Age. Poll 5 s, Fehler fail-loud sichtbar (kein stilles Leeren).
*/
type SandboxAgent = {
agent_id: string
role: string
title: string | null
exists: boolean
phase: string | null
ready: boolean | null
restarts: number | null
pod_age_s: number | null
node: string | null
cpu_request_milli: number | null
mem_request_mb: number | null
image_digest: string | null
heartbeat_age_s: number | null
}
type SandboxesData = {
agents: SandboxAgent[]
fleet_image_digest: string | null
all_running: boolean
generated_at: string
}
function phaseColor(agent: SandboxAgent): { color: string; label: string } {
if (!agent.exists || !agent.phase) return { color: "var(--foreground-dim)", label: "kein Pod" }
if (agent.phase === "Running" && agent.ready) return { color: "var(--status-running)", label: "Running" }
if (agent.phase === "Running") return { color: "#e6b450", label: "Running/NotReady" }
return { color: "var(--status-stopped)", label: agent.phase }
}
function hbColor(age: number | null): { color: string; label: string } {
if (age === null) return { color: "var(--foreground-dim)", label: "pending" }
if (age < 120) return { color: "var(--status-running)", label: `${Math.round(age)}s` }
if (age < 300) return { color: "#e6b450", label: `${Math.round(age)}s` }
return { color: "var(--status-stopped)", label: `${Math.round(age)}s` }
}
function fmtAge(sec: number): string {
if (sec < 60) return `${sec}s`
if (sec < 3600) return `${Math.floor(sec / 60)}m`
if (sec < 86400) return `${Math.floor(sec / 3600)}h ${Math.floor((sec % 3600) / 60)}m`
return `${Math.floor(sec / 86400)}d ${Math.floor((sec % 86400) / 3600)}h`
}
function fmtDigest(d: string | null): string {
if (!d) return "—"
return d.length > 19 ? `${d.slice(0, 19)}…` : d
}
export default function SandboxFleetView() {
const [data, setData] = useState<SandboxesData | null>(null)
const [error, setError] = useState<string | null>(null)
useEffect(() => {
let cancelled = false
const load = () => {
fetch("/api/fleet/sandboxes", { cache: "no-store" })
.then(async (res) => {
if (!res.ok) throw new Error((await res.json()).error || `HTTP ${res.status}`)
return res.json()
})
.then((d: SandboxesData) => {
if (!cancelled) {
setData(d)
setError(null)
}
})
.catch((e: Error) => {
if (!cancelled) setError(e.message)
})
}
load()
const timer = setInterval(load, 5000)
return () => {
cancelled = true
clearInterval(timer)
}
}, [])
if (error) {
return (
<div className="panel p-8 text-center" data-testid="fleet-sandboxes-error">
<h3 className="text-sm font-semibold text-[var(--status-stopped)] uppercase tracking-wider">
Fleet-Datenquelle nicht erreichbar
</h3>
<p className="text-xs text-[var(--foreground-dim)] mt-2 font-mono break-all">{error}</p>
</div>
)
}
if (!data) {
return (
<div className="flex items-center justify-center h-64" data-testid="fleet-sandboxes-loading">
<div className="text-xs text-[var(--foreground-dim)] font-mono uppercase tracking-wider">Lade Sandboxes…</div>
</div>
)
}
const running = data.agents.filter((a) => a.phase === "Running" && a.ready).length
return (
<div className="space-y-4" data-testid="fleet-sandboxes-view">
<div className="flex items-center justify-between flex-wrap gap-2">
<h1 className="text-lg font-semibold text-[var(--nvidia-green)] uppercase tracking-wider">Sandboxes</h1>
<div className="flex items-center gap-3 text-xs font-mono text-[var(--foreground-dim)]">
<span data-testid="fleet-sandboxes-count">
{running}/{data.agents.length} running
</span>
<span
className="px-2 py-0.5 rounded"
style={{
backgroundColor: data.all_running ? "rgba(118,185,0,0.15)" : "rgba(255,107,107,0.15)",
color: data.all_running ? "var(--status-running)" : "var(--status-stopped)",
}}
>
{data.all_running ? "ALLE GRÜN" : "DEGRADED"}
</span>
</div>
</div>
<div className="panel p-4">
<div className="text-[10px] text-[var(--foreground-dim)] uppercase tracking-wider">Fleet Image (digest-gepinnt)</div>
<div className="text-xs font-mono text-[var(--foreground)] mt-1" data-testid="fleet-sandboxes-image">
{fmtDigest(data.fleet_image_digest)}
</div>
</div>
<div className="grid grid-cols-1 lg:grid-cols-2 xl:grid-cols-3 gap-4">
{data.agents.map((agent) => {
const ph = phaseColor(agent)
const hb = hbColor(agent.heartbeat_age_s)
return (
<div key={agent.agent_id} className="panel p-4" data-testid={`fleet-sandbox-${agent.agent_id}`}>
<div className="flex items-center justify-between">
<div>
<div className="text-sm font-semibold text-[var(--foreground)] font-mono">{agent.agent_id}</div>
<div className="text-[10px] text-[var(--foreground-dim)] uppercase tracking-wider">{agent.role}</div>
</div>
<span
className="text-[10px] font-mono px-2 py-0.5 rounded uppercase"
style={{ backgroundColor: "rgba(255,255,255,0.06)", color: ph.color }}
data-testid={`fleet-sandbox-phase-${agent.agent_id}`}
>
{ph.label}
</span>
</div>
<div className="grid grid-cols-2 gap-x-4 gap-y-1 mt-3 text-xs">
<span className="text-[var(--foreground-dim)]">Node</span>
<span className="font-mono text-right text-[var(--foreground)]">{agent.node || "—"}</span>
<span className="text-[var(--foreground-dim)]">Pod-Age</span>
<span className="font-mono text-right text-[var(--foreground)]">
{agent.pod_age_s === null ? "—" : fmtAge(agent.pod_age_s)}
</span>
<span className="text-[var(--foreground-dim)]">Restarts</span>
<span
className="font-mono text-right"
style={{ color: agent.restarts && agent.restarts > 0 ? "#e6b450" : "var(--foreground)" }}
>
{agent.restarts ?? "—"}
</span>
<span className="text-[var(--foreground-dim)]">Requests</span>
<span className="font-mono text-right text-[var(--foreground)]">
{agent.cpu_request_milli ? `${agent.cpu_request_milli}m / ${agent.mem_request_mb}Mi` : "—"}
</span>
<span className="text-[var(--foreground-dim)]">Heartbeat</span>
<span className="font-mono text-right" style={{ color: hb.color }} data-testid={`fleet-sandbox-hb-${agent.agent_id}`}>
{hb.label}
</span>
<span className="text-[var(--foreground-dim)]">Image</span>
<span className="font-mono text-right text-[var(--foreground-dim)]">{fmtDigest(agent.image_digest)}</span>
</div>
</div>
)
})}
</div>
<div className="text-[10px] text-[var(--foreground-dim)] font-mono">
Stand: {new Date(data.generated_at).toLocaleTimeString("de-DE")} · Quelle: kube-state-metrics via Prometheus (read-only)
</div>
</div>
)
}