Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

perf(ui): add date range filter to prompt metrics (#2796)

Richard Krümmel37e72a4b7fe5
diff --git a/web/src/features/prompts/server/routers/promptRouter.ts b/web/src/features/prompts/server/routers/promptRouter.ts
index e565dd9db1..804710ca78 100644
--- a/web/src/features/prompts/server/routers/promptRouter.ts
+++ b/web/src/features/prompts/server/routers/promptRouter.ts
@@ -10,7 +10,7 @@ import {
import { type Prompt, Prisma } from "@langfuse/shared/src/db";
import { createPrompt } from "../actions/createPrompt";
-import { orderByToPrismaSql } from "@langfuse/shared";
+import { observationsTableCols, orderByToPrismaSql } from "@langfuse/shared";
import { promptsTableCols } from "@/src/server/api/definitions/promptsTable";
import { optionalPaginationZod, paginationZod } from "@langfuse/shared";
import {
@@ -619,6 +619,7 @@ export const promptRouter = createTRPCRouter({
z.object({
projectId: z.string(),
promptIds: z.array(z.string()),
+ filter: z.array(singleFilter).nullish(),
}),
)
.query(async ({ input, ctx }) => {
@@ -629,7 +630,11 @@ export const promptRouter = createTRPCRouter({
});
if (input.promptIds.length === 0) return [];
-
+ const filterCondition = tableColumnsToSqlFilterAndPrefix(
+ input.filter ?? [],
+ observationsTableCols,
+ "prompts",
+ );
const metrics = await ctx.prisma.$queryRaw<
Array<{
id: string;
@@ -647,18 +652,19 @@ export const promptRouter = createTRPCRouter({
LEFT JOIN LATERAL (
SELECT
count(*) AS "observationCount",
- MIN(ov.start_time) AS "firstUsed",
- MAX(ov.start_time) AS "lastUsed",
- PERCENTILE_CONT(0.5) WITHIN GROUP(ORDER BY ov.completion_tokens) AS "medianOutputTokens",
- PERCENTILE_CONT(0.5) WITHIN GROUP(ORDER BY ov.prompt_tokens) AS "medianInputTokens",
- PERCENTILE_CONT(0.5) WITHIN GROUP(ORDER BY ov.calculated_total_cost) AS "medianTotalCost",
- PERCENTILE_CONT(0.5) WITHIN GROUP(ORDER BY ov.latency) AS "medianLatency"
+ MIN(o.start_time) AS "firstUsed",
+ MAX(o.start_time) AS "lastUsed",
+ PERCENTILE_CONT(0.5) WITHIN GROUP(ORDER BY o.completion_tokens) AS "medianOutputTokens",
+ PERCENTILE_CONT(0.5) WITHIN GROUP(ORDER BY o.prompt_tokens) AS "medianInputTokens",
+ PERCENTILE_CONT(0.5) WITHIN GROUP(ORDER BY o.calculated_total_cost) AS "medianTotalCost",
+ PERCENTILE_CONT(0.5) WITHIN GROUP(ORDER BY o.latency) AS "medianLatency"
FROM
- "observations_view" ov
+ "observations_view" o
WHERE
- ov.prompt_id = p.id
+ o.prompt_id = p.id
AND "type" = 'GENERATION'
AND "project_id" = ${input.projectId}
+ ${filterCondition}
) AS observation_metrics ON true
WHERE "project_id" = ${input.projectId}
AND p.id in (${Prisma.join(input.promptIds)})
@@ -688,6 +694,7 @@ export const promptRouter = createTRPCRouter({
AND o.prompt_id IS NOT NULL
AND o.project_id = ${input.projectId}
AND p.id IN (${Prisma.join(input.promptIds)})
+ ${filterCondition}
GROUP BY 1,2
ORDER BY 1,2
),
@@ -726,6 +733,7 @@ export const promptRouter = createTRPCRouter({
AND o.type = 'GENERATION'
AND o.project_id = ${input.projectId}
AND o.prompt_id IN (${Prisma.join(input.promptIds)})
+ ${filterCondition}
GROUP BY
o.prompt_id,
o.trace_id
diff --git a/web/src/hooks/useTableDateRange.tsx b/web/src/hooks/useTableDateRange.tsx
index 1824ab10d6..5d0328cb58 100644
--- a/web/src/hooks/useTableDateRange.tsx
+++ b/web/src/hooks/useTableDateRange.tsx
@@ -34,15 +34,17 @@ export function useTableDateRange(
const [selectedOption, setSelectedOption] = useState<TableDateRangeOptions>(
validatedInitialRangeOption,
);
- const initialDateRange = {
- from: addMinutes(
- new Date(),
- -tableDateRangeAggregationSettings[
- validatedInitialRangeOption as keyof typeof tableDateRangeAggregationSettings
- ],
- ),
- };
-
+ const initialDateRange =
+ selectedOption !== "All time"
+ ? {
+ from: addMinutes(
+ new Date(),
+ -tableDateRangeAggregationSettings[
+ validatedInitialRangeOption as keyof typeof tableDateRangeAggregationSettings
+ ],
+ ),
+ }
+ : 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.tsx
index 5d536c825e..f4e0bb753c 100644
--- a/web/src/pages/project/[projectId]/prompts/[promptName]/metrics.tsx
+++ b/web/src/pages/project/[projectId]/prompts/[promptName]/metrics.tsx
@@ -16,7 +16,8 @@ import { formatIntervalSeconds } from "@/src/utils/dates";
import { GroupedScoreBadges } from "@/src/components/grouped-score-badge";
import useColumnVisibility from "@/src/features/column-visibility/hooks/useColumnVisibility";
import { Skeleton } from "@/src/components/ui/skeleton";
-import { ScoreDataType } from "@langfuse/shared";
+import { type FilterState, ScoreDataType } from "@langfuse/shared";
+import { useTableDateRange } from "@/src/hooks/useTableDateRange";
type PromptVersionTableRow = {
version: number;
@@ -38,13 +39,13 @@ type PromptMetric = PromptMetricsOutput[number];
type PromptCoreData = PromptCoreOutput["promptVersions"][number];
function joinPromptCoreAndMetricData(
- promptCoreData: PromptCoreOutput,
+ promptCoreData?: PromptCoreOutput,
promptMetricsData?: PromptMetricsOutput,
): {
status: "loading" | "error" | "success";
combinedData: (PromptCoreData & Partial<PromptMetric>)[] | undefined;
} {
- if (!promptCoreData) return { status: "error", combinedData: undefined }; // defensive should never happen
+ if (!promptCoreData) return { status: "loading", combinedData: undefined };
const { promptVersions } = promptCoreData;
@@ -87,7 +88,19 @@ export default function PromptVersionTable() {
"promptVersion",
"s",
);
+ const { selectedOption, dateRange, setDateRangeAndOption } =
+ useTableDateRange("7 days");
+ const dateRangeFilter: FilterState | null = dateRange?.from
+ ? [
+ {
+ column: "Start Time",
+ type: "datetime",
+ operator: ">=",
+ value: dateRange.from,
+ },
+ ]
+ : null;
const promptVersions = api.prompts.allVersions.useQuery(
{
projectId: projectId as string, // Typecast as query is enabled only when projectId is present
@@ -106,6 +119,7 @@ export default function PromptVersionTable() {
{
projectId: projectId as string, // Typecast as query is enabled only when projectId is present
promptIds,
+ filter: dateRangeFilter,
},
{
enabled: Boolean(projectId) && promptVersions.isSuccess,
@@ -340,10 +354,6 @@ export default function PromptVersionTable() {
columns,
);
- if (!promptVersions.data) {
- return <div>Loading...</div>;
- }
-
const totalCount = promptVersions?.data?.totalCount ?? 0;
const { combinedData } = joinPromptCoreAndMetricData(
@@ -414,6 +424,8 @@ export default function PromptVersionTable() {
setRowHeight={setRowHeight}
columnVisibility={columnVisibility}
setColumnVisibility={setColumnVisibilityState}
+ selectedOption={selectedOption}
+ setDateRangeAndOption={setDateRangeAndOption}
/>
</div>
<DataTable