From 93c3074ecb8a125f7c649f8e3221e8ea5fb53e03 Mon Sep 17 00:00:00 2001 From: moe-berri Date: Mon, 16 Nov 2026 04:03:02 +0800 Subject: [PATCH] fix(ui): default customer usage range to month to date (#51923) --- .../_components/customerUsageRange.test.ts | 21 +++++++++++++++---- .../_components/customerUsageRange.ts | 2 +- .../customers/page.integration.test.tsx | 4 ++++ .../customers/usage/page.integration.test.tsx | 5 +++-- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/litellm/ui/litellm-dashboard/src/app/(dashboard)/customers/_components/customerUsageRange.test.ts b/litellm/ui/litellm-dashboard/src/app/(dashboard)/customers/_components/customerUsageRange.test.ts index 39fc40958bc..4e9cbe9f05b 100644 --- a/litellm/ui/litellm-dashboard/src/app/(dashboard)/customers/_components/customerUsageRange.test.ts +++ b/litellm/ui/litellm-dashboard/src/app/(dashboard)/customers/_components/customerUsageRange.test.ts @@ -34,16 +34,29 @@ describe("customer usage calendar ranges", () => { ["?range=2024-02-01..2024-02-29&period=previous", "2024-02-01..2024-02-29", "previous"], ["?from=2024-02-01&to=2024-02-29", "2024-02-01..2024-02-29", null], ["?range=MTD&from=2024-01-01&to=2024-01-03", "2024-03-01..2024-03-01", "MTD"], - ["?range=garbage", "2024-03-01..2024-03-31", "month"], - ["?from=2024-02-30&to=2024-03-02", "2024-03-01..2024-03-31", "month"], - ["?from=2024-03-02&to=2024-03-01", "2024-03-01..2024-03-31", "month"], - ["?from=2024-01-01&to=2025-02-05", "2024-03-01..2024-03-31", "month"], + ["?range=month", "2024-03-01..2024-03-31", "month"], + ["?range=garbage", "2024-03-01..2024-03-01", "MTD"], + ["?from=2024-02-30&to=2024-03-02", "2024-03-01..2024-03-01", "MTD"], + ["?from=2024-03-02&to=2024-03-01", "2024-03-01..2024-03-01", "MTD"], + ["?from=2024-01-01&to=2025-02-05", "2024-03-01..2024-03-01", "MTD"], ])("resolves saved links and falls back for invalid dates in %s", (query, expected, preset) => { const selection = customerDateSelection(loadDates(query), new Date("2024-03-01T00:30:00Z")); expect(customerRangeParser.serialize(selection.dateValue)).toBe(expected); expect(selection.preset).toBe(preset); }); + it.each([ + ["2026-11-15T12:00:00Z", "2026-11-01..2026-11-15"], + ["2026-11-01T00:30:00+02:00", "2026-10-01..2026-10-31"], + ["2026-10-31T23:30:00-02:00", "2026-11-01..2026-11-01"], + ])("defaults an absent date query to UTC month to date at %s", (now, expected) => { + const selection = customerDateSelection(loadDates(""), new Date(now)); + const expectedPatch = { range: "MTD", period: null, from: null, to: null }; + expect(customerRangeParser.serialize(selection.dateValue)).toBe(expected); + expect(selection.preset).toBe("MTD"); + expect(customerDatePatch(selection.dateValue, selection.preset)).toEqual(expectedPatch); + }); + it("keeps saved calendar dates when their preset label is stale", () => { const now = new Date("2024-06-01T12:00:00Z"); const selection = customerDateSelection(loadDates("?range=2024-02-01..2024-02-29&period=previous"), now); diff --git a/litellm/ui/litellm-dashboard/src/app/(dashboard)/customers/_components/customerUsageRange.ts b/litellm/ui/litellm-dashboard/src/app/(dashboard)/customers/_components/customerUsageRange.ts index 11911b186d5..034c904e3c9 100644 --- a/litellm/ui/litellm-dashboard/src/app/(dashboard)/customers/_components/customerUsageRange.ts +++ b/litellm/ui/litellm-dashboard/src/app/(dashboard)/customers/_components/customerUsageRange.ts @@ -92,7 +92,7 @@ export const customerDateSelection = (params: DateParams, now = new Date()) => { } const custom = params.from && params.to ? customerRangeParser.parse(`${params.from}..${params.to}`) : null; if (custom) return { dateValue: custom, preset: null }; - return { dateValue: customerPresetRange("month", now), preset: "month" }; + return { dateValue: customerPresetRange("MTD", now), preset: "MTD" }; }; export const customerDatePatch = (value: DateRangePickerValue, preset: string | null) => { diff --git a/litellm/ui/litellm-dashboard/src/app/(dashboard)/customers/page.integration.test.tsx b/litellm/ui/litellm-dashboard/src/app/(dashboard)/customers/page.integration.test.tsx index 31c34696983..24392488907 100644 --- a/litellm/ui/litellm-dashboard/src/app/(dashboard)/customers/page.integration.test.tsx +++ b/litellm/ui/litellm-dashboard/src/app/(dashboard)/customers/page.integration.test.tsx @@ -122,6 +122,10 @@ describe("customer organization overview", () => { renderWithProviders(); await screen.findByRole("link", { name: "Customer 29" }); expect(customerLinks()[0]).toHaveTextContent("Customer 29"); + expect(customerLinks()[0]).toHaveAttribute( + "href", + "/ui/customers/usage?org=customer-29&from=2024-03-01&to=2024-03-15", + ); const overview = screen.getByRole("region", { name: "Overall customer usage" }); expect(within(overview).getByText("$100.00")).toBeInTheDocument(); expect(within(overview).getByText("20")).toBeInTheDocument(); diff --git a/litellm/ui/litellm-dashboard/src/app/(dashboard)/customers/usage/page.integration.test.tsx b/litellm/ui/litellm-dashboard/src/app/(dashboard)/customers/usage/page.integration.test.tsx index 3648c9169a1..3b5307189b8 100644 --- a/litellm/ui/litellm-dashboard/src/app/(dashboard)/customers/usage/page.integration.test.tsx +++ b/litellm/ui/litellm-dashboard/src/app/(dashboard)/customers/usage/page.integration.test.tsx @@ -217,8 +217,9 @@ describe("CustomerHierarchyUsagePage", () => { }); it.each([ - ["2026-11-15T00:30:00Z", "", "2026-11-01", "2026-11-30"], - ["2027-01-01T00:30:00Z", "", "2027-01-01", "2027-01-31"], + ["2026-11-15T00:30:00Z", "", "2026-11-01", "2026-11-15"], + ["2027-01-01T00:30:00Z", "", "2027-01-01", "2027-01-01"], + ["2026-11-15T00:30:00Z", "&range=month", "2026-11-01", "2026-11-30"], ["2026-11-15T00:30:00Z", "&range=MTD", "2026-11-01", "2026-11-15"], ["2026-11-30T23:30:00Z", "&range=today", "2026-11-30", "2026-11-30"], ])("uses UTC calendar dates at %s with %s", async (now, query, startDate, endDate) => {