feat: add on-demand OpenCode system monitor
Add a Desktop dashboard and text status command for disk space, RAM, CPU and Intel integrated graphics. Include visibility-driven sampling, tests, development tooling, and installation documentation.
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
import { createSignal, For, Show, type JSX } from "solid-js"
|
||||
import type { MonitorClient } from "./client.js"
|
||||
import { bytes, gpuPercent, percent, uptime } from "./format.js"
|
||||
import type { HistoryPoint } from "./types.js"
|
||||
|
||||
type Metric = "cpu" | "ram" | "gpu" | "disk"
|
||||
const paths = {
|
||||
cpu: "M8 3v3m4-3v3m4-3v3M8 18v3m4-3v3m4-3v3M3 8h3m-3 4h3m-3 4h3m12-8h3m-3 4h3m-3 4h3M7 6h10a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H7a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1Zm2 3h6v6H9Z",
|
||||
ram: "M4 7h16v10H4ZM7 17v3m4-3v3m4-3v3m4-3v3M8 10v4m4-4v4m4-4v4M2 10h2m-2 4h2m16-4h2m-2 4h2",
|
||||
gpu: "M3 6h17v12H3ZM20 10h2m-2 4h2M6 18v3m4-3v3m4-3v3M7 10h4v4H7Zm8 0h2m-2 4h2",
|
||||
disk: "M5 5h14l3 10v5H2v-5Zm-3 10h20M6 18h.01M9 18h.01",
|
||||
}
|
||||
|
||||
export function MetricIcon(props: { type: Metric }) {
|
||||
return <svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d={paths[props.type]} /></svg>
|
||||
}
|
||||
|
||||
function Bar(props: { value: number | null; label: string; kind?: Metric }) {
|
||||
return <div class="sm-bar" role="progressbar" aria-label={props.label} aria-valuemin={0} aria-valuemax={100}
|
||||
aria-valuenow={props.value === null ? undefined : Math.round(props.value)}
|
||||
aria-valuetext={props.value === null ? "Unavailable" : `${Math.round(props.value)}% used`}
|
||||
data-level={props.value !== null && props.value >= 95 ? "critical" : props.value !== null && props.value >= 85 ? "warning" : "normal"}>
|
||||
<div style={{ width: `${props.value ?? 0}%` }} />
|
||||
</div>
|
||||
}
|
||||
|
||||
function Sparkline(props: { points: HistoryPoint[]; metric: "cpu" | "ram" | "gpu" }) {
|
||||
const points = () => props.points.filter(point => point[props.metric] !== null)
|
||||
const coordinates = () => {
|
||||
const end = props.points.at(-1)?.at ?? 0
|
||||
return points().map(point => `${Math.max(0, 300 - (end - point.at) / 600)},${36 - (point[props.metric] ?? 0) * .32}`).join(" ")
|
||||
}
|
||||
return <div class="sm-trend" aria-label={`${props.metric.toUpperCase()} recent utilization`}>
|
||||
<svg viewBox="0 0 300 40" preserveAspectRatio="none" role="img" aria-label="Recent utilization, 0 to 100 percent">
|
||||
<path d="M0 39H300M0 20H300" class="sm-gridline" />
|
||||
<Show when={points().length > 1}><polyline points={coordinates()} fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" vector-effect="non-scaling-stroke" /></Show>
|
||||
</svg>
|
||||
<span>{points().length > 1 ? "Last 3 minutes · this window" : "Collecting history…"}</span>
|
||||
</div>
|
||||
}
|
||||
|
||||
function Card(props: { title: string; badge: string; kind: Metric; children: JSX.Element }) {
|
||||
return <section class={`sm-card sm-${props.kind}`} aria-label={props.title}>
|
||||
<header class="sm-card-head"><span class="sm-icon"><MetricIcon type={props.kind} /></span><h3>{props.title}</h3><span class="sm-chip">{props.badge}</span></header>
|
||||
{props.children}
|
||||
</section>
|
||||
}
|
||||
|
||||
export function Dashboard(props: {
|
||||
client: MonitorClient
|
||||
mode: "light" | "dark"
|
||||
compact?: boolean
|
||||
refreshMs: number
|
||||
setRefreshMs: (value: number) => void
|
||||
openWindow?: () => void
|
||||
}) {
|
||||
const client = props.client
|
||||
const [details, setDetails] = createSignal(false)
|
||||
return <div class="sm-root sm-dashboard" data-mode={props.mode} data-compact={props.compact ? "true" : "false"}>
|
||||
<div class="sm-toolbar">
|
||||
<div class="sm-live" data-state={!client.active() ? "paused" : client.error() || client.stale() ? "error" : "live"}>
|
||||
<span class="sm-dot" />{!client.active() ? "Paused" : client.error() ? "Connection error" : client.stale() ? "Stale readings" : "Live system overview"}
|
||||
</div>
|
||||
<div class="sm-controls">
|
||||
<select aria-label="Refresh interval" value={props.refreshMs} onChange={event => props.setRefreshMs(Number(event.currentTarget.value))}>
|
||||
<option value="2000">Every 2s</option><option value="3000">Every 3s</option><option value="5000">Every 5s</option><option value="10000">Every 10s</option>
|
||||
</select>
|
||||
<button type="button" aria-pressed={client.paused()} onClick={() => client.setPaused(!client.paused())}>{client.paused() ? "Resume" : "Pause"}</button>
|
||||
<button type="button" aria-label="Refresh system readings" title="Refresh system readings" disabled={client.busy()} onClick={() => void client.refresh()}>
|
||||
<svg viewBox="0 0 24 24" width="15" height="15" fill="none" stroke="currentColor" stroke-width="1.7" aria-hidden="true"><path d="M20 7v5h-5M4 17v-5h5M5.5 8a7 7 0 0 1 11.7-3L20 8M4 16l2.8 3A7 7 0 0 0 18.5 16" /></svg>
|
||||
</button>
|
||||
<Show when={props.compact && props.openWindow}><button type="button" onClick={() => props.openWindow?.()}>Expand</button></Show>
|
||||
</div>
|
||||
</div>
|
||||
<Show when={client.error()}><p role="alert" class="sm-error">{client.error()}. <Show when={client.snapshot()}>Showing the last successful reading.</Show></p></Show>
|
||||
<Show when={client.snapshot()} fallback={<div class="sm-loading">Reading disk, memory, CPU, and Intel graphics…</div>}>
|
||||
{snapshot => <>
|
||||
<div class="sm-machine"><strong>{snapshot().hostname}</strong><span>{snapshot().platform}</span><span>Uptime {uptime(snapshot().uptime)}</span><span class="sm-checked">Checked {client.age() < 2 ? "just now" : `${client.age()}s ago`}</span></div>
|
||||
<div class="sm-cards">
|
||||
<Card kind="cpu" title="CPU" badge={`${snapshot().cpu.cores} threads`}>
|
||||
<div class="sm-reading"><strong>{percent(snapshot().cpu.percent)}</strong><span>used</span></div>
|
||||
<p class="sm-device">{snapshot().cpu.name}</p>
|
||||
<Bar value={snapshot().cpu.percent} label="CPU utilization" />
|
||||
<div class="sm-pair"><span>All logical processors</span><span>{percent(snapshot().cpu.percent === null ? null : 100 - snapshot().cpu.percent!)} idle</span></div>
|
||||
<Sparkline points={client.history()} metric="cpu" />
|
||||
</Card>
|
||||
<Card kind="ram" title="Memory" badge="RAM">
|
||||
<div class="sm-reading"><strong>{percent(snapshot().ram.percent)}</strong><span>used</span></div>
|
||||
<p class="sm-device">{bytes(snapshot().ram.used)} of {bytes(snapshot().ram.total)}</p>
|
||||
<Bar value={snapshot().ram.percent} label="RAM utilization" />
|
||||
<div class="sm-pair"><span>Available memory</span><strong>{bytes(snapshot().ram.free)}</strong></div>
|
||||
<Sparkline points={client.history()} metric="ram" />
|
||||
</Card>
|
||||
<For each={snapshot().disks.items}>{disk => <Card kind="disk" title="Disk space" badge={disk.id}>
|
||||
<div class="sm-reading sm-space"><strong>{bytes(disk.free)}</strong><span>free</span></div>
|
||||
<p class="sm-device">{disk.name} · {bytes(disk.total)} capacity</p>
|
||||
<Bar value={disk.percent} label={`${disk.id} disk space used`} />
|
||||
<div class="sm-pair"><span>{bytes(disk.used)} used</span><strong>{percent(disk.percent)} used</strong></div>
|
||||
<div class="sm-disk-note"><span class="sm-dot" />{disk.percent >= 95 ? "Very little free space" : disk.percent >= 85 ? "Getting full" : "Plenty of free space"}<span>Checked every 30s</span></div>
|
||||
</Card>}</For>
|
||||
<Show when={!snapshot().disks.items.length}><Card kind="disk" title="Disk space" badge="Unavailable"><p class="sm-error">{snapshot().disks.error ?? "No fixed drives detected"}</p></Card></Show>
|
||||
<Card kind="gpu" title="Integrated graphics" badge="Intel">
|
||||
<div class="sm-reading"><strong>{percent(gpuPercent(snapshot()))}</strong><span>used</span></div>
|
||||
<p class="sm-device">{snapshot().gpu.adapter?.name ?? "Intel graphics"}</p>
|
||||
<Bar value={gpuPercent(snapshot())} label="Intel integrated GPU utilization" />
|
||||
<div class="sm-pair"><span>Shared RAM in use</span><strong>{bytes(snapshot().gpu.adapter?.sharedUsed)}</strong></div>
|
||||
<Sparkline points={client.history()} metric="gpu" />
|
||||
<Show when={snapshot().gpu.error}><p class="sm-error">{snapshot().gpu.error}</p></Show>
|
||||
</Card>
|
||||
</div>
|
||||
<Show when={snapshot().disks.error && snapshot().disks.items.length}><p class="sm-error">Disk readings may be stale: {snapshot().disks.error}</p></Show>
|
||||
<div class="sm-footer"><span><span class="sm-dot" />Local readings</span><span>Intel GPU checked {client.gpuAge()}s ago</span><button type="button" onClick={() => setDetails(!details())} aria-expanded={details()}>{details() ? "Hide details" : "Details"}</button></div>
|
||||
<Show when={details()}><div class="sm-details">
|
||||
<h4>CPU utilization per logical processor</h4>
|
||||
<div class="sm-cores"><For each={snapshot().cpu.perCore}>{(value, index) => <div title={`Thread ${index() + 1}: ${percent(value)}`}><span>{index() + 1}</span><Bar value={value} label={`CPU thread ${index() + 1}`} /><b>{percent(value)}</b></div>}</For></div>
|
||||
<p>GPU utilization is the busiest engine on the Intel adapter, with process counters combined per engine. Other GPUs are excluded. GPU checks are limited to once every 5 seconds.</p>
|
||||
<p>Integrated graphics borrows system RAM: {bytes(snapshot().gpu.adapter?.sharedUsed)} currently used, with a driver-reported limit of {bytes(snapshot().gpu.adapter?.sharedLimit)}. This is already part of memory usage, not extra dedicated VRAM.</p>
|
||||
<p>Sampling runs only while this dashboard is visible. Closing it or hiding this window stops automatic checks. Use /system to reopen it or /system_status for a one-time text report. History lives only in this window. Sizes use binary units (GiB).</p>
|
||||
</div></Show>
|
||||
</>}
|
||||
</Show>
|
||||
</div>
|
||||
}
|
||||
Reference in New Issue
Block a user