From 2a506547703d3e75c63bf17fcfb36812c1c2ca2f Mon Sep 17 00:00:00 2001 From: Dylan Trotter Date: Tue, 20 Oct 2026 20:22:15 +0000 Subject: [PATCH 1/2] devbox-provider: live machine usage dashboard (BB-25) --- plugins/devbox-provider/README.md | 17 ++ plugins/devbox-provider/app.tsx | 303 ++++++++++++++++++++++++- plugins/devbox-provider/incus.test.ts | 54 ++++- plugins/devbox-provider/incus.ts | 55 ++++- plugins/devbox-provider/server.test.ts | 158 ++++++++++++- plugins/devbox-provider/server.ts | 243 +++++++++++++++++++- 6 files changed, 822 insertions(+), 8 deletions(-) diff --git a/plugins/devbox-provider/README.md b/plugins/devbox-provider/README.md index 08de612a..44bbf634 100644 --- a/plugins/devbox-provider/README.md +++ b/plugins/devbox-provider/README.md @@ -42,6 +42,23 @@ bb (this plugin) ──Bearer──▶ devbox-gate :8081 (in-cluster) ──your Machines persist (`ephemeral: false`); nothing suspends them on its own. +## Usage dashboard + +**Devbox machines** in the sidebar shows live usage for every instance in +the connected project, including ones created outside bb (CLI-created +instances are marked as such by their absence of a bb badge). Each card has +CPU (with a sparkline), memory, disk, network in/out and uptime, and the +page marks machines bb manages. The page reads Incus through the same +connection token the rest of the plugin uses; it polls about every 5 seconds +while open and stops when you leave it. Project totals are labelled +**Allocated** because Incus reports the sum of the instances' configured +limits there, not live usage. Nothing is stored on the server: the +sparklines are an in-memory ring buffer that starts empty each visit. + +CPU percent needs two samples, so the first poll after the server restarts +(or after an instance restarts) shows no percentage until the next one. A +stopped instance reports disk only. + ## Settings | Setting | Default | Meaning | diff --git a/plugins/devbox-provider/app.tsx b/plugins/devbox-provider/app.tsx index 273f7908..2b059211 100644 --- a/plugins/devbox-provider/app.tsx +++ b/plugins/devbox-provider/app.tsx @@ -1,10 +1,14 @@ // The plugin's settings section: connect or disconnect devbox, and the // Tailscale sign-in links of machines being created, which are the one step // a person has to take. +// +// It also contributes the Devbox machines nav panel, which shows live usage +// for every instance in the connected project while it is open. import { useCallback, useEffect, useState } from "react"; import { definePluginApp, useBbNavigate, useRealtime, useRpc } from "@get-bb/plugin-sdk/app"; -import type { rpcContract } from "./server"; +import type { rpcContract, UsageResult, UsageRow, UsageTotals } from "./server"; import { Button } from "@/components/ui/button"; +import { cn } from "@/lib/utils"; interface Status { connected: boolean; @@ -178,6 +182,296 @@ function DevboxSection() { ); } +// ---- Devbox machines page ------------------------------------------------ + +const POLL_MS = 5000; +// Roughly two and a half minutes of history at the poll interval. +const HISTORY_LENGTH = 30; + +function formatBytes(bytes: number | null): string { + if (bytes === null) return "—"; + if (bytes < 1024) return `${bytes} B`; + const units = ["KiB", "MiB", "GiB", "TiB", "PiB"]; + let value = bytes / 1024; + let unit = 0; + while (value >= 1024 && unit < units.length - 1) { + value /= 1024; + unit += 1; + } + return `${value >= 100 ? value.toFixed(0) : value.toFixed(1)} ${units[unit]}`; +} + +function formatPercent(value: number | null): string { + if (value === null) return "—"; + return `${value.toFixed(value >= 10 ? 0 : 1)}%`; +} + +function formatUptime(seconds: number | null): string { + if (seconds === null) return "—"; + const days = Math.floor(seconds / 86400); + const hours = Math.floor((seconds % 86400) / 3600); + const minutes = Math.floor((seconds % 3600) / 60); + if (days > 0) return `${days}d ${hours}h`; + if (hours > 0) return `${hours}h ${minutes}m`; + return `${minutes}m`; +} + +// A small inline sparkline. Values are already a percentage of `max`, so the +// chart keeps a fixed 0–100 axis instead of rescaling to the window's peak. +function Sparkline({ values, max, className }: { values: number[]; max: number; className?: string }) { + if (values.length < 2) { + return