Skip to content

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

perf(ui): parallelize db queries in byId trpc routes (#3006)

Marc Klingen7aff2b2e61c0
diff --git a/web/src/server/api/routers/observations.ts b/web/src/server/api/routers/observations.ts
index ffd3bc57d6..b7292bd4d6 100644
--- a/web/src/server/api/routers/observations.ts
+++ b/web/src/server/api/routers/observations.ts
@@ -14,22 +14,22 @@ export const observationsRouter = createTRPCRouter({
}),
)
.query(async ({ input, ctx }) => {
- const observation = await ctx.prisma.observation.findFirstOrThrow({
- where: {
- id: input.observationId,
- traceId: input.traceId,
- projectId: input.projectId,
- },
- });
- const scores = observation.traceId
- ? await ctx.prisma.score.findMany({
- where: {
- traceId: observation.traceId,
- projectId: observation.projectId,
- },
- })
- : [];
+ const [observation, scores] = await Promise.all([
+ ctx.prisma.observation.findFirstOrThrow({
+ where: {
+ id: input.observationId,
+ traceId: input.traceId,
+ projectId: input.projectId,
+ },
+ }),
+ ctx.prisma.score.findMany({
+ where: {
+ traceId: input.traceId,
+ projectId: input.projectId,
+ },
+ }),
+ ]);
- return { ...observation, scores };
+ return { ...observation, scores: scores ?? [] };
}),
});
diff --git a/web/src/server/api/routers/traces.ts b/web/src/server/api/routers/traces.ts
index 6b1ec8ad91..283e2b75d6 100644
--- a/web/src/server/api/routers/traces.ts
+++ b/web/src/server/api/routers/traces.ts
@@ -277,57 +277,59 @@ export const traceRouter = createTRPCRouter({
}),
)
.query(async ({ input, ctx }) => {
- const trace = await ctx.prisma.trace.findFirstOrThrow({
- where: {
- id: input.traceId,
- projectId: input.projectId,
- },
- });
- const observations = await ctx.prisma.observationView.findMany({
- select: {
- id: true,
- traceId: true,
- projectId: true,
- type: true,
- startTime: true,
- endTime: true,
- name: true,
- parentObservationId: true,
- level: true,
- statusMessage: true,
- version: true,
- createdAt: true,
- model: true,
- modelParameters: true,
- promptTokens: true,
- completionTokens: true,
- totalTokens: true,
- unit: true,
- completionStartTime: true,
- timeToFirstToken: true,
- promptId: true,
- modelId: true,
- inputPrice: true,
- outputPrice: true,
- totalPrice: true,
- calculatedInputCost: true,
- calculatedOutputCost: true,
- calculatedTotalCost: true,
- },
- where: {
- traceId: {
- equals: input.traceId,
- not: null,
+ const [trace, observations, scores] = await Promise.all([
+ ctx.prisma.trace.findFirstOrThrow({
+ where: {
+ id: input.traceId,
+ projectId: input.projectId,
},
- projectId: trace.projectId,
- },
- });
- const scores = await ctx.prisma.score.findMany({
- where: {
- traceId: input.traceId,
- projectId: trace.projectId,
- },
- });
+ }),
+ ctx.prisma.observationView.findMany({
+ select: {
+ id: true,
+ traceId: true,
+ projectId: true,
+ type: true,
+ startTime: true,
+ endTime: true,
+ name: true,
+ parentObservationId: true,
+ level: true,
+ statusMessage: true,
+ version: true,
+ createdAt: true,
+ model: true,
+ modelParameters: true,
+ promptTokens: true,
+ completionTokens: true,
+ totalTokens: true,
+ unit: true,
+ completionStartTime: true,
+ timeToFirstToken: true,
+ promptId: true,
+ modelId: true,
+ inputPrice: true,
+ outputPrice: true,
+ totalPrice: true,
+ calculatedInputCost: true,
+ calculatedOutputCost: true,
+ calculatedTotalCost: true,
+ },
+ where: {
+ traceId: {
+ equals: input.traceId,
+ not: null,
+ },
+ projectId: input.projectId,
+ },
+ }),
+ ctx.prisma.score.findMany({
+ where: {
+ traceId: input.traceId,
+ projectId: input.projectId,
+ },
+ }),
+ ]);
const validatedScores = filterAndValidateDbScoreList(
scores,
traceException,
diff --git a/web/src/server/api/routers/users.ts b/web/src/server/api/routers/users.ts
index 48f9a1b066..01808fbe27 100644
--- a/web/src/server/api/routers/users.ts
+++ b/web/src/server/api/routers/users.ts
@@ -211,47 +211,45 @@ export const userRouter = createTRPCRouter({
}),
)
.query(async ({ input, ctx }) => {
- const agg = await ctx.prisma.$queryRaw<
- {
- userId: string;
- firstTrace: Date;
- lastTrace: Date;
- totalTraces: bigint;
- totalPromptTokens: bigint;
- totalCompletionTokens: bigint;
- totalTokens: bigint;
- firstObservation: Date;
- lastObservation: Date;
- totalObservations: bigint;
- sumCalculatedTotalCost: number;
- }[]
- >`
- SELECT
- t.user_id "userId",
- min(t."timestamp") "firstTrace",
- max(t."timestamp") "lastTrace",
- COUNT(distinct t.id)::bigint "totalTraces",
- COALESCE(SUM(o.prompt_tokens),0)::bigint "totalPromptTokens",
- COALESCE(SUM(o.completion_tokens),0)::bigint "totalCompletionTokens",
- COALESCE(SUM(o.total_tokens),0)::bigint "totalTokens",
- MIN(o.start_time) "firstObservation",
- MAX(o.start_time) "lastObservation",
- COUNT(distinct o.id)::bigint "totalObservations",
- SUM(COALESCE(o.calculated_total_cost, 0)) AS "sumCalculatedTotalCost"
- FROM traces t
- LEFT JOIN observations_view o on o.trace_id = t.id
- WHERE t.user_id is not null
- AND t.project_id = ${input.projectId}
- AND o.project_id = ${input.projectId}
- AND t.user_id = ${input.userId}
- GROUP BY 1
- ORDER BY "totalTokens" DESC
- LIMIT 50
- `;
-
- const lastScoresOfUsers = await ctx.prisma.$queryRaw<
- Array<LastUserScore>
- >`
+ const [agg, lastScoresOfUsers] = await Promise.all([
+ ctx.prisma.$queryRaw<
+ {
+ userId: string;
+ firstTrace: Date;
+ lastTrace: Date;
+ totalTraces: bigint;
+ totalPromptTokens: bigint;
+ totalCompletionTokens: bigint;
+ totalTokens: bigint;
+ firstObservation: Date;
+ lastObservation: Date;
+ totalObservations: bigint;
+ sumCalculatedTotalCost: number;
+ }[]
+ >`
+ SELECT
+ t.user_id "userId",
+ min(t."timestamp") "firstTrace",
+ max(t."timestamp") "lastTrace",
+ COUNT(distinct t.id)::bigint "totalTraces",
+ COALESCE(SUM(o.prompt_tokens),0)::bigint "totalPromptTokens",
+ COALESCE(SUM(o.completion_tokens),0)::bigint "totalCompletionTokens",
+ COALESCE(SUM(o.total_tokens),0)::bigint "totalTokens",
+ MIN(o.start_time) "firstObservation",
+ MAX(o.start_time) "lastObservation",
+ COUNT(distinct o.id)::bigint "totalObservations",
+ SUM(COALESCE(o.calculated_total_cost, 0)) AS "sumCalculatedTotalCost"
+ FROM traces t
+ LEFT JOIN observations_view o on o.trace_id = t.id
+ WHERE t.user_id is not null
+ AND t.project_id = ${input.projectId}
+ AND o.project_id = ${input.projectId}
+ AND t.user_id = ${input.userId}
+ GROUP BY 1
+ ORDER BY "totalTokens" DESC
+ LIMIT 50
+ `,
+ ctx.prisma.$queryRaw<Array<LastUserScore>>`
WITH ranked_scores AS (
SELECT
t.user_id,
@@ -282,7 +280,8 @@ export const userRouter = createTRPCRouter({
FROM
ranked_scores
WHERE rn = 1
- `;
+ `,
+ ]);
return {
userId: input.userId,