Skip to content

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

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

Marc Klingen112e7a2dcfef
diff --git a/web/src/components/table/use-cases/sessions.tsx b/web/src/components/table/use-cases/sessions.tsx
index 0bb2852203..55ab015f67 100644
--- a/web/src/components/table/use-cases/sessions.tsx
+++ b/web/src/components/table/use-cases/sessions.tsx
@@ -96,13 +96,23 @@ export default function SessionsTable({
order: "DESC",
});
- const sessions = api.sessions.all.useQuery({
- page: paginationState.pageIndex,
- limit: paginationState.pageSize,
+ const payloadCount = {
projectId,
filter: filterState,
+ orderBy: null,
+ page: 0,
+ limit: 1,
+ };
+
+ const payloadGetAll = {
+ ...payloadCount,
orderBy: orderByState,
- });
+ page: paginationState.pageIndex,
+ limit: paginationState.pageSize,
+ };
+
+ const sessions = api.sessions.all.useQuery(payloadGetAll);
+ const sessionCountQuery = api.sessions.countAll.useQuery(payloadCount);
const sessionMetrics = api.sessions.metrics.useQuery(
{
@@ -135,7 +145,7 @@ export default function SessionsTable({
},
);
- const totalCount = sessions.data?.totalCount ?? null;
+ const totalCount = sessionCountQuery.data?.totalCount ?? null;
useEffect(() => {
if (sessions.isSuccess) {
setDetailPageList(
diff --git a/web/src/server/api/routers/sessions.ts b/web/src/server/api/routers/sessions.ts
index c91e8beb73..ddbdd3dd86 100644
--- a/web/src/server/api/routers/sessions.ts
+++ b/web/src/server/api/routers/sessions.ts
@@ -29,6 +29,40 @@ const SessionFilterOptions = z.object({
export const sessionRouter = createTRPCRouter({
all: protectedProjectProcedure
+ .input(SessionFilterOptions)
+ .query(async ({ input, ctx }) => {
+ try {
+ const sessions = await ctx.prisma.$queryRaw<
+ Array<{
+ id: string;
+ createdAt: Date;
+ bookmarked: boolean;
+ public: boolean;
+ }>
+ >(
+ createSessionsAllQuery(
+ Prisma.sql`
+ s.id,
+ s."created_at" AS "createdAt",
+ s.bookmarked,
+ s.public
+ `,
+ input,
+ ),
+ );
+
+ return {
+ sessions,
+ };
+ } catch (e) {
+ console.error(e);
+ throw new TRPCError({
+ code: "INTERNAL_SERVER_ERROR",
+ message: "unable to get sessions",
+ });
+ }
+ }),
+ countAll: protectedProjectProcedure
.input(SessionFilterOptions)
.query(async ({ input, ctx }) => {
try {
@@ -40,53 +74,30 @@ export const sessionRouter = createTRPCRouter({
page: 0,
};
- const [sessions, totalCount] = await Promise.all([
- // sessions
- ctx.prisma.$queryRaw<
- Array<{
- id: string;
- createdAt: Date;
- bookmarked: boolean;
- public: boolean;
- }>
- >(
- createSessionsAllQuery(
- Prisma.sql`
- s.id,
- s."created_at" AS "createdAt",
- s.bookmarked,
- s.public
- `,
- input,
- ),
- ),
- // totalCount
- ctx.prisma.$queryRaw<
- Array<{
- totalCount: number;
- }>
- >(
- createSessionsAllQuery(
- Prisma.sql`
+ const totalCount = await ctx.prisma.$queryRaw<
+ Array<{
+ totalCount: number;
+ }>
+ >(
+ createSessionsAllQuery(
+ Prisma.sql`
count(*)::int as "totalCount"
`,
- inputForTotal,
- {
- ignoreOrderBy: true,
- },
- ),
+ inputForTotal,
+ {
+ ignoreOrderBy: true,
+ },
),
- ]);
+ );
return {
- sessions,
totalCount: totalCount[0].totalCount,
};
} catch (e) {
console.error(e);
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
- message: "unable to get sessions",
+ message: "unable to get session count",
});
}
}),