// Dashboard: the headline numbers, what is scheduled next, and recent events. import { el, pill, fmtRelative, fmtDateTime, fmtSeconds, setText, reconcile, copyText, } from "../util.js"; import { state, subscribe, refresh } from "../store.js"; import { timeline } from "../components.js"; const DONATIONS = [ { coin: "ETH", addr: "0xa6314ce4deaa8329abba301802e3f511b3fa80a6" }, { coin: "LTC", addr: "ltc1qpm26uccjgn4gx5pqxwzvs89mdp9hcshx9xj5a6" }, { coin: "BTC", addr: "bc1qzr70ur76jhm5gjsuqelzqaskumpkhkk2ewjgha" }, ]; export function dashboard(root) { const alarmTiles = el("div.tiles"); const trafficTiles = el("div.tiles"); const upcomingBody = el("tbody"); const feed = el("div"); const attention = el("div.stack"); const uptimeBody = el("tbody"); const sessionsBody = el("tbody"); const supportBody = el("div.stack"); root.append(el("div.stack", el("div.tile-band", el("div.tile-band-label", { text: "Alarms" }), alarmTiles, ), el("div.tile-band", el("div.tile-band-label", { text: "Traffic" }), trafficTiles, ), el("div.card", el("div.card-head", el("h2", { text: "Needs attention" })), attention, ), el("div.card", el("div.card-head", el("h2", { text: "Live uptimes" }), el("span.spacer"), el("a", { href: "#/streams", text: "All streams" }), ), el("div.table-wrap", el("table", el("thead", el("tr", el("th", { text: "Stream" }), el("th", { text: "Health" }), el("th", { text: "Uptime" }), )), uptimeBody, ), ), ), el("div.card", el("div.card-head", el("h2", { text: "Recent runs" })), el("div.table-wrap", el("table", el("thead", el("tr", el("th", { text: "Stream" }), el("th", { text: "Started" }), el("th", { text: "Ended" }), el("th", { text: "Duration" }), el("th", { text: "Reason" }), )), sessionsBody, ), ), ), el("div.card", el("div.card-head", el("h2", { text: "Coming up" }), el("span.spacer"), el("a", { href: "#/schedule", text: "Manage schedule" }), ), el("div.table-wrap", el("table", el("thead", el("tr", el("th", { text: "When" }), el("th", { text: "Stream" }), el("th", { text: "Action" }), el("th", { text: "Repeats" }), )), upcomingBody, ), ), ), el("div.card", el("div.card-head", el("h2", { text: "Recent activity" })), feed, ), el("div.card", el("div.card-head", el("h2", { text: "Support the project" })), el("p.sub", { text: "If Streamd saves you time, donations keep development going.", }), supportBody, ), )); supportBody.replaceChildren(...DONATIONS.map((d) => el("div.row", el("span.pill", { text: d.coin }), el("code.mono", { text: d.addr }), el("span.spacer"), el("button.tiny.ghost", { type: "button", text: "Copy", onclick: (ev) => copyText(d.addr, d.coin + " address", ev.currentTarget), }), ), )); function liveUptimeSeconds(s) { const health = s.health || ""; if (health !== "ok" && health !== "starting") return null; const base = Number(s.uptime_s) || 0; const fetched = state.refreshedAt || Date.now(); return base + Math.max(0, (Date.now() - fetched) / 1000); } function paint() { const t = (state.overview && state.overview.totals) || {}; const streams = state.streams || []; const counts = { streams: t.streams ?? streams.length, running: t.running ?? streams.filter((s) => s.health === "ok").length, down: t.down ?? streams.filter((s) => s.health === "down").length, starting: t.starting ?? streams.filter((s) => s.health === "starting").length, idle: t.ondemand_idle ?? 0, viewers: t.viewers ?? 0, capture: t.needs_capture ?? 0, }; reconcile(alarmTiles, [ { k: "down", label: "Down", v: counts.down, cls: counts.down ? "down alarm" : "" }, { k: "capture", label: "Need credentials", v: counts.capture, cls: counts.capture ? "warn alarm" : "" }, { k: "starting", label: "Starting", v: counts.starting, cls: counts.starting ? "warn alarm" : "" }, ], (d) => d.k, (d) => el(`div.tile${d.cls ? "." + d.cls.split(" ").join(".") : ""}`, el("div.k", { text: d.label }), el("div.v", { text: String(d.v) }), ), (node, d) => { node.className = `tile${d.cls ? " " + d.cls : ""}`; setText(node.querySelector(".v"), String(d.v)); }, ); reconcile(trafficTiles, [ { k: "live", label: "Live now", v: counts.running, cls: counts.running ? "ok" : "" }, { k: "viewers", label: "Viewers", v: counts.viewers, cls: "" }, { k: "idle", label: "On-demand idle", v: counts.idle, cls: "" }, { k: "total", label: "Streams", v: counts.streams, cls: "" }, ], (d) => d.k, (d) => el(`div.tile${d.cls ? "." + d.cls.split(" ").join(".") : ""}`, el("div.k", { text: d.label }), el("div.v", { text: String(d.v) }), ), (node, d) => { node.className = `tile${d.cls ? " " + d.cls : ""}`; setText(node.querySelector(".v"), String(d.v)); }, ); // Needs attention: anything down, or enabled but missing credentials. const problems = streams.filter( (s) => s.health === "down" || (s.enabled && s.needs_capture), ); if (!problems.length) { reconcile(attention, [{ id: "none" }], (x) => x.id, () => el("div.empty", el("p", { text: "Everything looks healthy." }), el("p", el("a", { href: "#/streams", text: "View all streams" })), ), () => {}); } else { reconcile(attention, problems, (s) => s.id, (s) => el("div.row", el("a", { href: `#/stream/${s.id}`, text: s.title || s.name, class: "grow" }), el("span.spacer"), el("span.sub.reason"), el("span.st"), ), (node, s) => { const a = node.querySelector("a"); a.href = `#/stream/${s.id}`; setText(a, s.title || s.name); setText(node.querySelector(".reason"), s.needs_capture ? "needs fresh credentials" : (s.last_error || "")); const st = node.querySelector(".st"); st.replaceChildren(pill(s.health || "stopped")); }, ); } // Live uptimes: prefer enabled streams; fall back to all if none enabled. const uptimeRows = (() => { const enabled = streams.filter((s) => s.enabled); return (enabled.length ? enabled : streams).slice().sort((a, b) => String(a.title || a.name).localeCompare(String(b.title || b.name))); })(); if (!uptimeRows.length) { reconcile(uptimeBody, [{ id: "none" }], (x) => x.id, () => el("tr", el("td.empty", { colspan: "3", text: "No streams yet." })), () => {}); } else { reconcile(uptimeBody, uptimeRows, (s) => s.id, () => el("tr", el("td.who"), el("td.health"), el("td.uptime.mono"), ), (tr, s) => { const who = tr.querySelector(".who"); who.replaceChildren(el("a", { href: `#/stream/${s.id}`, text: s.title || s.name, })); tr.querySelector(".health").replaceChildren(pill(s.health || "stopped")); const live = liveUptimeSeconds(s); setText(tr.querySelector(".uptime"), live == null ? "—" : fmtSeconds(live)); }, ); } const sessions = (state.overview && state.overview.recent_sessions) || []; if (!sessions.length) { reconcile(sessionsBody, [{ id: "none" }], (x) => x.id, () => el("tr", el("td.empty", { colspan: "5", text: "No completed runs yet — start a stream to begin history.", })), () => {}); } else { reconcile(sessionsBody, sessions, (s) => s.id, () => el("tr", el("td.who"), el("td.started"), el("td.ended"), el("td.dur.mono"), el("td.reason"), ), (tr, s) => { const who = tr.querySelector(".who"); who.replaceChildren(el("a", { href: `#/stream/${s.stream_id}`, text: s.stream_title || s.stream_name || `#${s.stream_id}`, })); const started = tr.querySelector(".started"); started.replaceChildren( el("div", { text: fmtRelative(s.started_at) }), el("div.sub", { text: fmtDateTime(s.started_at) }), ); const ended = tr.querySelector(".ended"); ended.replaceChildren( el("div", { text: fmtRelative(s.ended_at) }), el("div.sub", { text: fmtDateTime(s.ended_at) }), ); setText(tr.querySelector(".dur"), fmtSeconds(s.duration_s)); tr.querySelector(".reason").replaceChildren( pill(s.end_reason || s.health_at_end || "—"), ); }, ); } const upcoming = (state.overview && state.overview.upcoming) || []; if (!upcoming.length) { reconcile(upcomingBody, [{ id: "none" }], (x) => x.id, () => el("tr", el("td.empty", { colspan: "4", text: "Nothing scheduled." })), () => {}); } else { reconcile(upcomingBody, upcoming, (s) => s.id, () => el("tr", el("td.when"), el("td.who"), el("td.act"), el("td.rep")), (tr, s) => { const when = tr.querySelector(".when"); when.replaceChildren( el("div", { text: fmtRelative(s.next_fire_at) }), el("div.sub", { text: fmtDateTime(s.next_fire_at) }), ); const who = tr.querySelector(".who"); who.replaceChildren(el("a", { href: `#/stream/${s.stream_id}`, text: s.stream_name || `#${s.stream_id}`, })); tr.querySelector(".act").replaceChildren(pill(s.action)); setText(tr.querySelector(".rep"), s.kind === "weekly" ? `${daysLabel(s.days_mask)} ${s.time_of_day} ${s.tz}` : "once"); }, ); } const events = (state.overview && state.overview.recent_events) || []; feed.replaceChildren(timeline(events.map((e) => ({ ...e, message: e.stream_name ? `${e.stream_name}: ${e.message}` : e.message, })))); } const off = subscribe(paint); paint(); refresh("dashboard"); // Keep relative times and live uptimes honest without a full refetch. const tick = setInterval(paint, 1000); return () => { off(); clearInterval(tick); }; } const DAY_NAMES = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; export function daysLabel(mask) { const bits = Number(mask) || 0; if (!bits) return "—"; if (bits === 0x7f) return "every day"; const days = []; for (let i = 0; i < 7; i++) if (bits & (1 << i)) days.push(DAY_NAMES[i]); return days.join(", "); }