-
-
Notifications
You must be signed in to change notification settings - Fork 11.6k
refactor(ui): convert user agent and per-user usage charts to shadcn/recharts #32725
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
Merged
ryan-crabbe-berri
merged 3 commits into
litellm_internal_staging
from
litellm_shadcn_charts_user_agent_per_user
Jul 11, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
eec0484
refactor(ui): convert user agent and per-user usage charts to shadcn/…
ryan-crabbe-berri e77f828
test(ui): harden bar x-position parsing against recharts path format
ryan-crabbe-berri a018241
Merge remote-tracking branch 'origin/litellm_internal_staging' into l…
ryan-crabbe-berri 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
118 changes: 118 additions & 0 deletions
118
ui/litellm-dashboard/src/components/per_user_usage.test.tsx
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,118 @@ | ||
| import { render, screen, fireEvent, waitFor } from "@testing-library/react"; | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
| import PerUserUsage from "./per_user_usage"; | ||
| import * as networking from "./networking"; | ||
|
|
||
| vi.mock("./networking", () => ({ | ||
| perUserAnalyticsCall: vi.fn(), | ||
| })); | ||
|
|
||
| type UserRow = { | ||
| user_id: string; | ||
| user_email: string | null; | ||
| user_agent: string | null; | ||
| successful_requests: number; | ||
| failed_requests: number; | ||
| total_requests: number; | ||
| total_tokens: number; | ||
| spend: number; | ||
| }; | ||
|
|
||
| const userRow = (userId: string, userAgent: string | null, successfulRequests: number): UserRow => ({ | ||
| user_id: userId, | ||
| user_email: null, | ||
| user_agent: userAgent, | ||
| successful_requests: successfulRequests, | ||
| failed_requests: 0, | ||
| total_requests: successfulRequests, | ||
| total_tokens: 100, | ||
| spend: 1, | ||
| }); | ||
|
|
||
| describe("PerUserUsage", () => { | ||
| const mockPerUserAnalyticsCall = vi.mocked(networking.perUserAnalyticsCall); | ||
|
|
||
| const mockResponse = { | ||
| results: [ | ||
| userRow("u1", "curl/8.0", 5), | ||
| userRow("u2", "curl/8.0", 50), | ||
| userRow("u3", "curl/8.0", 8), | ||
| userRow("u4", null, 7), | ||
| userRow("u5", null, 500), | ||
| ], | ||
| total_count: 5, | ||
| page: 1, | ||
| page_size: 50, | ||
| total_pages: 1, | ||
| }; | ||
|
|
||
| const defaultProps = { | ||
| accessToken: "test-token", | ||
| selectedTags: [], | ||
| formatAbbreviatedNumber: (value: number) => String(value), | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| mockPerUserAnalyticsCall.mockClear(); | ||
| mockPerUserAnalyticsCall.mockResolvedValue(mockResponse); | ||
| }); | ||
|
|
||
| it("renders the user details table by default", async () => { | ||
| render(<PerUserUsage {...defaultProps} />); | ||
|
|
||
| await waitFor(() => { | ||
| expect(mockPerUserAnalyticsCall).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| expect(screen.getByText("Per User Usage")).toBeInTheDocument(); | ||
| await waitFor(() => { | ||
| expect(screen.getByText("u1")).toBeInTheDocument(); | ||
| }); | ||
| }); | ||
|
|
||
| it("renders the usage distribution as a stacked bar chart with the explicit palette and users formatter", async () => { | ||
| render(<PerUserUsage {...defaultProps} />); | ||
|
|
||
| await waitFor(() => { | ||
| expect(mockPerUserAnalyticsCall).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| fireEvent.click(screen.getByText("Usage Distribution")); | ||
|
|
||
| const panel = screen.getByText("User Usage Distribution").closest("div")?.parentElement; | ||
| expect(panel).not.toBeNull(); | ||
|
|
||
| await waitFor(() => { | ||
| expect(panel!.querySelectorAll("path.recharts-rectangle")).toHaveLength(4); | ||
| }); | ||
|
|
||
| const chart = panel!.querySelector('[data-slot="chart"]'); | ||
| expect(chart).not.toBeNull(); | ||
| expect(chart!.querySelectorAll(".recharts-bar")).toHaveLength(2); | ||
|
|
||
| const rectangles = Array.from(chart!.querySelectorAll("path.recharts-rectangle")); | ||
| const fills = new Set(rectangles.map((rect) => rect.getAttribute("fill"))); | ||
| expect(fills).toEqual(new Set(["var(--color-blue-500, #3b82f6)", "var(--color-green-500, #22c55e)"])); | ||
|
|
||
| const xPositions = new Set(rectangles.map((rect) => rect.getAttribute("d")?.match(/^M\s*([\d.]+)/)?.[1])); | ||
| expect(xPositions.size).toBe(3); | ||
|
|
||
| expect(chart!.textContent).toContain("curl/8.0"); | ||
| expect(chart!.textContent).toContain("Unknown"); | ||
| for (const bucket of [ | ||
| "1-9 requests", | ||
| "10-99 requests", | ||
| "100-999 requests", | ||
| "1K-9.9K requests", | ||
| "10K-99.9K requests", | ||
| "100K+ requests", | ||
| ]) { | ||
| expect(chart!.textContent).toContain(bucket); | ||
| } | ||
|
|
||
| const tickTexts = Array.from(chart!.querySelectorAll(".recharts-cartesian-axis-tick-value")).map( | ||
| (tick) => tick.textContent ?? "", | ||
| ); | ||
| expect(tickTexts.some((tick) => / users$/.test(tick))).toBe(true); | ||
| }); | ||
| }); |
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
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.
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.
Uh oh!
There was an error while loading. Please reload this page.