Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
/admin shows today's peak viewers and joins per hour for the last 24 h, owner only, kept under FORGE_APP_DATA across a restart #414
Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.
smith/arm:smith/399-admin-shows-today-s-peak-viewers-and-joi → main 5 commits fork 3 threads · 3 unresolved
10 files
changed +194
−3
Software/README.md +1
| @@ -567,6 +567,7 @@ reaches it as the owner. | ||
| 567 | 567 | or from the **Admin** tab that only the owner sees: |
| 568 | 568 | |
| 569 | 569 | - **Reports** (owner and moderators; `/admin` is open to a moderator who holds a forge identity) lists the open chat reports, newest first, each with **Remove** (take the line down for everyone), **Mute** (mute its author for good) and **Dismiss** (close the reports, nothing else). Each closes the line's reports and is written to the admin log. `/delete` and `/mute` from either door close them too. |
| 570 | - **Visitors** (owner only; a moderator's page has no such section, anyone else gets the 404) shows today's peak viewers and joins per hour for the last 24 hours, oldest first, as a text chart with no script. In the terminal the owner types `/visitors` for the same two numbers (anyone else is refused). Counts only, no names or addresses: the lobby counts every `/live` connect and disconnect and keeps them in `$FORGE_APP_DATA/so101/visitors.json` (saved at most once a minute and on close), so they survive a restart; the peak is per day and the hours roll off after 24. | |
| 570 | 571 | - **Table height** (-100..100 mm, `tableOffsetMm`) is a number field under Table height on `/admin`: |
| 571 | 572 | Save sends `PUT /settings` and shows the saved value or the server's error. |
| 572 | 573 | - **Pause the queue** for maintenance. The driver's turn ends (unless it is |
Software/so101/bench/fake-serve.ts +1
| @@ -52,6 +52,7 @@ const lobby = new Lobby({ | ||
| 52 | 52 | local, |
| 53 | 53 | chatFile: join(dir, "chat.json"), |
| 54 | 54 | adminFile: join(dir, "admin.json"), |
| 55 | countsFile: join(dir, "visitors.json"), | |
| 55 | 56 | turnsFile: join(dir, "turns.json"), |
| 56 | 57 | wordsFile: join(dir, "words.json"), |
| 57 | 58 | reportsFile: join(dir, "reports.json"), |
Software/so101/src/admin-page.ts +13
| @@ -53,6 +53,9 @@ th, td { text-align: right; padding: .3rem .5rem; border-bottom: 1px solid var(- | ||
| 53 | 53 | th:first-child, td:first-child { text-align: left; } |
| 54 | 54 | th { color: var(--dim); font-weight: 600; } |
| 55 | 55 | .scroll { overflow-x: auto; } |
| 56 | .bars li { grid-template-columns: 3.5rem 1fr; gap: .5rem; overflow-wrap: normal; font-family: ui-monospace, monospace; border: 0; padding: 0; } | |
| 57 | .bars li span:nth-child(2) { color: var(--blue); } | |
| 58 | .bars li b { color: var(--fg); font-weight: 600; } | |
| 56 | 59 | .inline { display: inline; margin: 0; } |
| 57 | 60 | .empty { color: var(--dim); } |
| 58 | 61 | @media (prefers-reduced-motion: reduce) { * { transition: none !important; } } |
| @@ -146,6 +149,8 @@ export interface AdminPage { | ||
| 146 | 149 | /** Mutes and bans in force, each with a button to lift it. */ |
| 147 | 150 | muted?: readonly Target[]; |
| 148 | 151 | banned?: readonly Target[]; |
| 152 | /** Owner only: today's peak and joins per hour for the last 24 hours, oldest first. */ | |
| 153 | visitors?: { peak: number; hours: readonly number[] }; | |
| 149 | 154 | done?: Done | null; |
| 150 | 155 | error?: string | null; |
| 151 | 156 | } |
| @@ -217,6 +222,13 @@ const inForce = (kind: "mute" | "ban", list: readonly Target[], canLift: (name: | ||
| 217 | 222 | : ""; |
| 218 | 223 | }; |
| 219 | 224 | |
| 225 | /** Today's peak and a 24-bar text chart, oldest first, the last row the current hour; no script, nothing from a person. */ | |
| 226 | const visitorsSection = (v: { peak: number; hours: readonly number[] }): string => { | |
outdated
Repeat of █ is read aloud char by char. Wrap the bars in <span aria-hidden="true"> and keep |
||
| 227 | const max = Math.max(1, ...v.hours); | |
| 228 | const bars = v.hours.map((n, i) => `<li><span>${i === 23 ? "now" : `-${23 - i}h`}</span><span><span aria-hidden="true">${"█".repeat(Math.round((n / max) * 16))}</span> <b>${n}</b></span></li>`).join(""); | |
| 229 | return `<section aria-labelledby="visitors"><h2 id="visitors">Visitors</h2><p>Peak viewers today: <b>${v.peak}</b></p><ol class="bars" aria-label="Joins per hour, last 24 hours, oldest first">${bars}</ol></section>`; | |
| 230 | }; | |
| 231 | ||
| 220 | 232 | export function renderAdmin(p: AdminPage): string { |
| 221 | 233 | const s = p.settings; |
| 222 | 234 | const d = p.snapshot.driver; |
| @@ -261,6 +273,7 @@ ${p.canEdit ? `<section aria-labelledby="table"><h2 id="table">Table height</h2> | ||
| 261 | 273 | <div class="grid"><label>Table height<small>mm above the model's floor, -100 to 100; the safe zone keeps the arm above it</small><input id="table-mm" type="number" inputmode="numeric" min="-100" max="100" step="1"></label></div> |
| 262 | 274 | <div class="save"><button type="button" class="go" id="table-save">Save</button> <button type="button" id="table-here">The arm rests on the table now</button> <span id="table-note" role="status"></span></div> |
| 263 | 275 | </section>` : ""} |
| 276 | ${p.canEdit && p.visitors ? visitorsSection(p.visitors) : ""} | |
| 264 | 277 | <section aria-labelledby="health"><h2 id="health">Health</h2>${health(p.health ?? [])}</section> |
| 265 | 278 | <section aria-labelledby="mods"><h2 id="mods">Moderators</h2>${mods}</section> |
| 266 | 279 | <section aria-labelledby="moderation"><h2 id="moderation">Moderation</h2>${inForce("mute", p.muted ?? [], canLift)}${inForce("ban", p.banned ?? [], canLift)}${actions}</section> |
Software/so101/src/admin-routes.ts +1 −1
| @@ -44,7 +44,7 @@ export function serveAdminPage(lobby: Lobby, req: IncomingMessage, res: ServerRe | ||
| 44 | 44 | return null; |
| 45 | 45 | } |
| 46 | 46 | const show = (status: number, done: Done | null, error: string | null) => |
| 47 | html(status, renderAdmin({ who, canEdit, settings: lobby.admin.settings, snapshot: lobby.turns.snapshot(), log: lobby.admin.log, removed: lobby.chat.removed, muted: lobby.chat.mutedList(), banned: lobby.accounts.bans(), health: lobby.reports.rows(), reports: lobby.lineReports.list(), done, error }), canEdit); | |
| 47 | html(status, renderAdmin({ who, canEdit, settings: lobby.admin.settings, snapshot: lobby.turns.snapshot(), log: lobby.admin.log, removed: lobby.chat.removed, muted: lobby.chat.mutedList(), banned: lobby.accounts.bans(), health: lobby.reports.rows(), reports: lobby.lineReports.list(), visitors: canEdit ? { peak: lobby.visitors.peakToday(), hours: lobby.visitors.joinsPerHour() } : undefined, done, error }), canEdit); | |
| 48 | 48 | if (req.method === "GET" || req.method === "HEAD") { |
| 49 | 49 | const done = url.searchParams.get("done"); |
| 50 | 50 | show(200, done && Object.hasOwn(DONE, done) ? (done as Done) : null, null); |
Software/so101/src/admin-visitors.test.ts added +96
| @@ -0,0 +1,96 @@ | ||
| 1 | import { afterEach, expect, test } from "bun:test"; | |
| 2 | import { mkdtempSync, rmSync } from "node:fs"; | |
| 3 | import { tmpdir } from "node:os"; | |
| 4 | import { join } from "node:path"; | |
| 5 | import WebSocket from "ws"; | |
| 6 | import { sign } from "./identity.ts"; | |
| 7 | import { Lobby } from "./lobby.ts"; | |
| 8 | import { startRelay, type RunningRelay } from "./relay.ts"; | |
| 9 | ||
| 10 | const KEY = "k".repeat(64); | |
| 11 | const later = Date.now() + 600_000; | |
| 12 | const as = (name: string, role: "user" | "admin") => { | |
| 13 | const identity = `id-${name}|${name}|${role}|${later}`; | |
| 14 | return { "x-forge-identity": identity, "x-forge-signature": sign(identity, KEY) }; | |
| 15 | }; | |
| 16 | const owner = as("trevato", "admin"); | |
| 17 | ||
| 18 | const sockets: WebSocket[] = []; | |
| 19 | let dir = ""; | |
| 20 | let relay: RunningRelay | undefined; | |
| 21 | let lobby: Lobby | undefined; | |
| 22 | afterEach(async () => { | |
| 23 | for (const s of sockets.splice(0)) s.terminate(); | |
| 24 | await relay?.close(); | |
| 25 | lobby?.close(); | |
| 26 | if (dir) rmSync(dir, { recursive: true, force: true }); | |
| 27 | dir = ""; | |
| 28 | relay = lobby = undefined; | |
| 29 | }); | |
| 30 | ||
| 31 | async function boot() { | |
| 32 | dir ||= mkdtempSync(join(tmpdir(), "so101-admin-visitors-")); | |
| 33 | lobby = new Lobby({ identityKey: KEY, adminFile: join(dir, "admin.json"), countsFile: join(dir, "visitors.json") }); | |
| 34 | relay = await startRelay({ port: 0, simulation: "http://127.0.0.1:1", lobby }); | |
| 35 | return `http://127.0.0.1:${relay.port}`; | |
| 36 | } | |
| 37 | ||
| 38 | type Msg = Record<string, unknown>; | |
| 39 | ||
| 40 | /** Open a socket and wait for its hello; `ask` sends a frame and resolves with the first frame of that type. */ | |
| 41 | async function open(base: string, headers: Record<string, string> = {}) { | |
| 42 | const ws = new WebSocket(`${base.replace("http", "ws")}/live`, { headers }); | |
| 43 | sockets.push(ws); | |
| 44 | const heard: Msg[] = []; | |
| 45 | const waiting: Array<[string, (m: Msg) => void]> = []; | |
| 46 | ws.on("message", (d) => { | |
| 47 | const m = JSON.parse(String(d)) as Msg; | |
| 48 | heard.push(m); | |
| 49 | for (const w of [...waiting]) if (w[0] === m.type) (waiting.splice(waiting.indexOf(w), 1), w[1](m)); | |
| 50 | }); | |
| 51 | await new Promise((ok, no) => (ws.once("open", ok), ws.once("error", no))); | |
| 52 | const ask = (frame: Msg, type: string) => new Promise<Msg>((ok) => (waiting.push([type, ok]), ws.send(JSON.stringify(frame)))); | |
| 53 | return { ws, ask }; | |
| 54 | } | |
| 55 | ||
| 56 | /** n visitors at once, all gone before it returns. */ | |
| 57 | async function visit(base: string, n: number) { | |
| 58 | const seats = await Promise.all(Array.from({ length: n }, () => open(base))); | |
| 59 | await Promise.all(seats.map(({ ws }) => new Promise((ok) => (ws.once("close", ok), ws.close())))); | |
| 60 | } | |
| 61 | ||
| 62 | test("the owner sees the peak and 24 bars; a moderator and a plain user do not; the counts survive a restart", async () => { | |
| 63 | let base = await boot(); | |
| 64 | const mod = as("bob", "user"); | |
| 65 | await fetch(`${base}/admin`, { method: "POST", headers: { ...owner, "content-type": "application/x-www-form-urlencoded" }, body: new URLSearchParams({ action: "mod-add", name: "bob" }), redirect: "manual" }); | |
| 66 | await visit(base, 3); | |
| 67 | const html = await (await fetch(`${base}/admin`, { headers: owner })).text(); | |
| 68 | expect(html).toContain("Peak viewers today: <b>"); | |
| 69 | expect(html.match(/<li><span>(now|-\d+h)<\/span>/g)?.length).toBe(24); | |
| 70 | expect(html).toContain('<span aria-hidden="true">'); | |
| 71 | expect(html).not.toMatch(/█{17}/); | |
| 72 | const peak = Number(/Peak viewers today: <b>(\d+)/.exec(html)![1]); | |
| 73 | expect(peak).toBe(3); | |
| 74 | expect(await (await fetch(`${base}/admin`, { headers: mod })).text()).not.toContain("Peak viewers"); | |
| 75 | expect((await fetch(`${base}/admin`, { headers: as("eve", "user") })).status).toBe(404); | |
| 76 | ||
| 77 | await relay!.close(); | |
| 78 | lobby!.close(); | |
| 79 | base = await boot(); | |
| 80 | const after = await (await fetch(`${base}/admin`, { headers: owner })).text(); | |
| 81 | expect(after).toContain(`Peak viewers today: <b>${peak}</b>`); | |
| 82 | expect(lobby!.visitors.joinsPerHour().reduce((a, b) => a + b, 0)).toBe(3); | |
| 83 | }); | |
| 84 | ||
| 85 | test("over /live only the owner can ask for the peak and joins per hour", async () => { | |
| 86 | const base = await boot(); | |
| 87 | await visit(base, 2); | |
| 88 | await fetch(`${base}/admin`, { method: "POST", headers: { ...owner, "content-type": "application/x-www-form-urlencoded" }, body: new URLSearchParams({ action: "mod-add", name: "bob" }), redirect: "manual" }); | |
| 89 | const seen = await (await open(base, owner)).ask({ type: "visitors" }, "visitors"); | |
| 90 | expect(seen.peak).toBe(2); | |
| 91 | expect((seen.hours as number[]).length).toBe(24); | |
| 92 | for (const headers of [as("bob", "user"), as("eve", "user"), {}]) { | |
| 93 | const r = await (await open(base, headers)).ask({ type: "visitors" }, "error"); | |
| 94 | expect(r.error).toBe(Object.keys(headers).length ? "only the owner can do that" : "join to take part"); | |
| 95 | } | |
| 96 | }); |
Software/so101/src/fake-serve.test.ts +13 −1
| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | import { afterEach, expect, test } from "bun:test"; |
| 2 | import { existsSync, mkdtempSync, rmSync } from "node:fs"; | |
| 2 | import WebSocket from "ws"; | |
| 3 | import { existsSync, readFileSync, mkdtempSync, rmSync } from "node:fs"; | |
| 3 | 4 | import { tmpdir } from "node:os"; |
| 4 | 5 | import { join } from "node:path"; |
| 5 | 6 | |
| @@ -42,6 +43,17 @@ test("fake-serve listens on PORT and HOST and keeps its data under FORGE_APP_DAT | ||
| 42 | 43 | expect(existsSync(join(dir, "so101", "poses.json"))).toBe(true); |
| 43 | 44 | }, 20_000); |
| 44 | 45 | |
| 46 | test("the visitor counts are kept as visitors.json under FORGE_APP_DATA", async () => { | |
| 47 | dir = mkdtempSync(join(tmpdir(), "so101-appdata-")); | |
| 48 | const { port } = await start({ PORT: "0", HOST: "127.0.0.1", FORGE_APP_DATA: dir, FORGE_APP_ENV: "", FORGE_APP_IDENTITY_KEY: "" }); | |
| 49 | const ws = new WebSocket(`ws://127.0.0.1:${port}/live`); | |
| 50 | await new Promise((ok, no) => (ws.once("open", ok), ws.once("error", no))); | |
| 51 | const file = join(dir, "so101", "visitors.json"); | |
| 52 | for (let i = 0; i < 50 && !existsSync(file); i++) await Bun.sleep(100); | |
| 53 | ws.close(); | |
| 54 | expect(JSON.parse(readFileSync(file, "utf8")).peak).toBe(1); | |
| 55 | }, 20_000); | |
| 56 | ||
| 45 | 57 | test("on the forge, an anonymous visitor watches but cannot command the arm", async () => { |
| 46 | 58 | dir = mkdtempSync(join(tmpdir(), "so101-appdata-")); |
| 47 | 59 | const { port, line } = await start({ PORT: "0", HOST: "127.0.0.1", FORGE_APP_DATA: dir, FORGE_APP_ENV: "preview", FORGE_APP_IDENTITY_KEY: "a key" }); |
Software/so101/src/lobby.ts +48
| @@ -17,6 +17,7 @@ | ||
| 17 | 17 | * {"type": "settings", "set": {…}} owner: any of src/admin.ts's settings |
| 18 | 18 | * {"type": "crew", "settings": {"enabled", "rules", "maxPerMinute"}} owner: the moderator's settings |
| 19 | 19 | * {"type": "removed"} owner: the latest removed lines, answered with {"type": "removed", "lines": [...]} |
| 20 | * {"type": "visitors"} owner: today's peak and joins per hour, answered with {"type": "visitors", "peak", "hours": [24 numbers, oldest first]} | |
| 20 | 21 | * {"type": "restore", "id": 12} owner: put back a removed line, or one the crew hid |
| 21 | 22 | * {"type": "dismiss", "id": 12} owner: dismiss a mute suggestion |
| 22 | 23 | * {"type": "hosts", "hosts": ["…"]} owner: hostnames chat may link to; any other link is refused |
| @@ -36,6 +37,7 @@ | ||
| 36 | 37 | * `publish`es to everyone: the arm's status, so a page needs this one socket for the pose, the chat and the queue. |
| 37 | 38 | */ |
| 38 | 39 | |
| 40 | import { readFileSync, renameSync, writeFileSync } from "node:fs"; | |
| 39 | 41 | import { randomBytes } from "node:crypto"; |
| 40 | 42 | import { dirname, join } from "node:path"; |
| 41 | 43 | import type { IncomingMessage } from "node:http"; |
| @@ -52,6 +54,7 @@ import { ChatRules } from "./chat-rules.ts"; | ||
| 52 | 54 | import { Reports } from "./reports.ts"; |
| 53 | 55 | import { WordFilter } from "./word-filter.ts"; |
| 54 | 56 | import { identify, LOCAL, type Person, type Who } from "./identity.ts"; |
| 57 | import { VisitorCounts } from "./visitor-counts.ts"; | |
| 55 | 58 | import { Turns, type TurnEvent } from "./turns.ts"; |
| 56 | 59 | import { parseReport, ViewerReports } from "./viewer-reports.ts"; |
| 57 | 60 | |
| @@ -75,6 +78,8 @@ export interface LobbyOptions { | ||
| 75 | 78 | hostsFile?: string; |
| 76 | 79 | /** The chat reports' file; none keeps them in memory. */ |
| 77 | 80 | reportsFile?: string; |
| 81 | /** The visitor counts' file (peak, joins per hour); none keeps them in memory. */ | |
| 82 | countsFile?: string; | |
| 78 | 83 | /** The admin settings' file; none keeps them in memory. */ |
| 79 | 84 | adminFile?: string; |
| 80 | 85 | /** The line and the current turn's file; none keeps them in memory. */ |
| @@ -124,6 +129,11 @@ export class Lobby { | ||
| 124 | 129 | /** Chat lines people reported, for the owner and moderators. */ |
| 125 | 130 | readonly lineReports: Reports; |
| 126 | 131 | readonly local: boolean; |
| 132 | /** Counts only: today's peak and joins per hour, for the owner's /admin. */ | |
| 133 | readonly visitors: VisitorCounts; | |
| 134 | private readonly countsFile: string | undefined; | |
| 135 | private countsSaved = 0; | |
| 136 | private countsTimer: ReturnType<typeof setTimeout> | undefined; | |
| 127 | 137 | readonly moderator: Moderator | undefined; |
| 128 | 138 | private readonly key: string; |
| 129 | 139 | private readonly wss = new WebSocketServer({ noServer: true, maxPayload: MAX_FRAME }); |
| @@ -156,6 +166,14 @@ export class Lobby { | ||
| 156 | 166 | this.filter = new WordFilter(options.wordsFile); |
| 157 | 167 | this.rules = new ChatRules(options.hostsFile ?? (options.wordsFile ? join(dirname(options.wordsFile), "hosts.json") : undefined)); |
| 158 | 168 | this.reports = new ViewerReports(this.now); |
| 169 | this.visitors = new VisitorCounts(this.now); | |
| 170 | this.countsFile = options.countsFile; | |
| 171 | if (this.countsFile) { | |
| 172 | try { | |
| 173 | this.visitors.restore(JSON.parse(readFileSync(this.countsFile, "utf8"))); | |
| 174 | } catch {} | |
| 175 | this.visitors.clearHere(); | |
| 176 | } | |
| 159 | 177 | this.lineReports = new Reports(options.reportsFile); |
| 160 | 178 | this.silentMs = options.silentMs ?? 15_000; |
| 161 | 179 | this.moderator = options.moderator; |
| @@ -304,6 +322,8 @@ export class Lobby { | ||
| 304 | 322 | close(): void { |
| 305 | 323 | clearInterval(this.pinger); |
| 306 | 324 | clearTimeout(this.presenceTimer); |
| 325 | clearTimeout(this.countsTimer); | |
| 326 | this.saveCounts(); | |
| 307 | 327 | for (const s of this.seats) s.ws.terminate(); |
| 308 | 328 | this.wss.close(); |
| 309 | 329 | this.turns.dispose(); |
| @@ -315,6 +335,8 @@ export class Lobby { | ||
| 315 | 335 | const seat: Seat = { ws, who, from, verified, heard: Date.now() }; |
| 316 | 336 | if (!who) seat.guest = this.claim(id); |
| 317 | 337 | this.seats.add(seat); |
| 338 | this.visitors.joined(); | |
| 339 | this.countsChanged(); | |
| 318 | 340 | this.presenceChanged(); |
| 319 | 341 | if (who) this.turns.connect(who.id); |
| 320 | 342 | this.send(seat, this.hello(seat)); |
| @@ -354,6 +376,8 @@ export class Lobby { | ||
| 354 | 376 | }); |
| 355 | 377 | ws.on("close", () => { |
| 356 | 378 | this.seats.delete(seat); |
| 379 | this.visitors.left(); | |
| 380 | this.countsChanged(); | |
| 357 | 381 | if (seat.guest) this.release(seat.guest.id); |
| 358 | 382 | this.presenceChanged(); |
| 359 | 383 | this.reports.drop(seat); |
| @@ -484,6 +508,8 @@ export class Lobby { | ||
| 484 | 508 | return; |
| 485 | 509 | case "removed": |
| 486 | 510 | return this.send(seat, { type: "removed", lines: this.chat.removed.slice(0, 20).map((r) => ({ id: r.message.id, by: r.by.name, from: r.message.from?.name ?? null, text: r.message.text })) }); |
| 511 | case "visitors": | |
| 512 | return this.send(seat, { type: "visitors", peak: this.visitors.peakToday(), hours: this.visitors.joinsPerHour() }); | |
| 487 | 513 | case "restore": { |
| 488 | 514 | if (!this.restoreLine(who, m.id)) this.send(seat, { type: "error", error: "that line is not in the log" }); |
| 489 | 515 | return; |
| @@ -750,6 +776,28 @@ export class Lobby { | ||
| 750 | 776 | return { type: "presence", here: this.seats.size, signedIn: people.size, watching }; |
| 751 | 777 | } |
| 752 | 778 | |
| 779 | private saveCounts(): void { | |
| 780 | if (!this.countsFile) return; | |
| 781 | this.countsSaved = Date.now(); | |
| 782 | try { | |
| 783 | writeFileSync(`${this.countsFile}.tmp`, JSON.stringify(this.visitors)); | |
| 784 | renameSync(`${this.countsFile}.tmp`, this.countsFile); | |
| 785 | } catch (err) { | |
| 786 | console.error(`lobby: counts not saved: ${err instanceof Error ? err.message : String(err)}`); | |
| 787 | } | |
| 788 | } | |
| 789 | ||
| 790 | /** Saves at most once a minute: now if the last save is old enough, else when the minute is up. */ | |
| 791 | private countsChanged(): void { | |
| 792 | if (!this.countsFile || this.countsTimer) return; | |
| 793 | const wait = Math.max(0, this.countsSaved + 60_000 - Date.now()); | |
| 794 | this.countsTimer = setTimeout(() => { | |
| 795 | this.countsTimer = undefined; | |
| 796 | this.saveCounts(); | |
| 797 | }, wait); | |
| 798 | this.countsTimer.unref?.(); | |
| 799 | } | |
| 800 | ||
| 753 | 801 | /** Who is here, for a "who" answer: a signed-in person once however many tabs, a guest by name and colour. No address or id. */ |
| 754 | 802 | private people(): Array<{ name: string; colour?: string; guest: boolean; role: "driving" | "queued" | "watching" }> { |
| 755 | 803 | const out: Array<{ name: string; colour?: string; guest: boolean; role: "driving" | "queued" | "watching" }> = []; |
Software/so101/src/terminal.test.ts +8
| @@ -144,6 +144,14 @@ describe("terminal table", () => { | ||
| 144 | 144 | expect(refused).toEqual({ status: 403, error: "admins only" }); |
| 145 | 145 | }); |
| 146 | 146 | |
| 147 | test("/visitors sends the frame and the answer prints the peak and 24 hours as text", () => { | |
| 148 | expect(typed(emptyRoom(), "/visitors")).toEqual({ send: { type: "visitors" } }); | |
| 149 | const hours = Array.from({ length: 24 }, (_, i) => i); | |
| 150 | const [line] = hear(emptyRoom(), { type: "visitors", peak: 7, hours }); | |
| 151 | expect(line).toBe(`peak viewers today 7 · joins per hour, oldest first: ${hours.join(" ")}`); | |
| 152 | expect(hear(emptyRoom(), { type: "error", error: "only the owner can do that" })).toEqual(["error: only the owner can do that"]); | |
| 153 | }); | |
| 154 | ||
| 147 | 155 | test("/health asks the reader, prints a line per viewer as text, or the refusal; without a reader it says so", async () => { |
| 148 | 156 | expect(typed(emptyRoom(), "/health")).toEqual({ health: true }); |
| 149 | 157 | const row = { viewer: "v1", name: "al\x1b[31mice", rttP50: 40, rttP95: 90, sentPerS: 30, droppedPerS: 2, camFps: 14.5, frameAgeMs: 120, stalls: 1, reconnects: 2 }; |
Software/so101/src/terminal.ts +8 −1
| @@ -26,6 +26,7 @@ | ||
| 26 | 26 | * /reports owner or moderator: the reported lines, newest first (they also hear of each new one) |
| 27 | 27 | * /dismiss #<id> owner or moderator: close that line's reports and nothing else (Lobby.dismissReported; it is logged) |
| 28 | 28 | * /removed owner: the latest removed chat lines, with their numbers and who removed them |
| 29 | * /visitors owner: today's peak viewers and joins per hour for the last 24 h (the counts /admin shows) | |
| 29 | 30 | * /restore #<n> owner: put a removed line back for everyone (Lobby.restoreLine; it is logged) |
| 30 | 31 | * /table here admin: put the table under the arm's lowest link now (POST /settings/table-here) |
| 31 | 32 | * /table <mm> admin: set the table offset, -100..100 mm (PUT /settings tableOffsetMm) |
| @@ -175,7 +176,7 @@ const said = (room: Room, m: unknown): string => { | ||
| 175 | 176 | return from ? `${tag}${plain(from.name)}: ${plain(message?.text)}` : `· ${plain(message?.text)}`; |
| 176 | 177 | }; |
| 177 | 178 | |
| 178 | export const HELP = "type to chat · /join /leave /ready /extend · /stop /resume (driver, owner) · /end [who] /table here|<mm> (owner) · /mute <who> /unmute <who> (owner, moderator) · /delete <who|#id> (owner, moderator) · /report <who|#id> · /reports /dismiss #<id> (owner, moderator) · /removed /restore #<id> (owner) · /health (owner, moderator) · /machine [off] · /who · /quit"; | |
| 179 | export const HELP = "type to chat · /join /leave /ready /extend · /stop /resume (driver, owner) · /end [who] /table here|<mm> (owner) · /mute <who> /unmute <who> (owner, moderator) · /delete <who|#id> (owner, moderator) · /report <who|#id> · /reports /dismiss #<id> (owner, moderator) · /removed /restore #<id> /visitors (owner) · /health (owner, moderator) · /machine [off] · /who · /quit"; | |
| 179 | 180 | |
| 180 | 181 | /** The Machine feed's socket beside the lobby's: same app, same port. */ |
| 181 | 182 | export const machineUrl = (live: string): string => live.replace(/\/live$/, "/machine"); |
| @@ -285,6 +286,10 @@ export function hear(room: Room, frame: Frame): string[] { | ||
| 285 | 286 | if (!lines.length) return ["no removed lines"]; |
| 286 | 287 | return lines.map((l: { id?: unknown; by?: unknown; from?: unknown; text?: unknown }) => `removed #${plain(l?.id)} by ${plain(l?.by)}, ${l?.from ? plain(l.from) : "system"}: ${plain(l?.text)}`); |
| 287 | 288 | } |
| 289 | case "visitors": { | |
| 290 | const hours = Array.isArray(frame.hours) ? frame.hours.filter((n): n is number => typeof n === "number") : []; | |
| 291 | return [`peak viewers today ${plain(frame.peak)} · joins per hour, oldest first: ${hours.map(plain).join(" ")}`]; | |
| 292 | } | |
| 288 | 293 | case "error": |
| 289 | 294 | return [`error: ${plain(frame.error)}`]; |
| 290 | 295 | default: |
| @@ -358,6 +363,8 @@ export function typed(room: Room, input: string): Typed { | ||
| 358 | 363 | } |
| 359 | 364 | case "removed": |
| 360 | 365 | return { send: { type: "removed" } }; |
| 366 | case "visitors": | |
| 367 | return { send: { type: "visitors" } }; | |
| 361 | 368 | case "restore": { |
| 362 | 369 | const number = /^#?(\d{1,15})$/.exec(arg); |
| 363 | 370 | return number ? { send: { type: "restore", id: Number(number[1]) } } : { print: "usage: /restore #<line number>" }; |
Software/so101/src/visitor-counts.ts +5
| @@ -54,6 +54,11 @@ export class VisitorCounts { | ||
| 54 | 54 | if (this.here > 0) this.here-- |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | /** Nobody is connected any more (a restart): the peak and the hours stay. */ | |
| 58 | clearHere(): void { | |
| 59 | this.here = 0 | |
| 60 | } | |
| 61 | ||
| 57 | 62 | peakToday(): number { |
| 58 | 63 | this.roll() |
| 59 | 64 | return this.peak |
new above
offline
5rem + 1fr + the inherited 1rem gap leaves too little at 360px for the bar; shrink the label column and add
overflow-wrap: normalhere so the bar does not break mid-run.