diff --git a/ui/litellm-dashboard/src/components/view_logs/LogsTableToolbar.tsx b/ui/litellm-dashboard/src/components/view_logs/LogsTableToolbar.tsx index 60657872ccc..eccf965fddf 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, startInput, endInput } = 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(); - }} + aria-label="Start time" + value={startInput} + onChange={(event) => onStartTimeChange(event.target.value)} /> to { - onEndTimeChange(event.target.value); - onResetToFirstPage(); - }} + aria-label="End time" + 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 8a0e529e2e8..1783bd403ec 100644 --- a/ui/litellm-dashboard/src/components/view_logs/RequestLogsPanel.test.tsx +++ b/ui/litellm-dashboard/src/components/view_logs/RequestLogsPanel.test.tsx @@ -3,10 +3,11 @@ 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 { 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) => { @@ -53,15 +54,38 @@ 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 ( +
+ +
+ ); + }, +})); + 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() }]), })); 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", @@ -101,6 +125,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) => @@ -146,6 +192,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(); @@ -312,6 +362,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(() => { @@ -479,6 +531,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(); @@ -487,18 +547,150 @@ 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(); + respondWithPages(2, 25, 80, SESSION_CURSOR); + 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("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(); + + 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")); + 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("2026-07-01T14:00Z")); + await waitFor(() => expect(lastCall()?.start_date).toBe("2026-07-01 14:00:00")); + 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("2026-07-01 14:00:00")); + + 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(); + 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" })); + + 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(); @@ -753,37 +945,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); @@ -791,6 +994,299 @@ 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("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"); + + 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)); + 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("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"); + + 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(); + }); + + 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" } })]); + 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", () => { it("shows the auto-refresh banner on the first page and hides it once stopped", async () => { const user = userEvent.setup(); @@ -801,6 +1297,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 0b5e5ff9616..8543cee8694 100644 --- a/ui/litellm-dashboard/src/components/view_logs/RequestLogsPanel.tsx +++ b/ui/litellm-dashboard/src/components/view_logs/RequestLogsPanel.tsx @@ -2,17 +2,23 @@ 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, +} 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 +31,61 @@ 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; +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), + 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 : ""; +}; + +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; + 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; @@ -41,19 +95,42 @@ 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 [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 { + search, + setSearch, + sorting, + onSortingChange, + pagination, + onPaginationChange, + columnFilters: urlColumnFilters, + onColumnFiltersChange, + } = useUrlTableState(TABLE_STATE_OPTIONS); + const [storedSessionCursors, setStoredSessionCursors] = useState({ + scope: "", + cursors: NO_SESSION_CURSORS, + }); + 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(() => { + 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,23 +149,27 @@ 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 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, @@ -116,20 +197,20 @@ 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: Boolean(selectedKeyId), }; - const { data: selectedKeyInfo } = useQuery(keyInfoQueryOptions); + const { data: selectedKeyInfo, isError: selectedKeyLoadFailed } = useQuery(keyInfoQueryOptions); const urlLogQueryOptions: UseQueryOptions = { queryKey: ["logs", "byId", urlLogId, accessToken], @@ -169,77 +250,64 @@ 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) => { - 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 handleSortingChange = useCallback>((updaterOrValue) => { - setSorting(updaterOrValue); - setSessionCursors({}); - setPagination((previous) => ({ ...previous, pageIndex: 0 })); - }, []); - - 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); + }, + [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); + setStoredSessionCursors((previous) => ({ + scope: sessionCursorScope, + cursors: { ...cursorsInScope(previous, sessionCursorScope), [requested.pageIndex]: nextCursor }, + })); + onPaginationChange(requested); }, - [usesSessionCursor, pagination, filteredLogs.next_session_cursor, logsQuery.isPlaceholderData], + [ + usesSessionCursor, + pagination, + onPaginationChange, + filteredLogs.next_session_cursor, + logsQuery.isPlaceholderData, + sessionCursorScope, + ], ); 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 +338,23 @@ 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 (selectedKeyId) { + if (!selectedKeyInfo && !selectedKeyLoadFailed) { + return
Loading key...
; + } return ( setSelectedKeyIdInfoView(null)} + onClose={() => void setSelectedKeyId(null)} backButtonText="Back to Logs" /> ); @@ -297,16 +371,17 @@ export default function RequestLogsPanel({ accessToken, token, userRole, userID, void logsQuery.refetch()} onRowClick={handleRowClick} onKeyHashClick={handleKeyHashClick} @@ -315,19 +390,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 5a9bac428c8..d121d5fc15f 100644 --- a/ui/litellm-dashboard/src/components/view_logs/RequestLogsTable.tsx +++ b/ui/litellm-dashboard/src/components/view_logs/RequestLogsTable.tsx @@ -17,6 +17,7 @@ interface RequestLogsTableProps { data: LogEntry[]; rowCount: number; isLoading: boolean; + isError?: boolean; isRefreshing: boolean; pagination: PaginationState; onPaginationChange: OnChangeFn; @@ -55,6 +56,7 @@ export function RequestLogsTable({ data, rowCount, isLoading, + isError, isRefreshing, pagination, onPaginationChange, @@ -101,6 +103,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..9e4fa0e704b 100644 --- a/ui/litellm-dashboard/src/components/view_logs/constants.ts +++ b/ui/litellm-dashboard/src/components/view_logs/constants.ts @@ -21,11 +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"]; -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" }, -]; +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" }, + LAST_24_HOURS, + { id: "7d", label: "Last 7 Days", value: 7, unit: "days" }, +] as const; + +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_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 new file mode 100644 index 00000000000..b247f78576d --- /dev/null +++ b/ui/litellm-dashboard/src/components/view_logs/useLogsTimeRange.test.tsx @@ -0,0 +1,224 @@ +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 LogsRange, type LogsTimeRange } from "./useLogsTimeRange"; + +const renderTimeRange = (searchParams = "") => { + const onUrlUpdate = vi.fn(); + const onChange = vi.fn(); + const adapterProps = { searchParams, onUrlUpdate, hasMemory: true, resetUrlUpdateQueueOnMount: false }; + const hook = renderHook(() => useLogsTimeRange(onChange), { wrapper: withNuqsTestingAdapter(adapterProps) }); + return { ...hook, onUrlUpdate, onChange }; +}; + +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(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).toEqual(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"); + + 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("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("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).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.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 () => { + 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("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); + }); + + 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("2026-07-01T14:00Z")); + 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(result.current.timeRange.range).toBe("24h")); + expect(lastParams(onUrlUpdate).has("range")).toBe(false); + expect(lastParams(onUrlUpdate).has("start")).toBe(false); + 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", + ); + + 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..4dca5c9fe96 --- /dev/null +++ b/ui/litellm-dashboard/src/components/view_logs/useLogsTimeRange.ts @@ -0,0 +1,141 @@ +import moment from "moment"; +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, + 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; + startInput: string; + endInput: string; +} + +interface PresetWindow { + preset: QuickSelectPresetId; + startTime: string; + endTime: string; +} + +interface Anchor { + range: LogsRange; + window: PresetWindow; +} + +const presetWindow = (preset: QuickSelectPresetId): PresetWindow => { + const option = QUICK_SELECT_OPTIONS.find((candidate) => candidate.id === preset) ?? DEFAULT_QUICK_SELECT_OPTION; + const now = moment(); + return { + 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; + 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 [anchor, setAnchor] = useState(() => anchorFor(range, null)); + if (anchor.range !== range) setAnchor(anchorFor(range, anchor.window)); + + const isCustom = range === CUSTOM_RANGE; + const lastPreset = anchor.window.preset; + 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) => { + void setParams({ range: preset, start: null, end: null }); + setAnchor(anchorFor(preset, null)); + onChange(); + }, + [setParams, onChange], + ); + + const toggleCustomRange = useCallback(() => { + void setParams( + isCustom + ? { range: lastPreset, start: null, end: null } + : { range: CUSTOM_RANGE, start: startTime, end: endTime }, + ); + onChange(); + }, [setParams, isCustom, lastPreset, 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); + setAnchor(anchorFor(DEFAULT_QUICK_SELECT_PRESET, null)); + onChange(); + }, [setParams, onChange]); + + return useMemo( + () => ({ timeRange, selectPreset, toggleCustomRange, setStartTime, setEndTime, reset }), + [timeRange, selectPreset, toggleCustomRange, setStartTime, setEndTime, reset], + ); +}