Skip to content

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

perf(ui): parallelize db queries in filterOptions routes (#3016)

Marc Klingen8b76feeffa0d
diff --git a/web/src/features/prompts/server/routers/promptRouter.ts b/web/src/features/prompts/server/routers/promptRouter.ts
index c6a08ce263..f60c5e9eb4 100644
--- a/web/src/features/prompts/server/routers/promptRouter.ts
+++ b/web/src/features/prompts/server/routers/promptRouter.ts
@@ -198,33 +198,36 @@ export const promptRouter = createTRPCRouter({
filterOptions: protectedProjectProcedure
.input(z.object({ projectId: z.string() }))
.query(async ({ input, ctx }) => {
- const names = await ctx.prisma.prompt.groupBy({
- where: {
- projectId: input.projectId,
- },
- by: ["name"],
- // limiting to 1k prompt names to avoid performance issues.
- // some users have unique names for large amounts of prompts
- // sending all prompt names to the FE exceeds the cloud function return size limit
- take: 1000,
- orderBy: {
- name: "asc",
- },
- });
- const tags: { value: string }[] = await ctx.prisma.$queryRaw`
- SELECT tags.tag as value
- FROM prompts, UNNEST(prompts.tags) AS tags(tag)
- WHERE prompts.project_id = ${input.projectId}
- GROUP BY tags.tag
- ORDER BY tags.tag ASC;
- `;
- const labels: { value: string }[] = await ctx.prisma.$queryRaw`
- SELECT labels.label as value
- FROM prompts, UNNEST(prompts.labels) AS labels(label)
- WHERE prompts.project_id = ${input.projectId}
- GROUP BY labels.label
- ORDER BY labels.label ASC;
- `;
+ const [names, tags, labels] = await Promise.all([
+ ctx.prisma.prompt.groupBy({
+ where: {
+ projectId: input.projectId,
+ },
+ by: ["name"],
+ // limiting to 1k prompt names to avoid performance issues.
+ // some users have unique names for large amounts of prompts
+ // sending all prompt names to the FE exceeds the cloud function return size limit
+ take: 1000,
+ orderBy: {
+ name: "asc",
+ },
+ }),
+ ctx.prisma.$queryRaw<{ value: string }[]>`
+ SELECT tags.tag as value
+ FROM prompts, UNNEST(prompts.tags) AS tags(tag)
+ WHERE prompts.project_id = ${input.projectId}
+ GROUP BY tags.tag
+ ORDER BY tags.tag ASC;
+ `,
+ ctx.prisma.$queryRaw<{ value: string }[]>`
+ SELECT labels.label as value
+ FROM prompts, UNNEST(prompts.labels) AS labels(label)
+ WHERE prompts.project_id = ${input.projectId}
+ GROUP BY labels.label
+ ORDER BY labels.label ASC;
+ `,
+ ]);
+
const res = {
name: names
.filter((n) => n.name !== null)
diff --git a/web/src/server/api/routers/generations/filterOptionsQuery.ts b/web/src/server/api/routers/generations/filterOptionsQuery.ts
index 108a390c6d..e772739213 100644
--- a/web/src/server/api/routers/generations/filterOptionsQuery.ts
+++ b/web/src/server/api/routers/generations/filterOptionsQuery.ts
@@ -32,36 +32,6 @@ export const filterOptionsQuery = protectedProjectProcedure
type: "GENERATION",
} as const;
- // Score names
- const scores = await ctx.prisma.score.groupBy({
- where: {
- projectId: input.projectId,
- timestamp: prismaStartTimeFilter,
- dataType: { in: ["NUMERIC", "BOOLEAN"] },
- },
- take: 1000,
- orderBy: {
- name: "asc",
- },
- by: ["name"],
- });
-
- // Model names
- const model = await ctx.prisma.observation.groupBy({
- by: ["model"],
- where: { ...queryFilter, startTime: prismaStartTimeFilter },
- take: 1000,
- orderBy: { model: "asc" },
- });
-
- // Observation names
- const name = await ctx.prisma.observation.groupBy({
- by: ["name"],
- where: { ...queryFilter, startTime: prismaStartTimeFilter },
- take: 1000,
- orderBy: { name: "asc" },
- });
-
const rawStartTimeFilter =
startTimeFilter && startTimeFilter.type === "datetime"
? datetimeFilterToPrismaSql(
@@ -71,12 +41,41 @@ export const filterOptionsQuery = protectedProjectProcedure
)
: Prisma.empty;
- // Prompt names
- const promptNames = await ctx.prisma.$queryRaw<
- Array<{
- promptName: string | null;
- }>
- >(Prisma.sql`
+ // Score names
+ const [scores, model, name, promptNames, traceNames] = await Promise.all([
+ // scores
+ ctx.prisma.score.groupBy({
+ where: {
+ projectId: input.projectId,
+ timestamp: prismaStartTimeFilter,
+ dataType: { in: ["NUMERIC", "BOOLEAN"] },
+ },
+ take: 1000,
+ orderBy: {
+ name: "asc",
+ },
+ by: ["name"],
+ }),
+ // model
+ ctx.prisma.observation.groupBy({
+ by: ["model"],
+ where: { ...queryFilter, startTime: prismaStartTimeFilter },
+ take: 1000,
+ orderBy: { model: "asc" },
+ }),
+ // name
+ ctx.prisma.observation.groupBy({
+ by: ["name"],
+ where: { ...queryFilter, startTime: prismaStartTimeFilter },
+ take: 1000,
+ orderBy: { name: "asc" },
+ }),
+ // promptNames
+ ctx.prisma.$queryRaw<
+ Array<{
+ promptName: string | null;
+ }>
+ >(Prisma.sql`
SELECT
p.name "promptName"
FROM prompts p
@@ -89,14 +88,13 @@ export const filterOptionsQuery = protectedProjectProcedure
GROUP BY p.name
ORDER BY p.name ASC
LIMIT 1000;
- `);
-
- // Trace names
- const traceName = await ctx.prisma.$queryRaw<
- Array<{
- traceName: string | null;
- }>
- >(Prisma.sql`
+ `),
+ // traceNames
+ ctx.prisma.$queryRaw<
+ Array<{
+ traceName: string | null;
+ }>
+ >(Prisma.sql`
SELECT
t.name "traceName"
FROM traces t
@@ -108,7 +106,8 @@ export const filterOptionsQuery = protectedProjectProcedure
GROUP BY t.name
ORDER BY t.name ASC
LIMIT 1000;
- `);
+ `),
+ ]);
// typecheck filter options, needs to include all columns with options
const res: ObservationOptions = {
@@ -118,7 +117,7 @@ export const filterOptionsQuery = protectedProjectProcedure
name: name
.filter((i) => i.name !== null)
.map((i) => ({ value: i.name as string })),
- traceName: traceName
+ traceName: traceNames
.filter((i) => i.traceName !== null)
.map((i) => ({
value: i.traceName as string,
diff --git a/web/src/server/api/routers/sessions.ts b/web/src/server/api/routers/sessions.ts
index 8c56abe183..00e9d990a1 100644
--- a/web/src/server/api/routers/sessions.ts
+++ b/web/src/server/api/routers/sessions.ts
@@ -76,17 +76,17 @@ export const sessionRouter = createTRPCRouter({
const userIds = await ctx.prisma.$queryRaw<
Array<{ value: string }>
>(Prisma.sql`
- SELECT
- traces.user_id AS value
- FROM traces
- WHERE
- traces.session_id IS NOT NULL
- AND traces.user_id IS NOT NULL
- AND traces.project_id = ${input.projectId}
- GROUP BY traces.user_id
- ORDER BY traces.user_id ASC
- LIMIT 1000;
- `);
+ SELECT
+ traces.user_id AS value
+ FROM traces
+ WHERE
+ traces.session_id IS NOT NULL
+ AND traces.user_id IS NOT NULL
+ AND traces.project_id = ${input.projectId}
+ GROUP BY traces.user_id
+ ORDER BY traces.user_id ASC
+ LIMIT 1000;
+ `);
const res: SessionOptions = {
userIds: userIds,
diff --git a/web/src/server/api/routers/traces.ts b/web/src/server/api/routers/traces.ts
index e78423573c..ffa849b2c2 100644
--- a/web/src/server/api/routers/traces.ts
+++ b/web/src/server/api/routers/traces.ts
@@ -206,29 +206,6 @@ export const traceRouter = createTRPCRouter({
: {}
: {};
- const scores = await ctx.prisma.score.groupBy({
- where: {
- projectId: input.projectId,
- timestamp: prismaTimestampFilter,
- dataType: { in: ["NUMERIC", "BOOLEAN"] },
- },
- take: 1000,
- orderBy: { name: "asc" },
- by: ["name"],
- });
- const names = await ctx.prisma.trace.groupBy({
- where: {
- projectId: input.projectId,
- timestamp: prismaTimestampFilter,
- },
- by: ["name"],
- // limiting to 1k trace names to avoid performance issues.
- // some users have unique names for large amounts of traces
- // sending all trace names to the FE exceeds the cloud function return size limit
- take: 1000,
- orderBy: { name: "asc" },
- });
-
const rawTimestampFilter =
timestampFilter && timestampFilter.type === "datetime"
? datetimeFilterToPrismaSql(
@@ -238,14 +215,38 @@ export const traceRouter = createTRPCRouter({
)
: Prisma.empty;
- const tags: { value: string }[] = await ctx.prisma.$queryRaw`
- SELECT tags.tag as value
- FROM traces, UNNEST(traces.tags) AS tags(tag)
- WHERE traces.project_id = ${input.projectId} ${rawTimestampFilter}
- GROUP BY tags.tag
- ORDER BY tags.tag ASC
- LIMIT 1000
- `;
+ const [scores, names, tags] = await Promise.all([
+ ctx.prisma.score.groupBy({
+ where: {
+ projectId: input.projectId,
+ timestamp: prismaTimestampFilter,
+ dataType: { in: ["NUMERIC", "BOOLEAN"] },
+ },
+ take: 1000,
+ orderBy: { name: "asc" },
+ by: ["name"],
+ }),
+ ctx.prisma.trace.groupBy({
+ where: {
+ projectId: input.projectId,
+ timestamp: prismaTimestampFilter,
+ },
+ by: ["name"],
+ // limiting to 1k trace names to avoid performance issues.
+ // some users have unique names for large amounts of traces
+ // sending all trace names to the FE exceeds the cloud function return size limit
+ take: 1000,
+ orderBy: { name: "asc" },
+ }),
+ ctx.prisma.$queryRaw<{ value: string }[]>`
+ SELECT tags.tag as value
+ FROM traces, UNNEST(traces.tags) AS tags(tag)
+ WHERE traces.project_id = ${input.projectId} ${rawTimestampFilter}
+ GROUP BY tags.tag
+ ORDER BY tags.tag ASC
+ LIMIT 1000
+ `,
+ ]);
const res: TraceOptions = {
scores_avg: scores.map((score) => score.name),
name: names