Skip to content

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

chore(events-table): move root observation swith to sidebar (#12094)

Nimar06c211338af6
diff --git a/packages/shared/src/server/repositories/events.ts b/packages/shared/src/server/repositories/events.ts
index d1d2a93195..e4046b545e 100644
--- a/packages/shared/src/server/repositories/events.ts
+++ b/packages/shared/src/server/repositories/events.ts
@@ -2033,6 +2033,44 @@ export const getEventsGroupedByExperimentName = async (
return res;
};
+/**
+ * Get grouped hasParentObservation boolean from events table
+ * Used for filter options (counts for "Is Root Observation" facet)
+ */
+export const getEventsGroupedByHasParentObservation = async (
+ projectId: string,
+ filter: FilterState,
+) => {
+ const eventsFilter = new FilterList(
+ createFilterFromFilterState(filter, eventsTableUiColumnDefinitions),
+ );
+
+ const appliedEventsFilter = eventsFilter.apply();
+
+ const queryBuilder = new EventsAggQueryBuilder({
+ projectId,
+ groupByColumn: "(e.parent_span_id != '')",
+ selectExpression:
+ "(e.parent_span_id != '') as hasParentObservation, count() as count",
+ })
+ .where(appliedEventsFilter)
+ .orderBy("ORDER BY hasParentObservation ASC")
+ .limit(2, 0);
+
+ const { query, params } = queryBuilder.buildWithParams();
+
+ return queryClickhouse<{ hasParentObservation: boolean; count: number }>({
+ query,
+ params,
+ tags: {
+ feature: "tracing",
+ type: "events",
+ kind: "analytic",
+ projectId,
+ },
+ });
+};
+
/**
* Delete events by trace IDs
* Used when traces are deleted to cascade the deletion to the events table
diff --git a/web/src/features/events/components/EventsTable.tsx b/web/src/features/events/components/EventsTable.tsx
index 1462a0f27e..69b138a963 100644
--- a/web/src/features/events/components/EventsTable.tsx
+++ b/web/src/features/events/components/EventsTable.tsx
@@ -64,12 +64,13 @@ import useColumnVisibility from "@/src/features/column-visibility/hooks/useColum
import { MemoizedIOTableCell } from "@/src/components/ui/IOTableCell";
import { useEventsTableData } from "@/src/features/events/hooks/useEventsTableData";
import { useEventsFilterOptions } from "@/src/features/events/hooks/useEventsFilterOptions";
-import {
- useEventsViewMode,
- type EventsViewMode,
-} from "@/src/features/events/hooks/useEventsViewMode";
-import { EventsViewModeToggle } from "@/src/features/events/components/EventsViewModeToggle";
-import { useObservationCountCheck } from "@/src/features/events/hooks/useObservationCountCheck";
+// Disabled for now because perhaps confusing
+// import {
+// useEventsViewMode,
+// type EventsViewMode,
+// } from "@/src/features/events/hooks/useEventsViewMode";
+// import { EventsViewModeToggle } from "@/src/features/events/components/EventsViewModeToggle";
+// import { useObservationCountCheck } from "@/src/features/events/hooks/useObservationCountCheck";
import { JsonSkeleton } from "@/src/components/ui/CodeJsonViewer";
import {
type RefreshInterval,
@@ -213,34 +214,44 @@ export default function ObservationsEventsTable({
const { timeRange, setTimeRange } = useTableDateRange(projectId);
+ // Disabled for now because perhaps confusing — replaced by "Is Root Observation"
+ // boolean facet in the sidebar (see filter-config.ts).
+ //
+ // RE-ENABLING THE VIEW MODE TOGGLE:
+ // To re-enable, uncomment the code below AND the viewModeFilter, viewModeToggle,
+ // auto-switch logic, and imports further down. However, note that the sidebar now
+ // has an "Is Root Observation" boolean facet that also controls `hasParentObservation`.
+ // Having BOTH active would create duplicate/conflicting filters. Pick one:
+ // - Sidebar facet only (current): remove this commented code entirely
+ // - Toolbar toggle only: uncomment this code, remove the boolean facet from
+ // web/src/features/events/config/filter-config.ts, and re-add
+ // `hasParentObservation` param to the useEventsFilterOptions call below
+ // - Both: would need deduplication logic to prevent conflicting filters
+ //
// View mode toggle (Trace vs Observation)
- const { viewMode, setViewMode: setViewModeRaw } =
- useEventsViewMode(projectId);
-
- // if the user explicitly choose to filter for trace, we don't want to auto switch them even if there are no "traces" aka root observations
- const [userExplicitChoice, setUserExplicitChoice] =
- useSessionStorage<EventsViewMode | null>(
- `eventsViewModeUserChoice-${projectId}`,
- null,
- );
-
- // Track date range(s) for which we've auto-switched (to avoid repeated switches)
- const [autoSwitchedForRange, setAutoSwitchedForRange] = useSessionStorage<
- string | null
- >(`eventsAutoSwitchRange-${projectId}`, null);
-
- // For filter options: trace mode filters to root items, observation mode shows all
- const hasParentObservation = viewMode === "observation" ? undefined : false;
-
- // Wrap setViewMode to reset pagination and track explicit user choice
- const setViewMode = useCallback(
- (mode: EventsViewMode) => {
- setUserExplicitChoice(mode); // Track explicit choice
- setViewModeRaw(mode);
- setPaginationState({ page: 1, limit: 50 });
- },
- [setUserExplicitChoice, setViewModeRaw, setPaginationState],
- );
+ // const { viewMode, setViewMode: setViewModeRaw } =
+ // useEventsViewMode(projectId);
+ //
+ // const [userExplicitChoice, setUserExplicitChoice] =
+ // useSessionStorage<EventsViewMode | null>(
+ // `eventsViewModeUserChoice-${projectId}`,
+ // null,
+ // );
+ //
+ // const [autoSwitchedForRange, setAutoSwitchedForRange] = useSessionStorage<
+ // string | null
+ // >(`eventsAutoSwitchRange-${projectId}`, null);
+ //
+ // const hasParentObservation = viewMode === "observation" ? undefined : false;
+ //
+ // const setViewMode = useCallback(
+ // (mode: EventsViewMode) => {
+ // setUserExplicitChoice(mode);
+ // setViewModeRaw(mode);
+ // setPaginationState({ page: 1, limit: 50 });
+ // },
+ // [setUserExplicitChoice, setViewModeRaw, setPaginationState],
+ // );
// for auto data refresh
const utils = api.useUtils();
@@ -317,11 +328,10 @@ export default function ObservationsEventsTable({
const oldFilterState = inputFilterState.concat(dateRangeFilter);
- // Fetch filter options (scoped to current view mode)
+ // Fetch filter options
const { filterOptions, isFilterOptionsPending } = useEventsFilterOptions({
projectId,
oldFilterState,
- hasParentObservation,
});
const queryFilter = useSidebarFilterState(
@@ -341,18 +351,18 @@ export default function ObservationsEventsTable({
[],
);
- // Create view mode filter (not shown in sidebar)
- const viewModeFilter: FilterState =
- viewMode === "trace"
- ? [
- {
- column: "hasParentObservation",
- type: "boolean",
- operator: "=",
- value: false, // Only root-level items (no parent)
- },
- ]
- : [];
+ // Disabled for now because perhaps confusing
+ // const viewModeFilter: FilterState =
+ // viewMode === "trace"
+ // ? [
+ // {
+ // column: "hasParentObservation",
+ // type: "boolean",
+ // operator: "=",
+ // value: false,
+ // },
+ // ]
+ // : [];
// Create user ID filter if userId is provided
const userIdFilter: FilterState = userId
@@ -379,57 +389,11 @@ export default function ObservationsEventsTable({
const combinedFilterState = queryFilter.filterState
.concat(dateRangeFilter)
- .concat(viewModeFilter)
.concat(userIdFilter)
.concat(sessionIdFilter);
// Use external filter state if provided, otherwise use combined filter state
const filterState = externalFilterState || combinedFilterState;
- // Filter state WITHOUT viewModeFilter - for auto-switch observation count check
- const filterStateWithoutViewMode = useMemo(() => {
- const filters: FilterState = [...queryFilter.filterState];
-
- if (dateRange?.from) {
- filters.push({
- column: "startTime",
- type: "datetime",
- operator: ">=",
- value: dateRange.from,
- });
- }
- if (dateRange?.to) {
- filters.push({
- column: "startTime",
- type: "datetime",
- operator: "<=",
- value: dateRange.to,
- });
- }
- if (userId) {
- filters.push({
- column: "User ID",
- type: "string",
- operator: "=",
- value: userId,
- });
- }
- if (sessionId) {
- filters.push({
- column: "Session ID",
- type: "string",
- operator: "=",
- value: sessionId,
- });
- }
-
- return filters;
- }, [
- queryFilter.filterState,
- dateRange?.from,
- dateRange?.to,
- userId,
- sessionId,
- ]);
// Use the custom hook for observations data fetching
const {
@@ -452,65 +416,9 @@ export default function ObservationsEventsTable({
setSelectedRows,
});
+ // Disabled for now because perhaps confusing
// === Auto-switch to observation mode when trace view is empty ===
-
- const dateRangeKey = useMemo(() => {
- if (!dateRange?.from) return null;
- return `${dateRange.from.getTime()}-${dateRange.to?.getTime() ?? "now"}`;
- }, [dateRange]);
-
- // Reset auto-switch tracking when date range changes
- useEffect(() => {
- if (
- dateRangeKey &&
- autoSwitchedForRange &&
- dateRangeKey !== autoSwitchedForRange
- ) {
- setAutoSwitchedForRange(null);
- }
- }, [dateRangeKey, autoSwitchedForRange, setAutoSwitchedForRange]);
-
- // Determine if we should check observation count for auto-switch
- const shouldCheckObservationCount =
- viewMode === "trace" &&
- totalCount === 0 &&
- observations.status === "success" &&
- userExplicitChoice !== "trace" &&
- dateRangeKey !== null &&
- autoSwitchedForRange !== dateRangeKey;
-
- const {
- observationCount,
- isSuccess: obsCountSuccess,
- isPending: obsCountPending,
- } = useObservationCountCheck({
- projectId,
- filterStateWithoutViewMode,
- enabled: shouldCheckObservationCount,
- });
-
- // Auto-switch to observation mode when conditions are met
- useEffect(() => {
- if (
- shouldCheckObservationCount &&
- obsCountSuccess &&
- observationCount !== null &&
- observationCount > 0 &&
- dateRangeKey
- ) {
- setViewModeRaw("observation");
- setAutoSwitchedForRange(dateRangeKey);
- }
- }, [
- shouldCheckObservationCount,
- obsCountSuccess,
- observationCount,
- dateRangeKey,
- setViewModeRaw,
- setAutoSwitchedForRange,
- ]);
-
- const isAutoSwitchPending = shouldCheckObservationCount && obsCountPending;
+ // (commented out along with view mode toggle)
useEffect(() => {
if (observations.status === "success") {
@@ -1269,12 +1177,14 @@ export default function ObservationsEventsTable({
setRowHeight={setRowHeight}
timeRange={timeRange}
setTimeRange={setTimeRange}
- viewModeToggle={
- <EventsViewModeToggle
- viewMode={viewMode}
- onViewModeChange={setViewMode}
- />
- }
+ // Disabled, for now moved to filter sidebar
+ // TODO: remove this toggle once v4 looks good as is
+ // viewModeToggle={
+ // <EventsViewModeToggle
+ // viewMode={viewMode}
+ // onViewModeChange={setViewMode}
+ // />
+ // }
refreshConfig={{
onRefresh: handleRefresh,
isRefreshing: observations.status === "loading",
@@ -1333,9 +1243,7 @@ export default function ObservationsEventsTable({
columns={columns}
peekView={peekConfig}
data={
- observations.status === "loading" ||
- isViewLoading ||
- isAutoSwitchPending
+ observations.status === "loading" || isViewLoading
? { isLoading: true, isError: false }
: observations.status === "error"
? {
diff --git a/web/src/features/events/config/filter-config.ts b/web/src/features/events/config/filter-config.ts
index 1fe720f6f5..e0f023a40d 100644
--- a/web/src/features/events/config/filter-config.ts
+++ b/web/src/features/events/config/filter-config.ts
@@ -24,7 +24,7 @@ export const observationEventsFilterConfig: FilterConfig = {
columnDefinitions: eventsTableCols,
- defaultExpanded: ["environment", "name"],
+ defaultExpanded: ["environment", "name", "hasParentObservation"],
facets: [
{
@@ -37,6 +37,12 @@ export const observationEventsFilterConfig: FilterConfig = {
column: "type",
label: getEventsColumnName("type"),
},
+ {
+ type: "boolean" as const,
+ column: "hasParentObservation",
+ label: "Is Root Observation",
+ invertValue: true, // "True" = hasParentObservation=false (is root)
+ },
{
type: "categorical" as const,
column: "traceName",
diff --git a/web/src/features/events/hooks/useEventsFilterOptions.ts b/web/src/features/events/hooks/useEventsFilterOptions.ts
index 7661183228..b2730f1b9f 100644
--- a/web/src/features/events/hooks/useEventsFilterOptions.ts
+++ b/web/src/features/events/hooks/useEventsFilterOptions.ts
@@ -75,6 +75,8 @@ export function useEventsFilterOptions({
experimentDatasetId: filterOptions.data?.experimentDatasetId ?? undefined,
experimentId: filterOptions.data?.experimentId ?? undefined,
experimentName: filterOptions.data?.experimentName ?? undefined,
+ hasParentObservation:
+ filterOptions.data?.hasParentObservation ?? undefined,
latency: [],
timeToFirstToken: [],
tokensPerSecond: [],
diff --git a/web/src/features/events/server/eventsService.ts b/web/src/features/events/server/eventsService.ts
index 9858f18237..36c89b5b78 100644
--- a/web/src/features/events/server/eventsService.ts
+++ b/web/src/features/events/server/eventsService.ts
@@ -17,6 +17,7 @@ import {
getEventsGroupedByExperimentDatasetId,
getEventsGroupedByExperimentId,
getEventsGroupedByExperimentName,
+ getEventsGroupedByHasParentObservation,
getNumericScoresGroupedByName,
getTracesGroupedByTags,
getObservationsBatchIOFromEventsTable,
@@ -151,6 +152,7 @@ export async function getEventFilterOptions(
experimentDatasetIds,
experimentIds,
experimentNames,
+ hasParentObservationResults,
] = await Promise.all([
getNumericScoresGroupedByName(projectId, traceTimestampFilters),
getCategoricalScoresGroupedByName(projectId, traceTimestampFilters),
@@ -169,6 +171,7 @@ export async function getEventFilterOptions(
getEventsGroupedByExperimentDatasetId(projectId, eventsFilter),
getEventsGroupedByExperimentId(projectId, eventsFilter),
getEventsGroupedByExperimentName(projectId, eventsFilter),
+ getEventsGroupedByHasParentObservation(projectId, eventsFilter),
]);
return {
@@ -257,6 +260,11 @@ export async function getEventFilterOptions(
value: i.experimentName as string,
count: i.count,
})),
+ hasParentObservation: hasParentObservationResults.map((i) => ({
+ // ClickHouse returns UInt8 (0/1) for computed boolean; normalize to "true"/"false"
+ value: i.hasParentObservation ? "true" : "false",
+ count: i.count,
+ })),
};
}
diff --git a/web/src/features/filters/hooks/useSidebarFilterState.tsx b/web/src/features/filters/hooks/useSidebarFilterState.tsx
index e7d3e32065..9164634de8 100644
--- a/web/src/features/filters/hooks/useSidebarFilterState.tsx
+++ b/web/src/features/filters/hooks/useSidebarFilterState.tsx
@@ -428,6 +428,7 @@ export function useSidebarFilterState(
if (facet.type === "boolean") {
const trueLabel = facet.trueLabel ?? "True";
const falseLabel = facet.falseLabel ?? "False";
+ const invert = facet.invertValue ?? false;
if (values.length === 0 || values.length === 2) return other;
if (values.includes(trueLabel)) {
@@ -437,7 +438,7 @@ export function useSidebarFilterState(
column,
type: "boolean" as const,
operator: "=" as const,
- value: true,
+ value: invert ? false : true,
},
];
}
@@ -448,7 +449,7 @@ export function useSidebarFilterState(
column,
type: "boolean" as const,
operator: "=" as const,
- value: false,
+ value: invert ? true : false,
},
];
}
@@ -1080,15 +1081,44 @@ export function useSidebarFilterState(
if (facet.type === "boolean") {
const trueLabel = facet.trueLabel ?? "True";
const falseLabel = facet.falseLabel ?? "False";
+ const invert = facet.invertValue ?? false;
const availableOptions = [trueLabel, falseLabel];
const filterEntry = filterByColumn.get(facet.column);
+
let selectedOptions = availableOptions;
if (filterEntry) {
const boolValue = filterEntry.value as boolean;
- selectedOptions = boolValue === true ? [trueLabel] : [falseLabel];
+ if (invert) {
+ // Inverted: filter value=true means falseLabel selected, value=false means trueLabel selected
+ selectedOptions = boolValue === true ? [falseLabel] : [trueLabel];
+ } else {
+ selectedOptions = boolValue === true ? [trueLabel] : [falseLabel];
+ }
}
const isActive = selectedOptions.length === 1;
+ // Build counts from options
+ const rawOptions = options[facet.column];
+ let counts: Map<string, number> = EMPTY_MAP;
+ if (Array.isArray(rawOptions) && rawOptions.length > 0) {
+ const { counts: processedCounts } = processOptions(rawOptions);
+ if (processedCounts.size > 0) {
+ counts = new Map<string, number>();
+ if (invert) {
+ // Inverted: trueLabel count comes from "false", falseLabel count comes from "true"
+ const falseCount = processedCounts.get("false") ?? 0;
+ const trueCount = processedCounts.get("true") ?? 0;
+ if (falseCount > 0) counts.set(trueLabel, falseCount);
+ if (trueCount > 0) counts.set(falseLabel, trueCount);
+ } else {
+ const trueCount = processedCounts.get("true") ?? 0;
+ const falseCount = processedCounts.get("false") ?? 0;
+ if (trueCount > 0) counts.set(trueLabel, trueCount);
+ if (falseCount > 0) counts.set(falseLabel, falseCount);
+ }
+ }
+ }
+
return {
type: "categorical",
column: facet.column,
@@ -1096,8 +1126,8 @@ export function useSidebarFilterState(
value: selectedOptions,
options: availableOptions,
- counts: EMPTY_MAP,
- loading: false,
+ counts,
+ loading: shouldShowLoading(facet.column),
expanded: expandedSet.has(facet.column),
isActive,
onChange: (values: string[]) => {
diff --git a/web/src/features/filters/lib/filter-config.ts b/web/src/features/filters/lib/filter-config.ts
index 15dca2397a..dc3ebae4cb 100644
--- a/web/src/features/filters/lib/filter-config.ts
+++ b/web/src/features/filters/lib/filter-config.ts
@@ -12,6 +12,7 @@ interface BooleanFacet {
label: string;
trueLabel?: string;
falseLabel?: string;
+ invertValue?: boolean; // When true, "True" label maps to filter value=false, used for parent_observation_id filter for is Root?
}
interface NumericFacet {