// Dashboard: the headline numbers, what is scheduled next, and recent events. import { el, pill, fmtRelative, fmtDateTime, setText, reconcile } from "../util.js"; import { state, subscribe, refresh } from "../store.js"; import { timeline } from "../components.js"; 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"); 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: "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, ), )); 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")); }, ); } 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 the relative times honest without a full refetch. const tick = setInterval(paint, 30000); 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(", "); }