Skip to content

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

fix: count histogram UI switching during widget editing (#13128)

Valery Meleshkinde7591722158
diff --git a/web/src/features/widgets/components/WidgetForm.tsx b/web/src/features/widgets/components/WidgetForm.tsx
index edbcfbb6b3..fc37bab312 100644
--- a/web/src/features/widgets/components/WidgetForm.tsx
+++ b/web/src/features/widgets/components/WidgetForm.tsx
@@ -155,6 +155,11 @@ const chartTypes: ChartType[] = [
* Pure function that resolves the correct aggregation and chart type given the
* current selections and valid aggregation list. Returns null when no change is
* needed.
+ *
+ * All constraints are resolved in a single pass so the output is a fixed point
+ * (running the function again on its own output always returns null). This
+ * prevents infinite React state-update loops when constraints conflict — e.g.
+ * HISTOGRAM requires "histogram" aggregation but "count" measure forces "count".
*/
export function resolveAggregationAndChartType(params: {
chartType: string;
@@ -168,34 +173,50 @@ export function resolveAggregationAndChartType(params: {
const { chartType, measure, currentAgg, validAggs } = params;
const supportsHistogram = validAggs.includes("histogram");
- // HISTOGRAM chart with a measure that doesn't support it — bail out of both
- if (chartType === "HISTOGRAM" && !supportsHistogram) {
- return { chartType: "NUMBER", aggregation: validAggs[0] ?? "count" };
- }
+ let targetChart = chartType;
+ let targetAgg = currentAgg as z.infer<typeof metricAggregations>;
- // HISTOGRAM chart forces histogram aggregation
- if (chartType === "HISTOGRAM" && currentAgg !== "histogram") {
- return { aggregation: "histogram" };
+ // HISTOGRAM chart needs a histogram-compatible measure
+ if (targetChart === "HISTOGRAM") {
+ if (!supportsHistogram) {
+ targetChart = "NUMBER";
+ } else {
+ targetAgg = "histogram";
+ }
}
- // Switched away from HISTOGRAM chart but aggregation still histogram
- if (chartType !== "HISTOGRAM" && currentAgg === "histogram") {
- return {
- aggregation: measure === "count" ? "count" : (validAggs[0] ?? "sum"),
- };
+ // Non-HISTOGRAM chart can't keep histogram aggregation
+ if (targetChart !== "HISTOGRAM" && targetAgg === "histogram") {
+ targetAgg =
+ measure === "count"
+ ? "count"
+ : ((validAggs[0] ?? "sum") as z.infer<typeof metricAggregations>);
}
- // "count" measure always uses "count" aggregation (outside HISTOGRAM charts)
- if (measure === "count" && currentAgg !== "count") {
- return { aggregation: "count" };
+ // "count" measure only supports "count" aggregation. If this conflicts with
+ // the chart type (e.g. HISTOGRAM requires "histogram"), bail the chart type
+ // rather than creating an unresolvable conflict.
+ if (measure === "count" && targetAgg !== "count") {
+ if (targetChart === "HISTOGRAM") {
+ targetChart = "NUMBER";
+ }
+ targetAgg = "count";
}
- // Current aggregation is not valid for the measure type
- if (!validAggs.includes(currentAgg as z.infer<typeof metricAggregations>)) {
- return { aggregation: validAggs[0] ?? "count" };
+ // Current aggregation not valid for the measure type
+ if (!validAggs.includes(targetAgg)) {
+ targetAgg = (validAggs[0] ?? "count") as z.infer<typeof metricAggregations>;
}
- return null;
+ // Only return if something changed
+ const result: {
+ aggregation?: z.infer<typeof metricAggregations>;
+ chartType?: string;
+ } = {};
+ if (targetChart !== chartType) result.chartType = targetChart;
+ if (targetAgg !== currentAgg) result.aggregation = targetAgg;
+
+ return Object.keys(result).length > 0 ? result : null;
}
/**
@@ -718,7 +739,8 @@ export function WidgetForm({
}, [viewVersion, selectedView, selectedMeasure]);
const measureSupportsHistogram =
- validAggregationsForMeasure.includes("histogram");
+ validAggregationsForMeasure.includes("histogram") &&
+ selectedMeasure !== "count";
// Sync aggregation and chart type when selections change
useEffect(() => {
diff --git a/web/src/features/widgets/components/resolveAggregationAndChartType.clienttest.ts b/web/src/features/widgets/components/resolveAggregationAndChartType.clienttest.ts
new file mode 100644
index 0000000000..89a605fa76
--- /dev/null
+++ b/web/src/features/widgets/components/resolveAggregationAndChartType.clienttest.ts
@@ -0,0 +1,181 @@
+import { resolveAggregationAndChartType } from "./WidgetForm";
+
+const allAggs = [
+ "sum",
+ "avg",
+ "count",
+ "max",
+ "min",
+ "p50",
+ "p75",
+ "p90",
+ "p95",
+ "p99",
+ "histogram",
+ "uniq",
+] as const;
+
+const restrictedAggs = ["count", "uniq"] as const;
+
+describe("resolveAggregationAndChartType", () => {
+ // --- Bug fix: count + histogram must not infinite-loop (LFE-9231) ---
+
+ it("bails HISTOGRAM chart to NUMBER when measure is count", () => {
+ const result = resolveAggregationAndChartType({
+ chartType: "HISTOGRAM",
+ measure: "count",
+ currentAgg: "count",
+ validAggs: [...allAggs],
+ });
+ // currentAgg is already "count" so only chartType changes
+ expect(result).toEqual({ chartType: "NUMBER" });
+ });
+
+ it("bails HISTOGRAM chart to NUMBER when switching measure to count while histogram is active", () => {
+ const result = resolveAggregationAndChartType({
+ chartType: "HISTOGRAM",
+ measure: "count",
+ currentAgg: "histogram",
+ validAggs: [...allAggs],
+ });
+ expect(result).toEqual({ chartType: "NUMBER", aggregation: "count" });
+ });
+
+ // --- Existing behavior that must be preserved ---
+
+ it("forces histogram aggregation for HISTOGRAM chart with numeric measure", () => {
+ const result = resolveAggregationAndChartType({
+ chartType: "HISTOGRAM",
+ measure: "latency",
+ currentAgg: "sum",
+ validAggs: [...allAggs],
+ });
+ expect(result).toEqual({ aggregation: "histogram" });
+ });
+
+ it("returns null when HISTOGRAM chart already has histogram aggregation on numeric measure", () => {
+ const result = resolveAggregationAndChartType({
+ chartType: "HISTOGRAM",
+ measure: "latency",
+ currentAgg: "histogram",
+ validAggs: [...allAggs],
+ });
+ expect(result).toBeNull();
+ });
+
+ it("bails HISTOGRAM to NUMBER when measure type does not support histogram", () => {
+ const result = resolveAggregationAndChartType({
+ chartType: "HISTOGRAM",
+ measure: "someStringMeasure",
+ currentAgg: "count",
+ validAggs: [...restrictedAggs],
+ });
+ // currentAgg is already "count" which matches validAggs[0], so only chartType changes
+ expect(result).toEqual({ chartType: "NUMBER" });
+ });
+
+ it("reverts histogram aggregation when switching away from HISTOGRAM chart", () => {
+ const result = resolveAggregationAndChartType({
+ chartType: "LINE_TIME_SERIES",
+ measure: "latency",
+ currentAgg: "histogram",
+ validAggs: [...allAggs],
+ });
+ expect(result).toEqual({ aggregation: "sum" });
+ });
+
+ it("reverts histogram aggregation to count when switching away from HISTOGRAM on count measure", () => {
+ const result = resolveAggregationAndChartType({
+ chartType: "LINE_TIME_SERIES",
+ measure: "count",
+ currentAgg: "histogram",
+ validAggs: [...allAggs],
+ });
+ expect(result).toEqual({ aggregation: "count" });
+ });
+
+ it("forces count aggregation for count measure", () => {
+ const result = resolveAggregationAndChartType({
+ chartType: "LINE_TIME_SERIES",
+ measure: "count",
+ currentAgg: "sum",
+ validAggs: [...allAggs],
+ });
+ expect(result).toEqual({ aggregation: "count" });
+ });
+
+ it("returns null when no changes needed", () => {
+ const result = resolveAggregationAndChartType({
+ chartType: "LINE_TIME_SERIES",
+ measure: "latency",
+ currentAgg: "avg",
+ validAggs: [...allAggs],
+ });
+ expect(result).toBeNull();
+ });
+
+ it("falls back to first valid agg when current is not valid", () => {
+ const result = resolveAggregationAndChartType({
+ chartType: "LINE_TIME_SERIES",
+ measure: "someStringMeasure",
+ currentAgg: "sum",
+ validAggs: [...restrictedAggs],
+ });
+ expect(result).toEqual({ aggregation: "count" });
+ });
+
+ // --- Exhaustive idempotency: output is always a fixed point ---
+
+ const chartTypes = [
+ "NUMBER",
+ "LINE_TIME_SERIES",
+ "BAR_TIME_SERIES",
+ "HORIZONTAL_BAR",
+ "VERTICAL_BAR",
+ "HISTOGRAM",
+ "PIE",
+ "PIVOT_TABLE",
+ ];
+ const measures = ["count", "latency", "stringMeasure"];
+ const aggsPerMeasure: Record<string, readonly string[]> = {
+ count: allAggs,
+ latency: allAggs,
+ stringMeasure: restrictedAggs,
+ };
+
+ const cases = chartTypes.flatMap((chartType) =>
+ measures.flatMap((measure) =>
+ [...aggsPerMeasure[measure]].map((agg) => ({
+ chartType,
+ measure,
+ agg,
+ validAggs: [...aggsPerMeasure[measure]] as string[],
+ })),
+ ),
+ );
+
+ it.each(cases)(
+ "idempotent: $chartType + $measure + $agg",
+ ({ chartType, measure, agg, validAggs }) => {
+ const first = resolveAggregationAndChartType({
+ chartType,
+ measure,
+ currentAgg: agg,
+ validAggs: validAggs as any,
+ });
+
+ // Apply the resolution (or keep original if null) and re-run
+ const resolvedChart = first?.chartType ?? chartType;
+ const resolvedAgg = first?.aggregation ?? agg;
+
+ const second = resolveAggregationAndChartType({
+ chartType: resolvedChart,
+ measure,
+ currentAgg: resolvedAgg,
+ validAggs: validAggs as any,
+ });
+
+ expect(second).toBeNull();
+ },
+ );
+});