Skip to content

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

perf: optimize events reads (#11241)

Max Deichmann0dd2a1ce0cf6
diff --git a/packages/shared/src/server/repositories/events.ts b/packages/shared/src/server/repositories/events.ts
index f497f9564c..dcc2e130b9 100644
--- a/packages/shared/src/server/repositories/events.ts
+++ b/packages/shared/src/server/repositories/events.ts
@@ -1732,8 +1732,9 @@ export const getObservationsBatchIOFromEventsTable = async (opts: {
observations: Array<{
id: string;
traceId: string;
- startTime: Date;
}>;
+ minStartTime: Date;
+ maxStartTime: Date;
}): Promise<
Array<Pick<Observation, "id" | "input" | "output" | "metadata">>
> => {
@@ -1745,10 +1746,9 @@ export const getObservationsBatchIOFromEventsTable = async (opts: {
const observationIds = opts.observations.map((o) => o.id);
const traceIds = [...new Set(opts.observations.map((o) => o.traceId))];
- // Calculate timestamp range with buffer for efficient filtering
- const timestamps = opts.observations.map((o) => o.startTime.getTime());
- const minTimestamp = new Date(Math.min(...timestamps) - 1000); // -1 second buffer
- const maxTimestamp = new Date(Math.max(...timestamps) + 1000); // +1 second buffer
+ // Use provided timestamp range with buffer for efficient filtering
+ const minTimestamp = new Date(opts.minStartTime.getTime() - 1000); // -1 second buffer
+ const maxTimestamp = new Date(opts.maxStartTime.getTime() + 1000); // +1 second buffer
const query = `
SELECT
diff --git a/web/src/__tests__/async/repositories/event-repository.servertest.ts b/web/src/__tests__/async/repositories/event-repository.servertest.ts
index d200da0c8e..a5ec8f4bcf 100644
--- a/web/src/__tests__/async/repositories/event-repository.servertest.ts
+++ b/web/src/__tests__/async/repositories/event-repository.servertest.ts
@@ -1755,10 +1755,12 @@ describe("Clickhouse Events Repository Test", () => {
const result = await getObservationsBatchIOFromEventsTable({
projectId,
observations: [
- { id: observation1Id, traceId, startTime: timestamp },
- { id: observation2Id, traceId, startTime: timestamp },
- { id: observation3Id, traceId, startTime: timestamp },
+ { id: observation1Id, traceId },
+ { id: observation2Id, traceId },
+ { id: observation3Id, traceId },
],
+ minStartTime: timestamp,
+ maxStartTime: timestamp,
});
expect(result).toBeDefined();
@@ -1827,9 +1829,11 @@ describe("Clickhouse Events Repository Test", () => {
const result = await getObservationsBatchIOFromEventsTable({
projectId,
observations: [
- { id: existingId, traceId, startTime: timestamp },
- { id: nonExistentId, traceId, startTime: timestamp },
+ { id: existingId, traceId },
+ { id: nonExistentId, traceId },
],
+ minStartTime: timestamp,
+ maxStartTime: timestamp,
});
// Should only return the existing one
@@ -1865,7 +1869,9 @@ describe("Clickhouse Events Repository Test", () => {
// Try to fetch with wrong projectId
const result = await getObservationsBatchIOFromEventsTable({
projectId, // Using default projectId, not differentProjectId
- observations: [{ id: observationId, traceId, startTime: timestamp }],
+ observations: [{ id: observationId, traceId }],
+ minStartTime: timestamp,
+ maxStartTime: timestamp,
});
// Should not return anything since projectId doesn't match
diff --git a/web/src/features/events/hooks/useEventsTableData.ts b/web/src/features/events/hooks/useEventsTableData.ts
index bdedd16b10..0770d3f55b 100644
--- a/web/src/features/events/hooks/useEventsTableData.ts
+++ b/web/src/features/events/hooks/useEventsTableData.ts
@@ -72,38 +72,40 @@ export function useEventsTableData({
refetchOnWindowFocus: true,
});
- const observationsForIO = useMemo(
- () =>
- observations.data?.observations
- ?.filter((o) => o.id && o.traceId && o.startTime)
- .map((o) => {
- if (!o.traceId) {
- throw new Error("Trace ID is required");
- }
- if (!o.startTime) {
- throw new Error("Start time is required");
- }
- return {
- id: o.id,
- traceId: o.traceId,
- startTime: o.startTime,
- };
- }) ?? [],
- [observations.data?.observations],
- );
+ const batchIOPayload = useMemo(() => {
+ const validObservations =
+ observations.data?.observations?.filter(
+ (o) => o.id && o.traceId && o.startTime,
+ ) ?? [];
- // Fetch I/O data
- const ioDataQuery = api.events.batchIO.useQuery(
- {
+ if (validObservations.length === 0) {
+ return null;
+ }
+
+ const startTimes = validObservations
+ .map((o) => o.startTime?.getTime() ?? 0)
+ .filter((t) => t > 0);
+
+ const minStartTime = new Date(Math.min(...startTimes));
+ const maxStartTime = new Date(Math.max(...startTimes));
+
+ return {
projectId,
- observations: observationsForIO,
- },
- {
- enabled: observations.isSuccess && observationsForIO.length > 0,
- refetchOnWindowFocus: false,
- staleTime: 0,
- },
- );
+ observations: validObservations.map((o) => ({
+ id: o.id,
+ traceId: o.traceId!,
+ })),
+ minStartTime,
+ maxStartTime,
+ };
+ }, [observations.data?.observations, projectId]);
+
+ // Fetch I/O data
+ const ioDataQuery = api.events.batchIO.useQuery(batchIOPayload!, {
+ enabled: observations.isSuccess && batchIOPayload !== null,
+ refetchOnWindowFocus: false,
+ staleTime: 0,
+ });
// Memoize joined data to prevent infinite re-renders
// Include ioDataQuery.isSuccess to ensure re-render when I/O loads
diff --git a/web/src/features/events/server/eventsRouter.ts b/web/src/features/events/server/eventsRouter.ts
index 164e954018..cd8ef5f57c 100644
--- a/web/src/features/events/server/eventsRouter.ts
+++ b/web/src/features/events/server/eventsRouter.ts
@@ -46,9 +46,10 @@ export const BatchIOInput = zodSchema.object({
zodSchema.object({
id: zodSchema.string(),
traceId: zodSchema.string(),
- startTime: zodSchema.date(),
}),
),
+ minStartTime: zodSchema.date(),
+ maxStartTime: zodSchema.date(),
});
export type BatchIOInput = z.infer<typeof BatchIOInput>;
@@ -129,6 +130,8 @@ export const eventsRouter = createTRPCRouter({
return getEventBatchIO({
projectId: ctx.session.projectId,
observations: input.observations,
+ minStartTime: input.minStartTime,
+ maxStartTime: input.maxStartTime,
});
},
);
diff --git a/web/src/features/events/server/eventsService.ts b/web/src/features/events/server/eventsService.ts
index 54e214267e..0dfb700294 100644
--- a/web/src/features/events/server/eventsService.ts
+++ b/web/src/features/events/server/eventsService.ts
@@ -213,8 +213,9 @@ interface GetEventBatchIOParams {
observations: Array<{
id: string;
traceId: string;
- startTime: Date;
}>;
+ minStartTime: Date;
+ maxStartTime: Date;
}
/**
@@ -226,5 +227,7 @@ export async function getEventBatchIO(
return getObservationsBatchIOFromEventsTable({
projectId: params.projectId,
observations: params.observations,
+ minStartTime: params.minStartTime,
+ maxStartTime: params.maxStartTime,
});
}