llmtop 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +259 -2
- package/bin/llmtop.mjs +101 -0
- package/dashboard/dist/apple-touch-icon.png +0 -0
- package/dashboard/dist/assets/index-BVbAwMVB.js +9 -0
- package/dashboard/dist/assets/index-Cp7Swcz5.css +1 -0
- package/dashboard/dist/assets/llmtop-lockup-DtFPJmhv.svg +1 -0
- package/dashboard/dist/assets/llmtop-lockup-reversed-D9OmQIVU.svg +1 -0
- package/dashboard/dist/favicon.ico +0 -0
- package/dashboard/dist/favicon.svg +1 -0
- package/dashboard/dist/index.html +19 -0
- package/dashboard/dist/llmtop-mark-192.png +0 -0
- package/dashboard/dist/llmtop-mark-512.png +0 -0
- package/dashboard/dist/site.webmanifest +11 -0
- package/dashboard/src/App.tsx +213 -0
- package/dashboard/src/controls/DateRange.tsx +108 -0
- package/dashboard/src/controls/Disclosure.tsx +15 -0
- package/dashboard/src/controls/Search.tsx +18 -0
- package/dashboard/src/controls/Select.tsx +48 -0
- package/dashboard/src/controls/popover.tsx +31 -0
- package/dashboard/src/dash.ts +23 -0
- package/dashboard/src/extra.test.ts +9 -0
- package/dashboard/src/extra.ts +16 -0
- package/dashboard/src/format.ts +29 -0
- package/dashboard/src/i18n.tsx +52 -0
- package/dashboard/src/index.ts +7 -0
- package/dashboard/src/main.tsx +7 -0
- package/dashboard/src/model.test.ts +110 -0
- package/dashboard/src/model.ts +255 -0
- package/dashboard/src/state.test.ts +48 -0
- package/dashboard/src/state.ts +61 -0
- package/dashboard/src/styles.css +401 -0
- package/dashboard/src/tip.ts +51 -0
- package/dashboard/src/treemap.test.ts +23 -0
- package/dashboard/src/treemap.ts +30 -0
- package/dashboard/src/ui.tsx +327 -0
- package/dashboard/src/views/Activity.tsx +177 -0
- package/dashboard/src/views/Context.tsx +183 -0
- package/dashboard/src/views/Models.tsx +155 -0
- package/dashboard/src/views/Overview.tsx +83 -0
- package/dashboard/src/views/Projects.tsx +116 -0
- package/dashboard/src/views/Sessions.tsx +54 -0
- package/dashboard/src/views/Subagents.tsx +102 -0
- package/docs/brand/README.md +91 -0
- package/docs/brand/brand.json +67 -0
- package/docs/brand/favicon.ico +0 -0
- package/docs/brand/png/apple-touch-icon.png +0 -0
- package/docs/brand/png/favicon-16.png +0 -0
- package/docs/brand/png/favicon-32.png +0 -0
- package/docs/brand/png/favicon-48.png +0 -0
- package/docs/brand/png/llmtop-lockup-reversed@2x.png +0 -0
- package/docs/brand/png/llmtop-lockup@2x.png +0 -0
- package/docs/brand/png/llmtop-mark-1024.png +0 -0
- package/docs/brand/png/llmtop-mark-192.png +0 -0
- package/docs/brand/png/llmtop-mark-512.png +0 -0
- package/docs/brand/png/llmtop-wordmark@2x.png +0 -0
- package/docs/brand/source/build_brand.py +173 -0
- package/docs/brand/svg/favicon.svg +1 -0
- package/docs/brand/svg/llmtop-glyph.svg +1 -0
- package/docs/brand/svg/llmtop-lockup-reversed.svg +1 -0
- package/docs/brand/svg/llmtop-lockup.svg +1 -0
- package/docs/brand/svg/llmtop-mark-light.svg +1 -0
- package/docs/brand/svg/llmtop-mark.svg +1 -0
- package/docs/brand/svg/llmtop-wordmark-reversed.svg +1 -0
- package/docs/brand/svg/llmtop-wordmark.svg +1 -0
- package/llmtop.config.example.json +10 -0
- package/package.json +71 -4
- package/prices.md +27 -0
- package/src/api.mjs +53 -0
- package/src/archive.mjs +142 -0
- package/src/config.mjs +55 -0
- package/src/context.mjs +151 -0
- package/src/dashboard.mjs +61 -0
- package/src/db.mjs +102 -0
- package/src/derive.mjs +346 -0
- package/src/effort.mjs +18 -0
- package/src/format.mjs +19 -0
- package/src/paths.mjs +82 -0
- package/src/prices-command.mjs +25 -0
- package/src/prices.mjs +146 -0
- package/src/providers/agy.mjs +438 -0
- package/src/providers/claude.mjs +274 -0
- package/src/providers/codex.mjs +589 -0
- package/src/providers/index.mjs +25 -0
- package/src/rebuilds.mjs +85 -0
- package/src/scan.mjs +214 -0
- package/src/serve.mjs +44 -0
- package/src/show.mjs +20 -0
- package/src/usage-command.mjs +110 -0
- package/src/usage.mjs +388 -0
- package/src/zone.mjs +15 -0
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
import { useDash } from "../dash.ts";
|
|
3
|
+
import { dayAdd, keep, slice, type Compaction, type Ctx, type Fact } from "../model.ts";
|
|
4
|
+
import { Delta, Empty, Kpi, Stats, tip, type TipRow } from "../ui.tsx";
|
|
5
|
+
|
|
6
|
+
// Whether the auto-compact window suits the sessions: how compactions went, what reading the context cost by its size, and
|
|
7
|
+
// the same requests replayed with compaction at other points.
|
|
8
|
+
|
|
9
|
+
const median = (xs: (number | null)[]) => {
|
|
10
|
+
const v = xs.filter((x): x is number => x !== null).sort((a, b) => a - b), m = v.length >> 1;
|
|
11
|
+
return v.length ? (v.length % 2 ? v[m] : (v[m - 1] + v[m]) / 2) : null;
|
|
12
|
+
};
|
|
13
|
+
const TAIL = 15; // requests after a session's last compaction under which it ended soon after
|
|
14
|
+
const LONG = 4; // compactions that make a session long
|
|
15
|
+
const NEAR = 0.03; // a point this close to where compaction fires now gives way to it
|
|
16
|
+
const PARTS = 4;
|
|
17
|
+
const round5 = (v: number) => Math.round(v / 5000) * 5000; // a firing point as read: measured points scatter by a few thousand tokens
|
|
18
|
+
const compacting = (C: Compaction[]) => C.reduce((n, x) => n + (x.sum ?? 0) + x.rb, 0);
|
|
19
|
+
const reading = (F: Fact[]) => F.reduce((n, x) => n + (x.k?.[0] ?? 0), 0);
|
|
20
|
+
const costOf = (F: Fact[]) => F.reduce((n, x) => n + x.c, 0);
|
|
21
|
+
|
|
22
|
+
export function Context({ F, list }: { F: Fact[]; list: string[] }) {
|
|
23
|
+
const { M, filter, t, L, f } = useDash();
|
|
24
|
+
const C = M.compactions.filter(keep(M, filter)), X = M.context.filter(keep(M, filter));
|
|
25
|
+
const pf = dayAdd(filter.from, -list.length), pt = dayAdd(filter.from, -1), prev = pf >= M.first;
|
|
26
|
+
const PC = prev ? M.compactions.filter(keep(M, filter, pf, pt)) : null, PF = prev ? slice(M, filter, pf, pt) : null;
|
|
27
|
+
if (!F.length) return <section className="card"><Empty /></section>;
|
|
28
|
+
const total = costOf(F);
|
|
29
|
+
const daily = <T extends { d: string }>(rows: T[], v: (x: T) => number) => list.map((d) => rows.reduce((n, x) => n + (x.d === d ? v(x) : 0), 0));
|
|
30
|
+
return (
|
|
31
|
+
<div className="stack ctx">
|
|
32
|
+
<div className="grid kpis">
|
|
33
|
+
<Kpi label={t("compactions")} value={f.int(C.length)} d={<Delta a={C.length} b={PC?.length} side="none" />} series={daily(C, () => 1)} />
|
|
34
|
+
<Kpi label={t("compacting")} value={<>{f.usd(compacting(C))} <small>{L.ofCost(f.pct(total ? compacting(C) / total : 0))}</small></>}
|
|
35
|
+
d={<Delta a={compacting(C)} b={PC ? compacting(PC) : undefined} side="none" />} series={daily(C, (x) => (x.sum ?? 0) + x.rb)} />
|
|
36
|
+
<Kpi label={t("readingCtx")} value={<>{f.usd(reading(F))} <small>{L.ofCost(f.pct(total ? reading(F) / total : 0))}</small></>}
|
|
37
|
+
d={<Delta a={reading(F)} b={PF ? reading(PF) : undefined} side="none" />} series={daily(F, (x) => x.k?.[0] ?? 0)} />
|
|
38
|
+
</div>
|
|
39
|
+
<div className="grid half">
|
|
40
|
+
<Compactions C={C} />
|
|
41
|
+
<Reading F={F} X={X} />
|
|
42
|
+
</div>
|
|
43
|
+
<Replay C={C} X={X} />
|
|
44
|
+
</div>
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// How compactions went: what each kept and left to work with, the wait, what one cost, the sessions that ended right after
|
|
49
|
+
// one, and the stretch from each to the next by its order in the session.
|
|
50
|
+
function Compactions({ C }: { C: Compaction[] }) {
|
|
51
|
+
const { t, L, f } = useDash();
|
|
52
|
+
const main = C.filter((x) => !x.sub), auto = C.filter((x) => x.auto === 1);
|
|
53
|
+
const parts = [[auto.filter((x) => !x.sub).length, L.nAuto], [main.filter((x) => x.auto === 0).length, L.nManual], [C.length - main.length, L.nInSubs]] as const;
|
|
54
|
+
const head = <header><h2>{t("compactions")}</h2><span className="hint">{parts.filter(([n]) => n).map(([n, l]) => l(f.int(n))).join(" · ")}</span></header>;
|
|
55
|
+
if (!C.length) return <section className="card">{head}<Empty /></section>;
|
|
56
|
+
const measured = median(auto.filter((x) => !x.sub).map((x) => x.pre)) ?? median(auto.map((x) => x.pre)), fire = measured && round5(measured), kept = median(main.map((x) => x.kept));
|
|
57
|
+
const waits = C.map((x) => x.wait).filter((x): x is number => x !== null);
|
|
58
|
+
const priced = C.filter((x) => x.sum !== null), sum = priced.reduce((n, x) => n + x.sum!, 0) / (priced.length || 1), rb = C.reduce((n, x) => n + x.rb, 0) / C.length;
|
|
59
|
+
const soon = new Set(main.filter((x) => x.tail !== null && x.tail < TAIL).map((x) => x.s)).size;
|
|
60
|
+
const groups = [1, 2, 3, 4].map((k) => main.filter((x) => (k < 4 ? x.nth === k : x.nth >= 4)));
|
|
61
|
+
return (
|
|
62
|
+
<section className="card">
|
|
63
|
+
{head}
|
|
64
|
+
{fire && kept && kept < fire ? <>
|
|
65
|
+
<ul className="legend"><li><i className="c0" />{t("keptAfter")}<b>{f.cmp(kept)}</b></li><li><i className="c1" />{t("roomToWork")}<b>{f.cmp(fire - kept)}</b></li></ul>
|
|
66
|
+
<div className="gauge">
|
|
67
|
+
<div className="track"><i className="c0" style={{ width: `${(kept / fire) * 100}%` }} /><i className="c1" style={{ flex: 1 }} /></div>
|
|
68
|
+
<div className="ticks"><span style={{ left: 0 }}>0</span><span style={{ left: `${(kept / fire) * 100}%` }}>{f.cmp(kept)}</span><span style={{ left: "100%" }}>{L.compactsAt(f.cmp(fire))}</span></div>
|
|
69
|
+
</div>
|
|
70
|
+
</> : null}
|
|
71
|
+
<Stats items={[
|
|
72
|
+
{ label: t("wait"), value: waits.length ? `${Math.round(median(waits)! / 1000)} s` : "—", sub: waits.length ? L.inAll(f.dur(waits.reduce((a, b) => a + b, 0) / 60000)) : undefined },
|
|
73
|
+
{ label: t("eachCosts"), value: f.usd(sum + rb), sub: L.eachSplit(f.usd(sum), f.usd(rb)).map((s) => <span key={s} className="line">{s}</span>) },
|
|
74
|
+
{ label: t("endedSoon"), value: f.int(soon), sub: L.endedWithin(TAIL) },
|
|
75
|
+
]} />
|
|
76
|
+
{main.length ? <div className="scroll"><table>
|
|
77
|
+
<thead><tr><th>{t("inSession")}</th><th className="num">{t("compactions")}</th><th className="num o1">{t("keptCol")}</th><th className="num">{t("reqsToNext")}</th><th className="num">{t("turnsToNext")}</th></tr></thead>
|
|
78
|
+
<tbody>{groups.map((g, k) => g.length ? <tr key={k}>
|
|
79
|
+
<td className="name">{L.nth[k]}</td><td className="num">{f.int(g.length)}</td><td className="num o1">{cell(median(g.map((x) => x.kept)), f.cmp)}</td>
|
|
80
|
+
<td className="num">{cell(median(g.map((x) => x.next)), f.int)}</td><td className="num">{cell(median(g.map((x) => x.turns)), f.int)}</td>
|
|
81
|
+
</tr> : null)}</tbody>
|
|
82
|
+
</table></div> : null}
|
|
83
|
+
</section>
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
const cell = (v: number | null, fmt: (n: number) => string) => (v === null ? "—" : fmt(v));
|
|
87
|
+
|
|
88
|
+
// The cost of reading the context by the context each request carried: rows add up to the overview's cost of reading the
|
|
89
|
+
// context, a part the host charged with no request included.
|
|
90
|
+
function Reading({ F, X }: { F: Fact[]; X: Ctx[] }) {
|
|
91
|
+
const { M, t, L, f } = useDash();
|
|
92
|
+
const E = M.edges, n = E.length + 1;
|
|
93
|
+
const b = new Array(n + 1).fill(0), q = new Array(n).fill(0);
|
|
94
|
+
for (const x of X) { x.b.forEach((v, j) => { b[j] += v; }); x.n.forEach((v, j) => { q[j] += v; }); }
|
|
95
|
+
const cost = b.reduce((a, v) => a + v, 0), reqs = q.reduce((a, v) => a + v, 0);
|
|
96
|
+
const head = <header><h2>{t("readingCtx")}</h2><span className="hint">{t("byCtx")}</span></header>;
|
|
97
|
+
if (!reqs) return <section className="card">{head}<Empty /></section>;
|
|
98
|
+
const k = (v: number) => f.cmp(v).replace(/K$/, "");
|
|
99
|
+
const label = (j: number) => (j === 0 ? L.under(f.cmp(E[0])) : j === n - 1 ? L.andOver(f.cmp(E[n - 2])) : `${f.cmp(E[j - 1]).endsWith("K") && f.cmp(E[j]).endsWith("K") ? k(E[j - 1]) : f.cmp(E[j - 1])}–${f.cmp(E[j])}`);
|
|
100
|
+
const at300 = E.indexOf(300e3) + 1, above = b.slice(at300, n).reduce((a, v) => a + v, 0), aboveQ = q.slice(at300).reduce((a, v) => a + v, 0);
|
|
101
|
+
const per = (j: number) => (q[j] ? b[j] / q[j] : 0), seen = q.map((v, j) => (v >= reqs * 0.01 ? j : -1)).filter((j) => j >= 0);
|
|
102
|
+
const top = seen.at(-1) ?? -1, base = seen.includes(2) ? 2 : seen[0] ?? -1;
|
|
103
|
+
const ctx = F.reduce((a, x) => a + x.i + x.cr + x.cw, 0) / (F.reduce((a, x) => a + x.q, 0) || 1);
|
|
104
|
+
const max = Math.max(...b.slice(0, n));
|
|
105
|
+
return (
|
|
106
|
+
<section className="card">
|
|
107
|
+
{head}
|
|
108
|
+
<Stats items={[
|
|
109
|
+
{ label: t("ctxPerReq"), value: f.cmp(ctx), sub: t("onAverage") },
|
|
110
|
+
{ label: L.above(f.cmp(300e3)), value: f.pct(cost ? above / cost : 0), sub: L.ofCostReqs(f.pct(aboveQ / reqs)) },
|
|
111
|
+
...(top >= 0 && base >= 0 && top !== base && per(base) ? [{ label: L.perReqAt(label(top)), value: f.times(per(top) / per(base)), sub: L.whatAt(label(base)) }] : []),
|
|
112
|
+
]} />
|
|
113
|
+
<div className="scroll"><table>
|
|
114
|
+
<thead><tr><th>{t("ctxCol")}</th><th className="cell-bar" /><th className="num o1">{t("requests")}</th><th className="num">{t("cost")}</th><th className="num">{t("perReqCol")}</th></tr></thead>
|
|
115
|
+
<tbody>
|
|
116
|
+
{q.map((v, j) => (v ? <tr key={j}>
|
|
117
|
+
<td className="name">{label(j)}</td><td className="cell-bar"><span className="bar1" style={{ width: `${max ? (b[j] / max) * 100 : 0}%` }} /></td>
|
|
118
|
+
<td className="num o1">{f.int(v)}</td><td className="num">{f.usd(b[j])}</td><td className="num"><b>{f.usd3(per(j))}</b></td>
|
|
119
|
+
</tr> : null))}
|
|
120
|
+
{b[n] >= cost * 0.005 ? <tr><td className="name">{t("noRequest")}</td><td className="cell-bar" /><td className="num o1">—</td><td className="num">{f.usd(b[n])}</td><td className="num">—</td></tr> : null}
|
|
121
|
+
</tbody>
|
|
122
|
+
<tfoot><tr><td>{t("all")}</td><td /><td className="num o1">{f.int(reqs)}</td><td className="num">{f.usd(cost)}</td><td className="num">{f.usd3(cost / reqs)}</td></tr></tfoot>
|
|
123
|
+
</table></div>
|
|
124
|
+
</section>
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// The main threads replayed with compaction at each point, against the point it fires at now. A point within NEAR of now
|
|
129
|
+
// gives way to it; when the models in the filter fire at different points now, now has a row of its own.
|
|
130
|
+
function Replay({ C, X }: { C: Compaction[]; X: Ctx[] }) {
|
|
131
|
+
const { M, t, L, f } = useDash();
|
|
132
|
+
const [scope, setScope] = useState<"all" | "long">("all");
|
|
133
|
+
const counts = new Map<number, number>();
|
|
134
|
+
for (const x of M.compactions) if (!x.sub) counts.set(x.s, (counts.get(x.s) ?? 0) + 1);
|
|
135
|
+
const long = (s: number) => (counts.get(s) ?? 0) >= LONG;
|
|
136
|
+
const rows = X.filter((x) => x.r && (scope === "all" || long(x.s))), real = C.filter((x) => !x.sub && (scope === "all" || long(x.s)));
|
|
137
|
+
const P = M.points, NOW = P.length;
|
|
138
|
+
// Models whose points sit within NEAR of each other fire at one point: the point of the model with the most requests.
|
|
139
|
+
const reqs = new Map<number, number>();
|
|
140
|
+
for (const x of rows) if (M.fires[x.m]) reqs.set(M.fires[x.m]!, (reqs.get(M.fires[x.m]!) ?? 0) + x.n.reduce((a, v) => a + v, 0));
|
|
141
|
+
const levels = [...reqs.keys()].sort((a, b) => a - b);
|
|
142
|
+
const one = levels.length && levels.at(-1)! <= levels[0] * (1 + NEAR) ? [...reqs].sort((a, b) => b[1] - a[1])[0][0] : null;
|
|
143
|
+
const point = (j: number) => {
|
|
144
|
+
const v = [0, 0, 0, 0], sess = new Set<number>();
|
|
145
|
+
for (const x of rows) { for (let k = 0; k < PARTS; k++) v[k] += x.r![j * PARTS + k]; if (x.r![j * PARTS] > 0) sess.add(x.s); }
|
|
146
|
+
return { n: v[0], sess: sess.size, parts: [v[1], v[2], v[3]], total: v[1] + v[2] + v[3] };
|
|
147
|
+
};
|
|
148
|
+
const shown = [
|
|
149
|
+
...P.map((p, j) => ({ j, at: p, now: false })).filter((x) => !(one && Math.abs(x.at - one) <= one * NEAR)),
|
|
150
|
+
...(levels.length ? [{ j: NOW, at: one ?? levels[0], now: true }] : []),
|
|
151
|
+
].sort((a, b) => a.at - b.at || Number(a.now) - Number(b.now)).map((x) => ({ ...x, ...point(x.j) }))
|
|
152
|
+
// Past the first point at which nothing compacts, the rows repeat it, save the default.
|
|
153
|
+
.filter((x, i, all) => x.now || x.j === NOW - 1 || x.n > 0 || !all.slice(0, i).some((y) => y.n === 0));
|
|
154
|
+
const base = shown.find((x) => x.now) ?? null, max = Math.max(1, ...shown.map((x) => x.total));
|
|
155
|
+
const kinds: [string, string][] = [["ck0", t("readingCtx")], ["ck1", t("compacting")], ["ck2", t("rebuildIdle")]];
|
|
156
|
+
const name = (x: (typeof shown)[number]) => (x.now ? (one ? f.cmp(round5(one)) : levels.map((v) => f.cmp(round5(v))).join(" · ")) : f.cmp(x.at));
|
|
157
|
+
return (
|
|
158
|
+
<section className="card">
|
|
159
|
+
<header>
|
|
160
|
+
<div className="ttl"><h2>{t("otherPoints")}</h2>
|
|
161
|
+
<button type="button" className="est" data-tip={tip(t("estimate"), L.estNotes.map((s): TipRow => ["", s, ""]))}>{t("estimate")}</button></div>
|
|
162
|
+
<div className="segs"><div className="seg" role="group">{(["all", "long"] as const).map((k) => <button key={k} aria-pressed={scope === k} onClick={() => setScope(k)}>{t(k === "all" ? "allSessions" : "long4")}</button>)}</div></div>
|
|
163
|
+
</header>
|
|
164
|
+
{rows.length ? <>
|
|
165
|
+
<ul className="legend">{kinds.map(([c, n]) => <li key={c}><i className={c} />{n}</li>)}</ul>
|
|
166
|
+
<div className="scroll"><table>
|
|
167
|
+
<thead><tr><th>{t("compactsAtCol")}</th><th className="num">{t("compactions")}</th><th className="num o2">{t("sessCompacted")}</th><th className="cell-bar o1" /><th className="num">{t("cost")}</th>{base ? <th className="num">{t("againstNow")}</th> : null}</tr></thead>
|
|
168
|
+
<tbody>{shown.map((x) => {
|
|
169
|
+
const d = base ? x.total - base.total : 0;
|
|
170
|
+
const rowsTip: TipRow[] = [...kinds.map(([c, n], k): TipRow => [c, n, f.usd(x.parts[k])]), ...(x.now ? [["", t("recorded"), L.recordedVal(f.int(real.length), f.usd(compacting(real)))] as TipRow] : [])];
|
|
171
|
+
return <tr key={`${x.j}`} className={x.now ? "now" : ""} data-tip={tip(`${t("compactsAtCol")} ${name(x)}`, rowsTip)}>
|
|
172
|
+
<td className="lbl">{name(x)}{x.now ? <span className="chip">{t("now")}</span> : null}{!x.now && x.at === P.at(-1) ? <span className="sub">{t("byDefault")}</span> : null}</td>
|
|
173
|
+
<td className="num">{f.int(x.n)}</td><td className="num o2">{f.int(x.sess)}</td>
|
|
174
|
+
<td className="cell-bar o1"><div className="split kinds" style={{ width: `${(x.total / max) * 100}%` }}>{x.parts.map((v, k) => <i key={k} className={kinds[k][0]} style={{ flex: v }} />)}</div></td>
|
|
175
|
+
<td className="num">{f.usd(x.total)}</td>
|
|
176
|
+
{base ? <td className="num">{x.now ? "—" : <span className={d < 0 ? "better" : d > 0 ? "worse" : ""}>{d < 0 ? "−" : "+"}{f.usd(Math.abs(d))}</span>}</td> : null}
|
|
177
|
+
</tr>;
|
|
178
|
+
})}</tbody>
|
|
179
|
+
</table></div>
|
|
180
|
+
</> : <Empty />}
|
|
181
|
+
</section>
|
|
182
|
+
);
|
|
183
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { useDash } from "../dash.ts";
|
|
2
|
+
import { behavior, costParts, mainLevel, totals, vsHigh, type Behavior, type Fact } from "../model.ts";
|
|
3
|
+
import { Disclosure } from "../controls/Disclosure.tsx";
|
|
4
|
+
import { Empty, Info, Legend, Rank, Swatch, tip, useCostKinds, type TipRow } from "../ui.tsx";
|
|
5
|
+
|
|
6
|
+
// The model and effort story, top to bottom: which model the money goes to against the work it does, how each
|
|
7
|
+
// model works, what share each effort level takes, and what one request writes at each level.
|
|
8
|
+
// Picking a model opens it in the overview.
|
|
9
|
+
export function Models({ F }: { F: Fact[] }) {
|
|
10
|
+
const { M, t, f, set } = useDash();
|
|
11
|
+
const open = (n: string) => set({ view: "overview", model: n });
|
|
12
|
+
const all = totals(M, F);
|
|
13
|
+
const per = M.models.map((n, m) => ({ n, m, c: M.mColor(m), tt: totals(M, F.filter((y) => y.m === m)) })).filter((r) => r.tt.q);
|
|
14
|
+
// Each model's cost split by what it paid for; web search and the part the table cannot split are named only when some model has them.
|
|
15
|
+
const kinds = useCostKinds(), parts = (r: (typeof per)[number]) => costParts(r.tt.c, r.tt.k);
|
|
16
|
+
const shown = kinds.filter((_, j) => j < 3 || per.some((r) => parts(r)[j]));
|
|
17
|
+
|
|
18
|
+
if (!per.length) return <section className="card"><Empty /></section>;
|
|
19
|
+
// The model's whole cost as one bar, its parts labelled with their share when there is room; amounts in the tooltip.
|
|
20
|
+
const kindBar = (name: string, c: number, v: number[]) => {
|
|
21
|
+
const sum = v.reduce((a, b) => a + b, 0);
|
|
22
|
+
if (!sum) return "—";
|
|
23
|
+
const about = tip(`${name} · ${f.usd(c)}`, v.map((x, j): TipRow => [kinds[j].c, kinds[j].n, `${f.usd(x)} · ${f.pct(x / sum)}`]).filter((_, j) => v[j]));
|
|
24
|
+
return (
|
|
25
|
+
<span className="split kinds" tabIndex={0} data-tip={about}>
|
|
26
|
+
{v.map((x, j) => (x ? <i key={j} className={kinds[j].c} style={{ width: `${(x / sum) * 100}%` }}>{x / sum >= 0.12 ? f.pct(x / sum) : ""}</i> : null))}
|
|
27
|
+
</span>
|
|
28
|
+
);
|
|
29
|
+
};
|
|
30
|
+
return (
|
|
31
|
+
<div className="stack">
|
|
32
|
+
<section className="card">
|
|
33
|
+
<header><h2>{t("whereMoney")}</h2><Legend groups={shown} metric="sess" style={{ margin: 0 }} /></header>
|
|
34
|
+
<div className="scroll"><table className="money">
|
|
35
|
+
<thead><tr><th>{t("model")}</th><th className="num">{t("cost")}</th><th className="num">{t("share")}</th><th className="num o2">{t("perM")}</th><th className="num o2">{t("cache")}</th><th className="num o1">{t("ctxPerReq")}</th><th className="wide">{t("costKinds")}</th></tr></thead>
|
|
36
|
+
<tbody>{per.map((r) => {
|
|
37
|
+
const tokens = r.tt.i + r.tt.cr + r.tt.cw + r.tt.o;
|
|
38
|
+
return (
|
|
39
|
+
<tr key={r.m} className="go" onClick={() => open(r.n)}>
|
|
40
|
+
<th className="name"><Swatch c={r.c} /><button className="rowlink" onClick={(e) => { e.stopPropagation(); open(r.n); }}>{r.n}</button></th>
|
|
41
|
+
<td className="num"><b>{f.usd(r.tt.c)}</b></td>
|
|
42
|
+
<td className="num">{f.pct(r.tt.c / (all.c || 1))}</td>
|
|
43
|
+
<td className="num o2">{tokens ? f.usd((r.tt.c / tokens) * 1e6) : "—"}</td>
|
|
44
|
+
<td className="num o2">{r.tt.q ? f.pct(r.tt.hit) : "—"}</td>
|
|
45
|
+
<td className="num o1">{r.tt.q ? f.cmp(r.tt.ctx) : "—"}</td>
|
|
46
|
+
<td className="wide" onClick={(e) => e.stopPropagation()}>{kindBar(r.n, r.tt.c, parts(r))}</td>
|
|
47
|
+
</tr>
|
|
48
|
+
);
|
|
49
|
+
})}</tbody>
|
|
50
|
+
</table></div>
|
|
51
|
+
<Disclosure label={t("figures")}>
|
|
52
|
+
<div className="scroll"><table>
|
|
53
|
+
<thead><tr><th>{t("model")}</th><th className="num">{t("requests")}</th><th className="num">{t("input")}</th><th className="num">{t("out")}</th><th className="num">{t("thinking")}</th><th className="num">{t("cacheRead")}</th><th className="num">{t("cacheWrite")}</th><th className="num">{t("cost")}</th></tr></thead>
|
|
54
|
+
<tbody>{per.map((r) => (
|
|
55
|
+
<tr key={r.m}>
|
|
56
|
+
<th className="name"><Swatch c={r.c} />{r.n}</th>
|
|
57
|
+
<td className="num">{f.int(r.tt.q)}</td><td className="num">{f.cmp(r.tt.i)}</td><td className="num">{f.cmp(r.tt.o)}</td><td className="num">{f.pct(r.tt.th / (r.tt.o || 1))}</td>
|
|
58
|
+
<td className="num">{f.cmp(r.tt.cr)}</td><td className="num">{f.cmp(r.tt.cw)}</td><td className="num">{f.usd(r.tt.c)}</td>
|
|
59
|
+
</tr>
|
|
60
|
+
))}</tbody>
|
|
61
|
+
</table></div>
|
|
62
|
+
</Disclosure>
|
|
63
|
+
</section>
|
|
64
|
+
<Behaviors B={behavior(M, F)} />
|
|
65
|
+
<Efforts F={F} />
|
|
66
|
+
</div>
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// How each model works, one ranked panel per figure; a model without enough base for a figure is left out of it.
|
|
71
|
+
function Behaviors({ B }: { B: Behavior[] }) {
|
|
72
|
+
const { M, t, L, f } = useDash();
|
|
73
|
+
// Every model is listed; one without enough base for the figure comes last with a dash, and why() says what it lacks.
|
|
74
|
+
const panel = (title: string, pick: (b: Behavior) => number | null, show: (v: number) => string, base: (b: Behavior) => TipRow, why: (b: Behavior) => string) => {
|
|
75
|
+
const rows = [...B].sort((a, b) => (pick(b) ?? -1) - (pick(a) ?? -1));
|
|
76
|
+
return (
|
|
77
|
+
<div className="panel">
|
|
78
|
+
<h3>{title}</h3>
|
|
79
|
+
{rows.some((b) => pick(b) !== null) ? <Rank metric="out" color fmt={show} rows={rows.map((b) => ({ n: M.models[b.m], c: M.mColor(b.m), v: pick(b) ?? 0, x: "", tip: tip(M.models[b.m], [base(b)]), none: pick(b) === null ? why(b) : undefined }))} /> : <Empty />}
|
|
80
|
+
</div>
|
|
81
|
+
);
|
|
82
|
+
};
|
|
83
|
+
const fewReq = (b: Behavior) => L.fewReq(f.int(b.q));
|
|
84
|
+
if (!B.some((b) => b.out !== null)) return null;
|
|
85
|
+
const req = (b: Behavior): TipRow => [M.mColor(b.m), t("requests"), f.int(b.q)];
|
|
86
|
+
return (
|
|
87
|
+
<section className="card">
|
|
88
|
+
<header><h2>{t("behave")}</h2></header>
|
|
89
|
+
<div className="panels">
|
|
90
|
+
{panel(t("bOut"), (b) => b.out, f.cmp, req, fewReq)}
|
|
91
|
+
{panel(t("bThink"), (b) => b.think, f.pct, req, fewReq)}
|
|
92
|
+
{panel(t("bSteps"), (b) => b.steps, f.dec, (b) => [M.mColor(b.m), t("ownerTurns"), f.int(b.turns)], (b) => (b.main ? L.fewTurns(f.int(b.turns)) : t("onlySub")))}
|
|
93
|
+
{panel(t("bTools"), (b) => b.tools, f.dec, req, fewReq)}
|
|
94
|
+
{panel(t("bFail"), (b) => b.fail, f.pct, (b) => [M.mColor(b.m), t("tools"), f.int(b.calls)], (b) => L.fewCalls(f.int(b.calls)))}
|
|
95
|
+
{panel(t("bSpeed"), (b) => b.tps, (v) => `${f.int(v)} ${t("tps")}`, (b) => [M.mColor(b.m), t("oneModel"), f.int(b.timed)], (b) => L.fewAlone(b.timed))}
|
|
96
|
+
</div>
|
|
97
|
+
</section>
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Each effort level's share of sessions, requests, output, thinking and cost, with the level whose cost departs most from its requests named
|
|
102
|
+
// above; beside it, what one request writes at each level, model by model.
|
|
103
|
+
function Efforts({ F }: { F: Fact[] }) {
|
|
104
|
+
const { M, t, L, f } = useDash();
|
|
105
|
+
const lv = M.levels.filter((e) => F.some((x) => x.e === e));
|
|
106
|
+
if (!lv.length) return null;
|
|
107
|
+
const total = (pick: (x: Fact) => number) => lv.map((e) => F.reduce((n, x) => n + (x.e === e ? pick(x) : 0), 0));
|
|
108
|
+
const share = (arr: number[]) => { const sum = arr.reduce((a, b) => a + b, 0) || 1; return arr.map((v) => v / sum); };
|
|
109
|
+
const q = total((x) => x.q), o = total((x) => x.o), th = total((x) => x.th), c = total((x) => x.c), sq = share(q), so = share(o), sth = share(th), sc = share(c);
|
|
110
|
+
let k = 0;
|
|
111
|
+
lv.forEach((_, i) => { if (Math.abs(sc[i] - sq[i]) > Math.abs(sc[k] - sq[k])) k = i; });
|
|
112
|
+
const lead = lv.length > 1 ? <p className="lead accent" style={{ borderColor: `var(--v${lv[k]}, var(--s0))` }}>{(sc[k] >= sq[k] ? L.leadMore : L.leadLess)(M.efforts[lv[k]], f.pct(sq[k]), f.pct(sc[k]))}</p> : null;
|
|
113
|
+
const bar = (label: string, sh: number[], raw: number[], fm: (n: number) => string) => [
|
|
114
|
+
<span key={`l${label}`}>{label}</span>,
|
|
115
|
+
<div key={`b${label}`} className="split">{lv.map((e, i) => (sh[i] ? <i key={e} className={`v${e}`} style={{ width: `${sh[i] * 100}%` }} data-tip={tip(M.efforts[e], [["v" + e, label, fm(raw[i]) + " · " + f.pct(sh[i])]])}>{sh[i] >= 0.14 ? f.pct(sh[i]) : ""}</i> : null))}</div>,
|
|
116
|
+
];
|
|
117
|
+
// Sessions whose main thread changed level; the Sessions table draws their levels.
|
|
118
|
+
const ss = [...new Set(F.filter((x) => !x.sub).map((x) => x.s))].map((s) => M.sessions[s]), sw = ss.filter((x) => x.runs.length > 1);
|
|
119
|
+
// Each session once, under the level its main thread ran most.
|
|
120
|
+
const n = lv.map((e) => ss.filter((x) => x.runs.length && mainLevel(x.runs) === e).length), sn = share(n);
|
|
121
|
+
// What effort controls: how much a request writes (thinking included) at each level, model by model, and against
|
|
122
|
+
// the same model's high within the sessions that ran both, since most sessions keep one level and the levels would
|
|
123
|
+
// otherwise compare different work. Cost per request follows the context more than the effort, so it stays in the tooltip.
|
|
124
|
+
const price = M.models.map((n, m) => {
|
|
125
|
+
const cells = lv.map((e) => { const x = F.filter((y) => y.m === m && y.e === e); return { e, q: x.reduce((a, y) => a + y.q, 0), o: x.reduce((a, y) => a + y.o, 0), th: x.reduce((a, y) => a + y.th, 0), c: x.reduce((a, y) => a + y.c, 0) }; });
|
|
126
|
+
return { n, m, cells: cells.map((r) => ({ ...r, vs: r.e !== 2 && r.q ? vsHigh(F, m, r.e, 2) : null })) };
|
|
127
|
+
}).filter((r) => r.cells.some((x) => x.q));
|
|
128
|
+
const allC = c.reduce((a, b) => a + b, 0) || 1;
|
|
129
|
+
return (
|
|
130
|
+
<div className="grid half">
|
|
131
|
+
<section className="card">
|
|
132
|
+
<header><h2>{t("effShare")}</h2><span className="hint">{f.int(sw.length)} / {f.int(ss.length)} {t("switches")}</span></header>
|
|
133
|
+
{lead}
|
|
134
|
+
<ul className="legend">{lv.map((e) => <li key={e}><i className={`v${e}`} />{M.efforts[e]}</li>)}</ul>
|
|
135
|
+
<div className="shares">{bar(t("sess"), sn, n, f.int)}{bar(t("requests"), sq, q, f.int)}{bar(t("out"), so, o, f.cmp)}{bar(t("thinking"), sth, th, f.cmp)}{bar(t("cost"), sc, c, f.usd)}</div>
|
|
136
|
+
</section>
|
|
137
|
+
<section className="card">
|
|
138
|
+
<header><div className="ttl"><h2>{t("price")}</h2><Info label={t("howToRead")}><p>{t("priceInfo")}</p></Info></div><span className="hint">token</span></header>
|
|
139
|
+
<div className="scroll"><table className="price">
|
|
140
|
+
<thead><tr><th>{t("model")}</th>{lv.map((e) => <th key={e} className="num"><span className="lv"><i className={`v${e}`} />{M.efforts[e]}</span></th>)}</tr></thead>
|
|
141
|
+
<tbody>{price.map((r) => (
|
|
142
|
+
<tr key={r.m}>
|
|
143
|
+
<th className="name"><Swatch c={M.mColor(r.m)} />{r.n}</th>
|
|
144
|
+
{r.cells.map((x) => (x.q ? (
|
|
145
|
+
<td key={x.e} className="num" tabIndex={0} data-tip={tip(r.n + " · " + M.efforts[x.e], [["v" + x.e, t("requests"), f.int(x.q)], ["v" + x.e, t("thinking") + " " + t("perReq"), f.cmp(x.th / x.q)], ["v" + x.e, t("cost") + " " + t("perReq"), f.usd3(x.c / x.q)], ["v" + x.e, t("costShare"), f.pct(x.c / allC)], ...(x.vs ? [["v" + x.e, `${t("vsLevel")} ${M.efforts[2]}`, x.vs.r ? `${f.times(x.vs.r)} · ${L.nBoth(x.vs.n)}` : L.fewBoth(x.vs.n)] as TipRow] : [])])}>
|
|
146
|
+
<span className="name">{f.cmp(x.o / x.q)}</span><span className={`sub${(x.vs?.r ?? 0) >= 1.2 ? " up" : ""}`}>{x.vs?.r ? `${f.times(x.vs.r)} ${M.efforts[2]}` : "\u00a0"}</span>
|
|
147
|
+
</td>
|
|
148
|
+
) : <td key={x.e} className="num x1"><span className="name">—</span><span className="sub">{"\u00a0"}</span></td>))}
|
|
149
|
+
</tr>
|
|
150
|
+
))}</tbody>
|
|
151
|
+
</table></div>
|
|
152
|
+
</section>
|
|
153
|
+
</div>
|
|
154
|
+
);
|
|
155
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
import { useDash } from "../dash.ts";
|
|
3
|
+
import { byDay, byDayKinds, dayAdd, group, mainLevel, sessionRows, slice, slotsOf, totals, type Fact } from "../model.ts";
|
|
4
|
+
import { Disclosure } from "../controls/Disclosure.tsx";
|
|
5
|
+
import { ChartSeg, Columns, CostCell, Delta, Empty, Kpi, Legend, MetricSeg, Rank, SessionMix, SplitSeg, Title, tip, useCostFlags, useCostKinds, type Side } from "../ui.tsx";
|
|
6
|
+
|
|
7
|
+
export function Overview({ F, list }: { F: Fact[]; list: string[] }) {
|
|
8
|
+
const { M, S, filter, t, L, f, set } = useDash();
|
|
9
|
+
const now = totals(M, F), len = list.length, pf = dayAdd(filter.from, -len);
|
|
10
|
+
const prev = pf >= M.first ? totals(M, slice(M, filter, pf, dayAdd(filter.from, -1))) : null;
|
|
11
|
+
const delta = (a: number, b: number | undefined, side: Side, points = false) => <Delta a={a} b={prev ? b : undefined} side={side} points={points} />;
|
|
12
|
+
// What the cache saved follows how much work was done, so its line shows the share of cost spent reading the context instead.
|
|
13
|
+
const reread = (x: ReturnType<typeof totals>) => x.k[0] / (x.c || 1);
|
|
14
|
+
const daily = (pick: (x: Fact[]) => number) => list.map((d) => pick(F.filter((x) => x.d === d)));
|
|
15
|
+
const tile = (label: string, value: ReactNode, d: ReactNode, series: number[]) => <Kpi label={label} value={value} d={d} series={series} />;
|
|
16
|
+
const metric = S.metric, flags = useCostFlags(F);
|
|
17
|
+
// The chart splits by model, project (the five largest and the rest) or effort level, and cost also by what it paid for;
|
|
18
|
+
// web search and the part that cannot be split show only when present. Sessions count once, under their main model
|
|
19
|
+
// and main level, so the stack adds up to the sessions.
|
|
20
|
+
const kinds = useCostKinds(), split = (S.split === "kind" && metric !== "cost") || (S.split === "provider" && M.providers.length <= 1) ? "model" : S.split;
|
|
21
|
+
const slots = slotsOf(list), hourly = slots !== list, sess = metric === "sess";
|
|
22
|
+
const kindRows = split === "kind" ? byDayKinds(F, slots) : [];
|
|
23
|
+
const keep = kinds.map((_, j) => j < 3 || kindRows.some((r) => r[j]));
|
|
24
|
+
const shown = M.projectRank.slice(0, 5), lv = M.levels.filter((e) => F.some((x) => x.e === e));
|
|
25
|
+
const by = {
|
|
26
|
+
model: { groups: M.models.map((n, i) => ({ n, c: M.mColor(i) })), key: (x: Fact) => (sess && M.sessions[x.s].parent >= 0 ? M.sessions[x.s].parent : x.m) },
|
|
27
|
+
provider: { groups: M.providers.map((n, i) => ({ n, c: M.prColor(i) })), key: (x: Fact) => M.sessions[x.s].pr ?? 0 },
|
|
28
|
+
project: { groups: [...shown.map((p) => ({ n: M.projects[p], c: M.pColor(p) })), { n: t("other"), c: "c0" }], key: (x: Fact) => { const k = shown.indexOf(M.sessions[x.s].p); return k < 0 ? 5 : k; } },
|
|
29
|
+
effort: { groups: lv.map((e) => ({ n: M.efforts[e], c: "v" + e })), key: (x: Fact) => { const r = M.sessions[x.s].runs; return lv.indexOf(sess && r.length ? mainLevel(r) : x.e); } },
|
|
30
|
+
};
|
|
31
|
+
const groups = split === "kind" ? kinds.filter((_, j) => keep[j]) : by[split].groups;
|
|
32
|
+
const rows = split === "kind" ? kindRows.map((r) => r.filter((_, j) => keep[j])) : byDay(F, slots, by[split].key, groups, metric);
|
|
33
|
+
const sums = groups.map((_, k) => rows.reduce((n, r) => n + r[k], 0));
|
|
34
|
+
const activeCols = groups.map((g, k) => ({ g, k })).filter(({ k }) => sums[k] > 0);
|
|
35
|
+
const gp = group(F, (x) => M.sessions[x.s].p, metric);
|
|
36
|
+
const projects = [...gp].map(([p, v]) => ({ n: M.projects[p], v, onClick: () => set({ project: M.projects[p] }) })).sort((a, b) => b.v - a.v).slice(0, 6);
|
|
37
|
+
const top = sessionRows(M, F).sort((a, b) => (metric === "cost" ? b.c - a.c : b.o - a.o)).slice(0, 6);
|
|
38
|
+
return (
|
|
39
|
+
<div className="stack">
|
|
40
|
+
<div className="grid kpis">
|
|
41
|
+
{tile(t("out"), f.cmp(now.o), delta(now.o, prev?.o, "none"), daily((x) => x.reduce((n, y) => n + y.o, 0)))}
|
|
42
|
+
{tile(t("costList"), <>{f.usd(now.c)}{flags.length ? <sup className="flag" tabIndex={0} data-tip={tip(t("costNotes"), flags)}>*</sup> : null}</>, delta(now.c, prev?.c, "none"), daily((x) => x.reduce((n, y) => n + y.c, 0)))}
|
|
43
|
+
{tile(t("sess"), f.int(now.n), delta(now.n, prev?.n, "none"), daily((x) => new Set(x.map((y) => y.s)).size))}
|
|
44
|
+
{tile(t("cache"), f.pct(now.hit), delta(now.hit, prev?.hit, "up", true), daily((x) => totals(M, x).hit))}
|
|
45
|
+
{tile(t("saved"), <span tabIndex={0} data-tip={tip(t("savedTip"), [])}>{f.usd(now.sv)}</span>, <>{L.reread(f.pct(reread(now)))} <Delta a={reread(now)} b={prev ? reread(prev) || undefined : undefined} side="down" points bare /></>, daily((x) => x.reduce((n, y) => n + y.sv, 0)))}
|
|
46
|
+
{tile(t("speed"), <>{f.int(now.tps)} <small>{t("tps")}</small></>, delta(now.tps, prev?.tps, "up"), daily((x) => totals(M, x).tps))}
|
|
47
|
+
</div>
|
|
48
|
+
<section className="card">
|
|
49
|
+
<header><h2>{hourly ? t("perHour") : t("perDay")}</h2><div className="segs"><MetricSeg /><SplitSeg split={split} /><ChartSeg /></div></header>
|
|
50
|
+
<Legend groups={groups} sums={sums} metric={metric} />
|
|
51
|
+
<Columns list={slots} rows={rows} groups={groups} metric={metric} />
|
|
52
|
+
<Disclosure label={hourly ? t("hourlyTable") : t("dailyTable")}>
|
|
53
|
+
<div className="scroll"><table>
|
|
54
|
+
<thead><tr><th>{hourly ? t("hour") : t("day")}</th>{activeCols.map(({ g }) => <th key={g.n} className="num">{g.n}</th>)}{activeCols.length > 1 ? <th className="num">{t("total")}</th> : null}</tr></thead>
|
|
55
|
+
<tbody>{slots.map((d, i) => (
|
|
56
|
+
<tr key={d}><th>{hourly ? d + ":00" : f.dshort(d as string)}</th>{activeCols.map(({ k }) => <td key={k} className="num">{rows[i][k] ? f.fmt(metric, rows[i][k]) : "—"}</td>)}{activeCols.length > 1 ? <td className="num name">{f.fmt(metric, activeCols.reduce((a, { k }) => a + rows[i][k], 0))}</td> : null}</tr>
|
|
57
|
+
))}</tbody>
|
|
58
|
+
</table></div>
|
|
59
|
+
</Disclosure>
|
|
60
|
+
</section>
|
|
61
|
+
<div className="grid two">
|
|
62
|
+
<section className="card">
|
|
63
|
+
<header><h2>{t("topSessions")}</h2><button className="link" onClick={() => set({ view: "sessions" })}>{t("seeAll")} →</button></header>
|
|
64
|
+
<div className="scroll"><table>
|
|
65
|
+
<thead><tr><th className="o1">{t("project")}</th><th>{t("title")}</th><th className="o1">{t("start")}</th><th className="num o1">{t("dur")}</th><th className="o2">{t("model")}</th><th className="num">{t("out")}</th><th className="num">{t("cost")}</th></tr></thead>
|
|
66
|
+
<tbody>{top.length ? top.map((r) => (
|
|
67
|
+
<tr key={r.s}>
|
|
68
|
+
<th className="name o1">{M.projects[r.x.p]}</th>
|
|
69
|
+
<td><Title title={r.x.title} /><span className="sub ph">{M.projects[r.x.p]} · {f.dur(r.x.dur)}</span></td>
|
|
70
|
+
<td className="o1">{f.startText(r.x)}</td>
|
|
71
|
+
<td className="num o1">{f.dur(r.x.dur)}</td>
|
|
72
|
+
<td className="o2">{M.models[r.x.parent] ?? ""}<SessionMix r={r} /></td>
|
|
73
|
+
<td className="num">{f.cmp(r.o)}</td>
|
|
74
|
+
<td className="num"><CostCell r={r} /></td>
|
|
75
|
+
</tr>
|
|
76
|
+
)) : <tr><td colSpan={7} className="empty">{t("nodata")}</td></tr>}</tbody>
|
|
77
|
+
</table></div>
|
|
78
|
+
</section>
|
|
79
|
+
<section className="card"><header><h2>{t("topProjects")}</h2><button className="link" onClick={() => set({ view: "projects" })}>{t("seeAll")} →</button></header>{projects.length ? <Rank rows={projects} metric={metric} /> : <Empty />}</section>
|
|
80
|
+
</div>
|
|
81
|
+
</div>
|
|
82
|
+
);
|
|
83
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { useLayoutEffect, useRef, useState, type ReactNode } from "react";
|
|
2
|
+
import { useDash } from "../dash.ts";
|
|
3
|
+
import { totals, type Fact, type Model, type Session } from "../model.ts";
|
|
4
|
+
import { squarify } from "../treemap.ts";
|
|
5
|
+
import { Mag, MetricSeg, Swatch, tip } from "../ui.tsx";
|
|
6
|
+
|
|
7
|
+
const at = (s: Session) => s.day + String(s.hour).padStart(2, "0") + String(s.min).padStart(2, "0");
|
|
8
|
+
|
|
9
|
+
// What a set of facts adds up to, per session where the figure is the session's (length, turns, compactions,
|
|
10
|
+
// lines): a session that crosses the window's edge counts whole.
|
|
11
|
+
function sum(M: Model, F: Fact[]) {
|
|
12
|
+
const ss = [...new Set(F.map((x) => x.s))].map((s) => M.sessions[s]);
|
|
13
|
+
const add = (k: (s: Session) => number) => ss.reduce((n, s) => n + k(s), 0);
|
|
14
|
+
return { tt: totals(M, F), n: ss.length, dur: add((s) => s.dur), owner: add((s) => s.owner), compact: add((s) => s.compact), lines: add((s) => s.la + s.lr), last: ss.reduce<Session | null>((a, s) => (!a || at(s) > at(a) ? s : a), null) };
|
|
15
|
+
}
|
|
16
|
+
type Sum = ReturnType<typeof sum>;
|
|
17
|
+
const ratio = (a: number, b: number) => (b ? a / b : NaN);
|
|
18
|
+
|
|
19
|
+
// Projects side by side: the share each takes of the chosen metric and how they compare.
|
|
20
|
+
// The project filter does not hide the others here; picking a project opens it in the overview.
|
|
21
|
+
export function Projects({ F }: { F: Fact[] }) {
|
|
22
|
+
const { M, t, f, set } = useDash();
|
|
23
|
+
const [order, setOrder] = useState<{ k: string; dir: 1 | -1 }>({ k: "cost", dir: -1 });
|
|
24
|
+
const open = (p: number) => set({ view: "overview", project: M.projects[p] });
|
|
25
|
+
|
|
26
|
+
const by = new Map<number, Fact[]>();
|
|
27
|
+
for (const x of F) { const p = M.sessions[x.s].p; by.set(p, [...(by.get(p) ?? []), x]); }
|
|
28
|
+
const rows = [...by].map(([p, pf]) => ({ p, ...sum(M, pf) })), all = sum(M, F);
|
|
29
|
+
|
|
30
|
+
type Col = { k: string; label: string; cls: string; get: (r: Sum & { p?: number }) => number | string; show: (v: number) => ReactNode };
|
|
31
|
+
const columns: Col[] = [
|
|
32
|
+
{ k: "last", label: t("lastActive"), cls: "o1", get: (r) => (r.last ? at(r.last) : ""), show: () => null },
|
|
33
|
+
{ k: "sess", label: t("sess"), cls: "num", get: (r) => r.n, show: f.int },
|
|
34
|
+
{ k: "cost", label: t("cost"), cls: "num", get: (r) => r.tt.c, show: f.usd },
|
|
35
|
+
{ k: "cps", label: t("costPerSess"), cls: "num o1", get: (r) => ratio(r.tt.c, r.n), show: f.usd },
|
|
36
|
+
{ k: "dur", label: t("avgDur"), cls: "num o2", get: (r) => ratio(r.dur, r.n), show: f.dur },
|
|
37
|
+
{ k: "turns", label: t("turnsPerSess"), cls: "num o2", get: (r) => ratio(r.owner, r.n), show: f.dec },
|
|
38
|
+
{ k: "compact", label: t("compactPerSess"), cls: "num o2", get: (r) => ratio(r.compact, r.n), show: f.dec },
|
|
39
|
+
{ k: "hit", label: t("cache"), cls: "num o2", get: (r) => r.tt.hit, show: f.pct },
|
|
40
|
+
{ k: "kloc", label: t("costPerKLines"), cls: "num o2", get: (r) => ratio(r.tt.c * 1000, r.lines), show: f.usd },
|
|
41
|
+
];
|
|
42
|
+
const col = columns.find((c) => c.k === order.k);
|
|
43
|
+
const key = (r: (typeof rows)[number]) => (order.k === "project" ? M.projects[r.p].toLowerCase() : col!.get(r));
|
|
44
|
+
// Missing ratios sort last either way.
|
|
45
|
+
rows.sort((a, b) => { const x = key(a), y = key(b); if (Number.isNaN(x)) return 1; if (Number.isNaN(y)) return -1; return (x < y ? -1 : x > y ? 1 : 0) * order.dir; });
|
|
46
|
+
const sortBy = (k: string) => setOrder(order.k === k ? { k, dir: order.dir === 1 ? -1 : 1 } : { k, dir: k === "project" ? 1 : -1 });
|
|
47
|
+
const th = (k: string, label: string, cls: string) => (
|
|
48
|
+
<th key={k} className={cls} aria-sort={order.k === k ? (order.dir > 0 ? "ascending" : "descending") : undefined}>
|
|
49
|
+
<button onClick={() => sortBy(k)}>{label}{order.k === k ? (order.dir > 0 ? "\u00a0↑" : "\u00a0↓") : ""}</button>
|
|
50
|
+
</th>
|
|
51
|
+
);
|
|
52
|
+
// Sessions and cost carry a bar against the largest project; the ratios do not, since they do not add up.
|
|
53
|
+
const most = { sess: Math.max(0, ...rows.map((r) => r.n)), cost: Math.max(0, ...rows.map((r) => r.tt.c)) };
|
|
54
|
+
const bar = (c: Col, r: Sum) => (c.k === "sess" || c.k === "cost" ? <Mag v={c.get(r) as number} max={most[c.k]}>{cell(c, r)}</Mag> : cell(c, r));
|
|
55
|
+
const cell = (c: Col, r: Sum) => {
|
|
56
|
+
if (c.k === "last") return r.last ? f.startText(r.last) : "—";
|
|
57
|
+
const v = c.get(r) as number;
|
|
58
|
+
return Number.isFinite(v) ? c.show(v) : "—";
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
return (
|
|
62
|
+
<div className="stack">
|
|
63
|
+
<section className="card">
|
|
64
|
+
<header><h2>{t("shareBy")}</h2><MetricSeg /></header>
|
|
65
|
+
<Treemap rows={rows} open={open} />
|
|
66
|
+
</section>
|
|
67
|
+
<section className="card">
|
|
68
|
+
<div className="scroll"><table className="compare">
|
|
69
|
+
<thead><tr>{th("project", t("project"), "")}{columns.map((c) => th(c.k, c.label, c.cls))}</tr></thead>
|
|
70
|
+
<tbody>
|
|
71
|
+
{rows.length ? rows.map((r) => (
|
|
72
|
+
<tr key={r.p} className="go" onClick={() => open(r.p)}>
|
|
73
|
+
<th className="name"><Swatch c={M.pColor(r.p)} /><button className="rowlink" onClick={(e) => { e.stopPropagation(); open(r.p); }}>{M.projects[r.p]}</button></th>
|
|
74
|
+
{columns.map((c) => <td key={c.k} className={c.cls}>{bar(c, r)}</td>)}
|
|
75
|
+
</tr>
|
|
76
|
+
)) : <tr><td colSpan={columns.length + 1} className="empty">{t("nodata")}</td></tr>}
|
|
77
|
+
</tbody>
|
|
78
|
+
{rows.length > 1 ? <tfoot><tr><th>{t("allProj")}</th>{columns.map((c) => <td key={c.k} className={c.cls}>{cell(c, all)}</td>)}</tr></tfoot> : null}
|
|
79
|
+
</table></div>
|
|
80
|
+
</section>
|
|
81
|
+
</div>
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Each project as a rectangle sized by the chosen metric; small ones keep their name in the tooltip.
|
|
86
|
+
function Treemap({ rows, open }: { rows: ({ p: number } & Sum)[]; open: (p: number) => void }) {
|
|
87
|
+
const { M, S, t, f } = useDash();
|
|
88
|
+
const box = useRef<HTMLDivElement>(null);
|
|
89
|
+
const [w, setW] = useState(0);
|
|
90
|
+
useLayoutEffect(() => {
|
|
91
|
+
const el = box.current!, ro = new ResizeObserver(() => setW(el.clientWidth));
|
|
92
|
+
ro.observe(el); setW(el.clientWidth);
|
|
93
|
+
return () => ro.disconnect();
|
|
94
|
+
}, []);
|
|
95
|
+
// Tall enough that mid-sized projects get a labelled tile: about 0.55 of the width, kept between 300 and 700 px.
|
|
96
|
+
const h = w < 640 ? 300 : Math.round(Math.min(700, Math.max(340, w * 0.55)));
|
|
97
|
+
const value = (r: Sum) => (S.metric === "cost" ? r.tt.c : S.metric === "sess" ? r.n : r.tt.o);
|
|
98
|
+
const items = rows.map((r) => ({ p: r.p, v: value(r) })).filter((x) => x.v > 0).sort((a, b) => b.v - a.v);
|
|
99
|
+
const total = items.reduce((n, x) => n + x.v, 0), rects = squarify(items.map((x) => x.v), { x: 0, y: 0, w, h });
|
|
100
|
+
return (
|
|
101
|
+
<div className="tree" ref={box} style={{ height: items.length ? h : "auto" }}>
|
|
102
|
+
{!items.length ? <div className="empty">{t("nodata")}</div> : w ? items.map((x, k) => {
|
|
103
|
+
const r = rects[k], c = M.pColor(x.p), name = M.projects[x.p], text = `${f.fmt(S.metric, x.v)} · ${f.pct(x.v / total)}`;
|
|
104
|
+
const label = r.w > 84 && r.h > 46;
|
|
105
|
+
// A tile thinner than its own border cannot be seen or clicked; the table below still lists the project.
|
|
106
|
+
if (r.w < 4 || r.h < 4) return null;
|
|
107
|
+
return (
|
|
108
|
+
<button key={x.p} className={`tile ${c}`} style={{ left: r.x, top: r.y, width: r.w, height: r.h, borderRadius: Math.min(10, r.w / 3, r.h / 3), padding: label ? undefined : 0 }}
|
|
109
|
+
aria-label={`${name}: ${text}`} data-tip={label ? undefined : tip(name, [[c, t(S.metric), text]])} onClick={() => open(x.p)}>
|
|
110
|
+
{label ? <><b>{name}</b><span>{text}</span></> : null}
|
|
111
|
+
</button>
|
|
112
|
+
);
|
|
113
|
+
}) : null}
|
|
114
|
+
</div>
|
|
115
|
+
);
|
|
116
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { useDash } from "../dash.ts";
|
|
2
|
+
import { mainLevel, sessionRows, type Fact, type SessionRow } from "../model.ts";
|
|
3
|
+
import type { SortKey } from "../state.ts";
|
|
4
|
+
import { Search } from "../controls/Search.tsx";
|
|
5
|
+
import { CostCell, SessionMix, RunsStrip, Title } from "../ui.tsx";
|
|
6
|
+
|
|
7
|
+
export function Sessions({ F }: { F: Fact[] }) {
|
|
8
|
+
const { M, S, t, L, f, filter, set, limit, more } = useDash();
|
|
9
|
+
const SORT: Record<SortKey, (r: SessionRow) => string | number> = {
|
|
10
|
+
start: (r) => r.x.day + String(r.x.hour).padStart(2, "0") + String(r.x.min).padStart(2, "0"),
|
|
11
|
+
dur: (r) => r.x.dur, turns: (r) => r.x.owner, out: (r) => r.o, cost: (r) => r.c, ctx: (r) => r.ctx,
|
|
12
|
+
};
|
|
13
|
+
const q = S.q.trim().toLowerCase();
|
|
14
|
+
const rows = sessionRows(M, F).filter((r) => !q || (r.x.title + " " + M.projects[r.x.p] + " " + (M.providers[r.x.pr ?? 0] ?? "")).toLowerCase().includes(q));
|
|
15
|
+
const key = SORT[S.sort];
|
|
16
|
+
rows.sort((a, b) => (key(a) < key(b) ? -1 : key(a) > key(b) ? 1 : 0) * S.dir);
|
|
17
|
+
const sort = (k: SortKey) => set(S.sort === k ? { dir: S.dir === 1 ? -1 : 1 } : { sort: k, dir: -1 });
|
|
18
|
+
const th = (k: SortKey, label: string, cls = "") => (
|
|
19
|
+
<th className={cls} aria-sort={S.sort === k ? (S.dir > 0 ? "ascending" : "descending") : undefined}>
|
|
20
|
+
<button onClick={() => sort(k)}>{label}{S.sort === k ? (S.dir > 0 ? " ↑" : " ↓") : ""}</button>
|
|
21
|
+
</th>
|
|
22
|
+
);
|
|
23
|
+
// A one-day window shows only the time each session started.
|
|
24
|
+
const when = (r: SessionRow) => filter.from === filter.to ? `${String(r.x.hour).padStart(2, "0")}:${String(r.x.min).padStart(2, "0")}` : f.startText(r.x);
|
|
25
|
+
return (
|
|
26
|
+
<section className="card">
|
|
27
|
+
<header><h2>{t("sessions")}</h2><Search value={S.q} placeholder={t("search")} clearLabel={t("clear")} onChange={(q) => set({ q }, { replace: true })} /></header>
|
|
28
|
+
<p className="hint" style={{ margin: "0 0 12px" }}>{f.int(Math.min(limit, rows.length))} / {f.int(rows.length)} {t("shown")}</p>
|
|
29
|
+
<div className="scroll"><table className="wraphead sesstable">
|
|
30
|
+
<thead><tr>{th("start", t("session"))}{th("dur", t("dur"), "num o1")}{th("turns", t("turns"), "num o2")}<th className="o1">{t("modelEffort")}</th>{th("ctx", t("ctxPerReq"), "num o2")}{th("out", t("out"), "num")}{th("cost", t("cost"), "num")}</tr></thead>
|
|
31
|
+
<tbody>{rows.length ? rows.slice(0, limit).map((r) => (
|
|
32
|
+
<tr key={r.s}>
|
|
33
|
+
<td>
|
|
34
|
+
<Title title={r.x.title} />
|
|
35
|
+
<span className="sub">{[M.providers.length > 1 ? M.providers[r.x.pr ?? 0] : "", M.projects[r.x.p], when(r), r.x.subs.length ? L.nSubs(r.x.subs.length) : "", r.lapses ? L.nLapses(r.lapses) : ""].filter(Boolean).map((p, k) => <span key={k}>{k ? " · " : ""}<span className="nw">{p}</span></span>)}</span>
|
|
36
|
+
</td>
|
|
37
|
+
<td className="num o1">{f.dur(r.x.dur)}</td>
|
|
38
|
+
<td className="num o2">{r.x.owner}</td>
|
|
39
|
+
<td className="o1">
|
|
40
|
+
{M.models[r.x.parent] ?? ""}
|
|
41
|
+
{r.x.runs.length ? <> · {M.efforts[mainLevel(r.x.runs)]}{r.x.runs.length > 1 ? <> <span className="x1">+{r.x.runs.length - 1}</span></> : null}</> : null}
|
|
42
|
+
<SessionMix r={r} />
|
|
43
|
+
{r.x.runs.length > 1 ? <RunsStrip runs={r.x.runs} /> : null}
|
|
44
|
+
</td>
|
|
45
|
+
<td className="num o2">{r.q ? f.cmp(r.ctx) : "—"}</td>
|
|
46
|
+
<td className="num">{f.cmp(r.o)}</td>
|
|
47
|
+
<td className="num"><CostCell r={r} /></td>
|
|
48
|
+
</tr>
|
|
49
|
+
)) : <tr><td colSpan={7} className="empty">{t("nodata")}</td></tr>}</tbody>
|
|
50
|
+
</table></div>
|
|
51
|
+
{rows.length > limit ? <p style={{ margin: "14px 0 0" }}><button className="link" onClick={more}>{t("more")}</button></p> : null}
|
|
52
|
+
</section>
|
|
53
|
+
);
|
|
54
|
+
}
|