🪢 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
import { type z } from "zod";
import { type FilterState, type singleFilter } from "@langfuse/shared";
import { type QueryType, type ViewVersion } from "@langfuse/shared/query";
import { mapLegacyUiTableFilterToView } from "@/src/features/dashboard/lib/dashboardUiTableToViewMapping";
// traces do not have a startTime or endTime column, so we need to map these to the timestamp column
export const createTracesTimeFilter = (
filters: FilterState,
columnName = "timestamp",
) => {
return filters.map((f) => {
if (f.column === "startTime" || f.column === "endTime") {
return {
...f,
column: columnName,
};
}
return f;
});
};
/** Filter to exclude events with empty trace_name (observations view only). */
const TRACE_NAME_NOT_NULL_FILTER: z.infer<typeof singleFilter> = {
type: "null",
column: "traceName",
operator: "is not null",
value: "",
};
/**
* Returns the view, filters, and metric for a "traces count" query.
* v1: queries traces view with count/count metric.
* v2: queries observations view with traceId/uniq metric (uniq(trace_id)).
*/
export function traceViewQuery(params: {
metricsVersion: ViewVersion | undefined;
globalFilterState: FilterState;
groupedByName?: boolean;
}): Pick<QueryType, "view" | "filters" | "metrics"> {
if (params.metricsVersion === "v2") {
const filters = [
...mapLegacyUiTableFilterToView("observations", params.globalFilterState),
...(params.groupedByName ? [TRACE_NAME_NOT_NULL_FILTER] : []),
];
return {
view: "observations",
filters,
metrics: [{ measure: "traceId", aggregation: "uniq" }],
};
}
return {
view: "traces",
filters: mapLegacyUiTableFilterToView("traces", params.globalFilterState),
metrics: [{ measure: "count", aggregation: "count" }],
};
}