Skip to content

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

fix(widgets): handle pivot table legacy sorting (#13306)

Nimareaeaef0c3a9c
diff --git a/web/src/features/widgets/components/DashboardWidget.tsx b/web/src/features/widgets/components/DashboardWidget.tsx
index ff85e9be4d..e5bf785b33 100644
--- a/web/src/features/widgets/components/DashboardWidget.tsx
+++ b/web/src/features/widgets/components/DashboardWidget.tsx
@@ -23,6 +23,7 @@ import { DownloadButton } from "@/src/features/widgets/chart-library/DownloadBut
import {
formatMetricName,
shouldUseWidgetSSE,
+ sanitizePivotTableDefaultSort,
} from "@/src/features/widgets/utils";
import { ChartLoadingState } from "@/src/features/widgets/chart-library/ChartLoadingState";
import {
@@ -101,7 +102,10 @@ export function DashboardWidget({
// Initialize sort state for pivot tables
const defaultSort =
widget.data?.chartConfig.type === "PIVOT_TABLE"
- ? widget.data?.chartConfig.defaultSort
+ ? sanitizePivotTableDefaultSort(widget.data.chartConfig.defaultSort, {
+ dimensions: widget.data.dimensions,
+ metrics: widget.data.metrics,
+ })
: undefined;
const [sortState, setSortState] = useState<OrderByState | null>(() => {
@@ -437,6 +441,7 @@ export function DashboardWidget({
metrics: widget.data.metrics.map(
(metric) => `${metric.agg}_${metric.measure}`,
),
+ defaultSort,
}),
}}
sortState={
diff --git a/web/src/features/widgets/components/WidgetForm.tsx b/web/src/features/widgets/components/WidgetForm.tsx
index 5519ab1127..e481eb8a21 100644
--- a/web/src/features/widgets/components/WidgetForm.tsx
+++ b/web/src/features/widgets/components/WidgetForm.tsx
@@ -71,6 +71,7 @@ import {
buildWidgetName,
buildWidgetDescription,
formatMetricName,
+ sanitizePivotTableDefaultSort,
} from "@/src/features/widgets/utils";
import {
MAX_PIVOT_TABLE_DIMENSIONS,
@@ -364,6 +365,22 @@ export function WidgetForm({
const [selectedChartType, setSelectedChartType] = useState<string>(
initialValues.chartType,
);
+ const initialDefaultSort =
+ initialValues.chartType === "PIVOT_TABLE"
+ ? sanitizePivotTableDefaultSort(initialValues.chartConfig?.defaultSort, {
+ dimensions: initialValues.dimensions ?? [],
+ metrics:
+ initialValues.metrics ??
+ (initialValues.measure && initialValues.aggregation
+ ? [
+ {
+ measure: initialValues.measure,
+ agg: initialValues.aggregation,
+ },
+ ]
+ : []),
+ })
+ : undefined;
const [rowLimit, setRowLimit] = useState<number>(
initialValues.chartConfig?.row_limit ?? 100,
);
@@ -373,10 +390,10 @@ export function WidgetForm({
// Default sort configuration for pivot tables
const [defaultSortColumn, setDefaultSortColumn] = useState<string>(
- initialValues.chartConfig?.defaultSort?.column ?? "none",
+ initialDefaultSort?.column ?? "none",
);
const [defaultSortOrder, setDefaultSortOrder] = useState<"ASC" | "DESC">(
- initialValues.chartConfig?.defaultSort?.order ?? "DESC",
+ initialDefaultSort?.order ?? "DESC",
);
// Filter state
@@ -474,6 +491,43 @@ export function WidgetForm({
[selectedChartType, defaultSortColumn, defaultSortOrder],
);
+ useEffect(() => {
+ if (selectedChartType !== "PIVOT_TABLE") return;
+
+ // Old widgets can carry persisted default sort keys for metrics or
+ // dimensions that are no longer part of the pivot query. Clear those stale
+ // sort columns so preview/save do not send invalid orderBy fields.
+ const sanitizedDefaultSort = sanitizePivotTableDefaultSort(
+ defaultSortColumn !== "none"
+ ? { column: defaultSortColumn, order: defaultSortOrder }
+ : undefined,
+ {
+ dimensions: pivotDimensions
+ .filter((field) => field && field !== "none")
+ .map((field) => ({ field })),
+ metrics: selectedMetrics
+ .filter((metric) => metric.measure && metric.measure !== "")
+ .map((metric) => ({
+ measure: metric.measure,
+ agg: metric.aggregation,
+ })),
+ },
+ );
+
+ if (defaultSortColumn !== "none" && !sanitizedDefaultSort) {
+ setDefaultSortColumn("none");
+ setDefaultSortOrder("DESC");
+ }
+ }, [
+ defaultSortColumn,
+ defaultSortOrder,
+ pivotDimensions,
+ selectedMetrics,
+ selectedChartType,
+ setDefaultSortColumn,
+ setDefaultSortOrder,
+ ]);
+
// Helper function to update pivot table dimensions
const updatePivotDimension = (index: number, value: string) => {
const newDimensions = [...pivotDimensions];
diff --git a/web/src/features/widgets/utils.ts b/web/src/features/widgets/utils.ts
index cb03599a28..c248d972b8 100644
--- a/web/src/features/widgets/utils.ts
+++ b/web/src/features/widgets/utils.ts
@@ -13,6 +13,43 @@ export type WidgetChartConfig = {
};
};
+type PivotSortMetric = {
+ measure: string;
+ agg: string;
+};
+
+type PivotSortDimension = {
+ field: string;
+};
+
+type PivotDefaultSort = NonNullable<WidgetChartConfig["defaultSort"]>;
+
+/**
+ * Old widgets can retain stale pivot defaultSort fields after metrics or
+ * dimensions change. Ignore those persisted sort keys instead of letting them
+ * reach QueryBuilder as invalid orderBy columns.
+ */
+export function sanitizePivotTableDefaultSort(
+ defaultSort: WidgetChartConfig["defaultSort"] | undefined,
+ params: {
+ dimensions: PivotSortDimension[];
+ metrics: PivotSortMetric[];
+ },
+): PivotDefaultSort | undefined {
+ if (!defaultSort) {
+ return undefined;
+ }
+
+ const validDimensionSort = params.dimensions.some(
+ (dimension) => dimension.field === defaultSort.column,
+ );
+ const validMetricSort = params.metrics.some(
+ (metric) => `${metric.agg}_${metric.measure}` === defaultSort.column,
+ );
+
+ return validDimensionSort || validMetricSort ? defaultSort : undefined;
+}
+
/**
* Formats a metric name for display, handling special cases like count_count -> Count
*/