Skip to content

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

chore: add filter options for sessions and remove traces final (#9589)

Max Deichmannd3388aac23c5
diff --git a/packages/shared/package.json b/packages/shared/package.json
index af06fc1bb7..297b12a80f 100644
--- a/packages/shared/package.json
+++ b/packages/shared/package.json
@@ -127,4 +127,4 @@
"@types/react": "~19.1.12",
"react": "~19.1.1"
}
-}
+}
\ No newline at end of file
diff --git a/packages/shared/src/server/repositories/traces.ts b/packages/shared/src/server/repositories/traces.ts
index 3b1d1ac10d..3f4baf1b1f 100644
--- a/packages/shared/src/server/repositories/traces.ts
+++ b/packages/shared/src/server/repositories/traces.ts
@@ -827,7 +827,7 @@ export const getTracesGroupedByName = async (
select
name as name,
count(*) as count
- from traces t FINAL
+ from traces t
WHERE t.project_id = {projectId: String}
AND t.name IS NOT NULL
AND t.name != ''
@@ -879,6 +879,112 @@ export const getTracesGroupedByName = async (
});
};
+export const getTracesGroupedBySessionId = async (
+ projectId: string,
+ filter: FilterState,
+ searchQuery?: string,
+ limit?: number,
+ offset?: number,
+ columns?: UiColumnMappings,
+) => {
+ const { tracesFilter } = getProjectIdDefaultFilter(projectId, {
+ tracesPrefix: "t",
+ });
+
+ tracesFilter.push(
+ ...createFilterFromFilterState(
+ filter,
+ columns ?? tracesTableUiColumnDefinitions,
+ ),
+ );
+
+ const tracesFilterRes = tracesFilter.apply();
+ const search = clickhouseSearchCondition(searchQuery, undefined, "t");
+
+ return measureAndReturn({
+ operationName: "getTracesGroupedBySessionId",
+ projectId,
+ minStartTime: filter?.find(
+ (f) =>
+ f.column === "timestamp" && (f.operator === ">=" || f.operator === ">"),
+ )?.value as Date | undefined,
+ input: {
+ params: {
+ limit,
+ offset,
+ projectId,
+ ...(tracesFilterRes ? tracesFilterRes.params : {}),
+ ...(searchQuery ? search.params : {}),
+ },
+ tags: {
+ feature: "tracing",
+ type: "trace",
+ kind: "analytic",
+ projectId,
+ operation_name: "getTracesGroupedBySessionId",
+ },
+ },
+ existingExecution: async (input) => {
+ // We mainly use queries like this to retrieve filter options.
+ // Therefore, we can skip final as some inaccuracy in count is acceptable.
+ const query = `
+ select
+ session_id as session_id,
+ count(*) as count
+ from traces t
+ WHERE t.project_id = {projectId: String}
+ AND t.session_id IS NOT NULL
+ AND t.session_id != ''
+ ${tracesFilterRes?.query ? `AND ${tracesFilterRes.query}` : ""}
+ ${search.query}
+ GROUP BY session_id
+ ORDER BY count desc
+ ${limit !== undefined && offset !== undefined ? `LIMIT {limit: Int32} OFFSET {offset: Int32}` : ""}
+ `;
+
+ return queryClickhouse<{
+ session_id: string;
+ count: string;
+ }>({
+ query,
+ params: input.params,
+ tags: { ...input.tags, experiment_amt: "original" },
+ });
+ },
+ newExecution: async (input) => {
+ // Extract the timestamp from filter for AMT table selection
+ const fromTimestamp = filter?.find(
+ (f) =>
+ f.column === "timestamp" &&
+ (f.operator === ">=" || f.operator === ">"),
+ )?.value as Date | undefined;
+ const traceAmt = getTimeframesTracesAMT(fromTimestamp);
+ const query = `
+ select
+ session_id as session_id,
+ count(*) as count
+ from ${traceAmt} t
+ WHERE t.project_id = {projectId: String}
+ AND t.session_id IS NOT NULL
+ AND t.session_id != ''
+ ${tracesFilterRes?.query ? `AND ${tracesFilterRes.query}` : ""}
+ ${search.query}
+ GROUP BY session_id
+ ${limit !== undefined && offset !== undefined ? `LIMIT {limit: Int32} OFFSET {offset: Int32}` : ""}
+ `;
+
+ return queryClickhouse<{
+ session_id: string;
+ count: string;
+ }>({
+ query,
+ params: input.params,
+ tags: { ...input.tags, experiment_amt: "new" },
+ });
+ },
+ });
+};
+
export const getTracesGroupedByUsers = async (
projectId: string,
filter: FilterState,
diff --git a/web/src/server/api/routers/traces.ts b/web/src/server/api/routers/traces.ts
index 72cd026d19..21d6c9be1d 100644
--- a/web/src/server/api/routers/traces.ts
+++ b/web/src/server/api/routers/traces.ts
@@ -39,6 +39,8 @@ import {
convertDateToClickhouseDateTime,
getAgentGraphData,
tracesTableUiColumnDefinitions,
+ getTracesGroupedByUsers,
+ getTracesGroupedBySessionId,
} from "@langfuse/shared/src/server";
import { TRPCError } from "@trpc/server";
import { createBatchActionJob } from "@/src/features/table/server/createBatchActionJob";
@@ -184,32 +186,54 @@ export const traceRouter = createTRPCRouter({
.query(async ({ input }) => {
const { timestampFilter } = input;
- const [numericScoreNames, categoricalScoreNames, traceNames, tags] =
- await Promise.all([
- getNumericScoresGroupedByName(
- input.projectId,
- timestampFilter ? [timestampFilter] : [],
- ),
- getCategoricalScoresGroupedByName(
- input.projectId,
- timestampFilter ? [timestampFilter] : [],
- ),
- getTracesGroupedByName(
- input.projectId,
- tracesTableUiColumnDefinitions,
- timestampFilter ? [timestampFilter] : [],
- ),
- getTracesGroupedByTags({
- projectId: input.projectId,
- filter: timestampFilter ? [timestampFilter] : [],
- }),
- ]);
+ const [
+ numericScoreNames,
+ categoricalScoreNames,
+ traceNames,
+ tags,
+ userIds,
+ sessionIds,
+ ] = await Promise.all([
+ getNumericScoresGroupedByName(
+ input.projectId,
+ timestampFilter ? [timestampFilter] : [],
+ ),
+ getCategoricalScoresGroupedByName(
+ input.projectId,
+ timestampFilter ? [timestampFilter] : [],
+ ),
+ getTracesGroupedByName(
+ input.projectId,
+ tracesTableUiColumnDefinitions,
+ timestampFilter ? [timestampFilter] : [],
+ ),
+ getTracesGroupedByTags({
+ projectId: input.projectId,
+ filter: timestampFilter ? [timestampFilter] : [],
+ }),
+ getTracesGroupedByUsers(
+ input.projectId,
+ timestampFilter ? [timestampFilter] : [],
+ ),
+ getTracesGroupedBySessionId(
+ input.projectId,
+ timestampFilter ? [timestampFilter] : [],
+ ),
+ ]);
return {
name: traceNames.map((n) => ({ value: n.name, count: n.count })),
scores_avg: numericScoreNames.map((s) => s.name),
score_categories: categoricalScoreNames,
tags: tags,
+ users: userIds.map((u) => ({
+ value: u.user,
+ count: u.count,
+ })),
+ sessions: sessionIds.map((s) => ({
+ value: s.session_id,
+ count: s.count,
+ })),
};
}),
byId: protectedGetTraceProcedure