Add dashboard backup catalog and health tools
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
"use client"
|
||||
|
||||
import { useEffect, useState } from "react"
|
||||
|
||||
type ActivityEntry = {
|
||||
id: string
|
||||
timestamp: string
|
||||
type: string
|
||||
message: string
|
||||
sandboxName?: string
|
||||
status?: "success" | "error" | "info" | "warning"
|
||||
}
|
||||
|
||||
function toneClass(status?: ActivityEntry["status"]) {
|
||||
if (status === "success") return "bg-[var(--status-running-bg)] text-[var(--status-running)]"
|
||||
if (status === "error") return "bg-red-500/15 text-red-300"
|
||||
if (status === "warning") return "bg-amber-400/15 text-amber-300"
|
||||
return "bg-[var(--background-tertiary)] text-[var(--foreground-dim)]"
|
||||
}
|
||||
|
||||
export default function ActivityPanel() {
|
||||
const [entries, setEntries] = useState<ActivityEntry[]>([])
|
||||
const [loading, setLoading] = useState(false)
|
||||
|
||||
async function loadActivity() {
|
||||
try {
|
||||
setLoading(true)
|
||||
const response = await fetch("/api/activity?limit=40", { cache: "no-store" })
|
||||
const data = await response.json()
|
||||
if (Array.isArray(data?.entries)) setEntries(data.entries)
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
loadActivity()
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<section className="panel p-5">
|
||||
<div className="flex items-start justify-between gap-4 max-md:flex-col">
|
||||
<div>
|
||||
<h2 className="text-sm font-semibold uppercase tracking-wider text-[var(--foreground)]">Activity Log</h2>
|
||||
<p className="mt-1 text-xs text-[var(--foreground-dim)]">Recent backup, restore, catalog, and support actions recorded by the controller.</p>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<button type="button" onClick={loadActivity} disabled={loading} className="action-button px-3 py-2">
|
||||
{loading ? "Refreshing..." : "Refresh"}
|
||||
</button>
|
||||
<a href="/api/support-bundle" className="action-button px-3 py-2">
|
||||
Support Bundle
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="mt-4 space-y-2">
|
||||
{entries.map((entry) => (
|
||||
<div key={entry.id} className="rounded-sm border border-[var(--border-subtle)] bg-[var(--background)] p-3">
|
||||
<div className="flex items-start justify-between gap-4">
|
||||
<div className="min-w-0">
|
||||
<p className="truncate text-xs font-mono text-[var(--foreground)]">{entry.message}</p>
|
||||
<p className="mt-1 text-[10px] uppercase tracking-wider text-[var(--foreground-dim)]">
|
||||
{entry.sandboxName || entry.type} / {new Date(entry.timestamp).toLocaleString()}
|
||||
</p>
|
||||
</div>
|
||||
<span className={`status-chip shrink-0 px-2 py-1 ${toneClass(entry.status)}`}>
|
||||
{entry.status || "info"}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
{entries.length === 0 && (
|
||||
<div className="rounded-sm border border-[var(--border-subtle)] bg-[var(--background)] p-4 text-sm text-[var(--foreground-dim)]">
|
||||
No activity recorded yet.
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</section>
|
||||
)
|
||||
}
|
||||
@@ -1,5 +1,10 @@
|
||||
"use client"
|
||||
|
||||
import { useEffect, useMemo, useState } from "react"
|
||||
import ActivityPanel from "./ActivityPanel"
|
||||
import SandboxHealthPanel from "./SandboxHealthPanel"
|
||||
import type { SandboxInventoryItem } from "../hooks/useSandboxInventory"
|
||||
|
||||
const helpSections = [
|
||||
{
|
||||
title: "Daily Flow",
|
||||
@@ -30,6 +35,7 @@ const helpSections = [
|
||||
title: "Backup / Restore",
|
||||
items: [
|
||||
"Backup downloads a compressed .tar.gz archive from a sandbox directory, usually /sandbox.",
|
||||
"Save to Catalog stores a backup on the controller host for later cloning or redeploying.",
|
||||
"Restore extracts a .tar.gz archive into the selected sandbox path.",
|
||||
"Replace target contents deletes existing files in the target directory before extraction; merge leaves existing files in place.",
|
||||
],
|
||||
@@ -52,9 +58,81 @@ const helpSections = [
|
||||
},
|
||||
]
|
||||
|
||||
export default function HelpPanel() {
|
||||
function HealthAccordion({ sandboxes }: { sandboxes: SandboxInventoryItem[] }) {
|
||||
const [open, setOpen] = useState(true)
|
||||
const [selectedSandboxId, setSelectedSandboxId] = useState("")
|
||||
const selectedSandbox = useMemo(
|
||||
() => sandboxes.find((sandbox) => sandbox.id === selectedSandboxId) || sandboxes[0] || null,
|
||||
[sandboxes, selectedSandboxId],
|
||||
)
|
||||
|
||||
useEffect(() => {
|
||||
if (!selectedSandboxId && sandboxes[0]) {
|
||||
setSelectedSandboxId(sandboxes[0].id)
|
||||
}
|
||||
}, [sandboxes, selectedSandboxId])
|
||||
|
||||
return (
|
||||
<section className="panel overflow-hidden">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setOpen((current) => !current)}
|
||||
aria-expanded={open}
|
||||
className="flex w-full items-center justify-between gap-4 p-5 text-left"
|
||||
>
|
||||
<div>
|
||||
<p className="text-[10px] font-mono uppercase tracking-wider text-[var(--nvidia-green)]">Operator Checks</p>
|
||||
<h2 className="mt-1 text-sm font-semibold uppercase tracking-wider text-[var(--foreground)]">Sandbox Health</h2>
|
||||
<p className="mt-1 text-xs text-[var(--foreground-dim)]">OpenShell reachability, runtime state, SSH config, and backup coverage.</p>
|
||||
</div>
|
||||
<svg
|
||||
className={`h-4 w-4 shrink-0 text-[var(--foreground-dim)] transition-transform ${open ? "rotate-90" : ""}`}
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
viewBox="0 0 24 24"
|
||||
aria-hidden="true"
|
||||
>
|
||||
<path strokeLinecap="square" strokeLinejoin="miter" strokeWidth={2} d="M9 5l7 7-7 7" />
|
||||
</svg>
|
||||
</button>
|
||||
|
||||
{open && (
|
||||
<div className="space-y-4 border-t border-[var(--border-subtle)] p-5">
|
||||
{sandboxes.length > 1 && (
|
||||
<label className="block max-w-lg space-y-2">
|
||||
<span className="text-[10px] uppercase tracking-wider text-[var(--foreground-dim)]">Sandbox</span>
|
||||
<select
|
||||
value={selectedSandbox?.id || ""}
|
||||
onChange={(event) => setSelectedSandboxId(event.target.value)}
|
||||
className="w-full rounded-sm border border-[var(--border-subtle)] bg-[var(--background-tertiary)] px-3 py-2 text-xs font-mono text-[var(--foreground)] focus:border-[var(--nvidia-green)] focus:outline-none"
|
||||
>
|
||||
{sandboxes.map((sandbox) => (
|
||||
<option key={sandbox.id} value={sandbox.id}>
|
||||
{sandbox.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
)}
|
||||
|
||||
{selectedSandbox ? (
|
||||
<SandboxHealthPanel sandbox={selectedSandbox} />
|
||||
) : (
|
||||
<div className="rounded-sm border border-[var(--border-subtle)] bg-[var(--background)] p-4 text-sm text-[var(--foreground-dim)]">
|
||||
No sandboxes are available for health checks yet.
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</section>
|
||||
)
|
||||
}
|
||||
|
||||
export default function HelpPanel({ sandboxes }: { sandboxes: SandboxInventoryItem[] }) {
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<HealthAccordion sandboxes={sandboxes} />
|
||||
|
||||
<section className="panel p-8">
|
||||
<p className="text-[10px] font-mono uppercase tracking-wider text-[var(--nvidia-green)]">Operator Guide</p>
|
||||
<h1 className="mt-2 text-xl font-semibold uppercase tracking-wider text-[var(--foreground)]">Help</h1>
|
||||
@@ -94,6 +172,8 @@ export default function HelpPanel() {
|
||||
))}
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<ActivityPanel />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"use client"
|
||||
|
||||
import { useState } from "react"
|
||||
import { useEffect, useState } from "react"
|
||||
import type { SandboxInventoryItem } from "../hooks/useSandboxInventory"
|
||||
|
||||
interface SandboxArchivePanelProps {
|
||||
@@ -8,14 +8,41 @@ interface SandboxArchivePanelProps {
|
||||
onRestoreComplete?: () => Promise<void> | void
|
||||
}
|
||||
|
||||
type BackupCatalogEntry = {
|
||||
id: string
|
||||
fileName: string
|
||||
sandboxName: string
|
||||
sourcePath: string
|
||||
size: number
|
||||
createdAt: string
|
||||
}
|
||||
|
||||
function formatBytes(value: number) {
|
||||
if (value < 1024) return `${value} B`
|
||||
if (value < 1024 * 1024) return `${Math.ceil(value / 1024)} KiB`
|
||||
return `${(value / 1024 / 1024).toFixed(1)} MiB`
|
||||
}
|
||||
|
||||
export default function SandboxArchivePanel({ sandbox, onRestoreComplete }: SandboxArchivePanelProps) {
|
||||
const [backupPath, setBackupPath] = useState("/sandbox")
|
||||
const [restorePath, setRestorePath] = useState("/sandbox")
|
||||
const [restoreReplace, setRestoreReplace] = useState(false)
|
||||
const [selectedArchive, setSelectedArchive] = useState<File | null>(null)
|
||||
const [busy, setBusy] = useState<"backup" | "restore" | null>(null)
|
||||
const [catalogBackups, setCatalogBackups] = useState<BackupCatalogEntry[]>([])
|
||||
const [busy, setBusy] = useState<"backup" | "catalog" | "restore" | `restore-${string}` | `delete-${string}` | null>(null)
|
||||
const [message, setMessage] = useState("")
|
||||
|
||||
async function loadCatalog() {
|
||||
const response = await fetch("/api/backups", { cache: "no-store" })
|
||||
const data = await response.json()
|
||||
if (!response.ok) throw new Error(data.error || "Failed to load backup catalog")
|
||||
setCatalogBackups(Array.isArray(data.backups) ? data.backups : [])
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
loadCatalog().catch(() => undefined)
|
||||
}, [sandbox.id])
|
||||
|
||||
async function backupSandbox() {
|
||||
if (!backupPath.trim() || busy) return
|
||||
try {
|
||||
@@ -49,6 +76,27 @@ export default function SandboxArchivePanel({ sandbox, onRestoreComplete }: Sand
|
||||
}
|
||||
}
|
||||
|
||||
async function saveCatalogBackup() {
|
||||
if (!backupPath.trim() || busy) return
|
||||
try {
|
||||
setBusy("catalog")
|
||||
setMessage("")
|
||||
const response = await fetch("/api/backups", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ sandboxId: sandbox.id, sourcePath: backupPath.trim() }),
|
||||
})
|
||||
const data = await response.json()
|
||||
if (!response.ok) throw new Error(data.error || "Failed to save backup to catalog")
|
||||
setMessage(`Saved ${data.backup.fileName} to the local backup catalog.`)
|
||||
await loadCatalog()
|
||||
} catch (error) {
|
||||
setMessage(error instanceof Error ? error.message : "Failed to save backup to catalog")
|
||||
} finally {
|
||||
setBusy(null)
|
||||
}
|
||||
}
|
||||
|
||||
async function restoreSandbox() {
|
||||
if (!selectedArchive || !restorePath.trim() || busy) return
|
||||
try {
|
||||
@@ -73,6 +121,44 @@ export default function SandboxArchivePanel({ sandbox, onRestoreComplete }: Sand
|
||||
}
|
||||
}
|
||||
|
||||
async function restoreCatalogBackup(backupId: string) {
|
||||
if (!restorePath.trim() || busy) return
|
||||
try {
|
||||
setBusy(`restore-${backupId}`)
|
||||
setMessage("")
|
||||
const response = await fetch(`/api/backups/${encodeURIComponent(backupId)}/restore`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ sandboxId: sandbox.id, targetPath: restorePath.trim(), replace: restoreReplace }),
|
||||
})
|
||||
const data = await response.json()
|
||||
if (!response.ok) throw new Error(data.error || "Failed to restore catalog backup")
|
||||
setMessage(data.note || `Restored catalog backup into ${restorePath.trim()}.`)
|
||||
await onRestoreComplete?.()
|
||||
} catch (error) {
|
||||
setMessage(error instanceof Error ? error.message : "Failed to restore catalog backup")
|
||||
} finally {
|
||||
setBusy(null)
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteCatalogBackup(backupId: string) {
|
||||
if (busy) return
|
||||
try {
|
||||
setBusy(`delete-${backupId}`)
|
||||
setMessage("")
|
||||
const response = await fetch(`/api/backups/${encodeURIComponent(backupId)}`, { method: "DELETE" })
|
||||
const data = await response.json()
|
||||
if (!response.ok) throw new Error(data.error || "Failed to delete catalog backup")
|
||||
setMessage("Deleted catalog backup.")
|
||||
await loadCatalog()
|
||||
} catch (error) {
|
||||
setMessage(error instanceof Error ? error.message : "Failed to delete catalog backup")
|
||||
} finally {
|
||||
setBusy(null)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-start justify-between gap-4 max-lg:flex-col">
|
||||
@@ -102,14 +188,24 @@ export default function SandboxArchivePanel({ sandbox, onRestoreComplete }: Sand
|
||||
className="w-full rounded-sm border border-[var(--border-subtle)] bg-[var(--background-tertiary)] px-3 py-2 text-xs font-mono text-[var(--foreground)] focus:outline-none focus:border-[var(--nvidia-green)]"
|
||||
/>
|
||||
</label>
|
||||
<button
|
||||
type="button"
|
||||
onClick={backupSandbox}
|
||||
disabled={!backupPath.trim() || busy !== null}
|
||||
className="rounded-sm bg-[var(--nvidia-green)] px-4 py-2 text-xs font-mono uppercase tracking-wider text-black disabled:opacity-50"
|
||||
>
|
||||
{busy === "backup" ? "Creating Backup..." : "Download Backup"}
|
||||
</button>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
<button
|
||||
type="button"
|
||||
onClick={backupSandbox}
|
||||
disabled={!backupPath.trim() || busy !== null}
|
||||
className="rounded-sm bg-[var(--nvidia-green)] px-4 py-2 text-xs font-mono uppercase tracking-wider text-black disabled:opacity-50"
|
||||
>
|
||||
{busy === "backup" ? "Creating Backup..." : "Download Backup"}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={saveCatalogBackup}
|
||||
disabled={!backupPath.trim() || busy !== null}
|
||||
className="action-button px-4 py-2"
|
||||
>
|
||||
{busy === "catalog" ? "Saving..." : "Save To Catalog"}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="rounded-sm border border-[var(--border-subtle)] bg-[var(--background)] p-4 space-y-3">
|
||||
@@ -155,6 +251,59 @@ export default function SandboxArchivePanel({ sandbox, onRestoreComplete }: Sand
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="rounded-sm border border-[var(--border-subtle)] bg-[var(--background)] p-4 space-y-3">
|
||||
<div className="flex items-start justify-between gap-4 max-md:flex-col">
|
||||
<div>
|
||||
<h6 className="text-[11px] font-semibold uppercase tracking-wider text-[var(--foreground)]">Backup Catalog</h6>
|
||||
<p className="mt-1 text-xs text-[var(--foreground-dim)]">Host-side cold storage for cloning and redeploying sandboxes later.</p>
|
||||
</div>
|
||||
<button type="button" onClick={() => loadCatalog().catch((error) => setMessage(error.message))} className="action-button px-3 py-2">
|
||||
Refresh Catalog
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
{catalogBackups.map((backup) => (
|
||||
<div key={backup.id} className="rounded-sm border border-[var(--border-subtle)] bg-[var(--background-tertiary)] p-3">
|
||||
<div className="flex items-start justify-between gap-4 max-lg:flex-col">
|
||||
<div className="min-w-0">
|
||||
<p className="truncate text-xs font-mono text-[var(--foreground)]">{backup.fileName}</p>
|
||||
<p className="mt-1 text-[10px] uppercase tracking-wider text-[var(--foreground-dim)]">
|
||||
{backup.sandboxName} / {backup.sourcePath} / {formatBytes(backup.size)} / {new Date(backup.createdAt).toLocaleString()}
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex shrink-0 flex-wrap gap-2">
|
||||
<a href={`/api/backups/${encodeURIComponent(backup.id)}/download`} className="action-button px-3 py-2">
|
||||
Download
|
||||
</a>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => restoreCatalogBackup(backup.id)}
|
||||
disabled={busy !== null || !restorePath.trim()}
|
||||
className="rounded-sm border border-[var(--nvidia-green)] bg-[var(--nvidia-green)] px-3 py-2 text-xs font-mono uppercase tracking-wider text-black disabled:opacity-50"
|
||||
>
|
||||
{busy === `restore-${backup.id}` ? "Restoring..." : "Restore Here"}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => deleteCatalogBackup(backup.id)}
|
||||
disabled={busy !== null}
|
||||
className="action-button px-3 py-2"
|
||||
>
|
||||
{busy === `delete-${backup.id}` ? "Deleting..." : "Delete"}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
{catalogBackups.length === 0 && (
|
||||
<div className="rounded-sm border border-[var(--border-subtle)] bg-[var(--background-tertiary)] p-4 text-sm text-[var(--foreground-dim)]">
|
||||
No catalog backups saved yet.
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{message && (
|
||||
<div className="rounded-sm border border-[var(--border-subtle)] bg-[var(--background)] p-3 text-xs text-[var(--foreground-dim)] whitespace-pre-wrap">
|
||||
{message}
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
"use client"
|
||||
|
||||
import { useCallback, useEffect, useState } from "react"
|
||||
import type { SandboxInventoryItem } from "../hooks/useSandboxInventory"
|
||||
|
||||
type HealthCheck = {
|
||||
key: string
|
||||
label: string
|
||||
ok: boolean
|
||||
detail: string
|
||||
}
|
||||
|
||||
type HealthResponse = {
|
||||
ok: boolean
|
||||
error?: string
|
||||
sandbox?: {
|
||||
id: string | null
|
||||
name: string
|
||||
phase: string
|
||||
}
|
||||
checks: HealthCheck[]
|
||||
backupCount?: number
|
||||
durationMs: number
|
||||
checkedAt: string
|
||||
}
|
||||
|
||||
export default function SandboxHealthPanel({ sandbox }: { sandbox: SandboxInventoryItem }) {
|
||||
const [health, setHealth] = useState<HealthResponse | null>(null)
|
||||
const [loading, setLoading] = useState(false)
|
||||
|
||||
const loadHealth = useCallback(async () => {
|
||||
try {
|
||||
setLoading(true)
|
||||
const response = await fetch(`/api/sandbox/${encodeURIComponent(sandbox.id)}/health`, { cache: "no-store" })
|
||||
const data = await response.json()
|
||||
setHealth(data)
|
||||
} catch (error) {
|
||||
setHealth({
|
||||
ok: false,
|
||||
error: error instanceof Error ? error.message : "Health check failed",
|
||||
checks: [],
|
||||
durationMs: 0,
|
||||
checkedAt: new Date().toISOString(),
|
||||
})
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}, [sandbox.id])
|
||||
|
||||
useEffect(() => {
|
||||
loadHealth()
|
||||
}, [loadHealth])
|
||||
|
||||
const checks = health?.checks || []
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-start justify-between gap-4 max-lg:flex-col">
|
||||
<div>
|
||||
<h5 className="text-xs font-semibold uppercase tracking-wider text-[var(--foreground)]">Sandbox Health</h5>
|
||||
<p className="mt-1 text-xs text-[var(--foreground-dim)]">
|
||||
Quick checks for OpenShell resolution, runtime phase, SSH config, and saved backups.
|
||||
</p>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
onClick={loadHealth}
|
||||
disabled={loading}
|
||||
className="action-button px-3 py-2"
|
||||
>
|
||||
{loading ? "Checking..." : "Refresh Health"}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 gap-3 md:grid-cols-2">
|
||||
{(checks.length ? checks : [{ key: "loading", label: "Health", ok: false, detail: loading ? "Checking..." : "No health data yet." }]).map((check) => (
|
||||
<div key={check.key} className="rounded-sm border border-[var(--border-subtle)] bg-[var(--background)] p-4">
|
||||
<div className="flex items-center justify-between gap-3">
|
||||
<h6 className="text-[11px] font-semibold uppercase tracking-wider text-[var(--foreground)]">{check.label}</h6>
|
||||
<span className={`status-chip px-2 py-1 ${check.ok ? "bg-[var(--status-running-bg)] text-[var(--status-running)]" : "bg-amber-400/15 text-amber-300"}`}>
|
||||
{check.ok ? "ok" : "check"}
|
||||
</span>
|
||||
</div>
|
||||
<p className="mt-2 text-xs leading-5 text-[var(--foreground-dim)]">{check.detail}</p>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{health && (
|
||||
<div className="rounded-sm border border-[var(--border-subtle)] bg-[var(--background)] p-3 text-xs text-[var(--foreground-dim)]">
|
||||
Checked {new Date(health.checkedAt).toLocaleString()} in {health.durationMs} ms
|
||||
{health.sandbox ? ` / ${health.sandbox.name} / ${health.sandbox.phase}` : ""}
|
||||
{health.error ? ` / ${health.error}` : ""}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user