Add Health and Spend for Admins

This commit is contained in:
2026-04-11 22:36:36 +02:00
parent fdb56490dd
commit 1edb5785e3
10 changed files with 667 additions and 126 deletions

View File

@@ -6,9 +6,18 @@ import type { PiecedTenant, TenantRequest } from "@/types";
import { StatusBadge } from "@/components/ui/status-badge";
import Link from "next/link";
type Tab = "requests" | "tenants";
type Tab = "requests" | "tenants" | "health";
type RequestFilter = "all" | "pending" | "provisioning" | "approved" | "rejected";
interface HealthData {
tenants: { total: number; phases: Record<string, number> };
spend: { global: number; perTenant: Record<string, number> };
services: {
litellm: { healthy: boolean; details?: any };
vllm: { healthy: boolean; details?: any };
};
}
interface AdminPanelProps {
initialTenants: PiecedTenant[];
}
@@ -30,6 +39,10 @@ export function AdminPanel({ initialTenants }: AdminPanelProps) {
const [loadingTenants, setLoadingTenants] = useState(false);
const [deleteModal, setDeleteModal] = useState<string | null>(null);
// Health state
const [health, setHealth] = useState<HealthData | null>(null);
const [loadingHealth, setLoadingHealth] = useState(false);
// Shared
const [error, setError] = useState("");
@@ -79,6 +92,34 @@ export function AdminPanel({ initialTenants }: AdminPanelProps) {
}
}, [tab, fetchTenants]);
// ─── Health fetching ───
const fetchHealth = useCallback(async () => {
setLoadingHealth(true);
try {
const res = await fetch("/api/admin/health");
if (!res.ok) throw new Error("Failed to fetch health");
const data = await res.json();
setHealth(data);
} catch (e: any) {
setError(e.message);
} finally {
setLoadingHealth(false);
}
}, []);
useEffect(() => {
if (tab === "health") {
fetchHealth();
}
}, [tab, fetchHealth]);
// Also fetch health for spend data when on tenants tab
useEffect(() => {
if (tab === "tenants" && !health) {
fetchHealth();
}
}, [tab, health, fetchHealth]);
// ─── Request actions ───
const handleApprove = async (id: string) => {
setActionLoading(id);
@@ -212,6 +253,19 @@ export function AdminPanel({ initialTenants }: AdminPanelProps) {
<div className="absolute bottom-0 left-0 right-0 h-0.5 bg-accent" />
)}
</button>
<button
onClick={() => setTab("health")}
className={`px-4 py-2.5 text-sm font-medium transition-colors relative ${
tab === "health"
? "text-accent"
: "text-text-muted hover:text-text-secondary"
}`}
>
{t("health")}
{tab === "health" && (
<div className="absolute bottom-0 left-0 right-0 h-0.5 bg-accent" />
)}
</button>
</div>
{/* Error banner */}
@@ -435,6 +489,9 @@ export function AdminPanel({ initialTenants }: AdminPanelProps) {
<th className="px-4 py-3 text-xs font-semibold uppercase tracking-wider text-text-muted hidden md:table-cell">
{t("packages")}
</th>
<th className="px-4 py-3 text-xs font-semibold uppercase tracking-wider text-text-muted hidden md:table-cell">
{t("spendChf")}
</th>
<th className="px-4 py-3 text-xs font-semibold uppercase tracking-wider text-text-muted hidden md:table-cell">
{t("created")}
</th>
@@ -444,76 +501,85 @@ export function AdminPanel({ initialTenants }: AdminPanelProps) {
</tr>
</thead>
<tbody>
{tenants.map((tenant) => (
<tr
key={tenant.metadata.name}
className={`border-b border-border last:border-0 hover:bg-surface-2/50 transition-colors ${
tenant.spec.suspend ? "opacity-60" : ""
}`}
>
<td className="px-4 py-3 font-mono text-xs text-accent">
{tenant.metadata.name}
</td>
<td className="px-4 py-3 text-text-primary">
<span>{tenant.spec.displayName}</span>
{tenant.spec.suspend && (
<span className="ml-2 px-1.5 py-0.5 text-[10px] font-medium bg-amber-400/15 text-amber-400 rounded">
{t("suspendedBadge")}
</span>
)}
</td>
<td className="px-4 py-3">
<StatusBadge
phase={tenant.status?.phase ?? "Pending"}
/>
</td>
<td className="px-4 py-3 text-xs text-text-secondary font-mono hidden md:table-cell">
{tenant.spec.packages?.join(", ") || "—"}
</td>
<td className="px-4 py-3 text-xs text-text-muted tabular-nums hidden md:table-cell">
{tenant.metadata.creationTimestamp
? new Date(
tenant.metadata.creationTimestamp
).toLocaleDateString()
: "—"}
</td>
<td className="px-4 py-3">
<div className="flex gap-1.5 flex-wrap">
<Link
href={`/tenants/${tenant.metadata.name}`}
className="px-2.5 py-1 text-xs font-medium bg-accent/15 text-accent rounded-md hover:bg-accent/25 transition-colors"
>
{t("manage")}
</Link>
<button
onClick={() =>
handleSuspend(
tenant.metadata.name,
!tenant.spec.suspend
)
}
disabled={actionLoading === tenant.metadata.name}
className="px-2.5 py-1 text-xs font-medium bg-amber-500/15 text-amber-400 rounded-md hover:bg-amber-500/25 transition-colors disabled:opacity-50"
>
{actionLoading === tenant.metadata.name
? "…"
: tenant.spec.suspend
? t("resume")
: t("suspend")}
</button>
<button
onClick={() =>
setDeleteModal(tenant.metadata.name)
}
disabled={actionLoading === tenant.metadata.name}
className="px-2.5 py-1 text-xs font-medium bg-red-500/15 text-red-400 rounded-md hover:bg-red-500/25 transition-colors disabled:opacity-50"
>
{t("deleteTenant")}
</button>
</div>
</td>
</tr>
))}
{tenants.map((tenant) => {
const tenantSpend =
health?.spend?.perTenant?.[tenant.metadata.name];
return (
<tr
key={tenant.metadata.name}
className={`border-b border-border last:border-0 hover:bg-surface-2/50 transition-colors ${
tenant.spec.suspend ? "opacity-60" : ""
}`}
>
<td className="px-4 py-3 font-mono text-xs text-accent">
{tenant.metadata.name}
</td>
<td className="px-4 py-3 text-text-primary">
<span>{tenant.spec.displayName}</span>
{tenant.spec.suspend && (
<span className="ml-2 px-1.5 py-0.5 text-[10px] font-medium bg-amber-400/15 text-amber-400 rounded">
{t("suspendedBadge")}
</span>
)}
</td>
<td className="px-4 py-3">
<StatusBadge
phase={tenant.status?.phase ?? "Pending"}
/>
</td>
<td className="px-4 py-3 text-xs text-text-secondary font-mono hidden md:table-cell">
{tenant.spec.packages?.join(", ") || "—"}
</td>
<td className="px-4 py-3 text-xs font-mono tabular-nums text-text-secondary hidden md:table-cell">
{tenantSpend !== undefined
? `CHF ${tenantSpend.toFixed(2)}`
: "—"}
</td>
<td className="px-4 py-3 text-xs text-text-muted tabular-nums hidden md:table-cell">
{tenant.metadata.creationTimestamp
? new Date(
tenant.metadata.creationTimestamp
).toLocaleDateString()
: "—"}
</td>
<td className="px-4 py-3">
<div className="flex gap-1.5 flex-wrap">
<Link
href={`/tenants/${tenant.metadata.name}`}
className="px-2.5 py-1 text-xs font-medium bg-accent/15 text-accent rounded-md hover:bg-accent/25 transition-colors"
>
{t("manage")}
</Link>
<button
onClick={() =>
handleSuspend(
tenant.metadata.name,
!tenant.spec.suspend
)
}
disabled={actionLoading === tenant.metadata.name}
className="px-2.5 py-1 text-xs font-medium bg-amber-500/15 text-amber-400 rounded-md hover:bg-amber-500/25 transition-colors disabled:opacity-50"
>
{actionLoading === tenant.metadata.name
? "…"
: tenant.spec.suspend
? t("resume")
: t("suspend")}
</button>
<button
onClick={() =>
setDeleteModal(tenant.metadata.name)
}
disabled={actionLoading === tenant.metadata.name}
className="px-2.5 py-1 text-xs font-medium bg-red-500/15 text-red-400 rounded-md hover:bg-red-500/25 transition-colors disabled:opacity-50"
>
{t("deleteTenant")}
</button>
</div>
</td>
</tr>
);
})}
</tbody>
</table>
</div>
@@ -522,6 +588,115 @@ export function AdminPanel({ initialTenants }: AdminPanelProps) {
</>
)}
{/* ───── HEALTH TAB ───── */}
{tab === "health" && (
<>
{loadingHealth ? (
<div className="bg-surface-1 border border-border rounded-xl p-12 text-center">
<div className="h-5 w-5 border-2 border-accent border-t-transparent rounded-full animate-spin mx-auto mb-2" />
<p className="text-text-muted text-xs">{t("loadingHealth")}</p>
</div>
) : health ? (
<div className="space-y-6">
{/* Service health indicators */}
<div>
<h3 className="text-xs font-semibold uppercase tracking-wider text-text-muted mb-3">
{t("serviceHealth")}
</h3>
<div className="grid grid-cols-1 md:grid-cols-2 gap-3">
<ServiceCard
name="vLLM"
subtitle={t("vllmDescription")}
healthy={health.services.vllm.healthy}
t={t}
/>
<ServiceCard
name="LiteLLM"
subtitle={t("litellmDescription")}
healthy={health.services.litellm.healthy}
t={t}
/>
</div>
</div>
{/* Tenant overview */}
<div>
<h3 className="text-xs font-semibold uppercase tracking-wider text-text-muted mb-3">
{t("tenantOverview")}
</h3>
<div className="grid grid-cols-2 md:grid-cols-4 gap-3">
<SummaryCard
label={t("totalTenants")}
value={health.tenants.total}
/>
<SummaryCard
label={t("running")}
value={
(health.tenants.phases["Running"] ?? 0) +
(health.tenants.phases["Ready"] ?? 0)
}
color="text-emerald-400"
/>
<SummaryCard
label={t("suspended")}
value={health.tenants.phases["Suspended"] ?? 0}
color="text-amber-400"
/>
<SummaryCard
label={t("errors")}
value={health.tenants.phases["Error"] ?? 0}
color="text-red-400"
/>
</div>
</div>
{/* Spend overview */}
<div>
<h3 className="text-xs font-semibold uppercase tracking-wider text-text-muted mb-3">
{t("spendOverview")}
</h3>
<div className="grid grid-cols-1 md:grid-cols-2 gap-3">
<div className="bg-surface-1 border border-border rounded-xl p-4">
<p className="text-xs text-text-muted uppercase tracking-wider mb-1">
{t("globalSpend")}
</p>
<p className="font-mono text-2xl font-semibold tabular-nums text-text-primary">
CHF {health.spend.global.toFixed(2)}
</p>
</div>
<div className="bg-surface-1 border border-border rounded-xl p-4">
<p className="text-xs text-text-muted uppercase tracking-wider mb-1">
{t("activeTenants")}
</p>
<p className="font-mono text-2xl font-semibold tabular-nums text-text-primary">
{Object.keys(health.spend.perTenant).length}
</p>
<p className="text-xs text-text-muted mt-1">
{t("tenantsWithSpend")}
</p>
</div>
</div>
</div>
{/* Refresh button */}
<div className="flex justify-end">
<button
onClick={fetchHealth}
disabled={loadingHealth}
className="px-4 py-2 text-xs font-medium bg-surface-2 border border-border rounded-lg text-text-secondary hover:text-text-primary transition-colors disabled:opacity-50"
>
{t("refresh")}
</button>
</div>
</div>
) : (
<div className="bg-surface-1 border border-border rounded-xl p-12 text-center">
<p className="text-text-secondary text-sm">{t("healthUnavailable")}</p>
</div>
)}
</>
)}
{/* ───── REJECT MODAL ───── */}
{rejectModal && (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-blur-sm">
@@ -596,6 +771,49 @@ export function AdminPanel({ initialTenants }: AdminPanelProps) {
);
}
function ServiceCard({
name,
subtitle,
healthy,
t,
}: {
name: string;
subtitle: string;
healthy: boolean;
t: any;
}) {
return (
<div className="bg-surface-1 border border-border rounded-xl p-4 flex items-center gap-4">
<div
className={`shrink-0 h-10 w-10 rounded-lg flex items-center justify-center ${
healthy ? "bg-emerald-400/15" : "bg-red-400/15"
}`}
>
<div
className={`h-3 w-3 rounded-full ${
healthy ? "bg-emerald-400" : "bg-red-400 animate-pulse"
}`}
/>
</div>
<div className="min-w-0">
<p className="text-sm font-medium text-text-primary">{name}</p>
<p className="text-xs text-text-muted truncate">{subtitle}</p>
</div>
<div className="ml-auto shrink-0">
<span
className={`text-xs font-medium px-2 py-0.5 rounded-full ${
healthy
? "bg-emerald-400/15 text-emerald-400"
: "bg-red-400/15 text-red-400"
}`}
>
{healthy ? t("statusHealthy") : t("statusDown")}
</span>
</div>
</div>
);
}
function RequestStatusBadge({ status }: { status: string }) {
const colors: Record<string, string> = {
pending: "bg-blue-400/15 text-blue-400",