From dbbc7a6811bf582809e334607cc171644a6ffa79 Mon Sep 17 00:00:00 2001 From: ryan-crabbe-berri Date: Tue, 15 Sep 2026 17:33:00 -0700 Subject: [PATCH 1/3] feat(ui): persist logs request state in the URL Move the Request Logs table's pagination, sorting, search and column filters from component state onto useUrlTableState so page, page_size, sort_by, sort_order, log_search and the filter_* keys survive reloads and can be shared as links. The drawer's log_id and session_id keys are untouched; the session and request id filters write filter_session and filter_request_id instead. The time range picker gets its own useLogsTimeRange hook: a range preset id lives in the URL (24h default) with start and end stored as UTC ISO minutes only for a custom range. Preset bounds are anchored when the range is picked or restored, so a shared link recomputes them from now instead of pinning stale timestamps. hide_health_checks becomes a URL boolean instead of sessionStorage, and clicking a key hash pushes ?key= so Back returns to the table. The in-memory keyset cursor map stays local and falls back to offset paging for a page restored from the URL. --- .../components/view_logs/LogsTableToolbar.tsx | 74 ++-- .../view_logs/RequestLogsPanel.test.tsx | 345 +++++++++++++++++- .../components/view_logs/RequestLogsPanel.tsx | 232 +++++++----- .../components/view_logs/RequestLogsTable.tsx | 3 + .../src/components/view_logs/constants.ts | 22 +- .../view_logs/useLogsTimeRange.test.tsx | 152 ++++++++ .../components/view_logs/useLogsTimeRange.ts | 119 ++++++ 7 files changed, 779 insertions(+), 168 deletions(-) create mode 100644 ui/litellm-dashboard/src/components/view_logs/useLogsTimeRange.test.tsx create mode 100644 ui/litellm-dashboard/src/components/view_logs/useLogsTimeRange.ts diff --git a/ui/litellm-dashboard/src/components/view_logs/LogsTableToolbar.tsx b/ui/litellm-dashboard/src/components/view_logs/LogsTableToolbar.tsx index 60657872ccc..07e7394235c 100644 --- a/ui/litellm-dashboard/src/components/view_logs/LogsTableToolbar.tsx +++ b/ui/litellm-dashboard/src/components/view_logs/LogsTableToolbar.tsx @@ -1,6 +1,5 @@ "use client"; -import moment from "moment"; import { CalendarDays } from "lucide-react"; import { useState } from "react"; @@ -9,61 +8,42 @@ import { Input } from "@/components/ui/input"; import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; import { Switch } from "@/components/ui/switch"; -import { QUICK_SELECT_OPTIONS } from "./constants"; +import { QUICK_SELECT_OPTIONS, type QuickSelectPresetId } from "./constants"; import { getTimeRangeDisplay } from "./logs_utils"; +import { CUSTOM_RANGE, type LogsTimeRange } from "./useLogsTimeRange"; interface LogsTableToolbarProps { - startTime: string; + timeRange: LogsTimeRange; + onPresetSelect: (preset: QuickSelectPresetId) => void; + onCustomRangeToggle: () => void; onStartTimeChange: (value: string) => void; - endTime: string; onEndTimeChange: (value: string) => void; - isCustomDate: boolean; - onIsCustomDateChange: (value: boolean) => void; - selectedTimeInterval: { value: number; unit: string }; - onSelectedTimeIntervalChange: (value: { value: number; unit: string }) => void; isLiveTail: boolean; onIsLiveTailChange: (value: boolean) => void; excludeInternalHealthChecks: boolean; onExcludeInternalHealthChecksChange: (value: boolean) => void; - onResetToFirstPage: () => void; onResetFilters: () => void; } export function LogsTableToolbar({ - startTime, + timeRange, + onPresetSelect, + onCustomRangeToggle, onStartTimeChange, - endTime, onEndTimeChange, - isCustomDate, - onIsCustomDateChange, - selectedTimeInterval, - onSelectedTimeIntervalChange, isLiveTail, onIsLiveTailChange, excludeInternalHealthChecks, onExcludeInternalHealthChecksChange, - onResetToFirstPage, onResetFilters, }: LogsTableToolbarProps) { const [quickSelectOpen, setQuickSelectOpen] = useState(false); + const { range, startTime, endTime } = timeRange; + const isCustomDate = range === CUSTOM_RANGE; - const applyQuickSelect = (option: { label: string; value: number; unit: string }) => { - onResetToFirstPage(); - onEndTimeChange(moment().format("YYYY-MM-DDTHH:mm")); - onStartTimeChange( - moment() - .subtract(option.value, option.unit as moment.unitOfTime.DurationConstructor) - .format("YYYY-MM-DDTHH:mm"), - ); - onSelectedTimeIntervalChange({ value: option.value, unit: option.unit }); - onIsCustomDateChange(false); - setQuickSelectOpen(false); - }; - - const selectedOption = QUICK_SELECT_OPTIONS.find( - (option) => option.value === selectedTimeInterval.value && option.unit === selectedTimeInterval.unit, - ); - const displayLabel = isCustomDate ? getTimeRangeDisplay(isCustomDate, startTime, endTime) : selectedOption?.label; + const displayLabel = isCustomDate + ? getTimeRangeDisplay(true, startTime, endTime) + : QUICK_SELECT_OPTIONS.find((option) => option.id === range)?.label; return (
@@ -80,23 +60,19 @@ export function LogsTableToolbar({
{QUICK_SELECT_OPTIONS.map((option) => ( ))}
-
@@ -108,21 +84,17 @@ export function LogsTableToolbar({ { - onStartTimeChange(event.target.value); - onResetToFirstPage(); - }} + onChange={(event) => onStartTimeChange(event.target.value)} /> to { - onEndTimeChange(event.target.value); - onResetToFirstPage(); - }} + onChange={(event) => onEndTimeChange(event.target.value)} />
)} diff --git a/ui/litellm-dashboard/src/components/view_logs/RequestLogsPanel.test.tsx b/ui/litellm-dashboard/src/components/view_logs/RequestLogsPanel.test.tsx index fda3b08b30b..1566c906bf0 100644 --- a/ui/litellm-dashboard/src/components/view_logs/RequestLogsPanel.test.tsx +++ b/ui/litellm-dashboard/src/components/view_logs/RequestLogsPanel.test.tsx @@ -5,8 +5,9 @@ import moment from "moment"; import { NuqsTestingAdapter, type UrlUpdateEvent } from "nuqs/adapters/testing"; import { beforeEach, describe, expect, it, vi } from "vitest"; -import { render, renderWithProviders, testQueryClient } from "../../../tests/test-utils"; +import { chooseSelectOption, render, renderWithProviders, testQueryClient } from "../../../tests/test-utils"; import type { LogEntry } from "./columns"; +import type { PaginatedResponse } from "./log_filter_logic"; import RequestLogsPanel from "./RequestLogsPanel"; vi.mock("../networking", async (importOriginal) => { @@ -54,9 +55,30 @@ vi.mock("./LogDetailsDrawer", () => ({ }, })); +vi.mock("../templates/key_info_view", () => ({ + default: function KeyInfoViewMock({ + keyId, + onClose, + backButtonText, + }: { + keyId: string; + onClose: () => void; + backButtonText: string; + }) { + return ( +
+ +
+ ); + }, +})); + const debounce = vi.hoisted(() => ({ settled: null as string | null })); -vi.mock("@tanstack/react-pacer/debouncer", () => ({ +vi.mock("@tanstack/react-pacer/debouncer", async (importOriginal) => ({ + ...(await importOriginal()), useDebouncedValue: vi.fn((value: unknown) => [debounce.settled ?? value, { cancel: vi.fn(), flush: vi.fn() }]), })); @@ -102,6 +124,28 @@ const defaultProps = { const row = (requestId: string) => document.querySelector(`[data-row-id="${requestId}"]`); const lastCall = () => vi.mocked(uiSpendLogsCall).mock.calls.at(-1)?.[0]; +const windowSeconds = () => { + const call = lastCall(); + if (!call) throw new Error("uiSpendLogsCall was not called"); + return moment + .utc(call.end_date, "YYYY-MM-DD HH:mm:ss") + .diff(moment.utc(call.start_date, "YYYY-MM-DD HH:mm:ss"), "seconds"); +}; +const fullPage = (count: number, prefix = "req") => + Array.from({ length: count }, (_, index) => logEntry({ request_id: `${prefix}-${index}` })); +const SESSION_CURSOR = "2026-07-07 09:50:13|key-1|sess-1"; +const respondWithPages = (page: number, pageSize: number, total: number, cursor: string | null = null) => { + const response: PaginatedResponse = { + data: fullPage(pageSize), + total, + page, + page_size: pageSize, + total_pages: Math.ceil(total / pageSize), + next_session_cursor: cursor, + has_more: page * pageSize < total, + }; + vi.mocked(uiSpendLogsCall).mockResolvedValue(response); +}; const onUrlUpdate = vi.fn<(event: UrlUpdateEvent) => void>(); const renderPanel = (searchParams?: string) => @@ -313,6 +357,8 @@ describe("RequestLogsPanel", () => { fireEvent.click(screen.getByTestId("pagination-next")); await waitFor(() => expect(lastCall()?.page).toBe(2)); + await waitFor(() => expect(urlParams().get("page")).toBe("2")); + fireEvent.change(screen.getByTestId("datatable-search"), { target: { value: "req-elsewhere" } }); await waitFor(() => { @@ -488,18 +534,97 @@ describe("RequestLogsPanel", () => { await user.click(screen.getByRole("button", { name: /Last 24 Hours/i })); await user.click(await screen.findByRole("button", { name: "Last 15 Minutes" })); - const windowSeconds = () => { - const call = lastCall(); - if (!call) throw new Error("no call"); - return moment - .utc(call.end_date, "YYYY-MM-DD HH:mm:ss") - .diff(moment.utc(call.start_date, "YYYY-MM-DD HH:mm:ss"), "seconds"); - }; - await waitFor(() => expect(windowSeconds()).toBeGreaterThanOrEqual(15 * 60)); expect(windowSeconds()).toBeLessThanOrEqual(16 * 60); }); + it("restores a preset from ?range= and requests that window", async () => { + renderPanel("?range=15m"); + + await waitFor(() => expect(uiSpendLogsCall).toHaveBeenCalled()); + expect(windowSeconds()).toBeGreaterThanOrEqual(15 * 60); + expect(windowSeconds()).toBeLessThanOrEqual(16 * 60); + expect(screen.getByRole("button", { name: /Last 15 Minutes/i })).toBeInTheDocument(); + }); + + it("falls back to the default 24 hour window for an unknown ?range=", async () => { + renderPanel("?range=3y"); + + await waitFor(() => expect(uiSpendLogsCall).toHaveBeenCalled()); + expect(windowSeconds()).toBeGreaterThanOrEqual(24 * 60 * 60); + expect(windowSeconds()).toBeLessThanOrEqual(24 * 60 * 60 + 60); + expect(screen.getByRole("button", { name: /Last 24 Hours/i })).toBeInTheDocument(); + }); + + it("writes the picked preset to ?range= from page 1 and drops the key for the default preset", async () => { + const user = userEvent.setup(); + renderPanel("?page=2"); + + await waitFor(() => expect(lastCall()?.page).toBe(2)); + await user.click(screen.getByRole("button", { name: /Last 24 Hours/i })); + await user.click(await screen.findByRole("button", { name: "Last Hour" })); + + await waitFor(() => expect(urlParams().get("range")).toBe("1h")); + expect(urlParams().get("page")).toBeNull(); + await waitFor(() => expect(lastCall()?.page).toBe(1)); + + await user.click(screen.getByRole("button", { name: /Last Hour/i })); + await user.click(await screen.findByRole("button", { name: "Last 24 Hours" })); + + await waitFor(() => expect(urlParams().get("range")).toBeNull()); + }); + + it("restores a custom range from ?range=custom&start=&end= and requests exactly that window", async () => { + renderPanel("?range=custom&start=2026-07-01T10:00Z&end=2026-07-02T10:30Z"); + + await waitFor(() => expect(uiSpendLogsCall).toHaveBeenCalled()); + expect(lastCall()?.start_date).toBe("2026-07-01 10:00:00"); + expect(lastCall()?.end_date).toBe("2026-07-02 10:30:00"); + expect(screen.getByLabelText("Start time")).toHaveValue(moment("2026-07-01T10:00Z").format("YYYY-MM-DDTHH:mm")); + expect(screen.getByLabelText("End time")).toHaveValue(moment("2026-07-02T10:30Z").format("YYYY-MM-DDTHH:mm")); + }); + + it("toggling Custom Range writes ?range=custom with the current bounds and edits rewrite ?start= as UTC", async () => { + const user = userEvent.setup(); + renderPanel(); + + await waitFor(() => expect(uiSpendLogsCall).toHaveBeenCalled()); + await user.click(screen.getByRole("button", { name: /Last 24 Hours/i })); + await user.click(await screen.findByRole("button", { name: "Custom Range" })); + + await waitFor(() => expect(urlParams().get("range")).toBe("custom")); + const start = urlParams().get("start"); + const end = urlParams().get("end"); + if (start === null || end === null) throw new Error("custom bounds were not written"); + expect(moment.utc(end).diff(moment.utc(start), "hours")).toBe(24); + expect(screen.getByLabelText("Start time")).toHaveValue(moment.utc(start).local().format("YYYY-MM-DDTHH:mm")); + + fireEvent.change(screen.getByLabelText("Start time"), { target: { value: "2026-07-01T10:00" } }); + + await waitFor(() => + expect(urlParams().get("start")).toBe(moment("2026-07-01T10:00").utc().format("YYYY-MM-DDTHH:mm[Z]")), + ); + await waitFor(() => + expect(lastCall()?.start_date).toBe(moment("2026-07-01T10:00").utc().format("YYYY-MM-DD HH:mm:ss")), + ); + expect(lastCall()?.page).toBe(1); + }); + + it("Reset Filters clears the range, bounds and filters from the URL", async () => { + const user = userEvent.setup(); + renderPanel("?range=custom&start=2026-07-01T10:00Z&end=2026-07-02T10:30Z&filter_team=team-1&log_search=abc"); + + await waitFor(() => expect(lastCall()?.params?.team_id).toBe("team-1")); + await user.click(screen.getByRole("button", { name: "Reset Filters" })); + + await waitFor(() => expect(urlParams().get("range")).toBeNull()); + for (const key of ["start", "end", "filter_team", "log_search", "page"]) { + expect(urlParams().get(key)).toBeNull(); + } + expect(await screen.findByRole("button", { name: /Last 24 Hours/i })).toBeInTheDocument(); + await waitFor(() => expect(lastCall()?.params?.team_id).toBeUndefined()); + }); + it("restores the default 24 hour window when filters are reset", async () => { const user = userEvent.setup(); renderPanel(); @@ -754,37 +879,48 @@ describe("RequestLogsPanel", () => { }); }); - describe("hide health checks", () => { + describe("hide health checks (?hide_health_checks=)", () => { const toggle = () => screen.getByRole("switch", { name: "Hide Health Checks" }); - it("defaults to showing health checks and refetches without them from page 1 when toggled on", async () => { + it("defaults to showing health checks and writes ?hide_health_checks=true from page 1 when toggled on", async () => { const user = userEvent.setup(); + respondWithPages(1, 25, 80, SESSION_CURSOR); renderPanel(); - await waitFor(() => expect(uiSpendLogsCall).toHaveBeenCalled()); + await waitFor(() => expect(row("req-0")).not.toBeNull()); expect(lastCall()?.params?.exclude_internal_health_checks).toBe(false); expect(toggle()).not.toBeChecked(); + fireEvent.click(screen.getByTestId("pagination-next")); + await waitFor(() => expect(lastCall()?.page).toBe(2)); await user.click(toggle()); await waitFor(() => expect(lastCall()?.params?.exclude_internal_health_checks).toBe(true)); expect(lastCall()?.page).toBe(1); + expect(lastCall()?.params?.session_cursor).toBeUndefined(); expect(toggle()).toBeChecked(); - expect(sessionStorage.getItem("excludeInternalHealthChecks")).toBe("true"); + expect(urlParams().get("hide_health_checks")).toBe("true"); + expect(urlParams().get("page")).toBeNull(); + expect(sessionStorage.getItem("excludeInternalHealthChecks")).toBeNull(); }); - it("restores the persisted toggle from sessionStorage", async () => { - sessionStorage.setItem("excludeInternalHealthChecks", "true"); - renderPanel(); + it("restores the toggle from ?hide_health_checks=true and drops the key again when toggled off", async () => { + const user = userEvent.setup(); + renderPanel("?hide_health_checks=true"); await waitFor(() => expect(uiSpendLogsCall).toHaveBeenCalled()); expect(lastCall()?.params?.exclude_internal_health_checks).toBe(true); expect(toggle()).toBeChecked(); + + await user.click(toggle()); + + await waitFor(() => expect(lastCall()?.params?.exclude_internal_health_checks).toBe(false)); + expect(urlParams().get("hide_health_checks")).toBeNull(); }); - it("falls back to showing health checks when the persisted value is malformed", async () => { - sessionStorage.setItem("excludeInternalHealthChecks", "{not json"); - renderPanel(); + it("shows health checks for a malformed ?hide_health_checks= and ignores the old sessionStorage flag", async () => { + sessionStorage.setItem("excludeInternalHealthChecks", "true"); + renderPanel("?hide_health_checks=maybe"); await waitFor(() => expect(uiSpendLogsCall).toHaveBeenCalled()); expect(lastCall()?.params?.exclude_internal_health_checks).toBe(false); @@ -792,6 +928,175 @@ describe("RequestLogsPanel", () => { }); }); + describe("URL-persisted table state", () => { + it("restores page, page size, sort, search and filters from the URL without opening the drawer", async () => { + const expectedParams = { + sort_by: "spend", + sort_order: "asc", + search: "needle", + team_id: "team-9", + status_filter: "failure", + cache_hit_filter: "hit", + key_alias: "alias-9", + user_id: "user-9", + end_user: "end-9", + error_code: "429", + error_message: "boom", + api_key: "hash-9", + session_id: "sess-9", + model_id: "model-9", + model: "gpt-9", + request_id: "req-9", + }; + respondWithPages(3, 50, 200); + renderPanel( + "?page=3&page_size=50&sort_by=spend&sort_order=asc&log_search=needle" + + "&filter_team=team-9&filter_status=failure&filter_cache=hit&filter_key_alias=alias-9&filter_user=user-9" + + "&filter_end_user=end-9&filter_error_code=429&filter_error_message=boom&filter_key_hash=hash-9" + + "&filter_session=sess-9&filter_model_id=model-9&filter_model=gpt-9&filter_request_id=req-9", + ); + + await waitFor(() => expect(row("req-0")).not.toBeNull()); + const call = lastCall(); + if (!call) throw new Error("uiSpendLogsCall was not called"); + expect(call.page).toBe(3); + expect(call.page_size).toBe(50); + expect(call.params).toMatchObject(expectedParams); + expect(screen.getByTestId("datatable-search")).toHaveValue("needle"); + expect(screen.getByTestId("pagination-page")).toHaveTextContent("Page 3 of 4"); + expect(screen.getByTestId("filter-chip-session_id")).toHaveTextContent("Session ID:sess-9"); + expect(screen.getByTestId("log-details-drawer")).toHaveTextContent("closed"); + }); + + it("falls back to the default sort column when ?sort_by= is not a sortable log field", async () => { + renderPanel("?sort_by=api_key&sort_order=asc"); + + await waitFor(() => expect(uiSpendLogsCall).toHaveBeenCalled()); + expect(lastCall()?.params?.sort_by).toBe("startTime"); + expect(lastCall()?.params?.sort_order).toBe("asc"); + }); + + it("writes the search box to ?log_search= only and returns to the first page", async () => { + renderPanel("?page=2"); + + await waitFor(() => expect(lastCall()?.page).toBe(2)); + fireEvent.change(screen.getByTestId("datatable-search"), { target: { value: "req-77" } }); + + await waitFor(() => expect(urlParams().get("log_search")).toBe("req-77")); + expect(urlParams().get("search")).toBeNull(); + expect(urlParams().get("page")).toBeNull(); + await waitFor(() => expect(lastCall()?.page).toBe(1)); + }); + + it("writes sort changes to ?sort_by= and ?sort_order= and returns to the first page", async () => { + const user = userEvent.setup(); + respondWith([logEntry({ request_id: "req-a" })]); + renderPanel("?page=2"); + + await waitFor(() => expect(lastCall()?.page).toBe(2)); + await chooseSelectOption(user, screen.getByTestId("sort-trigger-spend"), /ascending/i, "menuitem"); + + await waitFor(() => expect(urlParams().get("sort_by")).toBe("spend")); + expect(urlParams().get("sort_order")).toBe("asc"); + expect(urlParams().get("page")).toBeNull(); + await waitFor(() => expect(lastCall()?.params?.sort_by).toBe("spend")); + expect(lastCall()?.params?.sort_order).toBe("asc"); + expect(lastCall()?.page).toBe(1); + }); + + it("writes the next page to ?page= and the page size to ?page_size=", async () => { + const user = userEvent.setup(); + respondWithPages(1, 25, 80, SESSION_CURSOR); + renderPanel(); + + await waitFor(() => expect(row("req-0")).not.toBeNull()); + fireEvent.click(screen.getByTestId("pagination-next")); + + await waitFor(() => expect(urlParams().get("page")).toBe("2")); + expect(urlParams().get("page_size")).toBeNull(); + + await chooseSelectOption(user, screen.getByTestId("pagination-page-size"), "50"); + + await waitFor(() => expect(urlParams().get("page_size")).toBe("50")); + expect(urlParams().get("page")).toBeNull(); + await waitFor(() => expect(lastCall()?.page_size).toBe(50)); + expect(lastCall()?.page).toBe(1); + expect(lastCall()?.params?.session_cursor).toBeUndefined(); + }); + + it("applying the Session ID filter writes ?filter_session= and leaves the drawer's ?session_id= untouched", async () => { + const user = userEvent.setup(); + renderPanel(); + + await waitFor(() => expect(uiSpendLogsCall).toHaveBeenCalled()); + await user.click(screen.getByTestId("datatable-filters-trigger")); + await user.type(await screen.findByPlaceholderText("Enter session ID…"), "sess-9"); + await user.click(screen.getByTestId("filter-drawer-apply")); + + await waitFor(() => expect(urlParams().get("filter_session")).toBe("sess-9")); + expect(urlParams().get("session_id")).toBeNull(); + expect(urlParams().get("log_id")).toBeNull(); + await waitFor(() => expect(lastCall()?.params?.session_id).toBe("sess-9")); + expect(screen.getByTestId("log-details-drawer")).toHaveTextContent("closed"); + }); + + it("removing the Search chip clears ?log_search= while other filters stay in the URL", async () => { + const user = userEvent.setup(); + renderPanel("?log_search=sess-42&filter_team=team-1"); + + await waitFor(() => expect(lastCall()?.params?.search).toBe("sess-42")); + await user.click(screen.getByRole("button", { name: "Remove Search filter" })); + + await waitFor(() => expect(urlParams().get("log_search")).toBeNull()); + expect(urlParams().get("filter_team")).toBe("team-1"); + expect(screen.getByTestId("datatable-search")).toHaveValue(""); + }); + + it("keeps a URL page while the panel is inactive and requests it once the tab becomes active", async () => { + respondWithPages(3, 25, 80); + const view = renderWithProviders(, { + searchParams: "?page=3", + onUrlUpdate, + }); + + expect(uiSpendLogsCall).not.toHaveBeenCalled(); + view.rerender(); + + await waitFor(() => expect(uiSpendLogsCall).toHaveBeenCalled()); + expect(vi.mocked(uiSpendLogsCall).mock.calls[0]?.[0].page).toBe(3); + }); + }); + + describe("key hash takeover (?key=)", () => { + const keyInfoView = () => screen.getByTestId("key-info-view"); + + it("clicking a key hash pushes ?key= and swaps the table for KeyInfoView", async () => { + const user = userEvent.setup(); + respondWith([logEntry({ request_id: "req-1", metadata: { user_api_key: "sk-hash-9" } })]); + renderPanel(); + + await waitFor(() => expect(row("req-1")).not.toBeNull()); + await user.click(screen.getByText("sk-hash-9")); + + await waitFor(() => expect(urlParams().get("key")).toBe("sk-hash-9")); + expect(urlParams().get("log_id")).toBeNull(); + expect(historyModes()).toEqual(["push"]); + await waitFor(() => expect(keyInfoView()).toHaveAttribute("data-key-id", "sk-hash-9")); + expect(screen.queryByTestId("datatable-search")).not.toBeInTheDocument(); + }); + + it("opens KeyInfoView from ?key= on load and Back to Logs clears it", async () => { + const user = userEvent.setup(); + renderPanel("?key=sk-hash-9"); + + await waitFor(() => expect(keyInfoView()).toHaveAttribute("data-key-id", "sk-hash-9")); + await user.click(screen.getByRole("button", { name: "Back to Logs" })); + + await waitFor(() => expect(urlParams().get("key")).toBeNull()); + expect(await screen.findByTestId("datatable-search")).toBeInTheDocument(); + }); + }); + describe("live tail", () => { it("shows the auto-refresh banner on the first page and hides it once stopped", async () => { const user = userEvent.setup(); diff --git a/ui/litellm-dashboard/src/components/view_logs/RequestLogsPanel.tsx b/ui/litellm-dashboard/src/components/view_logs/RequestLogsPanel.tsx index 0b5e5ff9616..21c60b85a3f 100644 --- a/ui/litellm-dashboard/src/components/view_logs/RequestLogsPanel.tsx +++ b/ui/litellm-dashboard/src/components/view_logs/RequestLogsPanel.tsx @@ -2,17 +2,24 @@ import { useDebouncedValue } from "@tanstack/react-pacer/debouncer"; import { useQuery, type UseQueryOptions } from "@tanstack/react-query"; -import type { ColumnFiltersState, OnChangeFn, PaginationState, SortingState } from "@tanstack/react-table"; +import { + functionalUpdate, + type ColumnFiltersState, + type OnChangeFn, + type PaginationState, + type SortingState, +} from "@tanstack/react-table"; import moment from "moment"; +import { parseAsBoolean, parseAsString, useQueryState } from "nuqs"; import { useCallback, useEffect, useMemo, useState } from "react"; -import { DEFAULT_PAGE_SIZE_OPTIONS } from "@/components/shared/DataTable"; +import { DEFAULT_PAGE_SIZE_OPTIONS, useUrlTableState, type UrlTableStateOptions } from "@/components/shared/DataTable"; import { AutoRouterModelGroupsProvider } from "@/components/shared/table_cells"; import { DEBOUNCE_WAIT_MS } from "@/utils/debounceConstants"; import type { KeyResponse } from "../key_team_helpers/key_list"; import { keyInfoV1Call, uiSpendLogsCall } from "../networking"; import KeyInfoView from "../templates/key_info_view"; -import type { LogEntry } from "./columns"; +import { LOGS_SORT_FIELD_MAP, type LogEntry } from "./columns"; import { DEFAULT_LOGS_SORTING, formatLogsWindow, @@ -25,13 +32,52 @@ import { useLogDetailRouting } from "./logDetailRouting"; import { LogDetailsDrawer } from "./LogDetailsDrawer"; import { LiveTailBanner, LogsTableToolbar } from "./LogsTableToolbar"; import { RequestLogsTable } from "./RequestLogsTable"; +import { CUSTOM_RANGE, useLogsTimeRange } from "./useLogsTimeRange"; const PAGE_SIZE = DEFAULT_PAGE_SIZE_OPTIONS[0]; -const DEFAULT_INTERVAL = { value: 24, unit: "hours" }; const matchesLogId = (log: LogEntry, logId: string) => log.request_id === logId || log.litellm_call_id === logId; const findLogById = (logs: readonly LogEntry[], logId: string): LogEntry | null => logs.find((log) => log.request_id === logId) ?? logs.find((log) => log.litellm_call_id === logId) ?? null; +const FILTER_COLUMNS = [ + LOG_FILTER_IDS.TEAM_ID, + LOG_FILTER_IDS.STATUS, + LOG_FILTER_IDS.CACHE_STATUS, + LOG_FILTER_IDS.KEY_ALIAS, + LOG_FILTER_IDS.END_USER, + LOG_FILTER_IDS.ERROR_CODE, + LOG_FILTER_IDS.ERROR_MESSAGE, + LOG_FILTER_IDS.KEY_HASH, + LOG_FILTER_IDS.SESSION_ID, + LOG_FILTER_IDS.MODEL_ID, + LOG_FILTER_IDS.PUBLIC_MODEL_OR_SEARCH_TOOL, + LOG_FILTER_IDS.REQUEST_ID, + LOG_FILTER_IDS.USER_ID, +] as const; +type FilterColumn = (typeof FILTER_COLUMNS)[number]; + +const TABLE_STATE_OPTIONS: UrlTableStateOptions = { + sortFields: Object.keys(LOGS_SORT_FIELD_MAP), + defaultSort: DEFAULT_LOGS_SORTING[0], + defaultPageSize: PAGE_SIZE, + filterColumns: FILTER_COLUMNS, + urlKeys: { + search: "log_search", + filter_team_id: "filter_team", + filter_cache_hit: "filter_cache", + filter_user_id: "filter_user", + filter_session_id: "filter_session", + }, +}; + +const withSearchFilter = (filters: ColumnFiltersState, search: string): ColumnFiltersState => + search === "" ? filters : [...filters, { id: LOG_FILTER_IDS.SEARCH, value: search }]; + +const searchFilterValue = (filters: ColumnFiltersState): string => { + const value = filters.find((filter) => filter.id === LOG_FILTER_IDS.SEARCH)?.value; + return typeof value === "string" ? value : ""; +}; + interface RequestLogsPanelProps { accessToken: string; token: string; @@ -41,19 +87,40 @@ interface RequestLogsPanelProps { } export default function RequestLogsPanel({ accessToken, token, userRole, userID, isActive }: RequestLogsPanelProps) { - const [pagination, setPagination] = useState({ pageIndex: 0, pageSize: PAGE_SIZE }); - const [sorting, setSorting] = useState(DEFAULT_LOGS_SORTING); - const [columnFilters, setColumnFilters] = useState([]); + const { + search, + setSearch, + sorting, + onSortingChange, + pagination, + onPaginationChange, + columnFilters: urlColumnFilters, + onColumnFiltersChange, + } = useUrlTableState(TABLE_STATE_OPTIONS); const [sessionCursors, setSessionCursors] = useState>({}); - - const [startTime, setStartTime] = useState(moment().subtract(24, "hours").format("YYYY-MM-DDTHH:mm")); - const [endTime, setEndTime] = useState(moment().format("YYYY-MM-DDTHH:mm")); - const [isCustomDate, setIsCustomDate] = useState(false); - const [selectedTimeInterval, setSelectedTimeInterval] = useState<{ value: number; unit: string }>(DEFAULT_INTERVAL); - - const [selectedKeyIdInfoView, setSelectedKeyIdInfoView] = useState(null); + const [excludeInternalHealthChecks, setExcludeInternalHealthChecks] = useQueryState( + "hide_health_checks", + parseAsBoolean.withDefault(false), + ); + const [selectedKeyId, setSelectedKeyId] = useQueryState("key", parseAsString.withOptions({ history: "push" })); const [selectedLog, setSelectedLog] = useState(null); + const resetToFirstPage = useCallback(() => { + setSessionCursors({}); + onPaginationChange((previous) => ({ ...previous, pageIndex: 0 })); + }, [onPaginationChange]); + + const { + timeRange, + selectPreset, + toggleCustomRange, + setStartTime, + setEndTime, + reset: resetTimeRange, + } = useLogsTimeRange(resetToFirstPage); + const { startTime, endTime } = timeRange; + const isCustomDate = timeRange.range === CUSTOM_RANGE; + const { logId: urlLogId, sessionId: urlSessionId, @@ -72,24 +139,13 @@ export default function RequestLogsPanel({ accessToken, token, userRole, userID, sessionStorage.setItem("isLiveTail", JSON.stringify(isLiveTail)); }, [isLiveTail]); - const [excludeInternalHealthChecks, setExcludeInternalHealthChecks] = useState( - () => sessionStorage.getItem("excludeInternalHealthChecks") === "true", + const columnFilters = useMemo(() => withSearchFilter(urlColumnFilters, search), [urlColumnFilters, search]); + const [debouncedSearch] = useDebouncedValue(search, { wait: DEBOUNCE_WAIT_MS }); + const queryColumnFilters = useMemo( + () => withSearchFilter(urlColumnFilters, debouncedSearch), + [urlColumnFilters, debouncedSearch], ); - useEffect(() => { - sessionStorage.setItem("excludeInternalHealthChecks", JSON.stringify(excludeInternalHealthChecks)); - }, [excludeInternalHealthChecks]); - - const searchTerm = useMemo(() => { - const entry = columnFilters.find((filter) => filter.id === LOG_FILTER_IDS.SEARCH); - return typeof entry?.value === "string" ? entry.value : ""; - }, [columnFilters]); - const [debouncedSearch] = useDebouncedValue(searchTerm, { wait: DEBOUNCE_WAIT_MS }); - const queryColumnFilters = useMemo(() => { - const others = columnFilters.filter((filter) => filter.id !== LOG_FILTER_IDS.SEARCH); - return debouncedSearch === "" ? others : [...others, { id: LOG_FILTER_IDS.SEARCH, value: debouncedSearch }]; - }, [columnFilters, debouncedSearch]); - const { logsQuery, filteredLogs, allTeams, usesSessionCursor } = useLogFilterLogic({ accessToken, token, @@ -116,17 +172,17 @@ export default function RequestLogsPanel({ accessToken, token, userRole, userID, ); const keyInfoQueryOptions: UseQueryOptions = { - queryKey: ["requestLogsKeyInfo", selectedKeyIdInfoView, accessToken], + queryKey: ["requestLogsKeyInfo", selectedKeyId, accessToken], queryFn: async () => { - if (selectedKeyIdInfoView === null) return null; - const keyData = await keyInfoV1Call(accessToken, selectedKeyIdInfoView); + if (selectedKeyId === null) return null; + const keyData = await keyInfoV1Call(accessToken, selectedKeyId); return { ...keyData["info"], - token: selectedKeyIdInfoView, - api_key: selectedKeyIdInfoView, + token: selectedKeyId, + api_key: selectedKeyId, }; }, - enabled: selectedKeyIdInfoView !== null, + enabled: selectedKeyId !== null, }; const { data: selectedKeyInfo } = useQuery(keyInfoQueryOptions); @@ -174,72 +230,70 @@ export default function RequestLogsPanel({ accessToken, token, userRole, userID, filteredLogs.has_more === false || (filteredLogs.has_more === undefined && rows.length < pagination.pageSize); const rowCount = isLastPage ? rowsThroughThisPage : Math.max(filteredLogs.total, rowsThroughThisPage); - const handleSearchChange = useCallback((value: string) => { - setColumnFilters((previous) => { - const others = previous.filter((filter) => filter.id !== LOG_FILTER_IDS.SEARCH); - return value === "" ? others : [...others, { id: LOG_FILTER_IDS.SEARCH, value }]; - }); - setSessionCursors({}); - setPagination((previous) => ({ ...previous, pageIndex: 0 })); - }, []); + const handleSearchChange = useCallback( + (value: string) => { + setSearch(value); + setSessionCursors({}); + }, + [setSearch], + ); - const handleSortingChange = useCallback>((updaterOrValue) => { - setSorting(updaterOrValue); - setSessionCursors({}); - setPagination((previous) => ({ ...previous, pageIndex: 0 })); - }, []); + const handleSortingChange = useCallback>( + (updaterOrValue) => { + onSortingChange(updaterOrValue); + setSessionCursors({}); + }, + [onSortingChange], + ); - const handleColumnFiltersChange = useCallback>((updaterOrValue) => { - setColumnFilters(updaterOrValue); - setSessionCursors({}); - setPagination((previous) => ({ ...previous, pageIndex: 0 })); - }, []); - - const resetToFirstPage = useCallback(() => { - setSessionCursors({}); - setPagination((previous) => ({ ...previous, pageIndex: 0 })); - }, []); + const handleColumnFiltersChange = useCallback>( + (updaterOrValue) => { + const next = functionalUpdate(updaterOrValue, columnFilters); + onColumnFiltersChange(next.filter((filter) => filter.id !== LOG_FILTER_IDS.SEARCH)); + const nextSearch = searchFilterValue(next); + if (nextSearch !== search) setSearch(nextSearch); + setSessionCursors({}); + }, + [columnFilters, onColumnFiltersChange, search, setSearch], + ); const handlePaginationChange = useCallback>( (updaterOrValue) => { - const requested = typeof updaterOrValue === "function" ? updaterOrValue(pagination) : updaterOrValue; + const requested = functionalUpdate(updaterOrValue, pagination); if (!usesSessionCursor) { - setPagination(requested); + onPaginationChange(requested); return; } if (requested.pageSize !== pagination.pageSize) { setSessionCursors({}); - setPagination({ ...requested, pageIndex: 0 }); + onPaginationChange({ ...requested, pageIndex: 0 }); return; } if (requested.pageIndex !== pagination.pageIndex + 1) { - setPagination(requested); + onPaginationChange(requested); return; } const nextCursor = filteredLogs.next_session_cursor; if (!nextCursor || logsQuery.isPlaceholderData) return; setSessionCursors((previous) => ({ ...previous, [requested.pageIndex]: nextCursor })); - setPagination(requested); + onPaginationChange(requested); }, - [usesSessionCursor, pagination, filteredLogs.next_session_cursor, logsQuery.isPlaceholderData], + [usesSessionCursor, pagination, onPaginationChange, filteredLogs.next_session_cursor, logsQuery.isPlaceholderData], ); const handleExcludeInternalHealthChecksChange = useCallback( (value: boolean) => { - setExcludeInternalHealthChecks(value); + void setExcludeInternalHealthChecks(value); resetToFirstPage(); }, - [resetToFirstPage], + [setExcludeInternalHealthChecks, resetToFirstPage], ); const handleResetFilters = useCallback(() => { - setColumnFilters([]); - setStartTime(moment().subtract(24, "hours").format("YYYY-MM-DDTHH:mm")); - setEndTime(moment().format("YYYY-MM-DDTHH:mm")); - setIsCustomDate(false); - setSelectedTimeInterval(DEFAULT_INTERVAL); - resetToFirstPage(); - }, [resetToFirstPage]); + onColumnFiltersChange([]); + setSearch(""); + resetTimeRange(); + }, [onColumnFiltersChange, setSearch, resetTimeRange]); const handleRowClick = useCallback( (log: LogEntry) => { @@ -270,17 +324,20 @@ export default function RequestLogsPanel({ accessToken, token, userRole, userID, [selectLog, displaySessionId], ); - const handleKeyHashClick = useCallback((keyHash: string) => { - setSelectedKeyIdInfoView(keyHash); - }, []); + const handleKeyHashClick = useCallback( + (keyHash: string) => { + void setSelectedKeyId(keyHash); + }, + [setSelectedKeyId], + ); - if (selectedKeyInfo && selectedKeyIdInfoView && selectedKeyInfo.api_key === selectedKeyIdInfoView) { + if (selectedKeyInfo && selectedKeyId && selectedKeyInfo.api_key === selectedKeyId) { return ( setSelectedKeyIdInfoView(null)} + onClose={() => void setSelectedKeyId(null)} backButtonText="Back to Logs" /> ); @@ -297,7 +354,8 @@ export default function RequestLogsPanel({ accessToken, token, userRole, userID, void logsQuery.refetch()} onRowClick={handleRowClick} @@ -315,19 +373,15 @@ export default function RequestLogsPanel({ accessToken, token, userRole, userID, logsWindow={logsWindow} toolbarChildren={ } diff --git a/ui/litellm-dashboard/src/components/view_logs/RequestLogsTable.tsx b/ui/litellm-dashboard/src/components/view_logs/RequestLogsTable.tsx index c3d204e1ae3..942e9855d5a 100644 --- a/ui/litellm-dashboard/src/components/view_logs/RequestLogsTable.tsx +++ b/ui/litellm-dashboard/src/components/view_logs/RequestLogsTable.tsx @@ -16,6 +16,7 @@ interface RequestLogsTableProps { data: LogEntry[]; rowCount: number; isLoading: boolean; + isError?: boolean; isRefreshing: boolean; pagination: PaginationState; onPaginationChange: OnChangeFn; @@ -54,6 +55,7 @@ export function RequestLogsTable({ data, rowCount, isLoading, + isError, isRefreshing, pagination, onPaginationChange, @@ -97,6 +99,7 @@ export function RequestLogsTable({ columnFilters={columnFilters} onColumnFiltersChange={onColumnFiltersChange} isLoading={isLoading} + isError={isError} loadingMessage="Loading request logs…" noDataMessage={} size="compact" diff --git a/ui/litellm-dashboard/src/components/view_logs/constants.ts b/ui/litellm-dashboard/src/components/view_logs/constants.ts index ae44d53e217..0c5a3abfe36 100644 --- a/ui/litellm-dashboard/src/components/view_logs/constants.ts +++ b/ui/litellm-dashboard/src/components/view_logs/constants.ts @@ -21,11 +21,17 @@ export const AGENT_CALL_TYPES = ["asend_message"]; /** Call types that represent Batch API operations (creation and retrieval, sync and async). */ export const BATCH_CALL_TYPES = ["acreate_batch", "create_batch", "aretrieve_batch", "retrieve_batch"]; -export const QUICK_SELECT_OPTIONS: { label: string; value: number; unit: string }[] = [ - { label: "Last Minute", value: 1, unit: "minutes" }, - { label: "Last 15 Minutes", value: 15, unit: "minutes" }, - { label: "Last Hour", value: 1, unit: "hours" }, - { label: "Last 4 Hours", value: 4, unit: "hours" }, - { label: "Last 24 Hours", value: 24, unit: "hours" }, - { label: "Last 7 Days", value: 7, unit: "days" }, -]; +export const QUICK_SELECT_OPTIONS = [ + { id: "1m", label: "Last Minute", value: 1, unit: "minutes" }, + { id: "15m", label: "Last 15 Minutes", value: 15, unit: "minutes" }, + { id: "1h", label: "Last Hour", value: 1, unit: "hours" }, + { id: "4h", label: "Last 4 Hours", value: 4, unit: "hours" }, + { id: "24h", label: "Last 24 Hours", value: 24, unit: "hours" }, + { id: "7d", label: "Last 7 Days", value: 7, unit: "days" }, +] as const; + +export type QuickSelectPresetId = (typeof QUICK_SELECT_OPTIONS)[number]["id"]; + +export const QUICK_SELECT_PRESET_IDS: readonly QuickSelectPresetId[] = QUICK_SELECT_OPTIONS.map((option) => option.id); + +export const DEFAULT_QUICK_SELECT_PRESET: QuickSelectPresetId = "24h"; diff --git a/ui/litellm-dashboard/src/components/view_logs/useLogsTimeRange.test.tsx b/ui/litellm-dashboard/src/components/view_logs/useLogsTimeRange.test.tsx new file mode 100644 index 00000000000..df0d8367b70 --- /dev/null +++ b/ui/litellm-dashboard/src/components/view_logs/useLogsTimeRange.test.tsx @@ -0,0 +1,152 @@ +import { act, render, renderHook, waitFor } from "@testing-library/react"; +import moment from "moment"; +import { NuqsTestingAdapter, withNuqsTestingAdapter, type OnUrlUpdateFunction } from "nuqs/adapters/testing"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; + +import { LOCAL_DATETIME_FORMAT, useLogsTimeRange, type LogsTimeRange } from "./useLogsTimeRange"; + +const renderTimeRange = (searchParams = "") => { + const onUrlUpdate = vi.fn(); + const onChange = vi.fn(); + const hook = renderHook(() => useLogsTimeRange(onChange), { + wrapper: withNuqsTestingAdapter({ searchParams, onUrlUpdate, hasMemory: true }), + }); + return { ...hook, onUrlUpdate, onChange }; +}; + +const lastParams = (onUrlUpdate: ReturnType>) => + onUrlUpdate.mock.calls.at(-1)?.[0].searchParams ?? new URLSearchParams(); + +const minutesBetween = (startTime: string, endTime: string) => + moment(endTime, LOCAL_DATETIME_FORMAT).diff(moment(startTime, LOCAL_DATETIME_FORMAT), "minutes"); + +describe("useLogsTimeRange", () => { + beforeEach(() => { + vi.useFakeTimers({ shouldAdvanceTime: true }); + vi.setSystemTime(new Date("2026-07-07T10:00:00Z")); + }); + + afterEach(() => { + vi.useRealTimers(); + }); + + it("anchors a preset's bounds at mount time so the query window does not drift per render", () => { + const { result, rerender } = renderTimeRange("?range=15m"); + + const initial = result.current.timeRange; + expect(minutesBetween(initial.startTime, initial.endTime)).toBe(15); + expect(initial.endTime).toBe(moment().format(LOCAL_DATETIME_FORMAT)); + + vi.setSystemTime(new Date("2026-07-07T10:05:00Z")); + rerender(); + + expect(result.current.timeRange).toBe(initial); + }); + + it("re-anchors from now when the same preset is picked again", async () => { + const { result, onChange } = renderTimeRange("?range=15m"); + const initial = result.current.timeRange; + + vi.setSystemTime(new Date("2026-07-07T10:05:00Z")); + act(() => result.current.selectPreset("15m")); + + await waitFor(() => expect(result.current.timeRange.endTime).toBe(moment().format(LOCAL_DATETIME_FORMAT))); + expect(minutesBetween(result.current.timeRange.startTime, result.current.timeRange.endTime)).toBe(15); + expect(result.current.timeRange.startTime).not.toBe(initial.startTime); + expect(onChange).toHaveBeenCalledTimes(1); + }); + + it("recomputes the bounds from now when the URL range changes underneath it, as on Back", () => { + const seen: LogsTimeRange[] = []; + function Probe() { + seen.push(useLogsTimeRange(vi.fn()).timeRange); + return null; + } + const tree = (searchParams: string) => ( + + + + ); + const view = render(tree("?range=1h")); + const initial = seen.at(-1); + if (!initial) throw new Error("hook did not render"); + expect(minutesBetween(initial.startTime, initial.endTime)).toBe(60); + + vi.setSystemTime(new Date("2026-07-07T12:00:00Z")); + view.rerender(tree("?range=15m")); + + const afterBack = seen.at(-1); + if (!afterBack) throw new Error("hook did not re-render"); + expect(afterBack.range).toBe("15m"); + expect(afterBack.endTime).toBe(moment().format(LOCAL_DATETIME_FORMAT)); + expect(minutesBetween(afterBack.startTime, afterBack.endTime)).toBe(15); + }); + + it("round-trips a custom range through UTC minute-precision ISO strings", async () => { + const { result, onUrlUpdate } = renderTimeRange("?range=custom&start=2026-07-01T10:00Z&end=2026-07-02T10:30Z"); + + expect(result.current.timeRange).toEqual({ + range: "custom", + startTime: moment("2026-07-01T10:00Z").format(LOCAL_DATETIME_FORMAT), + endTime: moment("2026-07-02T10:30Z").format(LOCAL_DATETIME_FORMAT), + }); + + act(() => result.current.setEndTime("2026-07-03T08:15")); + + await waitFor(() => + expect(lastParams(onUrlUpdate).get("end")).toBe(moment("2026-07-03T08:15").utc().format("YYYY-MM-DDTHH:mm[Z]")), + ); + expect(result.current.timeRange.endTime).toBe("2026-07-03T08:15"); + }); + + it("treats an unparsable or cleared custom bound as empty instead of inventing a date", async () => { + const { result, onUrlUpdate } = renderTimeRange("?range=custom&start=not-a-date&end=2026-07-02T10:30Z"); + + expect(result.current.timeRange.startTime).toBe(""); + + act(() => result.current.setEndTime("")); + + await waitFor(() => expect(lastParams(onUrlUpdate).has("end")).toBe(false)); + expect(result.current.timeRange.endTime).toBe(""); + }); + + it("carries the current preset bounds into ?start= and ?end= when Custom Range is toggled on", async () => { + const { result, onUrlUpdate, onChange } = renderTimeRange("?range=4h"); + const { startTime, endTime } = result.current.timeRange; + + act(() => result.current.toggleCustomRange()); + + await waitFor(() => expect(lastParams(onUrlUpdate).get("range")).toBe("custom")); + expect(lastParams(onUrlUpdate).get("start")).toBe( + moment(startTime, LOCAL_DATETIME_FORMAT).utc().format("YYYY-MM-DDTHH:mm[Z]"), + ); + expect(lastParams(onUrlUpdate).get("end")).toBe( + moment(endTime, LOCAL_DATETIME_FORMAT).utc().format("YYYY-MM-DDTHH:mm[Z]"), + ); + expect(result.current.timeRange).toEqual({ range: "custom", startTime, endTime }); + expect(onChange).toHaveBeenCalledTimes(1); + + act(() => result.current.toggleCustomRange()); + + await waitFor(() => expect(lastParams(onUrlUpdate).has("range")).toBe(false)); + expect(lastParams(onUrlUpdate).has("start")).toBe(false); + expect(result.current.timeRange.range).toBe("24h"); + expect(minutesBetween(result.current.timeRange.startTime, result.current.timeRange.endTime)).toBe(24 * 60); + }); + + it("reset clears every time range key and refreshes the default bounds from now", async () => { + const { result, onUrlUpdate, onChange } = renderTimeRange( + "?range=custom&start=2026-07-01T10:00Z&end=2026-07-02T10:30Z", + ); + + vi.setSystemTime(new Date("2026-07-07T13:00:00Z")); + act(() => result.current.reset()); + + await waitFor(() => expect(lastParams(onUrlUpdate).has("range")).toBe(false)); + expect(lastParams(onUrlUpdate).has("start")).toBe(false); + expect(lastParams(onUrlUpdate).has("end")).toBe(false); + expect(result.current.timeRange.range).toBe("24h"); + expect(result.current.timeRange.endTime).toBe(moment().format(LOCAL_DATETIME_FORMAT)); + expect(onChange).toHaveBeenCalledTimes(1); + }); +}); diff --git a/ui/litellm-dashboard/src/components/view_logs/useLogsTimeRange.ts b/ui/litellm-dashboard/src/components/view_logs/useLogsTimeRange.ts new file mode 100644 index 00000000000..f7f3a75eaf5 --- /dev/null +++ b/ui/litellm-dashboard/src/components/view_logs/useLogsTimeRange.ts @@ -0,0 +1,119 @@ +import moment from "moment"; +import { createParser, parseAsStringLiteral, useQueryStates } from "nuqs"; +import { useCallback, useMemo, useState } from "react"; + +import { + DEFAULT_QUICK_SELECT_PRESET, + QUICK_SELECT_OPTIONS, + QUICK_SELECT_PRESET_IDS, + type QuickSelectPresetId, +} from "./constants"; + +export const CUSTOM_RANGE = "custom"; +export type LogsRange = QuickSelectPresetId | typeof CUSTOM_RANGE; + +export const LOCAL_DATETIME_FORMAT = "YYYY-MM-DDTHH:mm"; +const LOCAL_DATETIME_INPUT_FORMATS = [LOCAL_DATETIME_FORMAT, `${LOCAL_DATETIME_FORMAT}:ss`]; +const URL_DATETIME_FORMAT = "YYYY-MM-DDTHH:mm[Z]"; + +const RANGE_IDS: readonly LogsRange[] = [...QUICK_SELECT_PRESET_IDS, CUSTOM_RANGE]; + +const isLocalDateTime = (value: string): boolean => moment(value, LOCAL_DATETIME_INPUT_FORMATS, true).isValid(); + +const parseAsLocalDateTime = createParser({ + parse: (value) => { + const parsed = moment(value, moment.ISO_8601, true); + return parsed.isValid() ? parsed.format(LOCAL_DATETIME_FORMAT) : null; + }, + serialize: (value) => moment(value, LOCAL_DATETIME_INPUT_FORMATS, true).utc().format(URL_DATETIME_FORMAT), +}); + +const TIME_RANGE_PARSERS = { + range: parseAsStringLiteral(RANGE_IDS).withDefault(DEFAULT_QUICK_SELECT_PRESET), + start: parseAsLocalDateTime, + end: parseAsLocalDateTime, +}; + +export interface LogsTimeRange { + range: LogsRange; + startTime: string; + endTime: string; +} + +const DEFAULT_PRESET = + QUICK_SELECT_OPTIONS.find((option) => option.id === DEFAULT_QUICK_SELECT_PRESET) ?? QUICK_SELECT_OPTIONS[0]; + +const presetBounds = (range: LogsRange): LogsTimeRange => { + const preset = QUICK_SELECT_OPTIONS.find((option) => option.id === range) ?? DEFAULT_PRESET; + const now = moment(); + return { + range, + startTime: now.clone().subtract(preset.value, preset.unit).format(LOCAL_DATETIME_FORMAT), + endTime: now.format(LOCAL_DATETIME_FORMAT), + }; +}; + +export interface LogsTimeRangeState { + timeRange: LogsTimeRange; + selectPreset: (preset: QuickSelectPresetId) => void; + toggleCustomRange: () => void; + setStartTime: (value: string) => void; + setEndTime: (value: string) => void; + reset: () => void; +} + +export function useLogsTimeRange(onChange: () => void): LogsTimeRangeState { + const [{ range, start, end }, setParams] = useQueryStates(TIME_RANGE_PARSERS); + const [anchored, setAnchored] = useState(() => presetBounds(range)); + if (anchored.range !== range) setAnchored(presetBounds(range)); + + const isCustom = range === CUSTOM_RANGE; + const startTime = isCustom ? start ?? "" : anchored.startTime; + const endTime = isCustom ? end ?? "" : anchored.endTime; + const timeRange = useMemo(() => ({ range, startTime, endTime }), [range, startTime, endTime]); + + const selectPreset = useCallback( + (preset: QuickSelectPresetId) => { + void setParams({ range: preset, start: null, end: null }); + setAnchored(presetBounds(preset)); + onChange(); + }, + [setParams, onChange], + ); + + const toggleCustomRange = useCallback(() => { + void setParams( + isCustom + ? { range: null, start: null, end: null } + : { range: CUSTOM_RANGE, start: startTime || null, end: endTime || null }, + ); + onChange(); + }, [setParams, isCustom, startTime, endTime, onChange]); + + const setStartTime = useCallback( + (value: string) => { + void setParams({ start: isLocalDateTime(value) ? value : null }); + onChange(); + }, + [setParams, onChange], + ); + + const setEndTime = useCallback( + (value: string) => { + void setParams({ end: isLocalDateTime(value) ? value : null }); + onChange(); + }, + [setParams, onChange], + ); + + const reset = useCallback(() => { + void setParams(null); + setAnchored(presetBounds(DEFAULT_QUICK_SELECT_PRESET)); + onChange(); + }, [setParams, onChange]); + + return useMemo( + () => ({ timeRange, selectPreset, toggleCustomRange, setStartTime, setEndTime, reset }), + [timeRange, selectPreset, toggleCustomRange, setStartTime, setEndTime, reset], + ); +} From ce06ee0eb77eb009e1fa881891003add848dd717 Mon Sep 17 00:00:00 2001 From: ryan-crabbe-berri Date: Wed, 16 Sep 2026 09:19:07 -0700 Subject: [PATCH 2/3] fix(ui): clamp restored logs pages in one step and keep the last preset A ?page= past the end of the Request Logs list used to walk back one page per render, because an empty page counted only the rows before it. With keepPreviousData each step saw the same empty placeholder and fired another request, so ?page=500 on a three page list sent hundreds of calls. An empty page after the first now takes the server total, which the backend counts for exactly that case, so the table clamps straight to the real last page. Turning Custom Range off now returns to the preset picked before it with bounds recomputed from now, instead of always dropping to the 24 hour default. A URL change from custom back to a preset also re-anchors its bounds rather than reusing the ones from before custom was picked. The URL filter keys are now derived from LOG_FILTER_IDS, so a new log filter gets a filter_ key without a second list to update. --- .../view_logs/RequestLogsPanel.test.tsx | 66 ++++++++++++++++++- .../components/view_logs/RequestLogsPanel.tsx | 34 ++++------ .../view_logs/useLogsTimeRange.test.tsx | 62 +++++++++++++++-- .../components/view_logs/useLogsTimeRange.ts | 42 ++++++++---- 4 files changed, 164 insertions(+), 40 deletions(-) diff --git a/ui/litellm-dashboard/src/components/view_logs/RequestLogsPanel.test.tsx b/ui/litellm-dashboard/src/components/view_logs/RequestLogsPanel.test.tsx index 1566c906bf0..a7052e95df0 100644 --- a/ui/litellm-dashboard/src/components/view_logs/RequestLogsPanel.test.tsx +++ b/ui/litellm-dashboard/src/components/view_logs/RequestLogsPanel.test.tsx @@ -3,7 +3,7 @@ import { fireEvent, screen, waitFor, within } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import moment from "moment"; import { NuqsTestingAdapter, type UrlUpdateEvent } from "nuqs/adapters/testing"; -import { beforeEach, describe, expect, it, vi } from "vitest"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { chooseSelectOption, render, renderWithProviders, testQueryClient } from "../../../tests/test-utils"; import type { LogEntry } from "./columns"; @@ -191,6 +191,10 @@ describe("RequestLogsPanel", () => { debounce.settled = null; }); + afterEach(() => { + vi.useRealTimers(); + }); + describe("server-grouped session pagination (#38060)", () => { it("requests session-grouped pages of 25 rows by default without a cursor", async () => { renderPanel(); @@ -610,6 +614,34 @@ describe("RequestLogsPanel", () => { expect(lastCall()?.page).toBe(1); }); + it("toggling Custom Range off goes back to the preset picked before it, anchored at the current time", async () => { + vi.useFakeTimers({ toFake: ["Date"] }); + vi.setSystemTime(new Date("2026-07-07T10:00:00Z")); + const user = userEvent.setup(); + renderPanel(); + + await waitFor(() => expect(uiSpendLogsCall).toHaveBeenCalled()); + await user.click(screen.getByRole("button", { name: /Last 24 Hours/i })); + await user.click(await screen.findByRole("button", { name: "Last Hour" })); + await user.click(await screen.findByRole("button", { name: /Last Hour/i })); + await user.click(await screen.findByRole("button", { name: "Custom Range" })); + await waitFor(() => expect(urlParams().get("range")).toBe("custom")); + fireEvent.change(screen.getByLabelText("Start time"), { target: { value: "2026-07-01T10:00" } }); + await waitFor(() => + expect(lastCall()?.start_date).toBe(moment("2026-07-01T10:00").utc().format("YYYY-MM-DD HH:mm:ss")), + ); + + vi.setSystemTime(new Date("2026-07-07T10:05:00Z")); + await user.click(screen.getByRole("button", { name: "Custom Range" })); + + await waitFor(() => expect(urlParams().get("range")).toBe("1h")); + expect(urlParams().get("start")).toBeNull(); + expect(urlParams().get("end")).toBeNull(); + expect(screen.queryByLabelText("Start time")).not.toBeInTheDocument(); + await waitFor(() => expect(lastCall()?.end_date).toBe("2026-07-07 10:05:00")); + expect(lastCall()?.start_date).toBe("2026-07-07 09:05:00"); + }); + it("Reset Filters clears the range, bounds and filters from the URL", async () => { const user = userEvent.setup(); renderPanel("?range=custom&start=2026-07-01T10:00Z&end=2026-07-02T10:30Z&filter_team=team-1&log_search=abc"); @@ -968,6 +1000,23 @@ describe("RequestLogsPanel", () => { expect(screen.getByTestId("log-details-drawer")).toHaveTextContent("closed"); }); + it("clamps a URL page past the end straight to the last page without requesting the pages in between", async () => { + vi.mocked(uiSpendLogsCall).mockImplementation(async ({ page }) => { + const shared = { total: 60, page, page_size: 25, total_pages: 3 }; + if (page === 40) return { ...shared, data: [], next_session_cursor: null, has_more: false }; + if (page === 3) + return { ...shared, data: fullPage(10, "req-last"), next_session_cursor: null, has_more: false }; + return { ...shared, data: fullPage(25), next_session_cursor: SESSION_CURSOR, has_more: true }; + }); + renderPanel("?page=40"); + + await waitFor(() => expect(row("req-last-0")).not.toBeNull()); + expect(screen.getByTestId("pagination-page")).toHaveTextContent("Page 3 of 3"); + expect(screen.getByTestId("pagination-range")).toHaveTextContent("Showing 51-60 of 60"); + expect(urlParams().get("page")).toBe("3"); + expect(vi.mocked(uiSpendLogsCall).mock.calls.map(([options]) => options.page)).toEqual([40, 3]); + }); + it("falls back to the default sort column when ?sort_by= is not a sortable log field", async () => { renderPanel("?sort_by=api_key&sort_order=asc"); @@ -1095,6 +1144,21 @@ describe("RequestLogsPanel", () => { await waitFor(() => expect(urlParams().get("key")).toBeNull()); expect(await screen.findByTestId("datatable-search")).toBeInTheDocument(); }); + + it("Back after opening a key closes KeyInfoView and shows the logs table again", async () => { + const user = userEvent.setup(); + respondWith([logEntry({ request_id: "req-1", metadata: { user_api_key: "sk-hash-9" } })]); + const { goBack } = renderPanelWithHistory(); + + await waitFor(() => expect(row("req-1")).not.toBeNull()); + await user.click(screen.getByText("sk-hash-9")); + await waitFor(() => expect(keyInfoView()).toHaveAttribute("data-key-id", "sk-hash-9")); + + goBack(); + + await waitFor(() => expect(screen.queryByTestId("key-info-view")).not.toBeInTheDocument()); + expect(row("req-1")).not.toBeNull(); + }); }); describe("live tail", () => { diff --git a/ui/litellm-dashboard/src/components/view_logs/RequestLogsPanel.tsx b/ui/litellm-dashboard/src/components/view_logs/RequestLogsPanel.tsx index 21c60b85a3f..b0815fb8870 100644 --- a/ui/litellm-dashboard/src/components/view_logs/RequestLogsPanel.tsx +++ b/ui/litellm-dashboard/src/components/view_logs/RequestLogsPanel.tsx @@ -39,22 +39,11 @@ const matchesLogId = (log: LogEntry, logId: string) => log.request_id === logId const findLogById = (logs: readonly LogEntry[], logId: string): LogEntry | null => logs.find((log) => log.request_id === logId) ?? logs.find((log) => log.litellm_call_id === logId) ?? null; -const FILTER_COLUMNS = [ - LOG_FILTER_IDS.TEAM_ID, - LOG_FILTER_IDS.STATUS, - LOG_FILTER_IDS.CACHE_STATUS, - LOG_FILTER_IDS.KEY_ALIAS, - LOG_FILTER_IDS.END_USER, - LOG_FILTER_IDS.ERROR_CODE, - LOG_FILTER_IDS.ERROR_MESSAGE, - LOG_FILTER_IDS.KEY_HASH, - LOG_FILTER_IDS.SESSION_ID, - LOG_FILTER_IDS.MODEL_ID, - LOG_FILTER_IDS.PUBLIC_MODEL_OR_SEARCH_TOOL, - LOG_FILTER_IDS.REQUEST_ID, - LOG_FILTER_IDS.USER_ID, -] as const; -type FilterColumn = (typeof FILTER_COLUMNS)[number]; +type LogFilterId = (typeof LOG_FILTER_IDS)[keyof typeof LOG_FILTER_IDS]; +type FilterColumn = Exclude; + +const isFilterColumn = (id: LogFilterId): id is FilterColumn => id !== LOG_FILTER_IDS.SEARCH; +const FILTER_COLUMNS: readonly FilterColumn[] = Object.values(LOG_FILTER_IDS).filter(isFilterColumn); const TABLE_STATE_OPTIONS: UrlTableStateOptions = { sortFields: Object.keys(LOGS_SORT_FIELD_MAP), @@ -78,6 +67,14 @@ const searchFilterValue = (filters: ColumnFiltersState): string => { return typeof value === "string" ? value : ""; }; +const tableRowCount = (response: PaginatedResponse, { pageIndex, pageSize }: PaginationState): number => { + const pageRows = response.data.length; + if (pageRows === 0 && pageIndex > 0) return response.total; + const rowsThroughThisPage = pageIndex * pageSize + pageRows; + const isLastPage = response.has_more === false || (response.has_more === undefined && pageRows < pageSize); + return isLastPage ? rowsThroughThisPage : Math.max(response.total, rowsThroughThisPage); +}; + interface RequestLogsPanelProps { accessToken: string; token: string; @@ -225,10 +222,7 @@ export default function RequestLogsPanel({ accessToken, token, userRole, userID, const isDrawerOpen = displayLog !== null || displaySessionId !== null; const rows: LogEntry[] = filteredLogs.data; - const rowsThroughThisPage = pagination.pageIndex * pagination.pageSize + rows.length; - const isLastPage = - filteredLogs.has_more === false || (filteredLogs.has_more === undefined && rows.length < pagination.pageSize); - const rowCount = isLastPage ? rowsThroughThisPage : Math.max(filteredLogs.total, rowsThroughThisPage); + const rowCount = tableRowCount(filteredLogs, pagination); const handleSearchChange = useCallback( (value: string) => { diff --git a/ui/litellm-dashboard/src/components/view_logs/useLogsTimeRange.test.tsx b/ui/litellm-dashboard/src/components/view_logs/useLogsTimeRange.test.tsx index df0d8367b70..d5852afe19c 100644 --- a/ui/litellm-dashboard/src/components/view_logs/useLogsTimeRange.test.tsx +++ b/ui/litellm-dashboard/src/components/view_logs/useLogsTimeRange.test.tsx @@ -8,9 +8,8 @@ import { LOCAL_DATETIME_FORMAT, useLogsTimeRange, type LogsTimeRange } from "./u const renderTimeRange = (searchParams = "") => { const onUrlUpdate = vi.fn(); const onChange = vi.fn(); - const hook = renderHook(() => useLogsTimeRange(onChange), { - wrapper: withNuqsTestingAdapter({ searchParams, onUrlUpdate, hasMemory: true }), - }); + const adapterProps = { searchParams, onUrlUpdate, hasMemory: true, resetUrlUpdateQueueOnMount: false }; + const hook = renderHook(() => useLogsTimeRange(onChange), { wrapper: withNuqsTestingAdapter(adapterProps) }); return { ...hook, onUrlUpdate, onChange }; }; @@ -125,15 +124,68 @@ describe("useLogsTimeRange", () => { ); expect(result.current.timeRange).toEqual({ range: "custom", startTime, endTime }); expect(onChange).toHaveBeenCalledTimes(1); + }); + + it("toggling Custom Range off returns to the preset picked before it, with bounds refreshed from now", async () => { + const { result, onUrlUpdate, onChange } = renderTimeRange("?range=4h"); + + act(() => result.current.toggleCustomRange()); + await waitFor(() => expect(lastParams(onUrlUpdate).get("range")).toBe("custom")); + act(() => result.current.setStartTime("2026-07-01T10:00")); + await waitFor(() => + expect(lastParams(onUrlUpdate).get("start")).toBe(moment("2026-07-01T10:00").utc().format("YYYY-MM-DDTHH:mm[Z]")), + ); + expect(result.current.timeRange.startTime).toBe("2026-07-01T10:00"); + + vi.setSystemTime(new Date("2026-07-07T11:30:00Z")); + act(() => result.current.toggleCustomRange()); + + await waitFor(() => expect(lastParams(onUrlUpdate).get("range")).toBe("4h")); + expect(lastParams(onUrlUpdate).has("start")).toBe(false); + expect(lastParams(onUrlUpdate).has("end")).toBe(false); + await waitFor(() => expect(result.current.timeRange.range).toBe("4h")); + expect(result.current.timeRange.endTime).toBe(moment().format(LOCAL_DATETIME_FORMAT)); + expect(minutesBetween(result.current.timeRange.startTime, result.current.timeRange.endTime)).toBe(4 * 60); + expect(onChange).toHaveBeenCalledTimes(3); + }); + + it("falls back to the default preset when a custom range restored from the URL is toggled off", async () => { + const { result, onUrlUpdate } = renderTimeRange("?range=custom&start=2026-07-01T10:00Z&end=2026-07-02T10:30Z"); act(() => result.current.toggleCustomRange()); - await waitFor(() => expect(lastParams(onUrlUpdate).has("range")).toBe(false)); + await waitFor(() => expect(result.current.timeRange.range).toBe("24h")); + expect(lastParams(onUrlUpdate).has("range")).toBe(false); expect(lastParams(onUrlUpdate).has("start")).toBe(false); - expect(result.current.timeRange.range).toBe("24h"); + expect(result.current.timeRange.endTime).toBe(moment().format(LOCAL_DATETIME_FORMAT)); expect(minutesBetween(result.current.timeRange.startTime, result.current.timeRange.endTime)).toBe(24 * 60); }); + it("re-anchors from now when the URL leaves a custom range for the preset it started from", () => { + const seen: LogsTimeRange[] = []; + function Probe() { + seen.push(useLogsTimeRange(vi.fn()).timeRange); + return null; + } + const tree = (searchParams: string) => ( + + + + ); + const view = render(tree("?range=1h")); + view.rerender(tree("?range=custom&start=2026-07-01T10:00Z&end=2026-07-02T10:30Z")); + expect(seen.at(-1)?.range).toBe("custom"); + + vi.setSystemTime(new Date("2026-07-07T12:00:00Z")); + view.rerender(tree("?range=1h")); + + const restored = seen.at(-1); + if (!restored) throw new Error("hook did not re-render"); + expect(restored.range).toBe("1h"); + expect(restored.endTime).toBe(moment().format(LOCAL_DATETIME_FORMAT)); + expect(minutesBetween(restored.startTime, restored.endTime)).toBe(60); + }); + it("reset clears every time range key and refreshes the default bounds from now", async () => { const { result, onUrlUpdate, onChange } = renderTimeRange( "?range=custom&start=2026-07-01T10:00Z&end=2026-07-02T10:30Z", diff --git a/ui/litellm-dashboard/src/components/view_logs/useLogsTimeRange.ts b/ui/litellm-dashboard/src/components/view_logs/useLogsTimeRange.ts index f7f3a75eaf5..24fe0f38b04 100644 --- a/ui/litellm-dashboard/src/components/view_logs/useLogsTimeRange.ts +++ b/ui/litellm-dashboard/src/components/view_logs/useLogsTimeRange.ts @@ -40,19 +40,32 @@ export interface LogsTimeRange { endTime: string; } -const DEFAULT_PRESET = - QUICK_SELECT_OPTIONS.find((option) => option.id === DEFAULT_QUICK_SELECT_PRESET) ?? QUICK_SELECT_OPTIONS[0]; +interface PresetWindow { + preset: QuickSelectPresetId; + startTime: string; + endTime: string; +} -const presetBounds = (range: LogsRange): LogsTimeRange => { - const preset = QUICK_SELECT_OPTIONS.find((option) => option.id === range) ?? DEFAULT_PRESET; +interface Anchor { + range: LogsRange; + window: PresetWindow; +} + +const presetWindow = (preset: QuickSelectPresetId): PresetWindow => { + const option = QUICK_SELECT_OPTIONS.find((candidate) => candidate.id === preset) ?? QUICK_SELECT_OPTIONS[0]; const now = moment(); return { - range, - startTime: now.clone().subtract(preset.value, preset.unit).format(LOCAL_DATETIME_FORMAT), + preset, + startTime: now.clone().subtract(option.value, option.unit).format(LOCAL_DATETIME_FORMAT), endTime: now.format(LOCAL_DATETIME_FORMAT), }; }; +const anchorFor = (range: LogsRange, previous: PresetWindow | null): Anchor => { + if (range !== CUSTOM_RANGE) return { range, window: presetWindow(range) }; + return { range, window: previous ?? presetWindow(DEFAULT_QUICK_SELECT_PRESET) }; +}; + export interface LogsTimeRangeState { timeRange: LogsTimeRange; selectPreset: (preset: QuickSelectPresetId) => void; @@ -64,18 +77,19 @@ export interface LogsTimeRangeState { export function useLogsTimeRange(onChange: () => void): LogsTimeRangeState { const [{ range, start, end }, setParams] = useQueryStates(TIME_RANGE_PARSERS); - const [anchored, setAnchored] = useState(() => presetBounds(range)); - if (anchored.range !== range) setAnchored(presetBounds(range)); + const [anchor, setAnchor] = useState(() => anchorFor(range, null)); + if (anchor.range !== range) setAnchor(anchorFor(range, anchor.window)); const isCustom = range === CUSTOM_RANGE; - const startTime = isCustom ? start ?? "" : anchored.startTime; - const endTime = isCustom ? end ?? "" : anchored.endTime; + const lastPreset = anchor.window.preset; + const startTime = isCustom ? start ?? "" : anchor.window.startTime; + const endTime = isCustom ? end ?? "" : anchor.window.endTime; const timeRange = useMemo(() => ({ range, startTime, endTime }), [range, startTime, endTime]); const selectPreset = useCallback( (preset: QuickSelectPresetId) => { void setParams({ range: preset, start: null, end: null }); - setAnchored(presetBounds(preset)); + setAnchor(anchorFor(preset, null)); onChange(); }, [setParams, onChange], @@ -84,11 +98,11 @@ export function useLogsTimeRange(onChange: () => void): LogsTimeRangeState { const toggleCustomRange = useCallback(() => { void setParams( isCustom - ? { range: null, start: null, end: null } + ? { range: lastPreset, start: null, end: null } : { range: CUSTOM_RANGE, start: startTime || null, end: endTime || null }, ); onChange(); - }, [setParams, isCustom, startTime, endTime, onChange]); + }, [setParams, isCustom, lastPreset, startTime, endTime, onChange]); const setStartTime = useCallback( (value: string) => { @@ -108,7 +122,7 @@ export function useLogsTimeRange(onChange: () => void): LogsTimeRangeState { const reset = useCallback(() => { void setParams(null); - setAnchored(presetBounds(DEFAULT_QUICK_SELECT_PRESET)); + setAnchor(anchorFor(DEFAULT_QUICK_SELECT_PRESET, null)); onChange(); }, [setParams, onChange]); From 894ac1b25e432431dd2921fb50cc0afe7357f737 Mon Sep 17 00:00:00 2001 From: ryan-crabbe-berri Date: Wed, 16 Sep 2026 09:51:11 -0700 Subject: [PATCH 3/3] fix(ui): address review on logs-request url state Session keyset cursors are now tied to the query they came from, so Back across a drawer or key push can no longer page an unfiltered list with a cursor from a filtered one. A custom range with a missing or unparsable bound queries the anchor window instead of sending Invalid date, while the input stays empty. The ?key= view shows a loading state and falls through to the not-found view when the key fails to load. Tests pin a non-UTC zone for the UTC contract and cover the sort allow-list, the isError wiring, preset-only bounds and Live Tail storage --- .../components/view_logs/LogsTableToolbar.tsx | 6 +- .../view_logs/RequestLogsPanel.test.tsx | 176 ++++++++++++++++-- .../components/view_logs/RequestLogsPanel.tsx | 81 +++++--- .../src/components/view_logs/constants.ts | 10 +- .../view_logs/useLogsTimeRange.test.tsx | 70 ++++--- .../components/view_logs/useLogsTimeRange.ts | 18 +- 6 files changed, 277 insertions(+), 84 deletions(-) diff --git a/ui/litellm-dashboard/src/components/view_logs/LogsTableToolbar.tsx b/ui/litellm-dashboard/src/components/view_logs/LogsTableToolbar.tsx index 07e7394235c..eccf965fddf 100644 --- a/ui/litellm-dashboard/src/components/view_logs/LogsTableToolbar.tsx +++ b/ui/litellm-dashboard/src/components/view_logs/LogsTableToolbar.tsx @@ -38,7 +38,7 @@ export function LogsTableToolbar({ onResetFilters, }: LogsTableToolbarProps) { const [quickSelectOpen, setQuickSelectOpen] = useState(false); - const { range, startTime, endTime } = timeRange; + const { range, startTime, endTime, startInput, endInput } = timeRange; const isCustomDate = range === CUSTOM_RANGE; const displayLabel = isCustomDate @@ -85,7 +85,7 @@ export function LogsTableToolbar({ type="datetime-local" className="w-auto" aria-label="Start time" - value={startTime} + value={startInput} onChange={(event) => onStartTimeChange(event.target.value)} /> to @@ -93,7 +93,7 @@ export function LogsTableToolbar({ type="datetime-local" className="w-auto" aria-label="End time" - value={endTime} + value={endInput} onChange={(event) => onEndTimeChange(event.target.value)} />
diff --git a/ui/litellm-dashboard/src/components/view_logs/RequestLogsPanel.test.tsx b/ui/litellm-dashboard/src/components/view_logs/RequestLogsPanel.test.tsx index a7052e95df0..dc47d7655a9 100644 --- a/ui/litellm-dashboard/src/components/view_logs/RequestLogsPanel.test.tsx +++ b/ui/litellm-dashboard/src/components/view_logs/RequestLogsPanel.test.tsx @@ -58,15 +58,17 @@ vi.mock("./LogDetailsDrawer", () => ({ vi.mock("../templates/key_info_view", () => ({ default: function KeyInfoViewMock({ keyId, + keyData, onClose, backButtonText, }: { keyId: string; + keyData?: object; onClose: () => void; backButtonText: string; }) { return ( -
+
@@ -84,7 +86,7 @@ vi.mock("@tanstack/react-pacer/debouncer", async (importOriginal) => ({ import { useDebouncedValue } from "@tanstack/react-pacer/debouncer"; import { DEBOUNCE_WAIT_MS } from "@/utils/debounceConstants"; -import { uiSpendLogsCall } from "../networking"; +import { keyInfoV1Call, uiSpendLogsCall } from "../networking"; const logEntry = (overrides: Partial): LogEntry => ({ request_id: "req-1", @@ -530,6 +532,14 @@ describe("RequestLogsPanel", () => { }); describe("time range", () => { + beforeEach(() => { + vi.stubEnv("TZ", "America/New_York"); + }); + + afterEach(() => { + vi.unstubAllEnvs(); + }); + it("requests a ~15 minute window when Last 15 Minutes is picked", async () => { const user = userEvent.setup(); renderPanel(); @@ -562,6 +572,7 @@ describe("RequestLogsPanel", () => { it("writes the picked preset to ?range= from page 1 and drops the key for the default preset", async () => { const user = userEvent.setup(); + respondWithPages(2, 25, 80, SESSION_CURSOR); renderPanel("?page=2"); await waitFor(() => expect(lastCall()?.page).toBe(2)); @@ -584,11 +595,40 @@ describe("RequestLogsPanel", () => { await waitFor(() => expect(uiSpendLogsCall).toHaveBeenCalled()); expect(lastCall()?.start_date).toBe("2026-07-01 10:00:00"); expect(lastCall()?.end_date).toBe("2026-07-02 10:30:00"); - expect(screen.getByLabelText("Start time")).toHaveValue(moment("2026-07-01T10:00Z").format("YYYY-MM-DDTHH:mm")); - expect(screen.getByLabelText("End time")).toHaveValue(moment("2026-07-02T10:30Z").format("YYYY-MM-DDTHH:mm")); + expect(screen.getByLabelText("Start time")).toHaveValue("2026-07-01T06:00"); + expect(screen.getByLabelText("End time")).toHaveValue("2026-07-02T06:30"); + }); + + it("queries the default window instead of an invalid date when a custom bound is missing or unparsable", async () => { + vi.useFakeTimers({ toFake: ["Date"] }); + vi.setSystemTime(new Date("2026-07-07T10:00:00Z")); + const sqlTimestamp = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/; + renderPanel("?range=custom&start=not-a-date"); + + await waitFor(() => expect(uiSpendLogsCall).toHaveBeenCalled()); + expect(lastCall()?.start_date).toBe("2026-07-06 10:00:00"); + expect(lastCall()?.end_date).toBe("2026-07-07 10:00:00"); + const sentDates = vi + .mocked(uiSpendLogsCall) + .mock.calls.flatMap(([options]) => [options.start_date, options.end_date]); + expect(sentDates.every((value) => sqlTimestamp.test(value ?? ""))).toBe(true); + expect(screen.getByLabelText("Start time")).toHaveValue(""); + expect(screen.getByLabelText("End time")).toHaveValue(""); + }); + + it("ignores ?start= and ?end= when the range is a preset", async () => { + renderPanel("?range=1h&start=2026-07-01T10:00Z&end=2026-07-02T10:30Z"); + + await waitFor(() => expect(uiSpendLogsCall).toHaveBeenCalled()); + expect(windowSeconds()).toBeGreaterThanOrEqual(60 * 60); + expect(windowSeconds()).toBeLessThanOrEqual(61 * 60); + expect(screen.getByRole("button", { name: /Last Hour/i })).toBeInTheDocument(); + expect(screen.queryByLabelText("Start time")).not.toBeInTheDocument(); }); it("toggling Custom Range writes ?range=custom with the current bounds and edits rewrite ?start= as UTC", async () => { + vi.useFakeTimers({ toFake: ["Date"] }); + vi.setSystemTime(new Date("2026-07-07T10:00:00Z")); const user = userEvent.setup(); renderPanel(); @@ -597,20 +637,14 @@ describe("RequestLogsPanel", () => { await user.click(await screen.findByRole("button", { name: "Custom Range" })); await waitFor(() => expect(urlParams().get("range")).toBe("custom")); - const start = urlParams().get("start"); - const end = urlParams().get("end"); - if (start === null || end === null) throw new Error("custom bounds were not written"); - expect(moment.utc(end).diff(moment.utc(start), "hours")).toBe(24); - expect(screen.getByLabelText("Start time")).toHaveValue(moment.utc(start).local().format("YYYY-MM-DDTHH:mm")); + expect(urlParams().get("start")).toBe("2026-07-06T10:00Z"); + expect(urlParams().get("end")).toBe("2026-07-07T10:00Z"); + expect(screen.getByLabelText("Start time")).toHaveValue("2026-07-06T06:00"); fireEvent.change(screen.getByLabelText("Start time"), { target: { value: "2026-07-01T10:00" } }); - await waitFor(() => - expect(urlParams().get("start")).toBe(moment("2026-07-01T10:00").utc().format("YYYY-MM-DDTHH:mm[Z]")), - ); - await waitFor(() => - expect(lastCall()?.start_date).toBe(moment("2026-07-01T10:00").utc().format("YYYY-MM-DD HH:mm:ss")), - ); + await waitFor(() => expect(urlParams().get("start")).toBe("2026-07-01T14:00Z")); + await waitFor(() => expect(lastCall()?.start_date).toBe("2026-07-01 14:00:00")); expect(lastCall()?.page).toBe(1); }); @@ -627,9 +661,7 @@ describe("RequestLogsPanel", () => { await user.click(await screen.findByRole("button", { name: "Custom Range" })); await waitFor(() => expect(urlParams().get("range")).toBe("custom")); fireEvent.change(screen.getByLabelText("Start time"), { target: { value: "2026-07-01T10:00" } }); - await waitFor(() => - expect(lastCall()?.start_date).toBe(moment("2026-07-01T10:00").utc().format("YYYY-MM-DD HH:mm:ss")), - ); + await waitFor(() => expect(lastCall()?.start_date).toBe("2026-07-01 14:00:00")); vi.setSystemTime(new Date("2026-07-07T10:05:00Z")); await user.click(screen.getByRole("button", { name: "Custom Range" })); @@ -644,7 +676,10 @@ describe("RequestLogsPanel", () => { it("Reset Filters clears the range, bounds and filters from the URL", async () => { const user = userEvent.setup(); - renderPanel("?range=custom&start=2026-07-01T10:00Z&end=2026-07-02T10:30Z&filter_team=team-1&log_search=abc"); + respondWithPages(2, 25, 80, SESSION_CURSOR); + renderPanel( + "?page=2&range=custom&start=2026-07-01T10:00Z&end=2026-07-02T10:30Z&filter_team=team-1&log_search=abc", + ); await waitFor(() => expect(lastCall()?.params?.team_id).toBe("team-1")); await user.click(screen.getByRole("button", { name: "Reset Filters" })); @@ -1023,9 +1058,22 @@ describe("RequestLogsPanel", () => { await waitFor(() => expect(uiSpendLogsCall).toHaveBeenCalled()); expect(lastCall()?.params?.sort_by).toBe("startTime"); expect(lastCall()?.params?.sort_order).toBe("asc"); + expect(screen.getByTestId("sort-trigger-startTime").innerHTML).toContain('data-sort-indicator="asc"'); + }); + + it("keeps a URL page when the fetch fails instead of clamping it back to page 1", async () => { + vi.mocked(uiSpendLogsCall).mockRejectedValue(new Error("boom")); + renderPanel("?page=3"); + + await waitFor(() => expect(uiSpendLogsCall).toHaveBeenCalled()); + await waitFor(() => expect(screen.queryAllByTestId("skeleton-row")).toHaveLength(0)); + await expect(waitFor(() => expect(uiSpendLogsCall).toHaveBeenCalledTimes(2), { timeout: 300 })).rejects.toThrow(); + expect(vi.mocked(uiSpendLogsCall).mock.calls.map(([options]) => options.page)).toEqual([3]); + expect(onUrlUpdate).not.toHaveBeenCalled(); }); it("writes the search box to ?log_search= only and returns to the first page", async () => { + respondWithPages(2, 25, 80, SESSION_CURSOR); renderPanel("?page=2"); await waitFor(() => expect(lastCall()?.page).toBe(2)); @@ -1116,6 +1164,61 @@ describe("RequestLogsPanel", () => { }); }); + describe("session cursors across browser history", () => { + it("does not send another filter's keyset cursor when Back restores an unfiltered page", async () => { + const user = userEvent.setup(); + vi.mocked(uiSpendLogsCall).mockImplementation(async ({ page = 1, params }) => { + const scope = params?.session_id ?? "all"; + return { + data: fullPage(25, `${scope}-p${page}`), + total: 500, + page, + page_size: 25, + total_pages: 20, + next_session_cursor: `${scope}|${page}`, + has_more: true, + }; + }); + const { goBack } = renderPanelWithHistory(); + const goToNextPage = async (scope: string, page: number) => { + fireEvent.click(screen.getByTestId("pagination-next")); + await waitFor(() => expect(row(`${scope}-p${page}-0`)).not.toBeNull()); + }; + + await waitFor(() => expect(row("all-p1-0")).not.toBeNull()); + await goToNextPage("all", 2); + await goToNextPage("all", 3); + expect(lastCall()?.params?.session_cursor).toBe("all|2"); + + await user.click(row("all-p3-0") as HTMLElement); + await waitFor(() => expect(urlParams().get("log_id")).toBe("all-p3-0")); + await user.click(screen.getByRole("button", { name: "close-drawer" })); + await waitFor(() => expect(urlParams().get("log_id")).toBeNull()); + + await user.click(screen.getByTestId("datatable-filters-trigger")); + await user.type(await screen.findByPlaceholderText("Enter session ID…"), "sess-b"); + await user.click(screen.getByTestId("filter-drawer-apply")); + await waitFor(() => expect(row("sess-b-p1-0")).not.toBeNull()); + await goToNextPage("sess-b", 2); + await goToNextPage("sess-b", 3); + expect(lastCall()?.params?.session_cursor).toBe("sess-b|2"); + + const callsBeforeBack = vi.mocked(uiSpendLogsCall).mock.calls.length; + goBack(); + + await waitFor(() => expect(row("all-p3-0")).not.toBeNull()); + const unfilteredPageThreeCalls = vi + .mocked(uiSpendLogsCall) + .mock.calls.slice(callsBeforeBack) + .map(([options]) => options) + .filter((options) => options.page === 3 && options.page_size === 25 && !options.params?.session_id); + expect(unfilteredPageThreeCalls.length).toBeGreaterThan(0); + expect(unfilteredPageThreeCalls.map((options) => options.params?.session_cursor)).toEqual( + unfilteredPageThreeCalls.map(() => undefined), + ); + }); + }); + describe("key hash takeover (?key=)", () => { const keyInfoView = () => screen.getByTestId("key-info-view"); @@ -1145,6 +1248,30 @@ describe("RequestLogsPanel", () => { expect(await screen.findByTestId("datatable-search")).toBeInTheDocument(); }); + it("shows a loading state instead of the logs table while ?key= resolves", async () => { + vi.mocked(keyInfoV1Call).mockImplementationOnce(() => new Promise(() => {})); + renderPanel("?key=sk-hash-9"); + + expect(await screen.findByText("Loading key...")).toBeInTheDocument(); + expect(screen.queryByTestId("datatable-search")).not.toBeInTheDocument(); + expect(screen.queryByTestId("key-info-view")).not.toBeInTheDocument(); + }); + + it("shows KeyInfoView's not-found state when ?key= fails to load, and Back to Logs clears the key", async () => { + const user = userEvent.setup(); + vi.mocked(keyInfoV1Call).mockRejectedValueOnce(new Error("key not found")); + renderPanel("?key=sk-bogus"); + + await waitFor(() => expect(keyInfoView()).toHaveAttribute("data-key-found", "false")); + expect(keyInfoView()).toHaveAttribute("data-key-id", "sk-bogus"); + expect(screen.queryByTestId("datatable-search")).not.toBeInTheDocument(); + + await user.click(screen.getByRole("button", { name: "Back to Logs" })); + + await waitFor(() => expect(urlParams().get("key")).toBeNull()); + expect(await screen.findByTestId("datatable-search")).toBeInTheDocument(); + }); + it("Back after opening a key closes KeyInfoView and shows the logs table again", async () => { const user = userEvent.setup(); respondWith([logEntry({ request_id: "req-1", metadata: { user_api_key: "sk-hash-9" } })]); @@ -1171,6 +1298,17 @@ describe("RequestLogsPanel", () => { await user.click(screen.getByRole("button", { name: "Stop" })); expect(screen.queryByText("Auto-refreshing every 15 seconds")).not.toBeInTheDocument(); + expect(sessionStorage.getItem("isLiveTail")).toBe("false"); + expect(onUrlUpdate).not.toHaveBeenCalled(); + }); + + it("stays off on load when it was stopped earlier in this browser tab", async () => { + sessionStorage.setItem("isLiveTail", "false"); + renderPanel(); + + await waitFor(() => expect(uiSpendLogsCall).toHaveBeenCalled()); + expect(screen.queryByText("Auto-refreshing every 15 seconds")).not.toBeInTheDocument(); + expect(screen.getByRole("switch", { name: "Live Tail" })).not.toBeChecked(); }); }); }); diff --git a/ui/litellm-dashboard/src/components/view_logs/RequestLogsPanel.tsx b/ui/litellm-dashboard/src/components/view_logs/RequestLogsPanel.tsx index b0815fb8870..8543cee8694 100644 --- a/ui/litellm-dashboard/src/components/view_logs/RequestLogsPanel.tsx +++ b/ui/litellm-dashboard/src/components/view_logs/RequestLogsPanel.tsx @@ -7,7 +7,6 @@ import { type ColumnFiltersState, type OnChangeFn, type PaginationState, - type SortingState, } from "@tanstack/react-table"; import moment from "moment"; import { parseAsBoolean, parseAsString, useQueryState } from "nuqs"; @@ -67,6 +66,18 @@ const searchFilterValue = (filters: ColumnFiltersState): string => { return typeof value === "string" ? value : ""; }; +type SessionCursors = Readonly>; + +interface ScopedSessionCursors { + scope: string; + cursors: SessionCursors; +} + +const NO_SESSION_CURSORS: SessionCursors = {}; + +const cursorsInScope = (stored: ScopedSessionCursors, scope: string): SessionCursors => + stored.scope === scope ? stored.cursors : NO_SESSION_CURSORS; + const tableRowCount = (response: PaginatedResponse, { pageIndex, pageSize }: PaginationState): number => { const pageRows = response.data.length; if (pageRows === 0 && pageIndex > 0) return response.total; @@ -94,7 +105,10 @@ export default function RequestLogsPanel({ accessToken, token, userRole, userID, columnFilters: urlColumnFilters, onColumnFiltersChange, } = useUrlTableState(TABLE_STATE_OPTIONS); - const [sessionCursors, setSessionCursors] = useState>({}); + const [storedSessionCursors, setStoredSessionCursors] = useState({ + scope: "", + cursors: NO_SESSION_CURSORS, + }); const [excludeInternalHealthChecks, setExcludeInternalHealthChecks] = useQueryState( "hide_health_checks", parseAsBoolean.withDefault(false), @@ -103,7 +117,6 @@ export default function RequestLogsPanel({ accessToken, token, userRole, userID, const [selectedLog, setSelectedLog] = useState(null); const resetToFirstPage = useCallback(() => { - setSessionCursors({}); onPaginationChange((previous) => ({ ...previous, pageIndex: 0 })); }, [onPaginationChange]); @@ -143,6 +156,21 @@ export default function RequestLogsPanel({ accessToken, token, userRole, userID, [urlColumnFilters, debouncedSearch], ); + const sessionCursorScope = useMemo( + () => + JSON.stringify([ + queryColumnFilters, + sorting, + startTime, + endTime, + isCustomDate, + excludeInternalHealthChecks, + pagination.pageSize, + ]), + [queryColumnFilters, sorting, startTime, endTime, isCustomDate, excludeInternalHealthChecks, pagination.pageSize], + ); + const sessionCursors = cursorsInScope(storedSessionCursors, sessionCursorScope); + const { logsQuery, filteredLogs, allTeams, usesSessionCursor } = useLogFilterLogic({ accessToken, token, @@ -179,10 +207,10 @@ export default function RequestLogsPanel({ accessToken, token, userRole, userID, api_key: selectedKeyId, }; }, - enabled: selectedKeyId !== null, + enabled: Boolean(selectedKeyId), }; - const { data: selectedKeyInfo } = useQuery(keyInfoQueryOptions); + const { data: selectedKeyInfo, isError: selectedKeyLoadFailed } = useQuery(keyInfoQueryOptions); const urlLogQueryOptions: UseQueryOptions = { queryKey: ["logs", "byId", urlLogId, accessToken], @@ -224,29 +252,12 @@ export default function RequestLogsPanel({ accessToken, token, userRole, userID, const rows: LogEntry[] = filteredLogs.data; const rowCount = tableRowCount(filteredLogs, pagination); - const handleSearchChange = useCallback( - (value: string) => { - setSearch(value); - setSessionCursors({}); - }, - [setSearch], - ); - - const handleSortingChange = useCallback>( - (updaterOrValue) => { - onSortingChange(updaterOrValue); - setSessionCursors({}); - }, - [onSortingChange], - ); - const handleColumnFiltersChange = useCallback>( (updaterOrValue) => { const next = functionalUpdate(updaterOrValue, columnFilters); onColumnFiltersChange(next.filter((filter) => filter.id !== LOG_FILTER_IDS.SEARCH)); const nextSearch = searchFilterValue(next); if (nextSearch !== search) setSearch(nextSearch); - setSessionCursors({}); }, [columnFilters, onColumnFiltersChange, search, setSearch], ); @@ -259,7 +270,6 @@ export default function RequestLogsPanel({ accessToken, token, userRole, userID, return; } if (requested.pageSize !== pagination.pageSize) { - setSessionCursors({}); onPaginationChange({ ...requested, pageIndex: 0 }); return; } @@ -269,10 +279,20 @@ export default function RequestLogsPanel({ accessToken, token, userRole, userID, } const nextCursor = filteredLogs.next_session_cursor; if (!nextCursor || logsQuery.isPlaceholderData) return; - setSessionCursors((previous) => ({ ...previous, [requested.pageIndex]: nextCursor })); + setStoredSessionCursors((previous) => ({ + scope: sessionCursorScope, + cursors: { ...cursorsInScope(previous, sessionCursorScope), [requested.pageIndex]: nextCursor }, + })); onPaginationChange(requested); }, - [usesSessionCursor, pagination, onPaginationChange, filteredLogs.next_session_cursor, logsQuery.isPlaceholderData], + [ + usesSessionCursor, + pagination, + onPaginationChange, + filteredLogs.next_session_cursor, + logsQuery.isPlaceholderData, + sessionCursorScope, + ], ); const handleExcludeInternalHealthChecksChange = useCallback( @@ -325,11 +345,14 @@ export default function RequestLogsPanel({ accessToken, token, userRole, userID, [setSelectedKeyId], ); - if (selectedKeyInfo && selectedKeyId && selectedKeyInfo.api_key === selectedKeyId) { + if (selectedKeyId) { + if (!selectedKeyInfo && !selectedKeyLoadFailed) { + return
Loading key...
; + } return ( void setSelectedKeyId(null)} backButtonText="Back to Logs" @@ -354,11 +377,11 @@ export default function RequestLogsPanel({ accessToken, token, userRole, userID, pagination={pagination} onPaginationChange={handlePaginationChange} sorting={sorting} - onSortingChange={handleSortingChange} + onSortingChange={onSortingChange} columnFilters={columnFilters} onColumnFiltersChange={handleColumnFiltersChange} searchValue={search} - onSearchChange={handleSearchChange} + onSearchChange={setSearch} onRefresh={() => void logsQuery.refetch()} onRowClick={handleRowClick} onKeyHashClick={handleKeyHashClick} diff --git a/ui/litellm-dashboard/src/components/view_logs/constants.ts b/ui/litellm-dashboard/src/components/view_logs/constants.ts index 0c5a3abfe36..9e4fa0e704b 100644 --- a/ui/litellm-dashboard/src/components/view_logs/constants.ts +++ b/ui/litellm-dashboard/src/components/view_logs/constants.ts @@ -21,17 +21,21 @@ export const AGENT_CALL_TYPES = ["asend_message"]; /** Call types that represent Batch API operations (creation and retrieval, sync and async). */ export const BATCH_CALL_TYPES = ["acreate_batch", "create_batch", "aretrieve_batch", "retrieve_batch"]; +const LAST_24_HOURS = { id: "24h", label: "Last 24 Hours", value: 24, unit: "hours" } as const; + export const QUICK_SELECT_OPTIONS = [ { id: "1m", label: "Last Minute", value: 1, unit: "minutes" }, { id: "15m", label: "Last 15 Minutes", value: 15, unit: "minutes" }, { id: "1h", label: "Last Hour", value: 1, unit: "hours" }, { id: "4h", label: "Last 4 Hours", value: 4, unit: "hours" }, - { id: "24h", label: "Last 24 Hours", value: 24, unit: "hours" }, + LAST_24_HOURS, { id: "7d", label: "Last 7 Days", value: 7, unit: "days" }, ] as const; -export type QuickSelectPresetId = (typeof QUICK_SELECT_OPTIONS)[number]["id"]; +export type QuickSelectOption = (typeof QUICK_SELECT_OPTIONS)[number]; +export type QuickSelectPresetId = QuickSelectOption["id"]; export const QUICK_SELECT_PRESET_IDS: readonly QuickSelectPresetId[] = QUICK_SELECT_OPTIONS.map((option) => option.id); -export const DEFAULT_QUICK_SELECT_PRESET: QuickSelectPresetId = "24h"; +export const DEFAULT_QUICK_SELECT_OPTION: QuickSelectOption = LAST_24_HOURS; +export const DEFAULT_QUICK_SELECT_PRESET: QuickSelectPresetId = DEFAULT_QUICK_SELECT_OPTION.id; diff --git a/ui/litellm-dashboard/src/components/view_logs/useLogsTimeRange.test.tsx b/ui/litellm-dashboard/src/components/view_logs/useLogsTimeRange.test.tsx index d5852afe19c..b247f78576d 100644 --- a/ui/litellm-dashboard/src/components/view_logs/useLogsTimeRange.test.tsx +++ b/ui/litellm-dashboard/src/components/view_logs/useLogsTimeRange.test.tsx @@ -3,7 +3,7 @@ import moment from "moment"; import { NuqsTestingAdapter, withNuqsTestingAdapter, type OnUrlUpdateFunction } from "nuqs/adapters/testing"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; -import { LOCAL_DATETIME_FORMAT, useLogsTimeRange, type LogsTimeRange } from "./useLogsTimeRange"; +import { LOCAL_DATETIME_FORMAT, useLogsTimeRange, type LogsRange, type LogsTimeRange } from "./useLogsTimeRange"; const renderTimeRange = (searchParams = "") => { const onUrlUpdate = vi.fn(); @@ -16,30 +16,37 @@ const renderTimeRange = (searchParams = "") => { const lastParams = (onUrlUpdate: ReturnType>) => onUrlUpdate.mock.calls.at(-1)?.[0].searchParams ?? new URLSearchParams(); +const expectedRange = ( + range: LogsRange, + [startTime, endTime]: readonly [string, string], + [startInput, endInput]: readonly [string, string] = ["", ""], +): LogsTimeRange => ({ range, startTime, endTime, startInput, endInput }); + const minutesBetween = (startTime: string, endTime: string) => moment(endTime, LOCAL_DATETIME_FORMAT).diff(moment(startTime, LOCAL_DATETIME_FORMAT), "minutes"); describe("useLogsTimeRange", () => { beforeEach(() => { + vi.stubEnv("TZ", "America/New_York"); vi.useFakeTimers({ shouldAdvanceTime: true }); vi.setSystemTime(new Date("2026-07-07T10:00:00Z")); }); afterEach(() => { vi.useRealTimers(); + vi.unstubAllEnvs(); }); it("anchors a preset's bounds at mount time so the query window does not drift per render", () => { const { result, rerender } = renderTimeRange("?range=15m"); const initial = result.current.timeRange; - expect(minutesBetween(initial.startTime, initial.endTime)).toBe(15); - expect(initial.endTime).toBe(moment().format(LOCAL_DATETIME_FORMAT)); + expect(initial).toEqual(expectedRange("15m", ["2026-07-07T05:45", "2026-07-07T06:00"])); vi.setSystemTime(new Date("2026-07-07T10:05:00Z")); rerender(); - expect(result.current.timeRange).toBe(initial); + expect(result.current.timeRange).toEqual(initial); }); it("re-anchors from now when the same preset is picked again", async () => { @@ -84,29 +91,46 @@ describe("useLogsTimeRange", () => { it("round-trips a custom range through UTC minute-precision ISO strings", async () => { const { result, onUrlUpdate } = renderTimeRange("?range=custom&start=2026-07-01T10:00Z&end=2026-07-02T10:30Z"); - expect(result.current.timeRange).toEqual({ - range: "custom", - startTime: moment("2026-07-01T10:00Z").format(LOCAL_DATETIME_FORMAT), - endTime: moment("2026-07-02T10:30Z").format(LOCAL_DATETIME_FORMAT), - }); + const bounds = ["2026-07-01T06:00", "2026-07-02T06:30"] as const; + expect(result.current.timeRange).toEqual(expectedRange("custom", bounds, bounds)); act(() => result.current.setEndTime("2026-07-03T08:15")); - await waitFor(() => - expect(lastParams(onUrlUpdate).get("end")).toBe(moment("2026-07-03T08:15").utc().format("YYYY-MM-DDTHH:mm[Z]")), - ); + await waitFor(() => expect(lastParams(onUrlUpdate).get("end")).toBe("2026-07-03T12:15Z")); expect(result.current.timeRange.endTime).toBe("2026-07-03T08:15"); + expect(result.current.timeRange.endInput).toBe("2026-07-03T08:15"); }); - it("treats an unparsable or cleared custom bound as empty instead of inventing a date", async () => { + it("keeps an unparsable or cleared custom bound empty in the input but queries the default window for it", async () => { const { result, onUrlUpdate } = renderTimeRange("?range=custom&start=not-a-date&end=2026-07-02T10:30Z"); - expect(result.current.timeRange.startTime).toBe(""); + expect(result.current.timeRange).toEqual( + expectedRange("custom", ["2026-07-06T06:00", "2026-07-02T06:30"], ["", "2026-07-02T06:30"]), + ); act(() => result.current.setEndTime("")); await waitFor(() => expect(lastParams(onUrlUpdate).has("end")).toBe(false)); - expect(result.current.timeRange.endTime).toBe(""); + expect(result.current.timeRange.endInput).toBe(""); + expect(result.current.timeRange.endTime).toBe("2026-07-07T06:00"); + }); + + it("ignores ?start= and ?end= unless the range is custom", () => { + const { result } = renderTimeRange("?range=1h&start=2026-07-01T10:00Z&end=2026-07-02T10:30Z"); + + expect(result.current.timeRange).toEqual(expectedRange("1h", ["2026-07-07T05:00", "2026-07-07T06:00"])); + }); + + it("picking a preset while in a custom range drops ?start= and ?end=", async () => { + const { result, onUrlUpdate } = renderTimeRange("?range=custom&start=2026-07-01T10:00Z&end=2026-07-02T10:30Z"); + + act(() => result.current.selectPreset("1h")); + + await waitFor(() => expect(lastParams(onUrlUpdate).get("range")).toBe("1h")); + expect(lastParams(onUrlUpdate).has("start")).toBe(false); + expect(lastParams(onUrlUpdate).has("end")).toBe(false); + await waitFor(() => expect(result.current.timeRange.startTime).toBe("2026-07-07T05:00")); + expect(result.current.timeRange.endTime).toBe("2026-07-07T06:00"); }); it("carries the current preset bounds into ?start= and ?end= when Custom Range is toggled on", async () => { @@ -116,13 +140,11 @@ describe("useLogsTimeRange", () => { act(() => result.current.toggleCustomRange()); await waitFor(() => expect(lastParams(onUrlUpdate).get("range")).toBe("custom")); - expect(lastParams(onUrlUpdate).get("start")).toBe( - moment(startTime, LOCAL_DATETIME_FORMAT).utc().format("YYYY-MM-DDTHH:mm[Z]"), - ); - expect(lastParams(onUrlUpdate).get("end")).toBe( - moment(endTime, LOCAL_DATETIME_FORMAT).utc().format("YYYY-MM-DDTHH:mm[Z]"), - ); - expect(result.current.timeRange).toEqual({ range: "custom", startTime, endTime }); + expect(lastParams(onUrlUpdate).get("start")).toBe("2026-07-07T06:00Z"); + expect(lastParams(onUrlUpdate).get("end")).toBe("2026-07-07T10:00Z"); + const bounds = ["2026-07-07T02:00", "2026-07-07T06:00"] as const; + expect([startTime, endTime]).toEqual(bounds); + expect(result.current.timeRange).toEqual(expectedRange("custom", bounds, bounds)); expect(onChange).toHaveBeenCalledTimes(1); }); @@ -132,9 +154,7 @@ describe("useLogsTimeRange", () => { act(() => result.current.toggleCustomRange()); await waitFor(() => expect(lastParams(onUrlUpdate).get("range")).toBe("custom")); act(() => result.current.setStartTime("2026-07-01T10:00")); - await waitFor(() => - expect(lastParams(onUrlUpdate).get("start")).toBe(moment("2026-07-01T10:00").utc().format("YYYY-MM-DDTHH:mm[Z]")), - ); + await waitFor(() => expect(lastParams(onUrlUpdate).get("start")).toBe("2026-07-01T14:00Z")); expect(result.current.timeRange.startTime).toBe("2026-07-01T10:00"); vi.setSystemTime(new Date("2026-07-07T11:30:00Z")); diff --git a/ui/litellm-dashboard/src/components/view_logs/useLogsTimeRange.ts b/ui/litellm-dashboard/src/components/view_logs/useLogsTimeRange.ts index 24fe0f38b04..4dca5c9fe96 100644 --- a/ui/litellm-dashboard/src/components/view_logs/useLogsTimeRange.ts +++ b/ui/litellm-dashboard/src/components/view_logs/useLogsTimeRange.ts @@ -3,6 +3,7 @@ import { createParser, parseAsStringLiteral, useQueryStates } from "nuqs"; import { useCallback, useMemo, useState } from "react"; import { + DEFAULT_QUICK_SELECT_OPTION, DEFAULT_QUICK_SELECT_PRESET, QUICK_SELECT_OPTIONS, QUICK_SELECT_PRESET_IDS, @@ -38,6 +39,8 @@ export interface LogsTimeRange { range: LogsRange; startTime: string; endTime: string; + startInput: string; + endInput: string; } interface PresetWindow { @@ -52,7 +55,7 @@ interface Anchor { } const presetWindow = (preset: QuickSelectPresetId): PresetWindow => { - const option = QUICK_SELECT_OPTIONS.find((candidate) => candidate.id === preset) ?? QUICK_SELECT_OPTIONS[0]; + const option = QUICK_SELECT_OPTIONS.find((candidate) => candidate.id === preset) ?? DEFAULT_QUICK_SELECT_OPTION; const now = moment(); return { preset, @@ -82,9 +85,14 @@ export function useLogsTimeRange(onChange: () => void): LogsTimeRangeState { const isCustom = range === CUSTOM_RANGE; const lastPreset = anchor.window.preset; - const startTime = isCustom ? start ?? "" : anchor.window.startTime; - const endTime = isCustom ? end ?? "" : anchor.window.endTime; - const timeRange = useMemo(() => ({ range, startTime, endTime }), [range, startTime, endTime]); + const customStart = isCustom ? start : null; + const customEnd = isCustom ? end : null; + const startTime = customStart ?? anchor.window.startTime; + const endTime = customEnd ?? anchor.window.endTime; + const timeRange = useMemo( + () => ({ range, startTime, endTime, startInput: customStart ?? "", endInput: customEnd ?? "" }), + [range, startTime, endTime, customStart, customEnd], + ); const selectPreset = useCallback( (preset: QuickSelectPresetId) => { @@ -99,7 +107,7 @@ export function useLogsTimeRange(onChange: () => void): LogsTimeRangeState { void setParams( isCustom ? { range: lastPreset, start: null, end: null } - : { range: CUSTOM_RANGE, start: startTime || null, end: endTime || null }, + : { range: CUSTOM_RANGE, start: startTime, end: endTime }, ); onChange(); }, [setParams, isCustom, lastPreset, startTime, endTime, onChange]);