Skip to content

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

fix: include time filter into user metrics query (#4808)

Max Deichmannab5febb9c7fe
diff --git a/packages/shared/src/server/repositories/traces.ts b/packages/shared/src/server/repositories/traces.ts
index dd31350e97..f81348c400 100644
--- a/packages/shared/src/server/repositories/traces.ts
+++ b/packages/shared/src/server/repositories/traces.ts
@@ -680,10 +680,25 @@ export const getTotalUserCount = async (
});
};
-export const getUserMetrics = async (projectId: string, userIds: string[]) => {
+export const getUserMetrics = async (
+ projectId: string,
+ userIds: string[],
+ filter: FilterState,
+) => {
if (userIds.length === 0) {
return [];
}
+
+ // filter state contains date range filter for traces so far.
+ const chFilter = new FilterList(
+ createFilterFromFilterState(filter, tracesTableUiColumnDefinitions),
+ );
+ const chFilterRes = chFilter.apply();
+
+ const timestampFilter = chFilter.find(
+ (f) => f.field === "timestamp" && f.operator === ">=",
+ );
+
// this query uses window functions on observations + traces to always get only the first row and thereby remove deduplicates
// we filter wherever possible by project id and user id
const query = `
@@ -713,6 +728,7 @@ export const getUserMetrics = async (projectId: string, userIds: string[]) => {
observations o
WHERE
o.project_id = {projectId: String }
+ ${timestampFilter ? `AND o.start_time >= {traceTimestamp: DateTime64(3)} - ${OBSERVATIONS_TO_TRACE_INTERVAL}` : ""}
AND o.trace_id in (
SELECT
distinct id
@@ -721,6 +737,7 @@ export const getUserMetrics = async (projectId: string, userIds: string[]) => {
where
user_id IN ({userIds: Array(String) })
AND project_id = {projectId: String }
+ ${filter.length > 0 ? `AND ${chFilterRes.query}` : ""}
)
AND o.type = 'GENERATION'
) as o
@@ -740,6 +757,7 @@ export const getUserMetrics = async (projectId: string, userIds: string[]) => {
WHERE
t.user_id IN ({userIds: Array(String) })
AND t.project_id = {projectId: String }
+ ${filter.length > 0 ? `AND ${chFilterRes.query}` : ""}
) as t on t.id = o.trace_id
and t.project_id = o.project_id
WHERE
@@ -778,8 +796,17 @@ export const getUserMetrics = async (projectId: string, userIds: string[]) => {
params: {
projectId,
userIds,
+ ...chFilterRes.params,
+ ...(timestampFilter
+ ? {
+ traceTimestamp: convertDateToClickhouseDateTime(
+ (timestampFilter as DateTimeFilter).value,
+ ),
+ }
+ : {}),
},
});
+
return rows.map((row) => ({
userId: row.user_id,
maxTimestamp: parseClickhouseUTCDateTimeFormat(row.max_timestamp),
diff --git a/web/src/__tests__/async/users-ui-table.servertest.ts b/web/src/__tests__/async/users-ui-table.servertest.ts
index 0e04f091ae..bfc4d21664 100644
--- a/web/src/__tests__/async/users-ui-table.servertest.ts
+++ b/web/src/__tests__/async/users-ui-table.servertest.ts
@@ -52,7 +52,7 @@ describe("getUserMetrics function", () => {
await createObservationsInClickhouse([observation1, observation2]);
- const userMetrics = await getUserMetrics(projectId, [userId]);
+ const userMetrics = await getUserMetrics(projectId, [userId], []);
expect(userMetrics.length).toBe(1);
expect(userMetrics[0]).toMatchObject({
diff --git a/web/src/pages/project/[projectId]/users.tsx b/web/src/pages/project/[projectId]/users.tsx
index a6cb243e81..4c844174cb 100644
--- a/web/src/pages/project/[projectId]/users.tsx
+++ b/web/src/pages/project/[projectId]/users.tsx
@@ -89,6 +89,7 @@ export default function UsersPage() {
projectId,
userIds: users.data?.users.map((u) => u.userId) ?? [],
queryClickhouse: useClickhouse(),
+ filter: filterState,
},
{
enabled: users.isSuccess,
diff --git a/web/src/server/api/routers/users.ts b/web/src/server/api/routers/users.ts
index 1079671647..d656720368 100644
--- a/web/src/server/api/routers/users.ts
+++ b/web/src/server/api/routers/users.ts
@@ -121,6 +121,7 @@ export const userRouter = createTRPCRouter({
projectId: z.string(),
userIds: z.array(z.string().min(1)),
queryClickhouse: z.boolean().default(false),
+ filter: z.array(singleFilter).nullable(),
}),
)
.query(async ({ input, ctx }) => {
@@ -182,7 +183,11 @@ export const userRouter = createTRPCRouter({
if (input.userIds.length === 0) {
return [];
}
- const metrics = await getUserMetrics(input.projectId, input.userIds);
+ const metrics = await getUserMetrics(
+ input.projectId,
+ input.userIds,
+ input.filter ?? [],
+ );
return metrics.map((metric) => ({
userId: metric.userId,
@@ -262,7 +267,7 @@ export const userRouter = createTRPCRouter({
},
clickhouseExecution: async () => {
const result = (
- await getUserMetrics(input.projectId, [input.userId])
+ await getUserMetrics(input.projectId, [input.userId], [])
).shift();
return {