Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ a laptop.
`docs/design/v3/day/` (the prototype and its README); `built/` holds the built page's shots.
- `public/usage/`: the private usage page's files (setkernel-live#71), served by the Worker only with
a `USAGE_AUD` token (`run_worker_first` in `wrangler.jsonc`, pinned by `test/usage-page.test.mjs`).
`table.js` (a month's rows, cells and the box's text) is pure and tested. Never move them out of `/usage`.
`table.js` (a month's rows, cells and the box's text) is pure and tested; `page.js` draws it, tested
against a stand-in document. Never move them out of `/usage`.
- `public/lib/`: ES modules for the page. `state.js` is the public state, the Halifax day
(`homeDay`, `homeMidnight`) and the 24-hour replay buffer; the Durable Object and `?demo` both
run it. `line.js` derives each engineer's stage (state.js keeps the lanes). `words.js` (wording,
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,9 @@ Everything under `/usage` (the page Raghav reads it on, setkernel-live#71, and i
before the Worker sees the request). `/usage/data` names the newest hour stored, in any month, in a
`Usage-Newest-Hour` header (absent while nothing is stored); the body keeps its shape.
`public/usage/table.js` turns a month into the page's rows: fresh tokens are input + output + cache
writes, and a day or seat with no data is a dash, not zero.
writes, and a day or seat with no data is a dash, not zero. `public/usage/index.html`, `usage.css`
and `page.js` draw it (setkernel-live#78): `?month=YYYY-MM` picks the month, the newest hour is
the header's, and a 403, 503 or expired login shows a message. Its shots are in `docs/design/usage/`.

## When updates stop

Expand Down
Binary file added docs/design/usage/usage-1440x900-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/design/usage/usage-1440x900.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/design/usage/usage-375x812.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions public/usage/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<meta name="robots" content="noindex">
<title>Usage per seat · setkernel live</title>
<link rel="icon" href="/favicon.svg" type="image/svg+xml">
<link rel="stylesheet" href="/style.css">
<link rel="stylesheet" href="/usage/usage.css">
<script type="module" src="/usage/page.js"></script>
</head>
<body>
<main class="day">
<header class="mast">
<p class="brand"><a href="https://setkernel.com">setkernel</a> <span>live · usage</span></p>
<p class="fresh"><span id="asof">Loading</span><span class="wide"> · Only Raghav can open this page</span></p>
</header>
<section class="lead">
<h1 id="title">Tokens per seat</h1>
<p>Fresh tokens are input, output and cache writes. Days run midnight to midnight, Halifax time.</p>
</section>
<nav class="months" id="months" aria-label="Month"></nav>
<p class="status" id="status" role="status">Loading this month's usage…</p>
<ul class="sum" id="sum" hidden></ul>
<p class="hint" id="hint" hidden>Hover a number for its exact tokens, cache reads and dollars. On a phone, tap it.</p>
<div class="scroll" id="scroll" hidden><table id="usage"></table></div>
<p class="note">Dollars are what the same tokens would cost at API prices. The seats are on subscriptions, so this is a size guide, not a bill. Polaris covers Raghav's own sessions. A dash means no data, not zero.</p>
</main>
<div class="tip" id="tip" role="tooltip" hidden></div>
</body>
</html>
154 changes: 154 additions & 0 deletions public/usage/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
// live.setkernel.com/usage (setkernel-live#78): option B from setkernel-live#71, drawn from GET
// /usage/data through buildMonth (table.js). Everything is built with textContent. Hovering,
// focusing or tapping a number shows its box; Esc or a tap elsewhere closes it.
// Tested with a stand-in document in test/usage-page.test.mjs.
import { homeDay } from "../lib/state.js";
import { asOf, buildMonth, monthName, shiftMonth, tip } from "./table.js";

const $ = (id) => document.getElementById(id);
function el(tag, cls, text) {
const e = document.createElement(tag);
if (cls) e.className = cls;
if (text != null) e.textContent = text;
return e;
}

// What the page says when GET /usage/data doesn't answer with a month. 0 is no answer at all: an
// expired login is a redirect to Access's login page, which the CSP stops.
export function failure(status) {
if (status === 403) return "This login can't read usage. Reload the page to sign in again.";
if (status === 503) return "Usage isn't set up on this site, so there is nothing to show.";
return `Usage didn't load (${status ? `HTTP ${status}` : "no answer"}). Reload the page to try again; if your login expired, that signs you in.`;
}

function nav(month, current) {
const link = (m, text) => (m > current ? el("span", "off", text) : Object.assign(el("a", null, text), { href: `?month=${m}` }));
$("months").replaceChildren(link(shiftMonth(month, -1), `← ${monthName(shiftMonth(month, -1))}`), el("b", null, `${monthName(month)} ${month.slice(0, 4)}`), link(shiftMonth(month, 1), `${monthName(shiftMonth(month, 1))} →`));
}

// A number: its data-* attributes are the exact values its box shows.
function num(x, cls) {
const td = el("td", `h ${cls}`);
td.tabIndex = 0;
Object.assign(td.dataset, { who: x.who, when: x.when, f: x.f, c: x.c, u: x.u });
td.setAttribute("aria-label", x.label);
return td;
}
const dash = (cls = "") => el("td", `none ${cls}`, "–");

function bar(px) {
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg"), rect = document.createElementNS("http://www.w3.org/2000/svg", "rect");
for (const [k, v] of Object.entries({ class: "bar", width: px, height: 4, "aria-hidden": "true" })) svg.setAttribute(k, v);
for (const [k, v] of Object.entries({ width: px, height: 4, rx: 2 })) rect.setAttribute(k, v);
svg.append(rect);
return svg;
}

function draw(v, newest) {
$("title").textContent = `Tokens per seat, ${v.name}`;
$("asof").textContent = newest ? `Newest hour ends ${asOf(newest)}` : "Nothing stored yet";
const t = v.total.all;
$("sum").replaceChildren(...[["Fresh tokens this month", t?.text], ["Cache reads", t?.cached], ["At API prices, about", t?.usd], ["Busiest seat", v.busiest]]
.map(([k, val]) => { const li = el("li"); li.append(el("span", "k", k), el("span", "v", val ?? "–")); return li; }));
if (v.empty) $("status").textContent = `Nothing is stored for ${v.name} yet.`;
else $("status").hidden = true;

const head = el("tr");
head.append(...["Day", ...v.seats].map((s) => el("th", null, s)), el("th", "tot", "All seats"));
for (const th of head.children) th.setAttribute("scope", "col");
const body = el("tbody");
for (const r of v.rows) {
const tr = el("tr", [r.weekend && "we", r.today && "today"].filter(Boolean).join(" "));
const day = el("td", null, `${r.d} `);
day.append(el("span", "wd", r.wd));
if (r.today) day.append(el("span", "tag", "so far"));
tr.append(day, ...r.cells.map((c) => {
if (!c) return dash();
const td = num(c, `t${c.heat}`);
td.append(el("span", "m", c.text));
return td;
}));
if (r.all) {
const td = num(r.all, "tot"), m = el("span", "m", `${r.all.text} `);
m.append(el("span", "usd", r.all.usd));
td.append(m);
if (r.all.bar) td.append(bar(r.all.bar));
tr.append(td);
} else tr.append(dash("tot"));
body.append(tr);
}
const foot = el("tr");
foot.append(el("td", null, v.name.split(" ")[0]), ...v.total.cells.map((c) => {
if (!c) return dash();
const td = num(c, "");
td.append(el("span", "m", c.text), el("span", "s", c.usd));
return td;
}));
if (t) {
const td = num(t, "tot");
td.append(el("span", "m", t.text), el("span", "s", `${t.cached} cached`), el("span", "s", t.usd));
foot.append(td);
} else foot.append(dash("tot"));
const thead = el("thead"), tfoot = el("tfoot");
thead.append(head);
tfoot.append(foot);
$("usage").replaceChildren(thead, body, tfoot);
for (const id of ["sum", "scroll", ...(v.empty ? [] : ["hint"])]) $(id).hidden = false;
}

// The box, from the reference in setkernel-live#71: placed with the CSSOM, which the CSP allows.
let pinned = null, focused = null;
function show(td) {
const box = $("tip"), t = tip(td.dataset), dl = el("dl");
for (const [k, val] of t.rows) dl.append(el("dt", null, k), el("dd", null, val));
box.replaceChildren(el("div", "t-h", t.head), dl);
box.hidden = false;
const r = td.getBoundingClientRect(), w = box.offsetWidth, h = box.offsetHeight;
const x = Math.min(Math.max(8, r.left + r.width / 2 - w / 2), document.documentElement.clientWidth - w - 8);
const y = r.top - h - 8 < 8 ? r.bottom + 8 : r.top - h - 8;
box.style.left = `${x}px`;
box.style.top = `${y}px`;
}
function hide() {
pinned?.classList.remove("on");
pinned = focused = null;
$("tip").hidden = true;
}
function listen() {
const table = $("usage");
const cell = (n) => { const td = n?.closest?.("td.h"); return td && table.contains(td) ? td : null; };
table.addEventListener("mouseover", (e) => { const td = cell(e.target); if (td && !pinned) show(td); });
table.addEventListener("mouseout", (e) => { if (!pinned && cell(e.target) && !cell(e.relatedTarget)) hide(); });
table.addEventListener("focusin", (e) => { const td = cell(e.target); if (td) show(focused = td); });
table.addEventListener("focusout", () => { if (!pinned) hide(); });
document.addEventListener("click", (e) => {
const td = cell(e.target), again = td && td === pinned;
hide();
if (td && !again) { pinned = td; td.classList.add("on"); show(td); }
});
document.addEventListener("keydown", (e) => { if (e.key === "Escape") hide(); });
// the window's scroll and the table's own: move a pinned or focused box with its number (Tab
// scrolls a number into view after focusin), drop a hovered one
document.addEventListener("scroll", () => { const td = pinned ?? focused; td ? show(td) : hide(); }, { capture: true, passive: true });
}

async function load() {
const today = homeDay(Date.now()), current = today.slice(0, 7);
const asked = new URLSearchParams(location.search).get("month") ?? "";
const month = /^\d{4}-(0[1-9]|1[0-2])$/.test(asked) && asked <= current ? asked : current;
nav(month, current);
let res, data;
try {
res = await fetch(`/usage/data?month=${month}`, { cache: "no-store", redirect: "manual" });
if (res.ok) data = await res.json();
} catch {}
if (!data) {
$("status").textContent = failure(res?.status ?? 0);
$("asof").textContent = "Not loaded";
return;
}
draw(buildMonth(data, today), res.headers.get("Usage-Newest-Hour"));
listen();
}

export const ready = load();
74 changes: 74 additions & 0 deletions public/usage/usage.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* live.setkernel.com/usage (setkernel-live#78): option B from setkernel-live#71. The fonts, tokens,
masthead and lead come from /style.css; this adds the dark theme (the wall's colours, so the
contrast test in test/page.test.mjs covers them), the table, the shading and the box. */
:root { --heat: 24, 24, 26; }
@media (prefers-color-scheme: dark) {
:root {
--bg: #111214; --surface: #1b1c20; --ink: #eeede9; --ink-2: #bdbbb5; --ink-3: #9a9892;
--rule: #232427; --rule-2: #3d3e42; --band: #25262a; --bar: #77756f;
--signal: #f2735f; --focus: #8fb0ff;
--heat: 238, 237, 233; color-scheme: dark;
}
}
.lead { padding-bottom: var(--s3); }
.months { display: flex; gap: var(--s4); align-items: baseline; font-size: var(--f2); line-height: var(--l2); color: var(--ink-2); padding-bottom: var(--s3); }
.months .off { color: var(--ink-3); }
a:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.status { margin: 0 0 var(--s4); color: var(--ink-2); }
.sum { display: flex; flex-wrap: wrap; gap: var(--s3) var(--s5); margin: 0 0 var(--s4); padding: 0; list-style: none; }
.sum li { display: flex; flex-direction: column; }
.sum .k { font-size: var(--f1); line-height: var(--l1); color: var(--ink-3); }
.sum .v { font-size: var(--f4); line-height: var(--l3); font-weight: 600; }
.hint { margin: 0 0 var(--s2); font-size: var(--f1); line-height: var(--l1); color: var(--ink-3); }
.note { margin: var(--s3) 0 0; font-size: var(--f1); line-height: var(--l1); color: var(--ink-3); max-width: 760px; }

/* the table: one row per Halifax day, newest first; only it scrolls sideways on a phone */
.scroll { overflow-x: auto; border-top: 1px solid var(--rule-2); }
table { border-collapse: collapse; width: 100%; font-size: var(--f2); line-height: var(--l2); }
th, td { text-align: right; padding: var(--s2) var(--s3) var(--s2) 0; border-bottom: 1px solid var(--rule); vertical-align: top; white-space: nowrap; }
thead th { font-weight: 600; color: var(--ink-2); position: sticky; top: 0; background: var(--bg); }
th:first-child, td:first-child { text-align: left; position: sticky; left: 0; background: var(--bg); padding-left: 0; z-index: 1; }
td:first-child { font: var(--f1)/var(--l2) var(--mono); color: var(--ink-2); }
td:first-child .wd { color: var(--ink-3); }
td .m, td .s { display: block; }
td .m { color: var(--ink); }
td .s { font-size: var(--f1); line-height: var(--l1); color: var(--ink-3); }
td.none { color: var(--ink-3); padding-left: var(--s3); }
th.tot, td.tot { border-left: 1px solid var(--rule-2); padding-left: var(--s3); }
tr.we td:first-child { color: var(--ink-3); }
tr.today td, tr.today td:first-child { background-color: var(--surface); }
.tag { display: inline-block; margin-left: var(--s2); font: 600 var(--f1)/var(--l1) var(--sans); color: var(--signal); }
tfoot td { border-top: 1px solid var(--rule-2); border-bottom: 0; padding-top: var(--s3); }
tfoot td:first-child { font: 600 var(--f2)/var(--l2) var(--sans); color: var(--ink); }
tfoot td .m, td.tot .m { font-weight: 600; }
.usd { font-size: var(--f1); color: var(--ink-3); }
.bar { display: block; margin: var(--s1) 0 0 auto; }
.bar rect { fill: var(--bar); }

/* a seat's day, shaded on 8 steps by its size against the month's biggest (table.js, HEAT): a
class each, since the CSP allows no style attribute; laid over today's ground, not in place of it */
td.h { padding-left: var(--s3); cursor: default; background-image: linear-gradient(var(--tint, transparent), var(--tint, transparent)); }
td.t1 { --tint: rgba(var(--heat), .04); }
td.t2 { --tint: rgba(var(--heat), .06); }
td.t3 { --tint: rgba(var(--heat), .08); }
td.t4 { --tint: rgba(var(--heat), .10); }
td.t5 { --tint: rgba(var(--heat), .12); }
td.t6 { --tint: rgba(var(--heat), .14); }
td.t7 { --tint: rgba(var(--heat), .16); }
td.t8 { --tint: rgba(var(--heat), .18); }
td.h:focus { outline: none; }
td.h:hover, td.h.on { outline: 2px solid var(--ink); outline-offset: -2px; }
td.h:focus-visible { outline: 2px solid var(--focus); outline-offset: -2px; }

/* the box: a number's exact fresh tokens, cache reads and dollars */
.tip { position: fixed; z-index: 5; min-width: 220px; padding: var(--s2) var(--s3); background: var(--ink); color: var(--bg); border-radius: 6px; font-size: var(--f2); line-height: var(--l2); pointer-events: none; box-shadow: 0 4px 16px rgba(0, 0, 0, .18); }
.tip .t-h { font-weight: 600; margin-bottom: var(--s1); }
.tip dl { display: grid; grid-template-columns: auto auto; gap: 0 var(--s3); margin: 0; }
.tip dt { opacity: .72; }
.tip dd { margin: 0; text-align: right; font-weight: 600; }

@media (max-width: 719px) {
.sum .v { font-size: var(--f3); }
.wide { display: none; }
table { width: auto; }
}
Loading
Loading