From 10cc1d0192eb899827bd79f17db978b9f59a33b4 Mon Sep 17 00:00:00 2001 From: joshua Date: Mon, 26 Oct 2026 02:34:57 +0000 Subject: [PATCH] feat(ui): sub-day usage presets served from ClickHouse rollups Add Last 15m/1h/6h/24h presets and datetime inputs to the usage date picker, route ranges that do not start at midnight to /{entity}/activity/rollup, and label sub-day buckets with their time. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../_components/CostOptimizationView.test.tsx | 1 + .../useDailyActivityRange.test.tsx | 1 + .../EntityUsage/EntityUsage.test.tsx | 1 + .../components/EntityUsage/entityFetchFns.ts | 7 +++- .../components/UsagePageView.test.tsx | 2 + .../components/overview/overviewData.ts | 13 +++++- .../src/components/networking.tsx | 18 ++++++++ .../shared/advanced_date_picker.test.tsx | 42 +++++++++---------- .../shared/advanced_date_picker.tsx | 33 ++++++++------- .../components/shared/date_range_presets.ts | 19 +++++++++ 10 files changed, 99 insertions(+), 38 deletions(-) diff --git a/ui/litellm-dashboard/src/app/(dashboard)/cost-optimization/_components/CostOptimizationView.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/cost-optimization/_components/CostOptimizationView.test.tsx index a2f0ca5edc9..017ab6f3f26 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/cost-optimization/_components/CostOptimizationView.test.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/cost-optimization/_components/CostOptimizationView.test.tsx @@ -11,6 +11,7 @@ vi.mock("@/app/(dashboard)/hooks/useAuthorized", () => ({ vi.mock("@/components/networking", () => ({ organizationListCall: vi.fn().mockResolvedValue([]), + activityRollupCall: vi.fn(), dailyActivityAggregatedCall: vi.fn().mockResolvedValue({ results: [], metadata: {} }), })); diff --git a/ui/litellm-dashboard/src/app/(dashboard)/cost-optimization/_components/useDailyActivityRange.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/cost-optimization/_components/useDailyActivityRange.test.tsx index 90d6694ccba..add1483c346 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/cost-optimization/_components/useDailyActivityRange.test.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/cost-optimization/_components/useDailyActivityRange.test.tsx @@ -17,6 +17,7 @@ vi.mock("@/app/(dashboard)/usage/_components/hooks/useAggregatedDailyActivity", })); vi.mock("@/components/networking", () => ({ + activityRollupCall: vi.fn(), dailyActivityAggregatedCall: vi.fn().mockResolvedValue({ results: [], metadata: {} }), })); diff --git a/ui/litellm-dashboard/src/app/(dashboard)/usage/_components/components/EntityUsage/EntityUsage.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/usage/_components/components/EntityUsage/EntityUsage.test.tsx index 04b4dcf9be1..c7875c90d18 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/usage/_components/components/EntityUsage/EntityUsage.test.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/usage/_components/components/EntityUsage/EntityUsage.test.tsx @@ -64,6 +64,7 @@ beforeAll(() => { // Mock the networking module vi.mock("@/components/networking", () => ({ + activityRollupCall: vi.fn(), dailyActivityAggregatedCall: vi.fn(), dailyActivityKeyPageCall: vi.fn(), dailyActivityKeySearchCall: vi.fn(), diff --git a/ui/litellm-dashboard/src/app/(dashboard)/usage/_components/components/EntityUsage/entityFetchFns.ts b/ui/litellm-dashboard/src/app/(dashboard)/usage/_components/components/EntityUsage/entityFetchFns.ts index 6dc80ef4591..fafd4073219 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/usage/_components/components/EntityUsage/entityFetchFns.ts +++ b/ui/litellm-dashboard/src/app/(dashboard)/usage/_components/components/EntityUsage/entityFetchFns.ts @@ -1,4 +1,5 @@ import { + activityRollupCall, dailyActivityAggregatedCall, dailyActivityExportCall, dailyActivityKeyPageCall, @@ -6,6 +7,7 @@ import { dailyActivityModelTopKeysCall, } from "@/components/networking"; import type { EntityType } from "@/components/EntityUsageExport/types"; +import { isSubDayRange } from "@/components/shared/date_range_presets"; import type { DailyActivityAggregatedResponse, DailyActivityEntity, @@ -34,7 +36,10 @@ const entityApi = ( entity: DailyActivityEntity, defaults?: Pick, ): EntityApi => ({ - aggregated: (req) => dailyActivityAggregatedCall(entity, { ...defaults, ...req }), + aggregated: (req) => + entity !== "agent" && isSubDayRange(req.startTime, req.endTime) + ? activityRollupCall(entity, { ...defaults, ...req }) + : dailyActivityAggregatedCall(entity, { ...defaults, ...req }), keyPage: (req, offset, limit) => dailyActivityKeyPageCall(entity, { ...defaults, ...req }, offset, limit), searchKeys: (req, search, limit) => dailyActivityKeySearchCall(entity, { ...defaults, ...req }, search, limit), modelTopKeys: (req, model, byModelGroup, limit) => diff --git a/ui/litellm-dashboard/src/app/(dashboard)/usage/_components/components/UsagePageView.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/usage/_components/components/UsagePageView.test.tsx index adb6d8791a7..ee7e722a2c4 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/usage/_components/components/UsagePageView.test.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/usage/_components/components/UsagePageView.test.tsx @@ -75,6 +75,7 @@ beforeAll(() => { // Mock the networking module vi.mock("@/components/networking", () => ({ + activityRollupCall: vi.fn(), dailyActivityAggregatedCall: vi.fn(), dailyActivityKeyPageCall: vi.fn(), userDailyActivityUserPageCall: vi.fn(), @@ -516,6 +517,7 @@ describe("UsagePage", () => { }; beforeEach(() => { + vi.mocked(networking.activityRollupCall).mockImplementation((...args) => mockDailyActivityAggregatedCall(...args)); mockClientUsagePanel.mockClear(); vi.mocked(processActivityData).mockReset(); vi.mocked(processActivityData).mockImplementation((_data, key) => ({ diff --git a/ui/litellm-dashboard/src/app/(dashboard)/usage/_components/components/overview/overviewData.ts b/ui/litellm-dashboard/src/app/(dashboard)/usage/_components/components/overview/overviewData.ts index c103c2d7c60..07bf8682c99 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/usage/_components/components/overview/overviewData.ts +++ b/ui/litellm-dashboard/src/app/(dashboard)/usage/_components/components/overview/overviewData.ts @@ -76,9 +76,20 @@ export interface Series { const seriesKey = (index: number) => `s${index}`; +/** Daily rows carry `YYYY-MM-DD`; sub-day rollup buckets carry a UTC `YYYY-MM-DDTHH:mm:ss` timestamp. */ const formatDayLabel = (date: string): string => { - const parsed = new Date(`${date}T00:00:00`); + const subDay = date.includes("T"); + const parsed = new Date(subDay ? `${date}Z` : `${date}T00:00:00`); if (Number.isNaN(parsed.getTime())) return date; + if (subDay) { + return parsed.toLocaleString("en-US", { + month: "short", + day: "numeric", + hour: "2-digit", + minute: "2-digit", + hour12: false, + }); + } return parsed.toLocaleDateString("en-US", { month: "short", day: "numeric" }); }; diff --git a/ui/litellm-dashboard/src/components/networking.tsx b/ui/litellm-dashboard/src/components/networking.tsx index fd25242ec34..05d3841400f 100644 --- a/ui/litellm-dashboard/src/components/networking.tsx +++ b/ui/litellm-dashboard/src/components/networking.tsx @@ -1394,6 +1394,24 @@ export const dailyActivityAggregatedCall = ( query: dailyActivityQuery(entity, req, req.apiKeyLimit === undefined ? {} : { api_key_limit: req.apiKeyLimit }), }); +/** Sub-day usage from ClickHouse spend-log rollups; same response shape, `results[].date` is a bucket timestamp. */ +export const activityRollupCall = ( + entity: DailyActivityEntity, + req: DailyActivityRequest, +): Promise => { + const { + start_date: _start, + end_date: _end, + timezone: _timezone, + include_current_utc_day: _utcDay, + ...query + } = dailyActivityQuery(entity, req, req.apiKeyLimit === undefined ? {} : { api_key_limit: req.apiKeyLimit }); + return apiClient.get(`/${entity}/activity/rollup`, { + accessToken: req.accessToken, + query: { ...query, start: req.startTime.toISOString(), end: req.endTime.toISOString() }, + }); +}; + export const dailyActivityKeyPageCall = ( entity: DailyActivityEntity, req: DailyActivityRequest, diff --git a/ui/litellm-dashboard/src/components/shared/advanced_date_picker.test.tsx b/ui/litellm-dashboard/src/components/shared/advanced_date_picker.test.tsx index 555a7b1a9e2..e5f5f7f0a13 100644 --- a/ui/litellm-dashboard/src/components/shared/advanced_date_picker.test.tsx +++ b/ui/litellm-dashboard/src/components/shared/advanced_date_picker.test.tsx @@ -141,8 +141,8 @@ describe("AdvancedDatePicker", () => { openDropdown(container); - const startDateInput = screen.getByDisplayValue("2025-01-01"); - const endDateInput = screen.getByDisplayValue("2025-01-31"); + const startDateInput = screen.getByDisplayValue("2025-01-01T12:00"); + const endDateInput = screen.getByDisplayValue("2025-01-31T12:00"); expect(startDateInput).toBeInTheDocument(); expect(endDateInput).toBeInTheDocument(); @@ -153,10 +153,10 @@ describe("AdvancedDatePicker", () => { openDropdown(container); - const startDateInput = screen.getByDisplayValue("2025-01-01") as HTMLInputElement; - fireEvent.change(startDateInput, { target: { value: "2025-02-01" } }); + const startDateInput = screen.getByDisplayValue("2025-01-01T12:00") as HTMLInputElement; + fireEvent.change(startDateInput, { target: { value: "2025-02-01T12:00" } }); - expect(startDateInput.value).toBe("2025-02-01"); + expect(startDateInput.value).toBe("2025-02-01T12:00"); }); it("should show Apply and Cancel buttons", () => { @@ -211,8 +211,8 @@ describe("AdvancedDatePicker", () => { const today = moment().format("YYYY-MM-DD"); openDropdown(container); - fireEvent.change(screen.getByDisplayValue("2025-01-01"), { target: { value: today } }); - fireEvent.change(screen.getByDisplayValue("2025-01-31"), { target: { value: today } }); + fireEvent.change(screen.getByDisplayValue("2025-01-01T12:00"), { target: { value: `${today}T00:00` } }); + fireEvent.change(screen.getByDisplayValue("2025-01-31T12:00"), { target: { value: `${today}T23:59` } }); fireEvent.click(screen.getByRole("button", { name: "Apply" })); expect(mockOnValueChange).toHaveBeenNthCalledWith(1, expect.anything(), null); @@ -289,7 +289,7 @@ describe("AdvancedDatePicker", () => { await user.keyboard("{Enter}"); const presets = Array.from(container.querySelectorAll('[data-slot="advanced-date-picker-preset"]')); - expect(presets).toHaveLength(5); + expect(presets).toHaveLength(9); await user.tab(); expect(presets[0]).toHaveFocus(); @@ -300,7 +300,7 @@ describe("AdvancedDatePicker", () => { await user.keyboard("{Enter}"); expect(presets[1]).toHaveAttribute("aria-pressed", "true"); - expect(screen.getByText("Last 7 days").closest("button")).toHaveClass("bg-info/10"); + expect(screen.getByText("Last hour").closest("button")).toHaveClass("bg-info/10"); }); it("should show validation error for invalid date range", async () => { @@ -308,12 +308,12 @@ describe("AdvancedDatePicker", () => { openDropdown(container); - const startDateInput = screen.getByDisplayValue("2025-01-01"); - const endDateInput = screen.getByDisplayValue("2025-01-31"); + const startDateInput = screen.getByDisplayValue("2025-01-01T12:00"); + const endDateInput = screen.getByDisplayValue("2025-01-31T12:00"); // Set end date before start date - fireEvent.change(startDateInput, { target: { value: "2025-12-01" } }); - fireEvent.change(endDateInput, { target: { value: "2025-01-01" } }); + fireEvent.change(startDateInput, { target: { value: "2025-12-01T12:00" } }); + fireEvent.change(endDateInput, { target: { value: "2025-01-01T12:00" } }); await waitFor(() => { expect(screen.getByText("End date cannot be before start date")).toBeInTheDocument(); @@ -325,12 +325,12 @@ describe("AdvancedDatePicker", () => { openDropdown(container); - const startDateInput = screen.getByDisplayValue("2025-01-01"); - const endDateInput = screen.getByDisplayValue("2025-01-31"); + const startDateInput = screen.getByDisplayValue("2025-01-01T12:00"); + const endDateInput = screen.getByDisplayValue("2025-01-31T12:00"); // Set end date before start date - fireEvent.change(startDateInput, { target: { value: "2025-12-01" } }); - fireEvent.change(endDateInput, { target: { value: "2025-01-01" } }); + fireEvent.change(startDateInput, { target: { value: "2025-12-01T12:00" } }); + fireEvent.change(endDateInput, { target: { value: "2025-01-01T12:00" } }); await waitFor(() => { // Find the button element (the Apply button's actual button element) @@ -346,15 +346,15 @@ describe("AdvancedDatePicker", () => { await user.click(screen.getByRole("button", { expanded: false })); const start = screen.getByLabelText("Start date"); const end = screen.getByLabelText("End date"); - expect(start).toHaveValue("2025-01-01"); - expect(end).toHaveValue("2025-01-31"); + expect(start).toHaveValue("2025-01-01T12:00"); + expect(end).toHaveValue("2025-01-31T12:00"); - act(() => fireEvent.change(start, { target: { value: "2025-02-01" } })); + act(() => fireEvent.change(start, { target: { value: "2025-02-01T00:00" } })); expect(screen.getByText("End date cannot be before start date")).toBeInTheDocument(); expect(screen.getByRole("button", { name: "Apply" })).toBeDisabled(); expect(mockOnValueChange).not.toHaveBeenCalled(); - act(() => fireEvent.change(end, { target: { value: "2025-02-05" } })); + act(() => fireEvent.change(end, { target: { value: "2025-02-05T00:00" } })); expect(screen.queryByText("End date cannot be before start date")).not.toBeInTheDocument(); expect(screen.getByRole("button", { name: "Apply" })).toBeEnabled(); expect(mockOnValueChange).not.toHaveBeenCalled(); diff --git a/ui/litellm-dashboard/src/components/shared/advanced_date_picker.tsx b/ui/litellm-dashboard/src/components/shared/advanced_date_picker.tsx index a16e50bfaad..ac0346561c1 100644 --- a/ui/litellm-dashboard/src/components/shared/advanced_date_picker.tsx +++ b/ui/litellm-dashboard/src/components/shared/advanced_date_picker.tsx @@ -5,9 +5,11 @@ import type { DateRangePickerValue } from "./date_picker_types"; import moment from "moment"; import React, { useCallback, useEffect, useId, useRef, useState } from "react"; -import { relativeTimeOptions, type RelativeTimeOption } from "./date_range_presets"; +import { relativeTimeOptions, type RelativeTimeOption, isSubDayRange } from "./date_range_presets"; export { relativeTimeOptions, type RelativeTimeOption } from "./date_range_presets"; +const INPUT_FORMAT = "YYYY-MM-DDTHH:mm"; + interface AdvancedDatePickerProps { value: DateRangePickerValue; onValueChange: (value: DateRangePickerValue, presetShortLabel: string | null) => void; @@ -70,8 +72,8 @@ const AdvancedDatePicker: React.FC = ({ return { isValid: true, error: "" }; } - const start = moment(startDate, "YYYY-MM-DD"); - const end = moment(endDate, "YYYY-MM-DD"); + const start = moment(startDate, INPUT_FORMAT); + const end = moment(endDate, INPUT_FORMAT); if (!start.isValid() || !end.isValid()) { return { isValid: false, error: "Invalid date format" }; @@ -89,10 +91,10 @@ const AdvancedDatePicker: React.FC = ({ // Initialize form inputs when component mounts or value changes useEffect(() => { if (value.from) { - setStartDate(moment(value.from).format("YYYY-MM-DD")); + setStartDate(moment(value.from).format(INPUT_FORMAT)); } if (value.to) { - setEndDate(moment(value.to).format("YYYY-MM-DD")); + setEndDate(moment(value.to).format(INPUT_FORMAT)); } setTempValue(value); setPickedPreset(presetShortLabel ?? null); @@ -128,6 +130,7 @@ const AdvancedDatePicker: React.FC = ({ // CRITICAL: Apply the same date adjustment logic as the original component const adjustDateRange = useCallback((newValue: DateRangePickerValue): DateRangePickerValue => { if (!newValue.from) return newValue; + if (isSubDayRange(newValue.from, newValue.to ?? newValue.from)) return newValue; const adjustedValue = { ...newValue }; const adjustedStartTime = new Date(newValue.from); @@ -165,8 +168,8 @@ const AdvancedDatePicker: React.FC = ({ setPickedPreset(option.shortLabel); // Update the form inputs to reflect the selection - setStartDate(moment(from).format("YYYY-MM-DD")); - setEndDate(moment(to).format("YYYY-MM-DD")); + setStartDate(moment(from).format(INPUT_FORMAT)); + setEndDate(moment(to).format(INPUT_FORMAT)); // Don't close the dropdown - let user click Apply to confirm }; @@ -175,11 +178,11 @@ const AdvancedDatePicker: React.FC = ({ try { if (startDate && endDate && validation.isValid) { // Set times to start and end of day - const from = moment(startDate, "YYYY-MM-DD").startOf("day"); - const to = moment(endDate, "YYYY-MM-DD").endOf("day"); + const from = moment(startDate, INPUT_FORMAT); + const to = moment(endDate, INPUT_FORMAT); if (from.isValid() && to.isValid()) { - const newValue = { from: from.toDate(), to: to.toDate() }; + const newValue = adjustDateRange({ from: from.toDate(), to: to.toDate() }); setTempValue(newValue); // Check if this matches any preset option @@ -190,7 +193,7 @@ const AdvancedDatePicker: React.FC = ({ } catch (error) { console.warn("Invalid date format:", error); } - }, [startDate, endDate, validation.isValid, getMatchingOption, pickedPreset]); + }, [startDate, endDate, validation.isValid, getMatchingOption, pickedPreset, adjustDateRange]); // Update tempValue when inputs change useEffect(() => { @@ -221,10 +224,10 @@ const AdvancedDatePicker: React.FC = ({ // Reset form inputs if (value.from) { - setStartDate(moment(value.from).format("YYYY-MM-DD")); + setStartDate(moment(value.from).format(INPUT_FORMAT)); } if (value.to) { - setEndDate(moment(value.to).format("YYYY-MM-DD")); + setEndDate(moment(value.to).format(INPUT_FORMAT)); } // Reset selected option @@ -336,7 +339,7 @@ const AdvancedDatePicker: React.FC = ({ { setPickedPreset(null); @@ -359,7 +362,7 @@ const AdvancedDatePicker: React.FC = ({ { setPickedPreset(null); diff --git a/ui/litellm-dashboard/src/components/shared/date_range_presets.ts b/ui/litellm-dashboard/src/components/shared/date_range_presets.ts index 30a32f2519b..115d03af547 100644 --- a/ui/litellm-dashboard/src/components/shared/date_range_presets.ts +++ b/ui/litellm-dashboard/src/components/shared/date_range_presets.ts @@ -7,7 +7,26 @@ export interface RelativeTimeOption { getValue: () => { from: Date; to: Date }; } +/** Widest window the sub-day (ClickHouse rollup) usage path serves; longer ranges use the daily tables. */ +export const SUB_DAY_MAX_RANGE_MS = 7 * 24 * 60 * 60 * 1000; + +/** + * A range that does not start at a local midnight is a sub-day investigation window + * (Last 1h, 14:05 to 14:35): it is served from spend-log rollups and never snapped to day edges. + */ +export const isSubDayRange = (from: Date, to: Date): boolean => + !moment(from).isSame(moment(from).startOf("day")) && to.getTime() - from.getTime() <= SUB_DAY_MAX_RANGE_MS; + +const lastMinutes = (minutes: number) => () => ({ + from: moment().subtract(minutes, "minutes").toDate(), + to: moment().toDate(), +}); + export const relativeTimeOptions: readonly RelativeTimeOption[] = [ + { label: "Last 15 minutes", shortLabel: "15m", settingValue: "last_15_minutes", getValue: lastMinutes(15) }, + { label: "Last hour", shortLabel: "1h", settingValue: "last_1_hour", getValue: lastMinutes(60) }, + { label: "Last 6 hours", shortLabel: "6h", settingValue: "last_6_hours", getValue: lastMinutes(6 * 60) }, + { label: "Last 24 hours", shortLabel: "24h", settingValue: "last_24_hours", getValue: lastMinutes(24 * 60) }, { label: "Today", shortLabel: "today",