Skip to content

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

perf: users table (#1320)

Max Deichmann64c0e26da60e
diff --git a/web/prisma/migrations/20240305095119_add_observations_index/migration.sql b/web/prisma/migrations/20240305095119_add_observations_index/migration.sql
new file mode 100644
index 0000000000..d677f90654
--- /dev/null
+++ b/web/prisma/migrations/20240305095119_add_observations_index/migration.sql
@@ -0,0 +1,2 @@
+-- CreateIndex
+CREATE INDEX "observations_trace_id_project_id_idx" ON "observations"("trace_id", "project_id");
diff --git a/web/prisma/migrations/20240305100713_traces_add_index/migration.sql b/web/prisma/migrations/20240305100713_traces_add_index/migration.sql
new file mode 100644
index 0000000000..d96d039aa8
--- /dev/null
+++ b/web/prisma/migrations/20240305100713_traces_add_index/migration.sql
@@ -0,0 +1,2 @@
+-- CreateIndex
+CREATE INDEX "traces_id_user_id_idx" ON "traces"("id", "user_id");
diff --git a/web/prisma/schema.prisma b/web/prisma/schema.prisma
index 0172fb7dce..bc4cfa487b 100644
--- a/web/prisma/schema.prisma
+++ b/web/prisma/schema.prisma
@@ -216,6 +216,7 @@ model Trace {
@@index([sessionId])
@@index([name])
@@index([userId])
+ @@index([id, userId])
@@index([externalId])
@@index(timestamp)
@@index(release)
@@ -264,6 +265,7 @@ model Observation {
@@index([projectId, internalModel, startTime, unit])
@@index([traceId, projectId, type, startTime])
@@index([traceId, projectId, startTime])
+ @@index([traceId, projectId])
@@index([traceId])
@@index([type])
@@index(startTime)
diff --git a/web/src/server/api/routers/users.ts b/web/src/server/api/routers/users.ts
index 1e98bf4ec5..751a413953 100644
--- a/web/src/server/api/routers/users.ts
+++ b/web/src/server/api/routers/users.ts
@@ -20,7 +20,7 @@ export const userRouter = createTRPCRouter({
.input(UserAllOptions)
.query(async ({ input, ctx }) => {
const users = await ctx.prisma.$queryRaw<
- {
+ Array<{
userId: string;
firstTrace: Date | null;
lastTrace: Date | null;
@@ -33,31 +33,62 @@ export const userRouter = createTRPCRouter({
totalObservations: number;
totalCount: number;
sumCalculatedTotalCost: number;
- }[]
+ }>
>`
- SELECT
- t.user_id "userId",
- min(t."timestamp") "firstTrace",
- max(t."timestamp") "lastTrace",
- COUNT(distinct t.id)::int "totalTraces",
- COALESCE(SUM(o.prompt_tokens),0)::int "totalPromptTokens",
- COALESCE(SUM(o.completion_tokens),0)::int "totalCompletionTokens",
- COALESCE(SUM(o.total_tokens),0)::int "totalTokens",
- MIN(o.start_time) "firstObservation",
- MAX(o.start_time) "lastObservation",
- COUNT(distinct o.id)::int "totalObservations",
- (count(*) OVER())::int AS "totalCount",
- 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}
- GROUP BY 1
- ORDER BY "totalTokens" DESC
+ SELECT
+ t.user_id AS "userId",
+ MIN(t."timestamp") AS "firstTrace",
+ MAX(t."timestamp") AS "lastTrace",
+ COUNT(DISTINCT t.id)::int AS "totalTraces",
+ COALESCE(SUM(o.prompt_tokens), 0)::int AS "totalPromptTokens",
+ COALESCE(SUM(o.completion_tokens), 0)::int AS "totalCompletionTokens",
+ COALESCE(SUM(o.total_tokens), 0)::int AS "totalTokens",
+ MIN(o."firstObservation") AS "firstObservation",
+ MAX(o."lastObservation") AS "lastObservation",
+ COUNT(o."totalObservations")::int AS "totalObservations",
+ (COUNT(*) OVER ())::int AS "totalCount",
+ SUM(COALESCE(ov.calculated_total_cost, 0)) AS "sumCalculatedTotalCost"
+ FROM
+ traces t
+ LEFT JOIN LATERAL (
+ SELECT
+ COALESCE(SUM(o.prompt_tokens), 0)::int AS "prompt_tokens",
+ COALESCE(SUM(o.completion_tokens), 0)::int AS "completion_tokens",
+ COALESCE(SUM(o.total_tokens), 0)::int AS "total_tokens",
+ MIN(o.start_time) AS "firstObservation",
+ MAX(o.start_time) AS "lastObservation",
+ COUNT(DISTINCT o.id)::int AS "totalObservations"
+ FROM
+ observations o
+ WHERE
+ o.trace_id = t.id
+ AND o.project_id = ${input.projectId}
+ GROUP BY
+ t.user_id
+ ) o ON TRUE
+ LEFT JOIN LATERAL (
+ SELECT
+ SUM(COALESCE(ov.calculated_total_cost, 0)) AS "calculated_total_cost"
+ FROM
+ observations_view ov
+ WHERE
+ ov.trace_id = t.id
+ AND ov."type" = 'GENERATION'
+ AND ov.project_id = ${input.projectId}
+ GROUP BY
+ t.user_id
+ ) ov ON TRUE
+ WHERE
+ t.user_id IS NOT NULL
+ AND t.project_id = ${input.projectId}
+ GROUP BY
+ 1
+ ORDER BY
+ "totalTokens" DESC
LIMIT ${input.limit}
- OFFSET ${input.page * input.limit}
+ OFFSET ${input.page * input.limit};
`;
+
if (users.length === 0) {
return [];
}
@@ -150,6 +181,7 @@ export const userRouter = createTRPCRouter({
ORDER BY "totalTokens" DESC
LIMIT 50
`;
+
const lastScoresOfUsers = await ctx.prisma.$queryRaw<
Array<
Score & {