Skip to content

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

perf(tables): add start time filter to filter option queries (#2445)

Hassieb Pakzad3125f275e7d4
diff --git a/web/src/components/table/use-cases/generations.tsx b/web/src/components/table/use-cases/generations.tsx
index 3f05aea91c..f4cc3dfe8f 100644
--- a/web/src/components/table/use-cases/generations.tsx
+++ b/web/src/components/table/use-cases/generations.tsx
@@ -162,9 +162,12 @@ export default function GenerationsTable({
const totalCount = generations.data?.totalCount ?? 0;
+ const startTimeFilter = filterState.find((f) => f.column === "Start Time");
const filterOptions = api.generations.filterOptions.useQuery(
{
projectId,
+ startTimeFilter:
+ startTimeFilter?.type === "datetime" ? startTimeFilter : undefined,
},
{
trpc: {
diff --git a/web/src/components/table/use-cases/traces.tsx b/web/src/components/table/use-cases/traces.tsx
index ed70211c3c..63a7166445 100644
--- a/web/src/components/table/use-cases/traces.tsx
+++ b/web/src/components/table/use-cases/traces.tsx
@@ -146,9 +146,12 @@ export default function TracesTable({
// loading filter options individually from the remaining calls
// traces.all should load first together with everything else.
// This here happens in the background.
+ const timestampFilter = filterState.find((f) => f.column === "Timestamp");
const traceFilterOptions = api.traces.filterOptions.useQuery(
{
projectId,
+ timestampFilter:
+ timestampFilter?.type === "datetime" ? timestampFilter : undefined,
},
{
trpc: {
diff --git a/web/src/server/api/routers/generations/filterOptionsQuery.ts b/web/src/server/api/routers/generations/filterOptionsQuery.ts
index 3ef643c905..5525a1d8ee 100644
--- a/web/src/server/api/routers/generations/filterOptionsQuery.ts
+++ b/web/src/server/api/routers/generations/filterOptionsQuery.ts
@@ -1,20 +1,42 @@
import { z } from "zod";
-import { type ObservationOptions } from "@langfuse/shared";
+import {
+ datetimeFilterToPrismaSql,
+ timeFilter,
+ type ObservationOptions,
+} from "@langfuse/shared";
import { protectedProjectProcedure } from "@/src/server/api/trpc";
import { Prisma } from "@langfuse/shared/src/db";
export const filterOptionsQuery = protectedProjectProcedure
- .input(z.object({ projectId: z.string() }))
+ .input(
+ z.object({ projectId: z.string(), startTimeFilter: timeFilter.optional() }),
+ )
.query(async ({ input, ctx }) => {
+ const { startTimeFilter } = input;
+ const prismaStartTimeFilter =
+ startTimeFilter?.type === "datetime"
+ ? startTimeFilter?.operator === ">="
+ ? { gte: startTimeFilter.value }
+ : startTimeFilter?.operator === ">"
+ ? { gt: startTimeFilter.value }
+ : startTimeFilter?.operator === "<="
+ ? { lte: startTimeFilter.value }
+ : startTimeFilter?.operator === "<"
+ ? { lt: startTimeFilter.value }
+ : {}
+ : {};
+
const queryFilter = {
projectId: input.projectId,
type: "GENERATION",
} as const;
+ // Score names
const scores = await ctx.prisma.score.groupBy({
where: {
projectId: input.projectId,
+ timestamp: prismaStartTimeFilter,
},
take: 1000,
orderBy: {
@@ -23,18 +45,21 @@ export const filterOptionsQuery = protectedProjectProcedure
by: ["name"],
});
+ // Model names
const model = await ctx.prisma.observation.groupBy({
by: ["model"],
- where: queryFilter,
+ where: { ...queryFilter, startTime: prismaStartTimeFilter },
_count: { _all: true },
take: 1000,
orderBy: {
model: "desc",
},
});
+
+ // Observation names
const name = await ctx.prisma.observation.groupBy({
by: ["name"],
- where: queryFilter,
+ where: { ...queryFilter, startTime: prismaStartTimeFilter },
_count: { _all: true },
take: 1000,
orderBy: {
@@ -59,6 +84,17 @@ export const filterOptionsQuery = protectedProjectProcedure
GROUP BY 1
LIMIT 1000;
`);
+
+ // Trace names
+ const rawStartTimeFilter =
+ startTimeFilter && startTimeFilter.type === "datetime"
+ ? datetimeFilterToPrismaSql(
+ "start_time",
+ startTimeFilter.operator,
+ startTimeFilter.value,
+ )
+ : Prisma.empty;
+
const traceName = await ctx.prisma.$queryRaw<
Array<{
traceName: string | null;
@@ -73,6 +109,7 @@ export const filterOptionsQuery = protectedProjectProcedure
WHERE o.type = 'GENERATION'
AND o.project_id = ${input.projectId}
AND t.project_id = ${input.projectId}
+ ${rawStartTimeFilter}
GROUP BY 1
LIMIT 1000;
`);
diff --git a/web/src/server/api/routers/traces.ts b/web/src/server/api/routers/traces.ts
index 97c2e36318..dba5c3895c 100644
--- a/web/src/server/api/routers/traces.ts
+++ b/web/src/server/api/routers/traces.ts
@@ -11,7 +11,7 @@ import {
type ObservationView,
type ObservationLevel,
} from "@langfuse/shared/src/db";
-import { paginationZod } from "@langfuse/shared";
+import { paginationZod, timeFilter } from "@langfuse/shared";
import { type TraceOptions, singleFilter } from "@langfuse/shared";
import { tracesTableCols } from "@langfuse/shared";
import {
@@ -172,11 +172,31 @@ export const traceRouter = createTRPCRouter({
};
}),
filterOptions: protectedProjectProcedure
- .input(z.object({ projectId: z.string() }))
+ .input(
+ z.object({
+ projectId: z.string(),
+ timestampFilter: timeFilter.optional(),
+ }),
+ )
.query(async ({ input, ctx }) => {
+ const { timestampFilter } = input;
+ const prismaTimestampFilter =
+ timestampFilter?.type === "datetime"
+ ? timestampFilter?.operator === ">="
+ ? { gte: timestampFilter.value }
+ : timestampFilter?.operator === ">"
+ ? { gt: timestampFilter.value }
+ : timestampFilter?.operator === "<="
+ ? { lte: timestampFilter.value }
+ : timestampFilter?.operator === "<"
+ ? { lt: timestampFilter.value }
+ : {}
+ : {};
+
const scores = await ctx.prisma.score.groupBy({
where: {
projectId: input.projectId,
+ timestamp: prismaTimestampFilter,
},
take: 1000,
orderBy: {
@@ -189,6 +209,7 @@ export const traceRouter = createTRPCRouter({
const names = await ctx.prisma.trace.groupBy({
where: {
projectId: input.projectId,
+ timestamp: prismaTimestampFilter,
},
by: ["name"],
// limiting to 1k trace names to avoid performance issues.
@@ -204,11 +225,21 @@ export const traceRouter = createTRPCRouter({
id: true,
},
});
+
+ const rawTimestampFilter =
+ timestampFilter && timestampFilter.type === "datetime"
+ ? datetimeFilterToPrismaSql(
+ "timestamp",
+ timestampFilter.operator,
+ timestampFilter.value,
+ )
+ : Prisma.empty;
+
const tags: { count: number; value: string }[] = await ctx.prisma
.$queryRaw`
SELECT COUNT(*)::integer AS "count", tags.tag as value
FROM traces, UNNEST(traces.tags) AS tags(tag)
- WHERE traces.project_id = ${input.projectId}
+ WHERE traces.project_id = ${input.projectId} ${rawTimestampFilter}
GROUP BY tags.tag
LIMIT 1000
`;