keys chart: chart the keys that HAVE usage, not the ones with metadata
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/push/woodpecker Pipeline was successful
The first cut built a row per key in the metadata list and attached usage to it. Live, an account with three keys that had spent real money (homebot $11.84, office-pi $0.19, base-pi $0.19) came back with an EMPTY key list and four usage rows, so the chart drew one bar — the chat key — and quietly dropped the rest. Exactly the kind of "listing" this chart exists to compare. Now the usage rows are the authoritative set: every row gets a bar, metadata is attached when we have it, and a key with no usage still gets a zero row so a key you own is never invisible. The harness pins all three cases.
This commit is contained in:
1 parent
ffd42a2045
commit
b5b0703f0c
2 files changed
+121
-5
No files matched your search
+25
-5
@@ -1681,14 +1681,34 @@ function renderKeys(keys, keySpend) {
|
|||||||
const el = $('keys');
|
const el = $('keys');
|
||||||
keys = keys || []; keySpend = keySpend || [];
|
keys = keys || []; keySpend = keySpend || [];
|
||||||
keyRowsData = { keys: keys, spend: keySpend };
|
keyRowsData = { keys: keys, spend: keySpend };
|
||||||
const rows = keys.map(k => {
|
// One row per key that HAS usage, not per key in the metadata list: the usage
|
||||||
const s = keySpend.find(x => !x.is_chat && x.name === k.name) || {};
|
// rows are the authoritative set (a live account had three keys with spend and
|
||||||
return {
|
// an empty key list, so charting only the intersection showed one bar out of
|
||||||
|
// four). Metadata is attached when we have it; keys without usage still get a
|
||||||
|
// row, so a key you own is never invisible.
|
||||||
|
const metaFor = {};
|
||||||
|
keys.forEach(k => { metaFor[k.name] = k; });
|
||||||
|
const rows = [];
|
||||||
|
const seen = {};
|
||||||
|
keySpend.forEach(sp => {
|
||||||
|
if (sp.is_chat) return;
|
||||||
|
seen[sp.name] = true;
|
||||||
|
const k = metaFor[sp.name] || {};
|
||||||
|
rows.push({
|
||||||
|
name: sp.name, chat: false,
|
||||||
|
meta: (k.created_at ? 'created ' + k.created_at.slice(0, 10) : '') +
|
||||||
|
(k.fingerprint ? ' · ends …' + k.fingerprint : ''),
|
||||||
|
spend: Number(sp.spend) || 0, requests: Number(sp.requests) || 0
|
||||||
|
});
|
||||||
|
});
|
||||||
|
keys.forEach(k => {
|
||||||
|
if (seen[k.name]) return;
|
||||||
|
rows.push({
|
||||||
name: k.name, chat: false,
|
name: k.name, chat: false,
|
||||||
meta: (k.created_at ? 'created ' + k.created_at.slice(0, 10) : '') +
|
meta: (k.created_at ? 'created ' + k.created_at.slice(0, 10) : '') +
|
||||||
(k.fingerprint ? ' · ends …' + k.fingerprint : ''),
|
(k.fingerprint ? ' · ends …' + k.fingerprint : ''),
|
||||||
spend: Number(s.spend) || 0, requests: Number(s.requests) || 0
|
spend: 0, requests: 0
|
||||||
};
|
});
|
||||||
});
|
});
|
||||||
const chat = keySpend.find(x => x.is_chat);
|
const chat = keySpend.find(x => x.is_chat);
|
||||||
if (chat) {
|
if (chat) {
|
||||||
|
|||||||
@@ -0,0 +1,96 @@
|
|||||||
|
// The API keys as a chart: one bar row per key, the chat key included, and a
|
||||||
|
// Spend/Requests toggle that changes both the values and the order.
|
||||||
|
// Assertions are invariants (proportional lengths, sorting, presence), not the
|
||||||
|
// markup that happened to be rendered the day this was written.
|
||||||
|
const assert = (cond, msg) => { if (!cond) { console.error("FAIL: " + msg); process.exit(1); } };
|
||||||
|
|
||||||
|
const KEYS = [
|
||||||
|
{ name: "my-laptop", fingerprint: "abcd1234", created_at: "2026-11-01T10:00:00Z" },
|
||||||
|
{ name: "script", fingerprint: "efgh5678", created_at: "2026-11-02T10:00:00Z" },
|
||||||
|
];
|
||||||
|
const SPEND = [
|
||||||
|
{ name: "my-laptop", is_chat: false, spend: 12.5, requests: 100 },
|
||||||
|
{ name: "script", is_chat: false, spend: 2.5, requests: 400 },
|
||||||
|
{ name: "chat", is_chat: true, spend: 5.0, requests: 250 },
|
||||||
|
];
|
||||||
|
|
||||||
|
const widthOf = (html, label) => {
|
||||||
|
// the bar immediately following this key's label
|
||||||
|
const i = html.indexOf(label);
|
||||||
|
assert(i !== -1, "the chart has a row labelled " + label);
|
||||||
|
const seg = html.slice(i);
|
||||||
|
const m = seg.match(/key-bar-fill[^>]*width:(\d+)%/);
|
||||||
|
assert(m, "the " + label + " row has a bar width");
|
||||||
|
return parseInt(m[1], 10);
|
||||||
|
};
|
||||||
|
const valueOf = (html, label) => {
|
||||||
|
const i = html.indexOf(label);
|
||||||
|
const seg = html.slice(i, html.indexOf("key-bar-row", i + 1) === -1 ? undefined : html.indexOf("key-bar-row", i + 1));
|
||||||
|
const m = seg.match(/key-bar-val">([^<]*)</);
|
||||||
|
return m ? m[1] : "";
|
||||||
|
};
|
||||||
|
|
||||||
|
renderKeys(KEYS, SPEND);
|
||||||
|
let html = document.getElementById("keys").innerHTML;
|
||||||
|
|
||||||
|
console.log("1. every key is a row, the chat key included");
|
||||||
|
assert((html.match(/key-bar-row/g) || []).length === 3,
|
||||||
|
"three rows: two API keys + the built-in chat key (got " + (html.match(/key-bar-row/g) || []).length + ")");
|
||||||
|
assert(html.indexOf("Chat (built-in)") !== -1, "the chat key is a row, not a footnote");
|
||||||
|
assert(html.indexOf("No API keys yet") === -1, "not the empty state");
|
||||||
|
|
||||||
|
console.log("2. bars are proportional to the value, longest = 100%");
|
||||||
|
assert(widthOf(html, "my-laptop") === 100, "the top spender is the full-width bar (got " + widthOf(html, "my-laptop") + "%)");
|
||||||
|
assert(widthOf(html, "Chat (built-in)") === 40, "half the top spend reads as 40% (got " + widthOf(html, "Chat (built-in)") + "%)");
|
||||||
|
assert(widthOf(html, "script") === 20, "a fifth reads as 20% (got " + widthOf(html, "script") + "%)");
|
||||||
|
assert(html.indexOf("$12.50") !== -1, "the value is printed exactly, not just drawn");
|
||||||
|
|
||||||
|
console.log("3. rows are ordered by the chosen metric, biggest first");
|
||||||
|
assert(html.indexOf("my-laptop") < html.indexOf("Chat (built-in)") &&
|
||||||
|
html.indexOf("Chat (built-in)") < html.indexOf("script"),
|
||||||
|
"spend order: my-laptop ($12.50) > chat ($5.00) > script ($2.50)");
|
||||||
|
|
||||||
|
console.log("4. the toggle re-renders by REQUESTS, values and order together");
|
||||||
|
setKeyMetric("requests");
|
||||||
|
html = document.getElementById("keys").innerHTML;
|
||||||
|
assert(document.getElementById("km-requests").className.indexOf("active") !== -1, "Requests is the active button");
|
||||||
|
assert(document.getElementById("km-spend").className.indexOf("active") === -1, "Spend is no longer active");
|
||||||
|
assert(widthOf(html, "script") === 100, "by requests, the biggest requester is full width (got " + widthOf(html, "script") + "%)");
|
||||||
|
assert(html.indexOf("400 requests") !== -1, "requests are labelled as such, with a thousands separator");
|
||||||
|
assert(html.indexOf("script") < html.indexOf("my-laptop"), "the order changed with the metric");
|
||||||
|
setKeyMetric("spend");
|
||||||
|
html = document.getElementById("keys").innerHTML;
|
||||||
|
assert(widthOf(html, "my-laptop") === 100, "and switching back restores the spend view");
|
||||||
|
|
||||||
|
console.log("5. revoke stays where it belongs");
|
||||||
|
assert((html.match(/revokeKey\(/g) || []).length === 2,
|
||||||
|
"one Revoke per real key, none for the built-in chat key (got " + (html.match(/revokeKey\(/g) || []).length + ")");
|
||||||
|
|
||||||
|
console.log("5b. a key with usage but no metadata row is STILL charted");
|
||||||
|
renderKeys([], SPEND); // the live case: usage rows, an empty key list
|
||||||
|
{
|
||||||
|
const h = document.getElementById("keys").innerHTML;
|
||||||
|
assert((h.match(/key-bar-row/g) || []).length === 3,
|
||||||
|
"all three usage rows appear even with no key metadata (got " + (h.match(/key-bar-row/g) || []).length + ")");
|
||||||
|
assert(h.indexOf("my-laptop") !== -1 && h.indexOf("script") !== -1,
|
||||||
|
"the named usage rows are charted, not dropped for want of a metadata entry");
|
||||||
|
assert((h.match(/revokeKey\(/g) || []).length === 2,
|
||||||
|
"and each still gets its Revoke control");
|
||||||
|
}
|
||||||
|
renderKeys([{ name: "unused-key", fingerprint: "zz", created_at: "2026-11-03T00:00:00Z" }], SPEND);
|
||||||
|
{
|
||||||
|
const h = document.getElementById("keys").innerHTML;
|
||||||
|
assert((h.match(/key-bar-row/g) || []).length === 4,
|
||||||
|
"a key you own but have not used still gets a row (got " + (h.match(/key-bar-row/g) || []).length + ")");
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("6. the empty state means NO rows at all");
|
||||||
|
renderKeys([], []); // no keys and no chat usage
|
||||||
|
assert(document.getElementById("keys").innerHTML.indexOf("No API keys yet") !== -1,
|
||||||
|
"with nothing to show, the card says so");
|
||||||
|
renderKeys([], [{ name: "chat", is_chat: true, spend: 1, requests: 2 }]);
|
||||||
|
const chatOnly = document.getElementById("keys").innerHTML;
|
||||||
|
assert(chatOnly.indexOf("No API keys yet") === -1 && chatOnly.indexOf("Chat (built-in)") !== -1,
|
||||||
|
"with only chat usage, the chat row still shows — a chat key IS usage worth seeing");
|
||||||
|
|
||||||
|
console.log("DASHBOARD KEYS HARNESS OK: proportional bars, sorting, toggle, revoke placement");
|
||||||
Reference in new issue
Block a user