🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
chore(tables): persist date range in session storage (#3076)
marliessophie3e8ba54aa013
diff --git a/web/src/components/date-range-dropdowns.tsx b/web/src/components/date-range-dropdowns.tsxindex 2eaccad383..81ce17d1f6 100644--- a/web/src/components/date-range-dropdowns.tsx+++ b/web/src/components/date-range-dropdowns.tsx@@ -9,15 +9,14 @@ import {} from "@/src/components/ui/select";import {- DEFAULT_AGGREGATION_SELECTION,- tableDateRangeAggregationSettings,dashboardDateRangeAggregationSettings,DASHBOARD_AGGREGATION_PLACEHOLDER,type DashboardDateRangeOptions,type TableDateRangeOptions,DASHBOARD_AGGREGATION_OPTIONS,type DashboardDateRange,- TABLE_RANGE_DROPDOWN_OPTIONS,+ TABLE_AGGREGATION_OPTIONS,+ getDateFromOption,} from "@/src/utils/date-range-utils";import { Clock } from "lucide-react";@@ -101,24 +100,22 @@ export const TableDateRangeDropdown: React.FC<TableDateRangeDropdownProps> = ({setDateRangeAndOption,}) => {const onDropDownSelection = (value: TableDateRangeOptions) => {- if (value === DEFAULT_AGGREGATION_SELECTION) {- setDateRangeAndOption(DEFAULT_AGGREGATION_SELECTION, undefined);- return;- }- const setting =- tableDateRangeAggregationSettings[- value as keyof typeof tableDateRangeAggregationSettings- ];- setDateRangeAndOption(value, {- from: addMinutes(new Date(), -setting),- to: new Date(),+ const dateFromOption = getDateFromOption({+ filterSource: "TABLE",+ option: value,});++ const initialDateRange = !!dateFromOption+ ? { from: dateFromOption, to: new Date() }+ : undefined;++ setDateRangeAndOption(value, initialDateRange);};return (<BaseDateRangeDropdownselectedOption={selectedOption}- options={TABLE_RANGE_DROPDOWN_OPTIONS}+ options={TABLE_AGGREGATION_OPTIONS}onSelectionChange={onDropDownSelection}/>);diff --git a/web/src/components/table/use-cases/generations.tsx b/web/src/components/table/use-cases/generations.tsxindex 2e88c7fa31..af90d273c7 100644--- a/web/src/components/table/use-cases/generations.tsx+++ b/web/src/components/table/use-cases/generations.tsx@@ -113,7 +113,7 @@ export default function GenerationsTable({);const { selectedOption, dateRange, setDateRangeAndOption } =- useTableDateRange();+ useTableDateRange(projectId);const [inputFilterState, setInputFilterState] = useQueryFilterState([],diff --git a/web/src/components/table/use-cases/scores.tsx b/web/src/components/table/use-cases/scores.tsxindex 3fb76109a2..d9ba20dee6 100644--- a/web/src/components/table/use-cases/scores.tsx+++ b/web/src/components/table/use-cases/scores.tsx@@ -90,7 +90,7 @@ export default function ScoresTable({const [rowHeight, setRowHeight] = useRowHeightLocalStorage("scores", "s");const { selectedOption, dateRange, setDateRangeAndOption } =- useTableDateRange();+ useTableDateRange(projectId);const [userFilterState, setUserFilterState] = useQueryFilterState([],diff --git a/web/src/components/table/use-cases/sessions.tsx b/web/src/components/table/use-cases/sessions.tsxindex 55ab015f67..ebbbb8393f 100644--- a/web/src/components/table/use-cases/sessions.tsx+++ b/web/src/components/table/use-cases/sessions.tsx@@ -54,7 +54,7 @@ export default function SessionsTable({}: SessionTableProps) {const { setDetailPageList } = useDetailPageLists();const { selectedOption, dateRange, setDateRangeAndOption } =- useTableDateRange();+ useTableDateRange(projectId);const [userFilterState, setUserFilterState] = useQueryFilterState([],diff --git a/web/src/components/table/use-cases/traces.tsx b/web/src/components/table/use-cases/traces.tsxindex 4d8b7d92af..182d96e9de 100644--- a/web/src/components/table/use-cases/traces.tsx+++ b/web/src/components/table/use-cases/traces.tsx@@ -98,7 +98,7 @@ export default function TracesTable({);const { selectedOption, dateRange, setDateRangeAndOption } =- useTableDateRange();+ useTableDateRange(projectId);const [userFilterState, setUserFilterState] = useQueryFilterState([],"traces",diff --git a/web/src/features/prompts/components/prompts-table.tsx b/web/src/features/prompts/components/prompts-table.tsxindex b4c4c9c1fc..1132a050cf 100644--- a/web/src/features/prompts/components/prompts-table.tsx+++ b/web/src/features/prompts/components/prompts-table.tsx@@ -22,8 +22,6 @@ import { createColumnHelper } from "@tanstack/react-table";import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";import { joinTableCoreAndMetrics } from "@/src/components/table/utils/joinTableCoreAndMetrics";import { Skeleton } from "@/src/components/ui/skeleton";-import { useTableDateRange } from "@/src/hooks/useTableDateRange";-import { type FilterState } from "@langfuse/shared";import { useDebounce } from "@/src/hooks/useDebounce";type PromptTableRow = {@@ -51,9 +49,6 @@ export function PromptTable() {projectId,);- const { selectedOption, dateRange, setDateRangeAndOption } =- useTableDateRange("All time");-const [orderByState, setOrderByState] = useOrderByState({column: "createdAt",order: "DESC",@@ -63,24 +58,12 @@ export function PromptTable() {pageSize: withDefault(NumberParam, 50),});- const dateRangeFilter: FilterState = dateRange- ? [- {- column: "createdAt",- type: "datetime",- operator: ">=",- value: dateRange.from,- },- ]- : [];-- const combinedFilterState = filterState.concat(dateRangeFilter);const prompts = api.prompts.all.useQuery({page: paginationState.pageIndex,limit: paginationState.pageSize,projectId: projectId as string, // Typecast as query is enabled only when projectId is present- filter: combinedFilterState,+ filter: filterState,orderBy: orderByState,},{@@ -233,7 +216,7 @@ export function PromptTable() {promptsFilter={{...filterOptionTags,projectId: projectId as string,- filter: combinedFilterState,+ filter: filterState,orderBy: orderByState,}}/>@@ -261,8 +244,6 @@ export function PromptTable() {)}filterState={filterState}setFilterState={useDebounce(setFilterState)}- selectedOption={selectedOption}- setDateRangeAndOption={setDateRangeAndOption}columnsWithCustomSelect={["labels", "tags"]}actionButtons={<Link href={`/project/${projectId}/prompts/new`}>diff --git a/web/src/hooks/useDashboardDateRange.tsx b/web/src/hooks/useDashboardDateRange.tsxindex c251addf1b..386172d415 100644--- a/web/src/hooks/useDashboardDateRange.tsx+++ b/web/src/hooks/useDashboardDateRange.tsx@@ -26,7 +26,7 @@ export function useDashboardDateRange(): UseDashboardDateRangeOutput {to: StringParam,});- const initialRangeOption = "24 hours";+ const initialRangeOption: DashboardDateRangeAggregationOption = "24 hours";const initialRange: DashboardDateRange | undefined =queryParams.dateRange !== "Select a date range" &&diff --git a/web/src/hooks/useTableDateRange.tsx b/web/src/hooks/useTableDateRange.tsxindex 0d22461202..5e44588423 100644--- a/web/src/hooks/useTableDateRange.tsx+++ b/web/src/hooks/useTableDateRange.tsx@@ -1,14 +1,13 @@import { useState } from "react";-import { addMinutes } from "date-fns";import { useQueryParams, StringParam, withDefault } from "use-query-params";import {type TableDateRangeOptions,- tableDateRangeAggregationSettings,isValidTableDateRangeAggregationOption,type TableDateRangeAggregationOption,type TableDateRange,- DEFAULT_AGGREGATION_SELECTION,+ getDateFromOption,} from "@/src/utils/date-range-utils";+import useSessionStorage from "@/src/components/useSessionStorage";export interface UseTableDateRangeOutput {selectedOption: TableDateRangeOptions;@@ -19,33 +18,33 @@ export interface UseTableDateRangeOutput {) => void;}-export function useTableDateRange(- defaultDateRange: TableDateRangeOptions = "24 hours",-): UseTableDateRangeOutput {+export function useTableDateRange(projectId: string): UseTableDateRangeOutput {const [queryParams, setQueryParams] = useQueryParams({dateRange: withDefault(StringParam, "Select a date range"),});+ const defaultDateRange: TableDateRangeOptions = "24 hours";const validatedInitialRangeOption = isValidTableDateRangeAggregationOption(queryParams.dateRange,)? (queryParams.dateRange as TableDateRangeAggregationOption): defaultDateRange;- const [selectedOption, setSelectedOption] = useState<TableDateRangeOptions>(- validatedInitialRangeOption,- );- const initialDateRange =- selectedOption !== DEFAULT_AGGREGATION_SELECTION- ? {- from: addMinutes(- new Date(),- -tableDateRangeAggregationSettings[- validatedInitialRangeOption as keyof typeof tableDateRangeAggregationSettings- ],- ),- }- : undefined;+ const [selectedOption, setSelectedOption] =+ useSessionStorage<TableDateRangeOptions>(+ `tableDateRangeState-${projectId}`,+ validatedInitialRangeOption,+ );++ const dateFromOption = getDateFromOption({+ filterSource: "TABLE",+ option: selectedOption,+ });++ const initialDateRange = !!dateFromOption+ ? { from: dateFromOption }+ : undefined;+const [dateRange, setDateRange] = useState<TableDateRange | undefined>(initialDateRange,);diff --git a/web/src/pages/project/[projectId]/prompts/[promptName]/metrics.tsx b/web/src/pages/project/[projectId]/prompts/[promptName]/metrics.tsxindex 3e2ed48426..053783fb4e 100644--- a/web/src/pages/project/[projectId]/prompts/[promptName]/metrics.tsx+++ b/web/src/pages/project/[projectId]/prompts/[promptName]/metrics.tsx@@ -91,7 +91,7 @@ export default function PromptVersionTable() {"s",);const { selectedOption, dateRange, setDateRangeAndOption } =- useTableDateRange("7 days");+ useTableDateRange(projectId);const dateRangeFilter: FilterState | null = dateRange?.from? [diff --git a/web/src/pages/project/[projectId]/users.tsx b/web/src/pages/project/[projectId]/users.tsxindex 1fbc8fd151..c2b934578d 100644--- a/web/src/pages/project/[projectId]/users.tsx+++ b/web/src/pages/project/[projectId]/users.tsx@@ -52,7 +52,7 @@ export default function UsersPage() {});const { selectedOption, dateRange, setDateRangeAndOption } =- useTableDateRange();+ useTableDateRange(projectId);const dateRangeFilter: FilterState = dateRange? [diff --git a/web/src/server/api/routers/scores.ts b/web/src/server/api/routers/scores.tsindex d64d70ef08..672d591b4a 100644--- a/web/src/server/api/routers/scores.ts+++ b/web/src/server/api/routers/scores.ts@@ -8,17 +8,13 @@ import {import { auditLog } from "@/src/features/audit-logs/auditLog";import { composeAggregateScoreKey } from "@/src/features/scores/lib/aggregateScores";import {- DASHBOARD_AGGREGATION_OPTIONS,- DEFAULT_AGGREGATION_SELECTION,- TABLE_RANGE_DROPDOWN_OPTIONS,- dashboardDateRangeAggregationSettings,+ getDateFromOption,+ SelectedTimeOptionSchema,} from "@/src/utils/date-range-utils";-import { addMinutes } from "date-fns";import {createTRPCRouter,protectedProjectProcedure,} from "@/src/server/api/trpc";-import { tableDateRangeAggregationSettings } from "@/src/utils/date-range-utils";import {CreateAnnotationScoreData,orderBy,@@ -31,19 +27,6 @@ import {} from "@langfuse/shared";import { Prisma, type Score } from "@langfuse/shared/src/db";-const SelectedTimeOption = z- .discriminatedUnion("filterSource", [- z.object({- filterSource: z.literal("TABLE"),- option: z.enum(TABLE_RANGE_DROPDOWN_OPTIONS),- }),- z.object({- filterSource: z.literal("DASHBOARD"),- option: z.enum(DASHBOARD_AGGREGATION_OPTIONS),- }),- ])- .optional();-const ScoreFilterOptions = z.object({projectId: z.string(), // Required for protectedProjectProcedurefilter: z.array(singleFilter),@@ -54,32 +37,6 @@ const ScoreAllOptions = ScoreFilterOptions.extend({...paginationZod,});-const getDateFromOption = (- selectedTimeOption: z.infer<typeof SelectedTimeOption>,-): Date | undefined => {- if (!selectedTimeOption) return undefined;-- const { filterSource, option } = selectedTimeOption;- if (filterSource === "TABLE") {- const setting =- tableDateRangeAggregationSettings[- option as keyof typeof tableDateRangeAggregationSettings- ];-- return option !== DEFAULT_AGGREGATION_SELECTION- ? addMinutes(new Date(), -setting)- : undefined;- } else if (filterSource === "DASHBOARD") {- const setting =- dashboardDateRangeAggregationSettings[- option as keyof typeof dashboardDateRangeAggregationSettings- ];-- return addMinutes(new Date(), -setting.minutes);- }- return undefined;-};-export const scoresRouter = createTRPCRouter({all: protectedProjectProcedure.input(ScoreAllOptions)@@ -342,7 +299,7 @@ export const scoresRouter = createTRPCRouter({.input(z.object({projectId: z.string(),- selectedTimeOption: SelectedTimeOption,+ selectedTimeOption: SelectedTimeOptionSchema,}),).query(async ({ input, ctx }) => {diff --git a/web/src/utils/date-range-utils.ts b/web/src/utils/date-range-utils.tsindex 2a27cb91dc..058cd07989 100644--- a/web/src/utils/date-range-utils.ts+++ b/web/src/utils/date-range-utils.ts@@ -1,8 +1,9 @@import { type DateRange } from "react-day-picker";+import { z } from "zod";+import { addMinutes } from "date-fns";export const DEFAULT_DASHBOARD_AGGREGATION_SELECTION = "24 hours" as const;export const DASHBOARD_AGGREGATION_PLACEHOLDER = "Custom" as const;-export const DEFAULT_AGGREGATION_SELECTION = "All time" as const;export const DASHBOARD_AGGREGATION_OPTIONS = ["5 min",@@ -26,11 +27,7 @@ export const TABLE_AGGREGATION_OPTIONS = ["14 days","1 month","3 months",-] as const;--export const TABLE_RANGE_DROPDOWN_OPTIONS = [- ...TABLE_AGGREGATION_OPTIONS,- DEFAULT_AGGREGATION_SELECTION,+ "All time",] as const;export type DashboardDateRangeAggregationOption =@@ -56,9 +53,7 @@ export type DashboardDateRangeOptions =| DashboardDateRangeAggregationOption| typeof DASHBOARD_AGGREGATION_PLACEHOLDER;-export type TableDateRangeOptions =- | TableDateRangeAggregationOption- | typeof DEFAULT_AGGREGATION_SELECTION;+export type TableDateRangeOptions = TableDateRangeAggregationOption;export type DashboardDateRangeAggregationSettings = Record<DashboardDateRangeAggregationOption,{@@ -67,15 +62,9 @@ export type DashboardDateRangeAggregationSettings = Record<}>;-export type TableDateRangeAggregationSettings = Record<- TableDateRangeAggregationOption,- number->;-export const dateTimeAggregationOptions = [...TABLE_AGGREGATION_OPTIONS,...DASHBOARD_AGGREGATION_OPTIONS,- DEFAULT_AGGREGATION_SELECTION,] as const;export const dashboardDateRangeAggregationSettings: DashboardDateRangeAggregationSettings =@@ -118,77 +107,73 @@ export const dashboardDateRangeAggregationSettings: DashboardDateRangeAggregatio},};-export const tableDateRangeAggregationSettings: TableDateRangeAggregationSettings =- {- "3 months": 3 * 28 * 24 * 60,- "1 month": 28 * 24 * 60,- "14 days": 14 * 24 * 60,- "7 days": 7 * 24 * 60,- "3 days": 3 * 24 * 60,- "24 hours": 24 * 60,- "6 hours": 6 * 60,- "1 hour": 60,- "30 min": 30,- };+export const SelectedTimeOptionSchema = z+ .discriminatedUnion("filterSource", [+ z.object({+ filterSource: z.literal("TABLE"),+ option: z.enum(TABLE_AGGREGATION_OPTIONS),+ }),+ z.object({+ filterSource: z.literal("DASHBOARD"),+ option: z.enum(DASHBOARD_AGGREGATION_OPTIONS),+ }),+ ])+ .optional();++type SelectedTimeOption = z.infer<typeof SelectedTimeOptionSchema>;++const TABLE_DATE_RANGE_AGGREGATION_SETTINGS = new Map<+ TableDateRangeAggregationOption,+ number | null+>([+ ["3 months", 3 * 28 * 24 * 60],+ ["1 month", 28 * 24 * 60],+ ["14 days", 14 * 24 * 60],+ ["7 days", 7 * 24 * 60],+ ["3 days", 3 * 24 * 60],+ ["24 hours", 24 * 60],+ ["6 hours", 6 * 60],+ ["1 hour", 60],+ ["30 min", 30],+ ["All time", null],+]);++export const getDateFromOption = (+ selectedTimeOption: SelectedTimeOption,+): Date | undefined => {+ if (!selectedTimeOption) return undefined;++ const { filterSource, option } = selectedTimeOption;+ if (filterSource === "TABLE") {+ const setting = TABLE_DATE_RANGE_AGGREGATION_SETTINGS.get(option);+ if (!setting) return undefined;++ return addMinutes(new Date(), -setting);+ } else if (filterSource === "DASHBOARD") {+ const setting =+ dashboardDateRangeAggregationSettings[+ option as keyof typeof dashboardDateRangeAggregationSettings+ ];++ return addMinutes(new Date(), -setting.minutes);+ }+ return undefined;+};export function isValidDashboardDateRangeAggregationOption(- value: unknown,+ value?: string,): value is DashboardDateRangeAggregationOption {- return (- typeof value === "string" &&- DASHBOARD_AGGREGATION_OPTIONS.includes(- value as DashboardDateRangeAggregationOption,- )- );+ if (!value) return false;+ return (DASHBOARD_AGGREGATION_OPTIONS as readonly string[]).includes(value);}export function isValidTableDateRangeAggregationOption(- value: unknown,+ value?: string,): value is TableDateRangeAggregationOption {- return (- typeof value === "string" &&- TABLE_AGGREGATION_OPTIONS.includes(value as TableDateRangeAggregationOption)- );+ if (!value) return false;+ return (TABLE_AGGREGATION_OPTIONS as readonly string[]).includes(value);}-export const findClosestTableIntervalToDate = (- targetDate: Date,-): TableDateRangeAggregationOption | undefined => {- const currentDate = new Date();- const duration = Math.abs(currentDate.getTime() - targetDate.getTime());-- const diffs = TABLE_AGGREGATION_OPTIONS.map((interval) => {- const minutes = tableDateRangeAggregationSettings[interval];- return {- interval,- diff: Math.abs(duration - minutes * 60 * 1000),- };- });-- diffs.sort((a, b) => a.diff - b.diff);-- return diffs[0]?.interval;-};--export const findClosestDashboardIntervalToDate = (- targetDate: Date,-): DashboardDateRangeAggregationOption | undefined => {- const currentDate = new Date();- const duration = Math.abs(currentDate.getTime() - targetDate.getTime());-- const diffs = DASHBOARD_AGGREGATION_OPTIONS.map((interval) => {- const { minutes } = dashboardDateRangeAggregationSettings[interval];- return {- interval,- diff: Math.abs(duration - minutes * 60 * 1000),- };- });-- diffs.sort((a, b) => a.diff - b.diff);-- return diffs[0]?.interval;-};-export const findClosestDashboardInterval = (dateRange: DateRange,): DashboardDateRangeAggregationOption | undefined => {