Skip to content

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

perf(ui): split totalCount from scores.all to reduce loading time of scores table (#3075)

Marc Klingenc7b726a73af6
diff --git a/web/src/components/table/use-cases/scores.tsx b/web/src/components/table/use-cases/scores.tsx
index 934dfb1e9f..3fb76109a2 100644
--- a/web/src/components/table/use-cases/scores.tsx
+++ b/web/src/components/table/use-cases/scores.tsx
@@ -121,14 +121,24 @@ export default function ScoresTable({
order: "DESC",
});
- const scores = api.scores.all.useQuery({
- page: paginationState.pageIndex,
- limit: paginationState.pageSize,
+ const getCountPayload = {
projectId,
filter: filterState,
+ page: 0,
+ limit: 1,
+ orderBy: null,
+ };
+
+ const getAllPayload = {
+ ...getCountPayload,
+ page: paginationState.pageIndex,
+ limit: paginationState.pageSize,
orderBy: orderByState,
- });
- const totalCount = scores.data?.totalCount ?? null;
+ };
+
+ const scores = api.scores.all.useQuery(getAllPayload);
+ const totalScoreCountQuery = api.scores.countAll.useQuery(getCountPayload);
+ const totalCount = totalScoreCountQuery.data?.totalCount ?? null;
const filterOptions = api.scores.filterOptions.useQuery(
{
diff --git a/web/src/server/api/routers/scores.ts b/web/src/server/api/routers/scores.ts
index 5158b13f86..d64d70ef08 100644
--- a/web/src/server/api/routers/scores.ts
+++ b/web/src/server/api/routers/scores.ts
@@ -84,32 +84,22 @@ export const scoresRouter = createTRPCRouter({
all: protectedProjectProcedure
.input(ScoreAllOptions)
.query(async ({ input, ctx }) => {
- const filterCondition = tableColumnsToSqlFilterAndPrefix(
- input.filter,
- scoresTableCols,
- "traces_scores",
- );
-
- const orderByCondition = orderByToPrismaSql(
- input.orderBy,
- scoresTableCols,
- );
+ const { filterCondition, orderByCondition } =
+ parseScoresGetAllOptions(input);
- const [scores, scoresCount] = await Promise.all([
- // scores
- ctx.prisma.$queryRaw<
- Array<
- Score & {
- traceName: string | null;
- traceUserId: string | null;
- jobConfigurationId: string | null;
- authorUserImage: string | null;
- authorUserName: string | null;
- }
- >
- >(
- generateScoresQuery(
- Prisma.sql`
+ const scores = await ctx.prisma.$queryRaw<
+ Array<
+ Score & {
+ traceName: string | null;
+ traceUserId: string | null;
+ jobConfigurationId: string | null;
+ authorUserImage: string | null;
+ authorUserName: string | null;
+ }
+ >
+ >(
+ generateScoresQuery(
+ Prisma.sql`
s.id,
s.name,
s.value,
@@ -127,30 +117,39 @@ export const scoresRouter = createTRPCRouter({
u.image AS "authorUserImage",
u.name AS "authorUserName"
`,
- input.projectId,
- ctx.session.orgId,
- filterCondition,
- orderByCondition,
- input.limit,
- input.page,
- ),
+ input.projectId,
+ ctx.session.orgId,
+ filterCondition,
+ orderByCondition,
+ input.limit,
+ input.page,
),
- // scoresCount
- await ctx.prisma.$queryRaw<Array<{ totalCount: bigint }>>(
- generateScoresQuery(
- Prisma.sql` count(*) AS "totalCount"`,
- input.projectId,
- ctx.session.orgId,
- filterCondition,
- Prisma.empty,
- 1, // limit
- 0, // page
- ),
- ),
- ]);
+ );
return {
scores,
+ };
+ }),
+ countAll: protectedProjectProcedure
+ .input(ScoreAllOptions)
+ .query(async ({ input, ctx }) => {
+ const { filterCondition } = parseScoresGetAllOptions(input);
+
+ const scoresCount = await ctx.prisma.$queryRaw<
+ Array<{ totalCount: bigint }>
+ >(
+ generateScoresQuery(
+ Prisma.sql` count(*) AS "totalCount"`,
+ input.projectId,
+ ctx.session.orgId,
+ filterCondition,
+ Prisma.empty,
+ 1, // limit
+ 0, // page
+ ),
+ );
+
+ return {
totalCount:
scoresCount.length > 0 ? Number(scoresCount[0]?.totalCount) : 0,
};
@@ -373,6 +372,17 @@ export const scoresRouter = createTRPCRouter({
}),
});
+const parseScoresGetAllOptions = (input: z.infer<typeof ScoreAllOptions>) => {
+ const filterCondition = tableColumnsToSqlFilterAndPrefix(
+ input.filter,
+ scoresTableCols,
+ "traces_scores",
+ );
+
+ const orderByCondition = orderByToPrismaSql(input.orderBy, scoresTableCols);
+ return { filterCondition, orderByCondition };
+};
+
const generateScoresQuery = (
select: Prisma.Sql,
projectId: string,