mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-19 09:53:33 +00:00
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>
This commit is contained in:
parent
c4a89e7729
commit
10cc1d0192
10 changed files with 99 additions and 38 deletions
|
|
@ -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: {} }),
|
||||
}));
|
||||
|
||||
|
|
|
|||
|
|
@ -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: {} }),
|
||||
}));
|
||||
|
||||
|
|
|
|||
|
|
@ -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(),
|
||||
|
|
|
|||
|
|
@ -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<DailyActivityRequest, "excludeEntityIds">,
|
||||
): 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) =>
|
||||
|
|
|
|||
|
|
@ -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) => ({
|
||||
|
|
|
|||
|
|
@ -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" });
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -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<DailyActivityAggregatedResponse> => {
|
||||
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<DailyActivityAggregatedResponse>(`/${entity}/activity/rollup`, {
|
||||
accessToken: req.accessToken,
|
||||
query: { ...query, start: req.startTime.toISOString(), end: req.endTime.toISOString() },
|
||||
});
|
||||
};
|
||||
|
||||
export const dailyActivityKeyPageCall = (
|
||||
entity: DailyActivityEntity,
|
||||
req: DailyActivityRequest,
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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<AdvancedDatePickerProps> = ({
|
|||
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<AdvancedDatePickerProps> = ({
|
|||
// 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<AdvancedDatePickerProps> = ({
|
|||
// 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<AdvancedDatePickerProps> = ({
|
|||
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<AdvancedDatePickerProps> = ({
|
|||
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<AdvancedDatePickerProps> = ({
|
|||
} 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<AdvancedDatePickerProps> = ({
|
|||
|
||||
// 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<AdvancedDatePickerProps> = ({
|
|||
</label>
|
||||
<input
|
||||
id={`${inputId}-start`}
|
||||
type="date"
|
||||
type="datetime-local"
|
||||
value={startDate}
|
||||
onChange={(e) => {
|
||||
setPickedPreset(null);
|
||||
|
|
@ -359,7 +362,7 @@ const AdvancedDatePicker: React.FC<AdvancedDatePickerProps> = ({
|
|||
</label>
|
||||
<input
|
||||
id={`${inputId}-end`}
|
||||
type="date"
|
||||
type="datetime-local"
|
||||
value={endDate}
|
||||
onChange={(e) => {
|
||||
setPickedPreset(null);
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue