Skip to content

Commit 0f01802

Browse files
Mrun25shuchu
andauthored
fix(ui): stop treating the monitoring baseline as the latest window (feast-dev#7056)
The feature-monitoring UI picked the baseline metrics row as the newest window because the baseline is saved with metric_date = today. That made the monitoring tab land on the baseline instead of real data, listed 'baseline' as a granularity in the detail view, mixed window lengths in the time series, and compared mismatched histogram bins (raw counts on different bin edges, with a TypeError when the baseline was categorical and the window numeric). - FeatureMonitoringTab: select the latest non-baseline window row via selectLatestWindowMetric - FeatureMetricsDetail: filter baseline rows out of the granularity list and render each granularity's time series separately - HistogramChart: rebin numeric baselines onto the window's bin edges, compare densities instead of raw counts, and guard non-numeric baselines - add histogramUtils with unit tests (9 passing) Fixes feast-dev#7054 Signed-off-by: Mrun25 <[email protected]> Co-authored-by: Shuchu Han <[email protected]>
1 parent b6bdda4 commit 0f01802

5 files changed

Lines changed: 283 additions & 26 deletions

File tree

‎ui/src/pages/features/FeatureMonitoringTab.tsx‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ import {
2020
NumericHistogramChart,
2121
CategoricalHistogramChart,
2222
} from "../monitoring/components/HistogramChart";
23+
import {
24+
isNumericHistogram,
25+
selectLatestWindowMetric,
26+
} from "../monitoring/components/histogramUtils";
2327
import StatsPanel from "../monitoring/components/StatsPanel";
2428

2529
const FeatureMonitoringTab = () => {
@@ -45,12 +49,9 @@ const FeatureMonitoringTab = () => {
4549
return <EuiSkeletonText lines={6} />;
4650
}
4751

48-
const latestMetric = (() => {
49-
if (!metrics || metrics.length === 0) return null;
50-
const withData = metrics.filter((m) => m.row_count > 0);
51-
const candidates = withData.length > 0 ? withData : metrics;
52-
return candidates.reduce((a, b) => (a.metric_date > b.metric_date ? a : b));
53-
})();
52+
// The API returns the baseline as a row dated by computation day, so the
53+
// newest window must be picked among real window rows only (#7054).
54+
const latestMetric = selectLatestWindowMetric(metrics);
5455

5556
const baselineMetric =
5657
baselineMetrics && baselineMetrics.length > 0 ? baselineMetrics[0] : null;
@@ -84,7 +85,11 @@ const FeatureMonitoringTab = () => {
8485
{isNumeric && latestMetric.histogram && (
8586
<NumericHistogramChart
8687
histogram={latestMetric.histogram as NumericHistogram}
87-
baseline={baselineMetric?.histogram as NumericHistogram | null}
88+
baseline={
89+
isNumericHistogram(baselineMetric?.histogram)
90+
? baselineMetric?.histogram
91+
: null
92+
}
8893
title="Distribution"
8994
/>
9095
)}

‎ui/src/pages/monitoring/FeatureMetricsDetail.tsx‎

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
CategoricalHistogramChart,
2727
} from "./components/HistogramChart";
2828
import StatsPanel from "./components/StatsPanel";
29+
import { isBaselineMetricRow } from "./components/histogramUtils";
2930
import TimeSeriesAnalysis from "./components/TimeSeriesAnalysis";
3031
import { useDocumentTitle } from "../../hooks/useDocumentTitle";
3132

@@ -70,7 +71,10 @@ const FeatureMetricsDetail = () => {
7071
const granularities = new Set<string>();
7172
if (metrics) {
7273
for (const m of metrics) {
73-
if (m.row_count > 0) granularities.add(m.granularity);
74+
// The baseline is not a window length; it has its own View option.
75+
if (m.row_count > 0 && !isBaselineMetricRow(m)) {
76+
granularities.add(m.granularity);
77+
}
7478
}
7579
}
7680
return Array.from(granularities).sort();
@@ -100,12 +104,14 @@ const FeatureMetricsDetail = () => {
100104
return baselineMetric;
101105
}
102106
if (!metrics || metrics.length === 0) return null;
103-
const matching = metrics.filter(
107+
const windows = metrics.filter((m) => !isBaselineMetricRow(m));
108+
const matching = windows.filter(
104109
(m) => m.granularity === effectiveGranularity && m.row_count > 0,
105110
);
106111
if (matching.length === 0) {
107-
const withData = metrics.filter((m) => m.row_count > 0);
108-
const candidates = withData.length > 0 ? withData : metrics;
112+
const withData = windows.filter((m) => m.row_count > 0);
113+
const candidates = withData.length > 0 ? withData : windows;
114+
if (candidates.length === 0) return null;
109115
return candidates.reduce((a, b) =>
110116
a.metric_date > b.metric_date ? a : b,
111117
);
@@ -242,18 +248,41 @@ const FeatureMetricsDetail = () => {
242248
</EuiFlexItem>
243249
</EuiFlexGroup>
244250

245-
{metrics && metrics.length > 1 && (
246-
<>
247-
<EuiSpacer size="xl" />
248-
<TimeSeriesAnalysis
249-
metrics={metrics}
250-
featureType={activeMetric.feature_type}
251-
/>
252-
</>
251+
{metrics && effectiveGranularity !== BASELINE_KEY && (
252+
<TimeSeriesSection
253+
metrics={metrics}
254+
granularity={effectiveGranularity}
255+
featureType={activeMetric.feature_type}
256+
/>
253257
)}
254258
</EuiPageTemplate.Section>
255259
</EuiPageTemplate>
256260
);
257261
};
258262

263+
/**
264+
* One window length at a time: plotting every granularity (and the baseline)
265+
* by metric_date mixes spans, so the "drift" line has no meaning (#7054).
266+
*/
267+
const TimeSeriesSection = ({
268+
metrics,
269+
granularity,
270+
featureType,
271+
}: {
272+
metrics: NonNullable<ReturnType<typeof useFeatureMetrics>["data"]>;
273+
granularity: string;
274+
featureType: string;
275+
}) => {
276+
const windowRows = metrics.filter(
277+
(m) => !isBaselineMetricRow(m) && m.granularity === granularity,
278+
);
279+
if (windowRows.length === 0) return null;
280+
return (
281+
<>
282+
<EuiSpacer size="xl" />
283+
<TimeSeriesAnalysis metrics={windowRows} featureType={featureType} />
284+
</>
285+
);
286+
};
287+
259288
export default FeatureMetricsDetail;

‎ui/src/pages/monitoring/components/HistogramChart.tsx‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ import type {
1717
NumericHistogram,
1818
CategoricalHistogram,
1919
} from "../../../queries/useMonitoringApi";
20+
import {
21+
histogramDensities,
22+
isNumericHistogram,
23+
rebinNumericHistogram,
24+
} from "./histogramUtils";
2025

2126
const BAR_COLOR = "#006BB4";
2227
const BAR_COLOR_BASELINE = "#BD271E55";
@@ -70,10 +75,20 @@ const renderNumericSvg = (
7075
baseline: NumericHistogram | null | undefined,
7176
dim: ChartDimensions,
7277
) => {
78+
// Only overlay a well-formed numeric baseline: a feature whose type changed
79+
// (e.g. categorical baseline, numeric window) must not crash the tab, and
80+
// its baseline bins are re-binned onto the window's edges since each stored
81+
// histogram covers its own row's value range. Both series are drawn as
82+
// densities so windows of different sizes compare at the same scale (#7054).
83+
const validBaseline = isNumericHistogram(baseline) ? baseline : null;
84+
const windowDensities = histogramDensities(histogram.counts);
85+
const baselineDensities = validBaseline
86+
? histogramDensities(rebinNumericHistogram(validBaseline, histogram))
87+
: null;
7388
const maxCount = Math.max(
74-
...histogram.counts,
75-
...(baseline ? baseline.counts : []),
76-
1,
89+
...windowDensities,
90+
...(baselineDensities ? baselineDensities : []),
91+
1e-9,
7792
);
7893
const numBars = histogram.counts.length;
7994
const barWidth = Math.max(
@@ -86,7 +101,7 @@ const renderNumericSvg = (
86101

87102
const yTickFractions = [0, 0.25, 0.5, 0.75, 1];
88103
const yTicks = yTickFractions.map((f) => ({
89-
label: formatNumber(Math.round(maxCount * f), isCompact),
104+
label: formatNumber(maxCount * f, isCompact),
90105
y: dim.chartHeight - f * dim.chartHeight,
91106
}));
92107

@@ -135,16 +150,16 @@ const renderNumericSvg = (
135150
</g>
136151
))}
137152
{histogram.counts.map((count, i) => {
138-
const height = (count / maxCount) * dim.chartHeight;
153+
const height = (windowDensities[i] / maxCount) * dim.chartHeight;
139154
const x = dim.leftPad + i * (barWidth + dim.barGap);
140155
const binStart = histogram.bins[i];
141156
const binEnd =
142157
i < histogram.bins.length - 1
143158
? histogram.bins[i + 1]
144159
: binStart + histogram.bin_width;
145160
const baselineHeight =
146-
baseline && baseline.counts[i]
147-
? (baseline.counts[i] / maxCount) * dim.chartHeight
161+
baselineDensities && baselineDensities[i]
162+
? (baselineDensities[i] / maxCount) * dim.chartHeight
148163
: 0;
149164

150165
return (
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import {
2+
isBaselineMetricRow,
3+
isNumericHistogram,
4+
selectLatestWindowMetric,
5+
rebinNumericHistogram,
6+
histogramDensities,
7+
} from "./histogramUtils";
8+
import type { NumericHistogram } from "../../../queries/useMonitoringApi";
9+
10+
const windowRow = (overrides: Record<string, unknown>) => ({
11+
granularity: "daily",
12+
is_baseline: false,
13+
metric_date: "2026-10-18",
14+
row_count: 100,
15+
...overrides,
16+
});
17+
18+
describe("isBaselineMetricRow", () => {
19+
it("marks rows by is_baseline flag or baseline granularity", () => {
20+
expect(
21+
isBaselineMetricRow({ granularity: "baseline", is_baseline: true }),
22+
).toBe(true);
23+
expect(
24+
isBaselineMetricRow({ granularity: "baseline", is_baseline: false }),
25+
).toBe(true);
26+
expect(isBaselineMetricRow({ granularity: "daily" })).toBe(false);
27+
});
28+
});
29+
30+
describe("selectLatestWindowMetric", () => {
31+
it("never picks the baseline row even when it has the newest metric_date", () => {
32+
// The baseline is dated by computation day (today), windows by start (#7054).
33+
const baseline = windowRow({
34+
granularity: "baseline",
35+
is_baseline: true,
36+
metric_date: "2026-10-20",
37+
row_count: 1807,
38+
});
39+
const daily = windowRow({ metric_date: "2026-10-18" });
40+
const weekly = windowRow({
41+
granularity: "weekly",
42+
metric_date: "2026-10-12",
43+
});
44+
expect(selectLatestWindowMetric([baseline, daily, weekly])).toBe(daily);
45+
});
46+
47+
it("returns null when only baseline rows exist", () => {
48+
const baseline = windowRow({ granularity: "baseline", is_baseline: true });
49+
expect(selectLatestWindowMetric([baseline])).toBeNull();
50+
});
51+
52+
it("prefers rows with data, like the previous selection did", () => {
53+
const empty = windowRow({ metric_date: "2026-10-19", row_count: 0 });
54+
const withData = windowRow({ metric_date: "2026-10-18", row_count: 12 });
55+
expect(selectLatestWindowMetric([empty, withData])).toBe(withData);
56+
});
57+
58+
it("returns null for empty input", () => {
59+
expect(selectLatestWindowMetric([])).toBeNull();
60+
expect(selectLatestWindowMetric(null)).toBeNull();
61+
});
62+
});
63+
64+
describe("isNumericHistogram", () => {
65+
it("distinguishes numeric and categorical payloads", () => {
66+
expect(isNumericHistogram({ bins: [0, 1], counts: [5], bin_width: 1 })).toBe(
67+
true,
68+
);
69+
expect(
70+
isNumericHistogram({
71+
values: [{ value: "a", count: 3 }],
72+
other_count: 0,
73+
unique_count: 1,
74+
}),
75+
).toBe(false);
76+
expect(isNumericHistogram(null)).toBe(false);
77+
expect(isNumericHistogram(undefined)).toBe(false);
78+
});
79+
});
80+
81+
describe("rebinNumericHistogram", () => {
82+
it("maps a differently-ranged baseline onto the window's bin edges", () => {
83+
// Baseline covers [0, 2] in 2 bins of width 1; window covers [0, 1] in 2
84+
// bins of width 0.5 (the x100-unit-bug case from #7054, inverted).
85+
const baseline: NumericHistogram = {
86+
bins: [0, 1],
87+
counts: [10, 30],
88+
bin_width: 1,
89+
};
90+
const window: NumericHistogram = {
91+
bins: [0, 0.5],
92+
counts: [1, 1],
93+
bin_width: 0.5,
94+
};
95+
const rebinned = rebinNumericHistogram(baseline, window);
96+
// window bin [0,0.5) overlaps half of baseline bin [0,1): 5
97+
// window bin [0.5,1) overlaps the other half of [0,1): 5
98+
expect(rebinned[0]).toBeCloseTo(5);
99+
expect(rebinned[1]).toBeCloseTo(5);
100+
// Total mass is preserved when the target covers the source range.
101+
expect(rebinned[0] + rebinned[1]).toBeCloseTo(10);
102+
});
103+
104+
it("keeps identical bins identical", () => {
105+
const h: NumericHistogram = { bins: [0, 1, 2], counts: [3, 7], bin_width: 1 };
106+
expect(rebinNumericHistogram(h, h)).toEqual([3, 7]);
107+
});
108+
});
109+
110+
describe("histogramDensities", () => {
111+
it("normalizes to shares of the total so window size stops mattering", () => {
112+
expect(histogramDensities([125, 125])).toEqual([0.5, 0.5]);
113+
expect(histogramDensities([1807, 1807])).toEqual([0.5, 0.5]);
114+
expect(histogramDensities([0, 0])).toEqual([0, 0]);
115+
});
116+
});

0 commit comments

Comments
 (0)