🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
chore(comments): fetch comment counts by type in trace tree (#3098)
marliessophie66c810e4a74a
diff --git a/web/src/components/session/index.tsx b/web/src/components/session/index.tsxindex 7054554192..bafbdbd05b 100644--- a/web/src/components/session/index.tsx+++ b/web/src/components/session/index.tsx@@ -53,10 +53,10 @@ export const SessionPage: React.FC<{string[]>("emptySelectedConfigIds", []);- const commentCounts = api.comments.getCountsByObjectIds.useQuery(+ const commentCounts = api.comments.getCountByObjectId.useQuery({projectId,- objectIds: [sessionId],+ objectId: sessionId,objectType: "SESSION",},{ enabled: session.isSuccess },diff --git a/web/src/components/trace/TraceTimelineView.tsx b/web/src/components/trace/TraceTimelineView.tsxindex 2b82666e5c..4f4c096838 100644--- a/web/src/components/trace/TraceTimelineView.tsx+++ b/web/src/components/trace/TraceTimelineView.tsx@@ -46,14 +46,12 @@ const PREDEFINED_STEP_SIZES = [const getNestedObservationKeys = (observations: NestedObservation[],-): { keys: string[]; ids: string[] } => {+): string[] => {const keys: string[] = [];- const ids: string[] = [];const collectKeys = (obs: NestedObservation[]) => {obs.forEach((observation) => {keys.push(`observation-${observation.id}`);- ids.push(observation.id);if (observation.children) {collectKeys(observation.children);}@@ -61,7 +59,7 @@ const getNestedObservationKeys = (};collectKeys(observations);- return { keys, ids };+ return keys;};const calculateStepSize = (latency: number, scaleWidth: number) => {@@ -306,17 +304,16 @@ export function TraceTimelineView({() => nestObservations(observations),[observations],);- const { keys: nestedObservationKeys, ids: nestedObservationIds } = useMemo(+ const nestedObservationKeys = useMemo(() => getNestedObservationKeys(nestedObservations),[nestedObservations],);const session = useSession();- const observationCommentCounts = api.comments.getCountsByObjectIds.useQuery(+ const observationCommentCounts = api.comments.getCountByObjectType.useQuery({projectId: trace.projectId,- objectIds: nestedObservationIds,objectType: "OBSERVATION",},{@@ -330,10 +327,10 @@ export function TraceTimelineView({},);- const traceCommentCounts = api.comments.getCountsByObjectIds.useQuery(+ const traceCommentCounts = api.comments.getCountByObjectId.useQuery({projectId: trace.projectId,- objectIds: [trace.id],+ objectId: trace.id,objectType: "TRACE",},{diff --git a/web/src/components/trace/index.tsx b/web/src/components/trace/index.tsxindex d34cfa2430..2ddb7e556f 100644--- a/web/src/components/trace/index.tsx+++ b/web/src/components/trace/index.tsx@@ -27,7 +27,7 @@ import {} from "lucide-react";import { usdFormatter } from "@/src/utils/numbers";import Decimal from "decimal.js";-import { useCallback, useMemo, useState } from "react";+import { useCallback, useState } from "react";import { DeleteButton } from "@/src/components/deleteButton";import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";import { Tabs, TabsList, TabsTrigger } from "@/src/components/ui/tabs";@@ -57,16 +57,11 @@ export function Trace(props: {[],);- const observationObjectIds: string[] = useMemo(() => {- return props.observations.map(({ id }) => id);- }, [props.observations]);-const session = useSession();- const observationCommentCounts = api.comments.getCountsByObjectIds.useQuery(+ const observationCommentCounts = api.comments.getCountByObjectType.useQuery({projectId: props.trace.projectId,- objectIds: observationObjectIds,objectType: "OBSERVATION",},{@@ -80,10 +75,10 @@ export function Trace(props: {},);- const traceCommentCounts = api.comments.getCountsByObjectIds.useQuery(+ const traceCommentCounts = api.comments.getCountByObjectId.useQuery({projectId: props.trace.projectId,- objectIds: [props.trace.id],+ objectId: props.trace.id,objectType: "TRACE",},{diff --git a/web/src/server/api/routers/comments.ts b/web/src/server/api/routers/comments.tsindex c0a96ee982..7b0f966a0f 100644--- a/web/src/server/api/routers/comments.ts+++ b/web/src/server/api/routers/comments.ts@@ -209,11 +209,11 @@ export const commentsRouter = createTRPCRouter({});}}),- getCountsByObjectIds: protectedProjectProcedure+ getCountByObjectId: protectedProjectProcedure.input(z.object({projectId: z.string(),- objectIds: z.array(z.string()),+ objectId: z.string(),objectType: z.nativeEnum(CommentObjectType),}),)@@ -225,29 +225,58 @@ export const commentsRouter = createTRPCRouter({scope: "comments:read",});- const comments = await ctx.prisma.comment.findMany({- select: {- id: true,- objectId: true,- },+ const commentCount = await ctx.prisma.comment.count({where: {projectId: input.projectId,- objectId: { in: input.objectIds },+ objectId: input.objectId,objectType: input.objectType,},});- const commentCountByObject = new Map<string, number>();+ return new Map([[input.objectId, commentCount]]);+ } catch (error) {+ console.error(error);+ if (error instanceof TRPCError) {+ throw error;+ }+ throw new TRPCError({+ code: "INTERNAL_SERVER_ERROR",+ message: "Fetching comment count by object id failed.",+ });+ }+ }),+ getCountByObjectType: protectedProjectProcedure+ .input(+ z.object({+ projectId: z.string(),+ objectType: z.nativeEnum(CommentObjectType),+ }),+ )+ .query(async ({ input, ctx }) => {+ try {+ throwIfNoProjectAccess({+ session: ctx.session,+ projectId: input.projectId,+ scope: "comments:read",+ });- comments.forEach(({ objectId }) => {- const prevCount = commentCountByObject.get(objectId);- if (!!prevCount) {- commentCountByObject.set(objectId, prevCount + 1);- } else {- commentCountByObject.set(objectId, 1);- }+ // latency of query to be improved+ const commentCounts = await ctx.prisma.comment.groupBy({+ by: ["objectId"],+ where: {+ projectId: input.projectId,+ objectType: input.objectType,+ },+ _count: {+ objectId: true,+ },});- return commentCountByObject;+ return new Map(+ commentCounts.map(({ objectId, _count }) => [+ objectId,+ _count.objectId,+ ]),+ );} catch (error) {console.error(error);if (error instanceof TRPCError) {@@ -255,7 +284,7 @@ export const commentsRouter = createTRPCRouter({}throw new TRPCError({code: "INTERNAL_SERVER_ERROR",- message: "Fetching comment count failed.",+ message: "Fetching comment count by object type failed.",});}}),