-
Notifications
You must be signed in to change notification settings - Fork 199
feat(dashboard): add local usage statistics dashboard with session detail #948
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
myk1yt
wants to merge
32
commits into
Zoo-Code-Org:main
Choose a base branch
from
myk1yt:feature/local-usage-stats
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
e306231
feat(stats): define usage event and message contracts
k1yt d19a5c7
feat(stats): add append-only local usage store and aggregation
k1yt 0f0b7a4
feat(stats): record final usage for each API attempt
k1yt edfabe3
feat(stats): expose stats query export and clear handlers
k1yt ff1c8e7
feat(stats): add slash entry and statistics webview
k1yt a1b8e4b
fix(stats): resolve blockers B1/B2/B3 and highs H1/H3
k1yt 138f444
feat(stats): add autocomplete entry and time-axis groupBy in UI
k1yt 2dfffc0
test(stats): add coverage tests for UsageStatsService, UsageHeatmap, …
k1yt 41c927e
i18n(stats): add translations for 17 languages
k1yt 8f51026
fix(i18n): remove BOM from package.nls.ca.json
k1yt 98c3016
fix(i18n): remove BOM from all package.nls locale files
k1yt 11c3474
fix(i18n): restore missing opening brace in all package.nls locale files
k1yt b147f24
i18n(stats): apply CodeRabbit translation review fixes (de, fr, vi, z…
k1yt f238618
refactor(stats): convert all Korean comments to English
k1yt ea2e6f2
feat(dashboard): remove /stats command and add Dashboard sidebar entry
k1yt e5ae103
feat(dashboard): add DashboardView with summary, time range, and brea…
k1yt 5076f5b
feat(dashboard): add session list with titles and model/provider filters
k1yt d29720e
feat(dashboard): add session detail with expandable API call list
k1yt adaa163
feat(dashboard): add translations for all 17 languages
k1yt 81dae40
test(stats): remove stale 'stats' command test assertions
k1yt dd384cc
refactor(dashboard): remove orphaned StatsView, i18n relative time, e…
k1yt 2dc36de
feat(dashboard): default Custom date range to yesterday-today
k1yt 24c6d4e
fix(providers): add totalCost calculation using user-configured pricing
k1yt 81297ad
feat(dashboard): compute missing costs at query time and fix session …
k1yt 70cfa2c
feat(dashboard): add usage dashboard with mode column, multi-model ag…
k1yt 824ccff
feat(heatmap): blue gradient 6 levels, white borders, and 221 new tests
k1yt 1b560af
feat(dashboard): responsive heatmap, 30d/60d/120d/360d ranges, CI fix…
k1yt 883a9c6
feat(stats): make UsageHeatmap self-fetching for independent range se…
k1yt b0d82c1
test(stats): add comprehensive DashboardView test suite for codecov p…
k1yt 28585aa
fix(stats): remove unused variables in DashboardView.spec.tsx to fix …
k1yt 28d59c3
fix(stats): correct totalTokens calculation, provider pricing, and da…
k1yt f8551be
fix(stats): remove day axis from breakdown groupBy to eliminate dupli…
k1yt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,323 @@ | ||
| import { | ||
| UsageEventStatus, | ||
| UsageValueSource, | ||
| InclusionRule, | ||
| SourcedNumber, | ||
| UsageEventV1, | ||
| StatsQuery, | ||
| StatsBucket, | ||
| StatsSnapshot, | ||
| } from "../usage-stats.js" | ||
|
|
||
| describe("usage-stats schemas", () => { | ||
| // ── Enums ──────────────────────────────────────────────────────────── | ||
|
|
||
| describe("UsageEventStatus", () => { | ||
| it("should accept all valid statuses", () => { | ||
| expect(UsageEventStatus.parse("completed")).toBe("completed") | ||
| expect(UsageEventStatus.parse("failed")).toBe("failed") | ||
| expect(UsageEventStatus.parse("cancelled")).toBe("cancelled") | ||
| }) | ||
|
|
||
| it("should reject invalid status", () => { | ||
| expect(() => UsageEventStatus.parse("success")).toThrow() | ||
| }) | ||
| }) | ||
|
|
||
| describe("UsageValueSource", () => { | ||
| it("should accept all valid sources", () => { | ||
| expect(UsageValueSource.parse("provider")).toBe("provider") | ||
| expect(UsageValueSource.parse("estimated")).toBe("estimated") | ||
| expect(UsageValueSource.parse("backfilled")).toBe("backfilled") | ||
| }) | ||
|
|
||
| it("should reject invalid source", () => { | ||
| expect(() => UsageValueSource.parse("guessed")).toThrow() | ||
| }) | ||
| }) | ||
|
|
||
| describe("InclusionRule", () => { | ||
| it("should accept all valid rules", () => { | ||
| expect(InclusionRule.parse("included")).toBe("included") | ||
| expect(InclusionRule.parse("excluded")).toBe("excluded") | ||
| expect(InclusionRule.parse("unknown")).toBe("unknown") | ||
| }) | ||
| }) | ||
|
|
||
| // ── SourcedNumber ───────────────────────────────────────────────────── | ||
|
|
||
| describe("SourcedNumber", () => { | ||
| it("should parse a valid SourcedNumber", () => { | ||
| const result = SourcedNumber.parse({ value: 42, source: "provider" }) | ||
| expect(result).toEqual({ value: 42, source: "provider" }) | ||
| }) | ||
|
|
||
| it("should reject missing source", () => { | ||
| expect(() => SourcedNumber.parse({ value: 42 })).toThrow() | ||
| }) | ||
|
|
||
| it("should reject missing value", () => { | ||
| expect(() => SourcedNumber.parse({ source: "estimated" })).toThrow() | ||
| }) | ||
| }) | ||
|
|
||
| // ── UsageEventV1 ──────────────────────────────────────────────────────── | ||
|
|
||
| describe("UsageEventV1", () => { | ||
| const validEvent = { | ||
| schemaVersion: 1, | ||
| eventId: "evt-001", | ||
| idempotencyKey: "idem-001", | ||
| occurredAt: "2026-07-18T12:00:00.000Z", | ||
| timezoneOffsetMinutes: -540, | ||
| status: "completed", | ||
| attempt: 1, | ||
| taskId: "task-001", | ||
| provider: "anthropic", | ||
| model: "claude-sonnet-4-20250514", | ||
| mode: "code", | ||
| usage: { | ||
| inputTokens: { value: 1000, source: "provider" }, | ||
| outputTokens: { value: 500, source: "provider" }, | ||
| costUsd: { value: 0.015, source: "provider" }, | ||
| }, | ||
| semantics: { | ||
| cacheReadInInput: "included", | ||
| cacheWriteInInput: "included", | ||
| reasoningInOutput: "excluded", | ||
| }, | ||
| provenance: "live", | ||
| } | ||
|
|
||
| it("should parse a valid complete event", () => { | ||
| const result = UsageEventV1.parse(validEvent) | ||
| expect(result.eventId).toBe("evt-001") | ||
| expect(result.schemaVersion).toBe(1) | ||
| expect(result.usage.inputTokens?.value).toBe(1000) | ||
| }) | ||
|
|
||
| it("should accept optional parentTaskId", () => { | ||
| const result = UsageEventV1.parse({ ...validEvent, parentTaskId: "task-000" }) | ||
| expect(result.parentTaskId).toBe("task-000") | ||
| }) | ||
|
|
||
| it("should work without optional usage fields", () => { | ||
| const minimal = { ...validEvent, usage: {} } | ||
| const result = UsageEventV1.parse(minimal) | ||
| expect(result.usage.inputTokens).toBeUndefined() | ||
| }) | ||
|
|
||
| it("should accept backfilled provenance", () => { | ||
| const result = UsageEventV1.parse({ ...validEvent, provenance: "history-backfill" }) | ||
| expect(result.provenance).toBe("history-backfill") | ||
| }) | ||
|
|
||
| it("should reject schemaVersion !== 1", () => { | ||
| expect(() => UsageEventV1.parse({ ...validEvent, schemaVersion: 2 })).toThrow() | ||
| }) | ||
|
|
||
| it("should reject missing semantics", () => { | ||
| const { semantics: _semantics, ...withoutSemantics } = validEvent | ||
| expect(() => UsageEventV1.parse(withoutSemantics)).toThrow() | ||
| }) | ||
|
|
||
| it("should reject invalid provenance", () => { | ||
| expect(() => UsageEventV1.parse({ ...validEvent, provenance: "imported" })).toThrow() | ||
| }) | ||
|
|
||
| it("should reject missing required fields (eventId)", () => { | ||
| const { eventId: _eventId, ...withoutEventId } = validEvent | ||
| expect(() => UsageEventV1.parse(withoutEventId)).toThrow() | ||
| }) | ||
|
|
||
| it("should reject negative attempt", () => { | ||
| // z.number() accepts negatives, but attempt should be >= 0 logically | ||
| // This test confirms the schema accepts any number (no min constraint in V1) | ||
| const result = UsageEventV1.parse({ ...validEvent, attempt: 0 }) | ||
| expect(result.attempt).toBe(0) | ||
| }) | ||
| }) | ||
|
|
||
| // ── StatsQuery ─────────────────────────────────────────────────────── | ||
|
|
||
| describe("StatsQuery", () => { | ||
| it("should parse a valid query with preset", () => { | ||
| const result = StatsQuery.parse({ | ||
| preset: "7d", | ||
| timezone: "Asia/Seoul", | ||
| groupBy: ["day"], | ||
| }) | ||
| expect(result.preset).toBe("7d") | ||
| expect(result.includeCancelled).toBe(false) // default | ||
| }) | ||
|
|
||
| it("should parse a query with from/to range", () => { | ||
| const result = StatsQuery.parse({ | ||
| from: "2026-07-01T00:00:00Z", | ||
| to: "2026-07-18T00:00:00Z", | ||
| timezone: "UTC", | ||
| groupBy: ["provider", "model"], | ||
| }) | ||
| expect(result.from).toBe("2026-07-01T00:00:00Z") | ||
| expect(result.groupBy).toHaveLength(2) | ||
| }) | ||
|
|
||
| it("should default includeCancelled to false", () => { | ||
| const result = StatsQuery.parse({ | ||
| timezone: "UTC", | ||
| groupBy: [], | ||
| }) | ||
| expect(result.includeCancelled).toBe(false) | ||
| }) | ||
|
|
||
| it("should accept includeCancelled: true", () => { | ||
| const result = StatsQuery.parse({ | ||
| timezone: "UTC", | ||
| groupBy: [], | ||
| includeCancelled: true, | ||
| }) | ||
| expect(result.includeCancelled).toBe(true) | ||
| }) | ||
|
|
||
| it("should reject more than 3 groupBy dimensions", () => { | ||
| expect(() => | ||
| StatsQuery.parse({ | ||
| timezone: "UTC", | ||
| groupBy: ["day", "week", "month", "provider"], | ||
| }), | ||
| ).toThrow() | ||
| }) | ||
|
|
||
| it("should reject invalid preset", () => { | ||
| expect(() => | ||
| StatsQuery.parse({ | ||
| preset: "90d", | ||
| timezone: "UTC", | ||
| groupBy: [], | ||
| }), | ||
| ).toThrow() | ||
| }) | ||
|
|
||
| it("should reject missing timezone", () => { | ||
| expect(() => | ||
| StatsQuery.parse({ | ||
| groupBy: [], | ||
| }), | ||
| ).toThrow() | ||
| }) | ||
|
|
||
| it("should reject invalid groupBy dimension", () => { | ||
| expect(() => | ||
| StatsQuery.parse({ | ||
| timezone: "UTC", | ||
| groupBy: ["hour"], | ||
| }), | ||
| ).toThrow() | ||
| }) | ||
| }) | ||
|
|
||
| // ── StatsBucket ────────────────────────────────────────────────────── | ||
|
|
||
| describe("StatsBucket", () => { | ||
| const validBucket = { | ||
| key: { day: "2026-07-18" }, | ||
| events: 10, | ||
| completedCalls: 8, | ||
| failedCalls: 1, | ||
| cancelledCalls: 1, | ||
| inputTokens: 5000, | ||
| outputTokens: 2500, | ||
| cacheReadTokens: 1000, | ||
| cacheWriteTokens: 500, | ||
| reasoningTokens: 200, | ||
| totalTokens: 7500, | ||
| costUsd: 0.075, | ||
| unknownEventCount: 0, | ||
| } | ||
|
|
||
| it("should parse a valid bucket", () => { | ||
| const result = StatsBucket.parse(validBucket) | ||
| expect(result.events).toBe(10) | ||
| expect(result.key.day).toBe("2026-07-18") | ||
| }) | ||
|
|
||
| it("should reject missing required numeric field", () => { | ||
| const { costUsd: _costUsd, ...withoutCost } = validBucket | ||
| expect(() => StatsBucket.parse(withoutCost)).toThrow() | ||
| }) | ||
|
|
||
| it("should accept empty key record", () => { | ||
| const result = StatsBucket.parse({ ...validBucket, key: {} }) | ||
| expect(Object.keys(result.key)).toHaveLength(0) | ||
| }) | ||
| }) | ||
|
|
||
| // ── StatsSnapshot ───────────────────────────────────────────────────── | ||
|
|
||
| describe("StatsSnapshot", () => { | ||
| const validQuery = { | ||
| timezone: "UTC", | ||
| groupBy: ["day"], | ||
| } | ||
| const validBucket = { | ||
| key: { day: "2026-07-18" }, | ||
| events: 5, | ||
| completedCalls: 4, | ||
| failedCalls: 1, | ||
| cancelledCalls: 0, | ||
| inputTokens: 2000, | ||
| outputTokens: 1000, | ||
| cacheReadTokens: 0, | ||
| cacheWriteTokens: 0, | ||
| reasoningTokens: 0, | ||
| totalTokens: 3000, | ||
| costUsd: 0.03, | ||
| unknownEventCount: 0, | ||
| } | ||
| const validSnapshot = { | ||
| query: validQuery, | ||
| generatedAt: "2026-07-18T12:00:00.000Z", | ||
| buckets: [validBucket], | ||
| totals: validBucket, | ||
| coverage: { | ||
| firstEventAt: "2026-07-01T00:00:00.000Z", | ||
| lastEventAt: "2026-07-18T12:00:00.000Z", | ||
| recordingPaused: false, | ||
| backfilledEventCount: 0, | ||
| }, | ||
| } | ||
|
|
||
| it("should parse a valid snapshot", () => { | ||
| const result = StatsSnapshot.parse(validSnapshot) | ||
| expect(result.buckets).toHaveLength(1) | ||
| expect(result.coverage.recordingPaused).toBe(false) | ||
| }) | ||
|
|
||
| it("should accept empty buckets array", () => { | ||
| const result = StatsSnapshot.parse({ ...validSnapshot, buckets: [] }) | ||
| expect(result.buckets).toHaveLength(0) | ||
| }) | ||
|
|
||
| it("should accept optional firstEventAt/lastEventAt omitted", () => { | ||
| const result = StatsSnapshot.parse({ | ||
| ...validSnapshot, | ||
| coverage: { | ||
| recordingPaused: true, | ||
| backfilledEventCount: 0, | ||
| }, | ||
| }) | ||
| expect(result.coverage.firstEventAt).toBeUndefined() | ||
| expect(result.coverage.lastEventAt).toBeUndefined() | ||
| }) | ||
|
|
||
| it("should reject missing coverage", () => { | ||
| const { coverage: _coverage, ...withoutCoverage } = validSnapshot | ||
| expect(() => StatsSnapshot.parse(withoutCoverage)).toThrow() | ||
| }) | ||
|
|
||
| it("should reject missing totals", () => { | ||
| const { totals: _totals, ...withoutTotals } = validSnapshot | ||
| expect(() => StatsSnapshot.parse(withoutTotals)).toThrow() | ||
| }) | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Test name contradicts its body.
The test is titled "should reject negative attempt" but it parses
attempt: 0and asserts it's accepted, not rejected. No negative value is ever passed. The name should reflect what's actually verified (that any number, including 0, is accepted — nominconstraint).💚 Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents