Skip to content

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

perf: reduce trace scans for scores counts (#5860)

Steffen Schmitz4c5366fa2032
diff --git a/packages/shared/src/server/repositories/scores.ts b/packages/shared/src/server/repositories/scores.ts
index 0e125c0b13..9d75845079 100644
--- a/packages/shared/src/server/repositories/scores.ts
+++ b/packages/shared/src/server/repositories/scores.ts
@@ -346,9 +346,7 @@ export const getScoresUiCount = async (props: {
offset?: number;
}) => {
const rows = await getScoresUiGeneric<{ count: string }>({
- select: `
- count(*) as count
- `,
+ select: "count",
tags: { kind: "count" },
...props,
});
@@ -394,32 +392,7 @@ export const getScoresUiTable = async (props: {
created_at: string;
updated_at: string;
}>({
- select: `
- s.id,
- s.project_id,
- s.environment,
- s.name,
- s.value,
- s.string_value,
- s.timestamp,
- s.source,
- s.data_type,
- s.comment,
- s.trace_id,
- s.observation_id,
- s.author_user_id,
- t.user_id,
- t.name,
- t.tags,
- s.created_at,
- s.updated_at,
- s.source,
- s.config_id,
- s.queue_id,
- t.user_id,
- t.name as trace_name,
- t.tags as trace_tags
- `,
+ select: "rows",
tags: { kind: "analytic" },
...props,
});
@@ -449,7 +422,7 @@ export const getScoresUiTable = async (props: {
};
export const getScoresUiGeneric = async <T>(props: {
- select: string;
+ select: "count" | "rows";
projectId: string;
filter: FilterState;
orderBy: OrderByState;
@@ -457,26 +430,56 @@ export const getScoresUiGeneric = async <T>(props: {
offset?: number;
tags?: Record<string, string>;
}): Promise<T[]> => {
- const { select, projectId, filter, orderBy, limit, offset } = props;
+ const { projectId, filter, orderBy, limit, offset } = props;
+
+ const select =
+ props.select === "count"
+ ? "count(*) as count"
+ : `
+ s.id,
+ s.project_id,
+ s.environment,
+ s.name,
+ s.value,
+ s.string_value,
+ s.timestamp,
+ s.source,
+ s.data_type,
+ s.comment,
+ s.trace_id,
+ s.observation_id,
+ s.author_user_id,
+ t.user_id,
+ t.name,
+ t.tags,
+ s.created_at,
+ s.updated_at,
+ s.source,
+ s.config_id,
+ s.queue_id,
+ t.user_id,
+ t.name as trace_name,
+ t.tags as trace_tags
+ `;
const { scoresFilter } = getProjectIdDefaultFilter(projectId, {
tracesPrefix: "t",
});
-
scoresFilter.push(
...createFilterFromFilterState(filter, scoresTableUiColumnDefinitions),
);
-
const scoresFilterRes = scoresFilter.apply();
- // TODO: Can we realistically apply a traces time filter here? Is an order by event_ts a risk?
+ // Only join traces for rows or if there is a trace filter on counts
+ const performTracesJoin =
+ props.select === "rows" ||
+ scoresFilter.some((f) => f.clickhouseTable === "traces");
+
const query = `
SELECT
${select}
FROM scores s final
- LEFT JOIN traces t
- ON s.trace_id = t.id
- AND t.project_id = s.project_id
+ ${performTracesJoin ? "LEFT JOIN traces t ON s.trace_id = t.id AND t.project_id = s.project_id" : ""}
WHERE s.project_id = {projectId: String}
${scoresFilterRes?.query ? `AND ${scoresFilterRes.query}` : ""}
${orderByToClickhouseSql(orderBy ?? null, scoresTableUiColumnDefinitions)}
diff --git a/web/src/__tests__/async/sessions-trpc.servertest.ts b/web/src/__tests__/async/sessions-trpc.servertest.ts
index 27906cd7ef..55fe8744b2 100644
--- a/web/src/__tests__/async/sessions-trpc.servertest.ts
+++ b/web/src/__tests__/async/sessions-trpc.servertest.ts
@@ -155,4 +155,34 @@ describe("traces trpc", () => {
expect(sessions.sessions).toBeDefined();
});
});
+
+ describe("sessions.countAll", () => {
+ it("should count sessions", async () => {
+ // Setup
+ const sessionId = randomUUID();
+
+ await prisma.traceSession.create({
+ data: {
+ id: sessionId,
+ projectId,
+ },
+ });
+
+ const trace = createTrace({
+ project_id: projectId,
+ session_id: sessionId,
+ });
+ await createTracesCh([trace]);
+
+ // When
+ const sessions = await caller.sessions.countAll({
+ projectId,
+ filter: null,
+ orderBy: null,
+ });
+
+ // Then
+ expect(sessions.totalCount).toBeGreaterThan(0);
+ });
+ });
});