Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Use consistent colors for the MCP errors-by-type pie chart (#81943)

Sébastien7e6c62a1058a
diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/ai-auditing/mcp-analytics/components/McpBreakoutChart.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/ai-auditing/mcp-analytics/components/McpBreakoutChart.tsx
index 390ff3adb79..0d69f4b6e88 100644
--- a/enterprise/frontend/src/metabase-enterprise/monitor/ai-auditing/mcp-analytics/components/McpBreakoutChart.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/monitor/ai-auditing/mcp-analytics/components/McpBreakoutChart.tsx
@@ -143,8 +143,17 @@ function McpBreakoutChartInner({
maxCategories,
otherLabel: t`Other`,
getColor: themeColor,
+ errorsOnly,
});
- }, [data, jsQuery, labelMapper, display, maxCategories, themeColor]);
+ }, [
+ data,
+ jsQuery,
+ labelMapper,
+ display,
+ maxCategories,
+ themeColor,
+ errorsOnly,
+ ]);
return (
<BreakoutChartCard
diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/ai-auditing/mcp-analytics/raw-series.ts b/enterprise/frontend/src/metabase-enterprise/monitor/ai-auditing/mcp-analytics/raw-series.ts
index 3bea84b2b6b..aa094b61886 100644
--- a/enterprise/frontend/src/metabase-enterprise/monitor/ai-auditing/mcp-analytics/raw-series.ts
+++ b/enterprise/frontend/src/metabase-enterprise/monitor/ai-auditing/mcp-analytics/raw-series.ts
@@ -1,3 +1,4 @@
+import { getColorsForValues } from "metabase/ui/colors/charts";
import type { ColorName } from "metabase/ui/colors/types";
import type { DatasetQuery, VisualizationDisplay } from "metabase-types/api";
@@ -12,8 +13,29 @@ type Opts = {
maxCategories?: number;
otherLabel: string;
getColor: GetColor;
+ /**
+ * Every row already represents an error, so slices must never land on the app's
+ * success-green accent — that would read as "this share succeeded" right below a
+ * chart using red/green for error/success.
+ */
+ errorsOnly?: boolean;
};
+/**
+ * Color a pie's dimension values, keeping the success-green accent out of the mix so no
+ * slice of an all-error breakdown can be misread as "succeeded".
+ */
+function getErrorPieColors(
+ rows: unknown[][],
+ dimensionIndex: number,
+ getColor: GetColor,
+): Record<string, string> {
+ const values = [...new Set(rows.map((row) => String(row[dimensionIndex])))];
+ return getColorsForValues(values, null, {
+ accent1: getColor("feedback-negative-strong"),
+ });
+}
+
/**
* Keep the top `max - 1` rows (already count-ordered) and fold the remaining long tail into a
* single "Other" row whose metric is the summed overflow. Returns rows unchanged when there's
@@ -56,7 +78,7 @@ export function toCountBreakoutRawSeries(
return null;
}
- const { display, maxCategories, otherLabel, getColor } = opts;
+ const { display, maxCategories, otherLabel, getColor, errorsOnly } = opts;
const cols = response.data.cols;
const dimensionIndex = cols.findIndex((c) => c.source === "breakout");
const metricIndex = cols.findIndex((c) => c.source === "aggregation");
@@ -65,11 +87,24 @@ export function toCountBreakoutRawSeries(
const countColumnName =
metricIndex >= 0 ? (cols[metricIndex].name ?? "count") : "count";
+ const rows = collapseToTopN(
+ response.data.rows,
+ dimensionIndex,
+ metricIndex,
+ maxCategories,
+ otherLabel,
+ cols.length,
+ );
+
const visualizationSettings =
display === "pie"
? {
"pie.dimension": dimensionName,
"pie.metric": countColumnName,
+ ...(errorsOnly &&
+ dimensionIndex >= 0 && {
+ "pie.colors": getErrorPieColors(rows, dimensionIndex, getColor),
+ }),
}
: {
"graph.x_axis.title_text": "",
@@ -91,14 +126,7 @@ export function toCountBreakoutRawSeries(
},
data: {
...response.data,
- rows: collapseToTopN(
- response.data.rows,
- dimensionIndex,
- metricIndex,
- maxCategories,
- otherLabel,
- cols.length,
- ),
+ rows,
},
},
];
diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/ai-auditing/mcp-analytics/raw-series.unit.spec.ts b/enterprise/frontend/src/metabase-enterprise/monitor/ai-auditing/mcp-analytics/raw-series.unit.spec.ts
new file mode 100644
index 00000000000..a49f49552f6
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/monitor/ai-auditing/mcp-analytics/raw-series.unit.spec.ts
@@ -0,0 +1,116 @@
+import { color } from "metabase/ui/colors/palette";
+import { createMockStructuredDatasetQuery } from "metabase-types/api/mocks";
+
+import { toCountBreakoutRawSeries } from "./raw-series";
+
+const jsQuery = createMockStructuredDatasetQuery();
+
+const opts = {
+ display: "row" as const,
+ otherLabel: "Other",
+ getColor: () => "#509EE3",
+};
+
+/** A single-breakout count response: one `breakout` dimension col + one `aggregation` metric col. */
+const response = (rows: unknown[][]) => ({
+ data: {
+ cols: [
+ { source: "breakout", name: "error_type" },
+ { source: "aggregation", name: "count" },
+ ],
+ rows,
+ },
+});
+
+const rowsOf = (series: ReturnType<typeof toCountBreakoutRawSeries>) =>
+ series?.[0].data.rows;
+
+const pieColorsOf = (series: ReturnType<typeof toCountBreakoutRawSeries>) => {
+ const settings = series?.[0].card.visualization_settings;
+ return settings && "pie.colors" in settings
+ ? settings["pie.colors"]
+ : undefined;
+};
+
+describe("toCountBreakoutRawSeries collapse behavior", () => {
+ it("folds the long tail into a single summed 'Other' row when rows exceed maxCategories", () => {
+ const series = toCountBreakoutRawSeries(
+ response([
+ ["A", 10],
+ ["B", 7],
+ ["C", 5],
+ ["D", 3],
+ ["E", 1],
+ ]),
+ jsQuery,
+ { ...opts, maxCategories: 3 },
+ );
+
+ // keep top (max - 1) = 2, fold the remaining 3 into "Other" summing 5 + 3 + 1 = 9
+ expect(rowsOf(series)).toEqual([
+ ["A", 10],
+ ["B", 7],
+ ["Other", 9],
+ ]);
+ });
+
+ it("leaves rows untouched when the count is within maxCategories", () => {
+ const rows = [
+ ["A", 10],
+ ["B", 7],
+ ];
+ const series = toCountBreakoutRawSeries(response(rows), jsQuery, {
+ ...opts,
+ maxCategories: 3,
+ });
+ expect(rowsOf(series)).toEqual(rows);
+ });
+
+ it("leaves rows untouched when maxCategories is unset", () => {
+ const rows = [
+ ["A", 10],
+ ["B", 7],
+ ["C", 5],
+ ];
+ const series = toCountBreakoutRawSeries(response(rows), jsQuery, opts);
+ expect(rowsOf(series)).toEqual(rows);
+ });
+
+ it("returns null without data or query", () => {
+ expect(toCountBreakoutRawSeries(undefined, jsQuery, opts)).toBeNull();
+ expect(
+ toCountBreakoutRawSeries(response([["A", 1]]), null, opts),
+ ).toBeNull();
+ });
+});
+
+describe("toCountBreakoutRawSeries error pie coloring", () => {
+ it("never assigns the success-green accent to a pie slice when errorsOnly is set", () => {
+ const series = toCountBreakoutRawSeries(
+ response([
+ ["timeout", 10],
+ ["validation_error", 7],
+ ["rate_limit", 5],
+ ]),
+ jsQuery,
+ { ...opts, display: "pie", errorsOnly: true },
+ );
+
+ expect(Object.values(pieColorsOf(series) ?? {})).not.toContain(
+ color("accent1"),
+ );
+ });
+
+ it("leaves pie coloring to the default palette when errorsOnly is not set", () => {
+ const series = toCountBreakoutRawSeries(
+ response([
+ ["tool_a", 10],
+ ["tool_b", 7],
+ ]),
+ jsQuery,
+ { ...opts, display: "pie" },
+ );
+
+ expect(pieColorsOf(series)).toBeUndefined();
+ });
+});