OHHHHHH05 asked for 按模型筛选 next to the provider filter, with requests,
input/output tokens, cache and estimated cost for the range, gateway and
session-log calls alike. Usage › Requests could already be narrowed to a
model, but only by clicking it in the chart's ranking or typing it in the
search box. Now it has its own filter, after Provider, in the app's menu
(sessPick/openSessCombo, no <select>). It lists the models the other
filters keep, by tokens (/api/usage/requests' by.model, which is counted
without the model's pick). Picking one sends model=, which the server
already applied to both sources, so the sum line (requests, in, out,
cache read/write), the cost head, the through/not-through-magpie cells,
the chart and the rows are all that model's. All models clears it. A
pick made in the ranking shows in the filter too. No API change, no new
strings ("All models", "Model", "{n} tokens" are already in every
locale), app.js untouched (the plugin's page.js only).
Run (at d83823d26 + this; rebased onto b1a1b950f, a routing-classifier
change that touches no Usage file):
- new usage-ledger-model-pick.test.cjs, en zh zh-TW ja de at 1100 and
420: webkit 14/14 on wsl-run (magpie-desktop), chromium 10/10 on
wsl-run. mote's browsers were crashing/timing out on launch under
another session's full GUI suite there, so the GUI runs moved to WSL.
- usage-model-at-provider and usage-ledger-leave: green in both engines.
- usage-ledger: 2 reds, "newest live route needs no return-to-live
button", the same at origin/main d83823d26 run whole (filed #3104,
#4078).
- i18n-keys: 1 red, "Unmarked" in plugins/routing (filed #4524).
- go test -tags nogui ./internal/gui on mote: only
TestSettingsSaveKeepsWhatItDoesNotSend red (SearchAll, filed #4514).
Broken by hand: with the sessPick line taken out of page.js the new
test is 0/10 in chromium (wsl-run); its token note, first written with
a wrong sum, went red in every language before it was corrected.
Not tried: a real gateway's ledger in the running app; the full GUI
suite (one is already running on mote).
160 lines
9.9 KiB
JavaScript
160 lines
9.9 KiB
JavaScript
// Run with Node's test runner and Playwright on the module path; see README.md.
|
|
// The Usage page's Requests tab has a Model filter beside its Provider
|
|
// filter (OHHHHHH05 on X: "为用量面板增加“按模型筛选”,与现有的供应商筛选并列").
|
|
// It is the app's own menu, the models the other filters keep with their
|
|
// tokens; a pick asks for that model's requests, gateway and session-log
|
|
// rows alike, and the page's totals and source cells are then the model's.
|
|
// All models asks for every one again, and a click on a model in the chart's
|
|
// ranking shows in the filter too. Every language, wide and narrow, in
|
|
// Chromium and WebKit; no backend, the API is faked.
|
|
const assert = require("node:assert/strict");
|
|
const fs = require("node:fs/promises");
|
|
const path = require("node:path");
|
|
const { test } = require("node:test");
|
|
const { chromium, webkit } = require("playwright");
|
|
const reader = require("./reader.cjs");
|
|
|
|
const assets = path.resolve(__dirname, "../assets");
|
|
const now = Date.now();
|
|
const H = 3600e3;
|
|
const hour = (i) => new Date(Math.floor(now / H) * H - i * H).toISOString();
|
|
|
|
// glm-5.3 through the gateway; kimi-k3 once through it and once read from
|
|
// Claude Code's own session file
|
|
const ROWS = [
|
|
{ model: "glm-5.3", source: "", provider: "zhipu", providerName: "Zhipu", in: 1000, out: 500 },
|
|
{ model: "kimi-k3", source: "", provider: "moonshot", providerName: "Moonshot", in: 3000, out: 700 },
|
|
{ model: "kimi-k3", source: "log", provider: "moonshot", providerName: "Moonshot", in: 2000, out: 300 },
|
|
].map((x, i) => ({
|
|
route_id: x.source ? 0 : 900 + i, t: new Date(now - (i + 1) * 60e3).toISOString(), agent: "claude", agentName: "Claude Code", icon: "claudecode-color",
|
|
provider: x.provider, providerName: x.providerName, req: x.model, model: x.model, served: x.model, in: x.in, out: x.out, cache_read: 100, cache_write: 10,
|
|
ms: 2000, status: 200, rid: "req_" + i, ep: "/v1/messages", cost: 0.01, priced: true, source: x.source,
|
|
}));
|
|
const totals = (rows) => ({
|
|
calls: rows.length, errors: 0, input: rows.reduce((n, r) => n + r.in, 0), output: rows.reduce((n, r) => n + r.out, 0),
|
|
cache_read: rows.reduce((n, r) => n + r.cache_read, 0), cache_write: rows.reduce((n, r) => n + r.cache_write, 0), cost: rows.length * 0.01, unpriced: 0,
|
|
});
|
|
const share = (id) => ({ id, ...totals(ROWS.filter((r) => r.model === id)) });
|
|
|
|
function ledger(q) {
|
|
const model = q.get("model");
|
|
const rows = ROWS.filter((r) => !model || r.model === model);
|
|
const tot = totals(rows);
|
|
return {
|
|
period: q.get("period"), rows, offset: 0, total: rows.length, ...tot,
|
|
through: totals(rows.filter((r) => !r.source)), direct: totals(rows.filter((r) => r.source === "log")),
|
|
// the models' shares are of the rows without the model's pick
|
|
by: { model: [share("kimi-k3"), share("glm-5.3")] },
|
|
bucket: "hour",
|
|
series: [{ label: "", time: hour(0), ...tot, by: { model: Object.fromEntries(["kimi-k3", "glm-5.3"].map((m) => { const s = share(m); return [m, { calls: s.calls, tokens: s.input + s.output, cost: s.cost }]; })) } }],
|
|
agents: [{ id: "claude", name: "Claude Code", icon: "claudecode-color" }],
|
|
providers: [{ id: "zhipu", name: "Zhipu" }, { id: "moonshot", name: "Moonshot" }],
|
|
};
|
|
}
|
|
|
|
function server(lang, asked) {
|
|
return async (route) => {
|
|
const url = new URL(route.request().url());
|
|
const json = (data) => route.fulfill({ json: data });
|
|
if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:false};` });
|
|
if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
|
|
if (url.pathname === "/api/state") return json({ agents: [], profiles: [], settings: { lang, theme: "light" } });
|
|
if (url.pathname === "/api/usage/requests") { asked.push(url.searchParams); return json(ledger(url.searchParams)); }
|
|
if (url.pathname === "/api/usage/quotas") return json([]);
|
|
if (url.pathname === "/api/usage") return json({ calls: 0, errors: 0, input: 0, output: 0, reasoning: 0, unpriced: 0, cost: 0, bucket: "day", series: [], agents: [], models: [], path: "" });
|
|
if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], models: [], gateway: { running: false } });
|
|
if (url.pathname === "/api/sessions") return json({ sessions: [], dirs: [] });
|
|
if (url.pathname === "/api/sessions/stats") return json({ from: "", to: "", days: [], agents: {} });
|
|
if (url.pathname === "/api/groups") return json({ groups: [], models: [] });
|
|
if (url.pathname.startsWith("/api/")) return json({});
|
|
const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname);
|
|
const contentType = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)];
|
|
await route.fulfill({ body: await fs.readFile(file), contentType });
|
|
};
|
|
}
|
|
|
|
const LANGS = ["en", "zh", "zh-TW", "ja", "de"];
|
|
const WIDTHS = [1100, 420];
|
|
|
|
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
|
|
test(engine + ": a Model filter beside the Provider filter", async (t) => {
|
|
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
|
|
t.after(() => browser.close());
|
|
for (const lang of LANGS) for (const width of WIDTHS) {
|
|
await t.test(lang + " " + width, async () => {
|
|
const errors = [], asked = [];
|
|
const ctx = await browser.newContext({ viewport: { width, height: 800 }, reducedMotion: "reduce" });
|
|
const p = await ctx.newPage();
|
|
p.setDefaultTimeout(5000);
|
|
p.setDefaultNavigationTimeout(60000);
|
|
p.on("pageerror", (e) => errors.push(e.message));
|
|
await p.route("**/*", server(lang, asked));
|
|
await p.goto("http://magpie.test/");
|
|
await p.locator('[data-view="usage"]').first().click();
|
|
await p.locator(":is(#usageTab .opt, .nav-s):visible").nth(1).click();
|
|
await p.locator("#ledWrap .led tbody tr").first().waitFor();
|
|
await p.waitForTimeout(300);
|
|
const w = await p.evaluate(() => ({ all: t("All models"), head: t("Model"), provider: t("All providers"), tokens: t("{n} tokens", { n: fmtN(6220) }) }));
|
|
if (lang !== "en") assert.notEqual(w.all, "All models", "All models is in " + lang);
|
|
|
|
// beside the provider's filter, in the same row, the page not wider than the window
|
|
const pick = p.locator("#ledModel");
|
|
await pick.waitFor();
|
|
assert.equal((await pick.textContent()).trim(), w.all);
|
|
assert.equal((await p.locator("#ledProvider").textContent()).trim(), w.provider);
|
|
assert.equal(await pick.evaluate((b) => b.previousElementSibling.id === "ledProviderMore" && b.parentElement === document.querySelector("#ledProvider").parentElement), true);
|
|
const box = await pick.boundingBox();
|
|
assert(box.x >= 0 && box.x + box.width <= width, "the filter in the window: " + JSON.stringify(box));
|
|
assert.equal(await p.evaluate(() => document.scrollingElement.scrollWidth <= innerWidth), true, "no sideways scroll");
|
|
|
|
// its menu: the app's, with each model's tokens
|
|
await reader.click(p, pick);
|
|
const menu = p.locator(".sess-menu");
|
|
await menu.waitFor();
|
|
assert.equal(await p.locator("select").count(), 0);
|
|
assert.equal((await menu.locator(".pm-head").textContent()).trim(), w.head);
|
|
assert.deepEqual(await menu.locator(".pm-item .pm-name").allTextContents(), [w.all, "kimi-k3", "glm-5.3"]);
|
|
assert.equal((await menu.locator(".pm-item").nth(1).locator(".pm-note").textContent()).trim(), w.tokens);
|
|
|
|
// kimi-k3: its requests, the gateway's and the session file's
|
|
const before = asked.length;
|
|
await reader.click(p, menu.locator(".pm-item").nth(1));
|
|
await p.waitForFunction((n) => document.querySelectorAll("#ledWrap .led tbody tr.led-row").length === 2, before);
|
|
let q = asked.at(-1);
|
|
assert(asked.length > before);
|
|
assert.equal(q.get("model"), "kimi-k3");
|
|
assert.equal(q.get("q"), null);
|
|
assert.equal((await pick.textContent()).trim(), "kimi-k3");
|
|
assert.equal(await pick.evaluate((b) => b.classList.contains("set")), true);
|
|
const sum = await p.locator("#ledSum").textContent();
|
|
const said = await p.evaluate(() => [t("{n} in", { n: fmtN(5000) }), t("{n} out", { n: fmtN(1000) }), t("{n} cache read", { n: fmtN(200) }), t("{n} cache write", { n: fmtN(20) })]);
|
|
for (const x of said) assert(sum.includes(x), "the model's " + x + " in the sum: " + sum);
|
|
assert(await p.locator("#ledWrap .led tbody tr.led-row").evaluateAll((rs) => rs.every((r) => r.textContent.includes("kimi-k3"))));
|
|
// the source cells: one through magpie, one not
|
|
const cells = await p.locator("#ledVia .blk .v").allTextContents();
|
|
assert.equal(cells.length, 3);
|
|
assert.deepEqual(cells.map((c) => parseInt(c, 10)), [2, 1, 1]);
|
|
|
|
// All models: every one again
|
|
await reader.click(p, pick);
|
|
await menu.waitFor();
|
|
await reader.click(p, menu.locator(".pm-item").first());
|
|
await p.waitForFunction(() => document.querySelectorAll("#ledWrap .led tbody tr.led-row").length === 3);
|
|
q = asked.at(-1);
|
|
assert.equal(q.get("model"), null);
|
|
assert.equal((await pick.textContent()).trim(), w.all);
|
|
|
|
// a model clicked in the chart's ranking is the filter's pick too
|
|
if (width > 700) {
|
|
await reader.click(p, p.locator("#ledRank button.rk").filter({ hasText: "glm-5.3" }));
|
|
await p.waitForFunction(() => document.querySelectorAll("#ledWrap .led tbody tr.led-row").length === 1);
|
|
assert.equal(asked.at(-1).get("model"), "glm-5.3");
|
|
assert.equal((await pick.textContent()).trim(), "glm-5.3");
|
|
}
|
|
assert.deepEqual(errors, []);
|
|
await ctx.close();
|
|
});
|
|
}
|
|
});
|
|
}
|