// deck-it - the Dashboard: one fixed screen // Copyright (C) 2026 Danilo M. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License version 2 as // published by the Free Software Foundation. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License along // with this program; if not, write to the Free Software Foundation, Inc., // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. // The Dashboard tab: a top band of chips, music and spending always on, a // row of one to three busy modules, and the action icons. The page keeps the // trend history; the modules' APIs are the existing ones. import { offline } from "./offline.js"; export const HISTORY = 120; // trend samples: two minutes at 1 s const RUNNING = ["k1c", "sbbuilder", "obs"]; // the running row's order const G = { cpu: "", mem: "\u{f035b}", gpu: "\u{f08ae}", net: "\u{f04e1}", wifi: "", charging: "\u{f0084}", tux: "" }; const BATTERY = ["", "", "", "", ""]; // Nerd Font distro logos by os-release ID. const DISTRO = { slackware: "", arch: "", debian: "", fedora: "", ubuntu: "", gentoo: "", opensuse: "", void: "", }; const SPEND = ["var(--udt-accent)", "var(--udt-blue, #89b4fa)", "var(--udt-green, #a6e3a1)"]; const OTHER = "var(--udt-surface2)"; const esc = (s) => String(s ?? "").replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`); const rate = (b) => (b >= 1048576 ? `${(b / 1048576).toFixed(1)}M` : `${Math.round((b ?? 0) / 1024)}k`); const gib = (b) => (b / 2 ** 30).toFixed(1); const money = (v) => (v == null ? "—" : `$${Number(v).toFixed(2)}`); const tok = (n) => (n >= 1e9 ? `${(n / 1e9).toFixed(1)}B` : n >= 1e6 ? `${(n / 1e6).toFixed(1)}M` : n >= 1e3 ? `${(n / 1e3).toFixed(1)}k` : String(n ?? 0)); const pct = (v) => (v == null ? "—" : `${Math.round(v)}%`); const short = (m) => String(m ?? "").split("/").pop(); const uptime = (s) => { const d = Math.floor(s / 86400), h = Math.floor((s % 86400) / 3600); return d ? `${d}d ${h}h` : `${h}h`; }; export function push(list, v, n = HISTORY) { list.push(v); if (list.length > n) list.shift(); return list; } // A filled area over a 120x100 viewBox, the newest sample at the right edge; // the largest value reaches 12% from the top. export function area(samples, max, n = HISTORY) { if (!samples.length) return ""; const x = (i) => ((n - samples.length + i) * 120) / (n - 1); const y = (v) => 100 - Math.min(1, v / (max || 1)) * 88; return `M${x(0)},100 ` + samples.map((v, i) => `L${x(i)},${y(v)}`).join(" ") + " L120,100 Z"; } // The top 3 paid models (llm.py sorts them by cost), then the rest as "other". export function spendParts(models) { const paid = (models ?? []).filter((m) => m.cost > 0); const rest = paid.slice(3).reduce((a, m) => a + m.cost, 0); return rest > 0 ? [...paid.slice(0, 3), { model: "other", cost: rest }] : paid.slice(0, 3); } export function batteryGlyph(b) { if (b?.status === "charging" || b?.status === "full") return G.charging; return BATTERY[Math.min(4, Math.max(0, Math.round((b?.level ?? 0) / 25)))]; } // A line too long for its slot slides to its end and back (deck.css .marq); // one that fits stays still. Only changed values are written, so the // animation is not restarted by every poll. function marquee(el) { for (const line of el.querySelectorAll(".what, .meta")) { const over = line.firstElementChild.offsetWidth - line.clientWidth; line.classList.toggle("marq", over > 0); const d = `${-Math.max(0, over)}px`; if (line.style.getPropertyValue("--d") !== d) { line.style.setProperty("--d", d); line.style.setProperty("--t", `${2 + Math.max(0, over) / 30}s`); // ~30 px/s plus the pauses } } } const SKELETON = `
·
${G.cpu}
${G.mem}
${G.gpu}
${G.net}
context
5h
7d
${G.wifi}
`; export default { // names: the enabled modules; poll(name, seconds, update) and open(tab) // come from app.js. mount(section, names, { poll, open }) { const on = new Set(names); const root = document.createElement("div"); root.className = "dash"; root.innerHTML = SKELETON; section.append(root); const $ = (s) => root.querySelector(s); const chip = (sel, v, s, fill, warn) => { const c = $(sel); c.querySelector(".v").textContent = v; if (s != null) c.querySelector(".s").textContent = s; if (fill != null) c.querySelector(".fill").style.width = `${Math.max(0, Math.min(100, fill))}%`; c.classList.toggle("warn", Boolean(warn)); }; // A disabled module's parts are left out. $(".dash-id").hidden = !on.has("stats"); $(".chips.sys").hidden = !on.has("stats"); for (const c of [".c-ctx", ".c-5h", ".c-7d"]) $(c).hidden = !on.has("llm"); $(".dash-spend").hidden = !on.has("llm"); $(".c-bat").hidden = $(".c-wifi").hidden = !on.has("tablet"); $(".dash-music").hidden = !on.has("music"); const hist = { cpu: [], down: [], up: [] }; if (on.has("stats")) poll("stats", 1, (st) => { $(".logo").textContent = DISTRO[st.os?.id] ?? G.tux; $(".os").textContent = st.os?.name ?? ""; $(".kernel").textContent = [st.os?.codename, `Linux ${st.os?.kernel ?? ""}`].filter(Boolean).join(" · "); $(".up").textContent = `up ${uptime(st.uptime)}`; push(hist.cpu, st.cpu); push(hist.down, st.net.down); push(hist.up, st.net.up); $(".c-cpu .n").textContent = st.cpuName ?? ""; $(".c-cpu path").setAttribute("d", area(hist.cpu, Math.max(25, ...hist.cpu))); chip(".c-cpu", `${st.cpu}%`, st.cpuTemp != null ? `${st.cpuTemp}°C` : "", null, st.cpuTemp >= 85); chip(".c-mem", gib(st.memUsed * 1024), `/ ${Math.round(st.memTotal / 1048576)} GiB`, (100 * st.memUsed) / st.memTotal); const g = st.gpu; $(".c-gpu").hidden = !g; if (g) { $(".c-gpu .n").textContent = g.name ?? ""; chip(".c-gpu", g.temp != null ? `${g.temp}°C` : "—", g.memTotal ? `${gib(g.memUsed)} / ${Math.round(g.memTotal / 2 ** 30)} GiB` : `${gib(g.memUsed)} GiB`, g.memTotal ? (100 * g.memUsed) / g.memTotal : 0, g.temp >= 85); } const max = Math.max(1, ...hist.down, ...hist.up); const [down, up] = $(".c-net").querySelectorAll("path"); down.setAttribute("d", area(hist.down, max)); up.setAttribute("d", area(hist.up, max)); chip(".c-net", `↓${rate(st.net.down)} ↑${rate(st.net.up)}`); }); const program = (title, p) => { const model = `${esc(p.model ? short(p.model) : "—")}`; if (!(p.cost > 0)) { // local or free models: tokens, not dollars return `

${esc(title)}

${model} session${tok(p.session_tokens)} total${tok(p.tokens)}
`; } const parts = spendParts(p.models); const total = parts.reduce((a, m) => a + m.cost, 0); const color = (m, i) => (m.model === "other" ? OTHER : SPEND[i]); return `

${esc(title)}

${model}
session
${money(p.session_cost)}
${tok(p.session_tokens)} · ${p.session_calls ?? 0} calls
total
${money(p.cost)}
${tok(p.tokens)} · ${p.calls ?? 0} calls
${parts.map((m, i) => ``).join("")}
${parts.map((m, i) => `${esc(short(m.model))} ${money(m.cost)}`).join("")}
`; }; if (on.has("llm")) poll("llm", 5, (st) => { const spend = $(".dash-spend"); if (st.offline) { offline(spend, { icon: "/logos/claude.svg", title: "Usage dashboard is off", detail: "Start llm-cost-dashboard and the spending appears here." }); for (const c of [".c-ctx", ".c-5h", ".c-7d"]) chip(c, "—", null, 0); return; } const c = st.claude ?? {}; chip(".c-ctx", pct(c.context_pct), null, c.context_pct, c.context_pct >= 80); chip(".c-5h", pct(c.five_hour_pct), null, c.five_hour_pct, c.five_hour_pct >= 80); chip(".c-7d", pct(c.seven_day_pct), null, c.seven_day_pct, c.seven_day_pct >= 80); spend._offline = null; spend.innerHTML = program("opencode", st.opencode ?? {}) + program("Local Agent", st.assistant ?? {}); }); if (on.has("tablet")) poll("tablet", 10, (t) => { const b = t.battery ?? {}, w = t.wifi ?? {}; const charging = b.status === "charging" || b.status === "full"; $(".c-bat .ic").textContent = batteryGlyph(b); chip(".c-bat", b.level != null ? `${b.level}%` : "—", null, b.level, b.level < 20 && !charging); chip(".c-wifi", w.rssi != null ? `${w.rssi} dBm` : "—", w.band ?? "", w.rssi == null ? 0 : 2 * (w.rssi + 100)); // -100 dBm empty, -50 full }); if (on.has("music")) import("./modules/music.js").then(({ default: music }) => { const el = $(".dash-music"); music.mount(el); poll("music", music.interval ?? 2, (st) => music.update(st, el)); }); // The running row: slots are created now, in RUNNING order, and shown // while their module is busy. const row = $(".dash-running"); for (const name of RUNNING.filter((n) => on.has(n))) { const el = document.createElement("div"); el.className = `card slot slot-${name}`; el.hidden = true; row.append(el); import(`./modules/${name}.js`).then((mod) => { if (!mod.busy || !mod.slot) return; const tab = mod.default?.tab; el.addEventListener("click", (e) => { if (tab && !e.target.closest("button")) open(tab); }); // ponytail: the K1C and OBS tabs poll the same API; two polls a second, one cache if it matters. poll(name, mod.interval ?? mod.default?.interval ?? 2, (st) => { el.hidden = !mod.busy(st); if (!el.hidden) { mod.slot(st, el); marquee(el); } row.hidden = !row.querySelector(".slot:not([hidden])"); }); }); } if (on.has("actions")) poll("actions", 30, (st) => { const acts = $(".dash-acts"); const key = (st.actions ?? []).map((a) => a.id).join(","); if (acts._key === key) return; acts._key = key; acts.textContent = ""; for (const a of st.actions ?? []) { const b = document.createElement("button"); b.className = "nf"; b.textContent = a.icon; b.title = a.label; b.setAttribute("aria-label", a.label); b.onclick = async () => { let ok = false; try { ok = (await fetch(`/api/actions/${a.id}`, { method: "POST", headers: { "Content-Type": "application/json" }, body: "{}" })).ok; } catch {} if (!ok) { b.classList.add("fail"); setTimeout(() => b.classList.remove("fail"), 1500); } }; acts.append(b); } }); }, };