216 lines
8.9 KiB
TypeScript
216 lines
8.9 KiB
TypeScript
// Browser-only fixture. Real Management/UsagePanel, no SSO, no production API.
|
||
import { createRoot } from "react-dom/client";
|
||
import Management from "../../src/components/Management";
|
||
import type { ClusterNode } from "../../src/types";
|
||
import type { ClaudeUsage, ClaudeTokenTotals, NodeMetrics, ClaudeSourceDay } from "@bbbug/shared/protocol";
|
||
import { claudeApiCostUsd, claudeTokenTotal } from "@bbbug/shared/protocol";
|
||
import type { ClaudeUsageSource } from "@bbbug/shared/claude_usage_source";
|
||
import { api } from "../../src/lib/api";
|
||
import { setLang } from "../../src/lib/i18n";
|
||
import "../../src/styles.css";
|
||
|
||
const now = Date.now();
|
||
|
||
function metrics(usage: ClaudeUsage | null, over: Partial<NodeMetrics> = {}): NodeMetrics {
|
||
return {
|
||
at: now - 8_000, cores: 10, cpuPct: 22, loadPct: 31, uptimeSec: 86_400,
|
||
diskUsedPct: 54, diskFreeBytes: 220e9, memTotalBytes: 32 * 2 ** 30,
|
||
memUsedPct: 61, swapUsedBytes: 1.2 * 2 ** 30,
|
||
slowAt: now - 20_000, slowError: null, slowMs: 240,
|
||
agent: { mode: "pty", loggedIn: true, authMethod: "claude.ai", checkedAt: now - 20_000, error: null, authGateMs: 0 },
|
||
claudeUsage: usage,
|
||
...over,
|
||
};
|
||
}
|
||
|
||
function node(over: Partial<ClusterNode> & { id: string; platform: ClusterNode["platform"] }): ClusterNode {
|
||
return {
|
||
baseUrl: `http://${over.id}:5180`,
|
||
status: "online", lastSeenAt: now - 4_000,
|
||
evalInFlight: 0, evalCapacity: 3, fixInFlight: 0, fixCapacity: 1,
|
||
version: "local-fixture", createdAt: now - 86400_000, live: true,
|
||
clusters: ["default"],
|
||
...over,
|
||
};
|
||
}
|
||
|
||
const day = 86_400_000;
|
||
|
||
/**
|
||
* A `days`-long daily series ending today, scaled off the lifetime totals so
|
||
* the window numbers stay plausible next to them.
|
||
*/
|
||
function dailySeries(tokens: ClaudeTokenTotals, days: number) {
|
||
const share = 1 / (days * 6);
|
||
return Array.from({ length: days }, (_, i) => {
|
||
const tick = { date: new Date(now - (days - 1 - i) * day).toISOString().slice(0, 10) };
|
||
const scale = share * (i % 5 === 0 ? 0 : 1);
|
||
const t: ClaudeTokenTotals = {
|
||
input: Math.round(tokens.input * scale),
|
||
output: Math.round(tokens.output * scale),
|
||
cacheRead: Math.round(tokens.cacheRead * scale),
|
||
cacheCreation: Math.round(tokens.cacheCreation * scale),
|
||
};
|
||
const apiCostUsd = claudeApiCostUsd(t, "sonnet");
|
||
return { ...tick, tokens: t, apiCostUsd, bySource: splitDay(claudeTokenTotal(t), apiCostUsd, i, days) };
|
||
});
|
||
}
|
||
|
||
/**
|
||
* [token share, API-price share] per source, from the 10-29 scan of the real
|
||
* fleet (evaluator-android-01 / fei-ios-01). The two columns differ on purpose:
|
||
* report generation is a sliver of the tokens but a real slice of the price.
|
||
*/
|
||
const SOURCE_MIX: [ClaudeUsageSource, number, number][] = [
|
||
["bugfix", 0.70, 0.66], ["feature", 0.13, 0.12], ["verify", 0.08, 0.075], ["eval", 0.055, 0.07],
|
||
["asking", 0.008, 0.012], ["asking_edit", 0.004, 0.006], ["report", 0.003, 0.03],
|
||
["human", 0.006, 0.008], ["rsi", 0.001, 0.01], ["eddy", 0.0005, 0.001],
|
||
];
|
||
|
||
/** Split one machine-day; 未识别 ramps up over the last ten days so the 增长 flag has something to flag. */
|
||
function splitDay(tokens: number, usd: number, i: number, days: number): ClaudeSourceDay[] {
|
||
if (tokens <= 0) return [];
|
||
const unknown = i >= days - 10 ? 0.004 * (i - (days - 11)) : 0.002;
|
||
const mix: [ClaudeUsageSource, number, number][] = [...SOURCE_MIX, ["unknown", unknown, unknown]];
|
||
const tw = mix.reduce((a, m) => a + m[1], 0);
|
||
const uw = mix.reduce((a, m) => a + m[2], 0);
|
||
return mix.map(([source, t, u], k) => ({
|
||
source,
|
||
tokens: Math.round(tokens * t / tw),
|
||
apiCostUsd: usd * u / uw,
|
||
sessions: Math.max(1, Math.round(((k * 7 + i) % 5) + t * 40)),
|
||
}));
|
||
}
|
||
|
||
function makeUsage(
|
||
{ coveredDays = 30, ...over }: Partial<ClaudeUsage> & {
|
||
tokens: ClaudeTokenTotals; tokensAsOf: string; startedAt: number; coveredDays?: number;
|
||
},
|
||
): ClaudeUsage {
|
||
return {
|
||
at: now - 35_000, ms: 290, error: null,
|
||
email: null, plan: "max", machineId: null,
|
||
fiveHour: null, sevenDay: null, sevenDaySonnet: null, sevenDayOpus: null,
|
||
quotaAt: null,
|
||
apiCostUsd: claudeApiCostUsd(over.tokens, "sonnet"),
|
||
daily: dailySeries(over.tokens, coveredDays),
|
||
dailyFromAt: now - (coveredDays - 1) * day,
|
||
...over,
|
||
};
|
||
}
|
||
const nodes: ClusterNode[] = [
|
||
// A Mac running two satellites: same machineId, same account. Both cards
|
||
// show the machine's tokens; the fleet totals count them once.
|
||
node({
|
||
id: "lemon-ios-01.shiro0.icu", platform: "ios",
|
||
metrics: metrics(makeUsage({
|
||
email: "lemon@bytedance.com", machineId: "mach-lemon",
|
||
fiveHour: { usedPct: 91, resetsAt: now + 48 * 60_000 },
|
||
sevenDay: { usedPct: 67, resetsAt: now + 2 * day },
|
||
quotaAt: now - 35_000,
|
||
tokens: { input: 12_400_000, output: 980_000, cacheRead: 88_000_000, cacheCreation: 6_200_000 },
|
||
tokensAsOf: "2026-09-12",
|
||
startedAt: now - 265 * day,
|
||
})),
|
||
}),
|
||
node({
|
||
id: "lemon-android-01.shiro0.icu", platform: "android",
|
||
metrics: metrics(makeUsage({
|
||
at: now - 41_000, ms: 310,
|
||
email: "lemon@bytedance.com", machineId: "mach-lemon",
|
||
fiveHour: { usedPct: 91, resetsAt: now + 48 * 60_000 },
|
||
sevenDay: { usedPct: 67, resetsAt: now + 2 * day },
|
||
quotaAt: now - 41_000,
|
||
tokens: { input: 12_400_000, output: 980_000, cacheRead: 88_000_000, cacheCreation: 6_200_000 },
|
||
tokensAsOf: "2026-09-12",
|
||
startedAt: now - 265 * day,
|
||
})),
|
||
}),
|
||
// Own probe 429'd, so its meters must fill from the sibling on the same
|
||
// account and say so, rather than reading 未知.
|
||
node({
|
||
id: "fei-android-01.shiro0.icu", platform: "android",
|
||
metrics: metrics(makeUsage({
|
||
at: now - 12_000, ms: 268, error: "oauth usage HTTP 429",
|
||
email: "fei@bytedance.com", machineId: "mach-fei-a",
|
||
tokens: { input: 7_800_000, output: 620_000, cacheRead: 40_000_000, cacheCreation: 2_400_000 },
|
||
tokensAsOf: "2026-09-11",
|
||
startedAt: now - 40 * day,
|
||
})),
|
||
}),
|
||
// Same Claude login, different Mac: quota shared, tokens NOT merged.
|
||
node({
|
||
id: "fei-ios-01.shiro0.icu", platform: "ios",
|
||
metrics: metrics(makeUsage({
|
||
email: "fei@bytedance.com", machineId: "mach-fei-i",
|
||
fiveHour: { usedPct: 38, resetsAt: now + 3 * 3600_000 },
|
||
sevenDay: { usedPct: 52, resetsAt: now + 2 * day },
|
||
quotaAt: now - 35_000,
|
||
tokens: { input: 4_200_000, output: 410_000, cacheRead: 21_000_000, cacheCreation: 1_100_000 },
|
||
tokensAsOf: "2026-09-12",
|
||
startedAt: now - 80 * day,
|
||
})),
|
||
}),
|
||
// Enrolled 9 days ago: prorates to $200 × 9/30 instead of a full month.
|
||
node({
|
||
id: "ux-ios-01.shiro0.icu", platform: "ios",
|
||
metrics: metrics(makeUsage({
|
||
email: "ux@bytedance.com", machineId: "mach-ux",
|
||
fiveHour: { usedPct: 12, resetsAt: now + 90 * 60_000 },
|
||
sevenDay: { usedPct: 21, resetsAt: now + day },
|
||
quotaAt: now - 35_000,
|
||
tokens: { input: 3_272, output: 1_289_779, cacheRead: 372_006_214, cacheCreation: 4_336_744 },
|
||
tokensAsOf: "2026-09-12",
|
||
startedAt: now - 9 * day,
|
||
coveredDays: 9,
|
||
})),
|
||
}),
|
||
node({
|
||
id: "fei-android-02.shiro0.icu", platform: "android",
|
||
metrics: metrics(null),
|
||
}),
|
||
// Satellite too old for a daily series: left out of the window totals and
|
||
// called out, not silently counted as zero.
|
||
node({
|
||
id: "fei-ios-03.shiro0.icu", platform: "ios",
|
||
live: false, status: "offline", lastSeenAt: now - 18 * 60_000,
|
||
metrics: metrics({
|
||
...makeUsage({
|
||
at: now - 20 * 60_000, ms: 301,
|
||
email: "ios-03@bytedance.com",
|
||
fiveHour: { usedPct: 12, resetsAt: now - 10 * 60_000 },
|
||
sevenDay: { usedPct: 44, resetsAt: now + day },
|
||
tokens: { input: 1_050_000, output: 90_000, cacheRead: 3_400_000, cacheCreation: 220_000 },
|
||
tokensAsOf: "2026-09-10",
|
||
startedAt: now - 12 * day,
|
||
}),
|
||
daily: null, dailyFromAt: null,
|
||
}),
|
||
}),
|
||
];
|
||
|
||
// The usage tab fetches its per-source detail itself; serve it from the
|
||
// fixture nodes instead of a control plane.
|
||
(api as { claudeSources: typeof api.claudeSources }).claudeSources = async () => ({
|
||
entries: nodes.flatMap((n) => {
|
||
const usage = n.metrics?.claudeUsage;
|
||
if (!usage) return [];
|
||
const withSamples: ClaudeUsage = usage.machineId === "mach-lemon"
|
||
? { ...usage, unknownSamples: [
|
||
{ prefix: "帮我把 feature/qi-story-widget-thought-sticky rebase 到 develop", cwd: "ai_workspace", sessions: 1, tokens: 20_800_000 },
|
||
{ prefix: "I do git fetch --refetch on TikTok and return", cwd: "ai_workspace_ios", sessions: 2, tokens: 1_300_000 },
|
||
] }
|
||
: usage;
|
||
return [{ nodeId: n.id, usage: withSamples }];
|
||
}),
|
||
});
|
||
|
||
setLang("zh");
|
||
createRoot(document.getElementById("root")!).render(
|
||
<div className="page-modal" style={{ position: "static", inset: "auto", width: "100%", minHeight: "100vh", borderRadius: 0 }}>
|
||
<div className="page-modal-body" style={{ padding: "24px 32px 48px" }}>
|
||
<Management supers={[]} admins={[]} onSave={async () => {}} nodes={nodes} initialTab="usage" />
|
||
</div>
|
||
</div>,
|
||
);
|