Skip to content

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

fix: exclude clickhouse default date from session duration calc (#5726)

Steffen Schmitzb5cad2693f00
diff --git a/packages/shared/src/server/services/sessions-ui-table-service.ts b/packages/shared/src/server/services/sessions-ui-table-service.ts
index e7ef090085..ce4217fc02 100644
--- a/packages/shared/src/server/services/sessions-ui-table-service.ts
+++ b/packages/shared/src/server/services/sessions-ui-table-service.ts
@@ -271,7 +271,9 @@ const getSessionsTableGeneric = async <T>(props: FetchSessionsTableProps) => {
? `
,
sum(o.obs_count) as total_observations,
- date_diff('millisecond', min(min_start_time), max(max_end_time)) as duration,
+ -- Use minIf, because ClickHouse fills 1970-01-01 on left joins. We assume that no
+ -- LLM session started on that date so this behaviour should yield better results.
+ date_diff('millisecond', minIf(min_start_time, min_start_time > '1970-01-01'), max(max_end_time)) as duration,
sumMap(o.sum_usage_details) as session_usage_details,
sumMap(o.sum_cost_details) as session_cost_details,
arraySum(mapValues(mapFilter(x -> positionCaseInsensitive(x.1, 'input') > 0, sumMap(o.sum_cost_details)))) as session_input_cost,
diff --git a/web/src/__tests__/async/sessions-ui-table.servertest.ts b/web/src/__tests__/async/sessions-ui-table.servertest.ts
index e4ff6ffa10..3acb92f4d4 100644
--- a/web/src/__tests__/async/sessions-ui-table.servertest.ts
+++ b/web/src/__tests__/async/sessions-ui-table.servertest.ts
@@ -322,4 +322,67 @@ describe("trpc.sessions", () => {
expect(Number(session2?.session_output_usage)).toBeGreaterThan(0);
expect(Number(session2?.session_total_usage)).toBeGreaterThan(0);
});
+
+ it("LFE-4113: should GET correct metrics for a list of sessions without observations", async () => {
+ const { projectId } = await createOrgProjectAndApiKey();
+ const sessionId = v4();
+
+ await prisma.traceSession.createMany({
+ data: [
+ {
+ id: sessionId,
+ projectId: projectId,
+ },
+ ],
+ });
+
+ const traces = [
+ createTrace({
+ session_id: sessionId,
+ project_id: projectId,
+ user_id: "user1",
+ }),
+ createTrace({
+ session_id: sessionId,
+ project_id: projectId,
+ user_id: "user3",
+ }),
+ ];
+
+ await createTracesCh(traces);
+
+ // Only trace 2 has observations
+ const observations = [
+ createObservation({
+ trace_id: traces[1].id,
+ project_id: projectId,
+ start_time: new Date().getTime() - 1000,
+ }),
+ createObservation({
+ trace_id: traces[1].id,
+ project_id: projectId,
+ start_time: new Date().getTime(),
+ }),
+ ];
+
+ await createObservationsCh(observations);
+
+ const sessions = await getSessionsWithMetrics({
+ projectId: projectId,
+ filter: [
+ {
+ column: "id",
+ type: "stringOptions",
+ operator: "any of",
+ value: [sessionId],
+ },
+ ],
+ });
+
+ expect(sessions.length).toBe(1);
+
+ expect(sessions[0]).toBeDefined();
+ expect(sessions[0]?.trace_count).toBe(2);
+ expect(sessions[0]?.duration).toEqual("1000");
+ });
});