Skip to content

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

fix(ui): metrics on users table (#1526)

Marc Klingen530d0a613d07
diff --git a/web/src/pages/project/[projectId]/users.tsx b/web/src/pages/project/[projectId]/users.tsx
index ca170def04..ba2c9a098d 100644
--- a/web/src/pages/project/[projectId]/users.tsx
+++ b/web/src/pages/project/[projectId]/users.tsx
@@ -44,10 +44,15 @@ export default function UsersPage() {
// this API call will return an empty array if there are no users.
// Hence this adds one fast unnecessary API call if there are no users.
- const userMetrics = api.users.metrics.useQuery({
- projectId,
- userIds: users.data?.users.map((u) => u.userId) ?? [],
- });
+ const userMetrics = api.users.metrics.useQuery(
+ {
+ projectId,
+ userIds: users.data?.users.map((u) => u.userId) ?? [],
+ },
+ {
+ enabled: users.isSuccess,
+ },
+ );
type UserCoreOutput = RouterOutput["users"]["all"]["users"][number];
type UserMetricsOutput = RouterOutput["users"]["metrics"][number];
@@ -101,20 +106,7 @@ export default function UsersPage() {
header: "First Event",
cell: ({ row }) => {
const value: unknown = row.getValue("firstEvent");
- if (userMetrics.isFetching) {
- return <Skeleton className="h-3 w-1/2" />;
- }
- if (typeof value === "string") {
- return <>{value}</>;
- }
- },
- },
- {
- accessorKey: "totalCost",
- header: "Total Cost",
- cell: ({ row }) => {
- const value: unknown = row.getValue("totalCost");
- if (userMetrics.isFetching) {
+ if (!userMetrics.isSuccess) {
return <Skeleton className="h-3 w-1/2" />;
}
if (typeof value === "string") {
@@ -127,7 +119,7 @@ export default function UsersPage() {
header: "Last Event",
cell: ({ row }) => {
const value: unknown = row.getValue("lastEvent");
- if (userMetrics.isFetching) {
+ if (!userMetrics.isSuccess) {
return <Skeleton className="h-3 w-1/2" />;
}
if (typeof value === "string") {
@@ -140,7 +132,7 @@ export default function UsersPage() {
header: "Total Events",
cell: ({ row }) => {
const value: unknown = row.getValue("totalEvents");
- if (userMetrics.isFetching) {
+ if (!userMetrics.isSuccess) {
return <Skeleton className="h-3 w-1/2" />;
}
if (typeof value === "string") {
@@ -153,7 +145,20 @@ export default function UsersPage() {
header: "Total Tokens",
cell: ({ row }) => {
const value: unknown = row.getValue("totalTokens");
- if (userMetrics.isFetching) {
+ if (!userMetrics.isSuccess) {
+ return <Skeleton className="h-3 w-1/2" />;
+ }
+ if (typeof value === "string") {
+ return <>{value}</>;
+ }
+ },
+ },
+ {
+ accessorKey: "totalCost",
+ header: "Total Cost",
+ cell: ({ row }) => {
+ const value: unknown = row.getValue("totalCost");
+ if (!userMetrics.isSuccess) {
return <Skeleton className="h-3 w-1/2" />;
}
if (typeof value === "string") {
@@ -166,7 +171,7 @@ export default function UsersPage() {
header: "Last Score",
cell: ({ row }) => {
const value: Score | null = row.getValue("lastScore");
- if (userMetrics.isFetching) {
+ if (!userMetrics.isSuccess) {
return <Skeleton className="h-3 w-1/2" />;
}
@@ -227,13 +232,13 @@ export default function UsersPage() {
(Number(t.totalTraces) || 0) +
(Number(t.totalObservations) || 0),
),
- totalTokens: t.totalTokens
- ? compactNumberFormatter(t.totalTokens)
- : undefined,
+ totalTokens: compactNumberFormatter(t.totalTokens ?? 0),
lastScore: t.lastScore,
- totalCost: t.sumCalculatedTotalCost
- ? usdFormatter(t.sumCalculatedTotalCost, 2, 2)
- : undefined,
+ totalCost: usdFormatter(
+ t.sumCalculatedTotalCost ?? 0,
+ 2,
+ 2,
+ ),
};
}),
}
diff --git a/web/src/server/api/routers/users.ts b/web/src/server/api/routers/users.ts
index 5cd2c47c7a..3fafbe0fb2 100644
--- a/web/src/server/api/routers/users.ts
+++ b/web/src/server/api/routers/users.ts
@@ -68,7 +68,6 @@ export const userRouter = createTRPCRouter({
if (input.userIds.length === 0) {
return [];
}
- const userIds = input.userIds.map((id) => `'${id}'`).join(",");
const users = await ctx.prisma.$queryRaw<
Array<{
userId: string;
@@ -84,54 +83,54 @@ export const userRouter = createTRPCRouter({
sumCalculatedTotalCost: number;
}>
>`
- SELECT
- t.user_id AS "userId",
- MIN(t."timestamp") AS "firstTrace",
- MAX(t."timestamp") AS "lastTrace",
- 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"
+ t.user_id AS "userId",
+ MIN(t."timestamp") AS "firstTrace",
+ MAX(t."timestamp") AS "lastTrace",
+ 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
- observations_view ov
+ 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
- ov.trace_id = t.id
- AND ov."type" = 'GENERATION'
- AND ov.project_id = ${input.projectId}
+ t.user_id IN (${Prisma.join(input.userIds)})
+ AND t.project_id = ${input.projectId}
GROUP BY
- t.user_id
- ) ov ON TRUE
- WHERE
- t.user_id IN (${userIds})
- AND t.project_id = ${input.projectId}
- GROUP BY
- 1;
- `;
+ 1;
+ `;
if (users.length === 0) {
return [];
@@ -144,33 +143,33 @@ export const userRouter = createTRPCRouter({
}
>
>`
- WITH ranked_scores AS (
- SELECT
- t.user_id,
- s.*,
- ROW_NUMBER() OVER (PARTITION BY t.user_id ORDER BY s."timestamp" DESC) AS rn
- FROM
- scores s
- JOIN traces t ON t.id = s.trace_id
- WHERE
- s.trace_id IS NOT NULL
- AND t.project_id = ${input.projectId}
- AND t.user_id IN (${Prisma.join(users.map((user) => user.userId))})
- AND t.user_id IS NOT NULL
- )
- SELECT
- user_id "userId",
- "id",
- "timestamp",
- "name",
- "value",
- observation_id "observationId",
- trace_id "traceId",
- "comment"
- FROM
- ranked_scores
- WHERE rn = 1
- `;
+ WITH ranked_scores AS (
+ SELECT
+ t.user_id,
+ s.*,
+ ROW_NUMBER() OVER (PARTITION BY t.user_id ORDER BY s."timestamp" DESC) AS rn
+ FROM
+ scores s
+ JOIN traces t ON t.id = s.trace_id
+ WHERE
+ s.trace_id IS NOT NULL
+ AND t.project_id = ${input.projectId}
+ AND t.user_id IN (${Prisma.join(users.map((user) => user.userId))})
+ AND t.user_id IS NOT NULL
+ )
+ SELECT
+ user_id "userId",
+ "id",
+ "timestamp",
+ "name",
+ "value",
+ observation_id "observationId",
+ trace_id "traceId",
+ "comment"
+ FROM
+ ranked_scores
+ WHERE rn = 1
+ `;
return users.map((topUser) => {
const user = users.find((user) => user.userId === topUser.userId);
if (!user) {