Skip to content

Commit 86dfc27

Browse files
jdonelsonclaude
andcommitted
console: map unreadable freshness lags in computeStats
The percentile hooks return null for a reading that could not be taken, as useLatestLag does, and computeStats maps both to UNREADABLE through one helper. queries.ts no longer imports from freshnessRows. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 1638148 commit 86dfc27

4 files changed

Lines changed: 32 additions & 37 deletions

File tree

‎console/src/platform/freshness/freshnessRows.test.ts‎

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -120,21 +120,13 @@ const hydrationByObjectId = new Map<string, HydrationCounts>(
120120
);
121121

122122
/**
123-
* What the percentile query returns. `absent` has no readings in the range, so
124-
* it has no entry.
123+
* What the percentile query returns, null where a reading could not be taken.
124+
* `absent` has no readings in the range, so it has no entry.
125125
*/
126126
const percentilesByObjectId = new Map<string, LagPercentiles>([
127127
["spiky", { peak: 9_000, p90: 430, p99: 9_000, p99_9: 9_000 }],
128128
["steady", { peak: 3_100, p90: 3_050, p99: 3_100, p99_9: 3_100 }],
129-
[
130-
"unreadable",
131-
{
132-
peak: UNREADABLE,
133-
p90: UNREADABLE,
134-
p99: UNREADABLE,
135-
p99_9: UNREADABLE,
136-
},
137-
],
129+
["unreadable", { peak: null, p90: null, p99: null, p99_9: null }],
138130
]);
139131

140132
const rowsFor = (predicate: Predicate, threshold: number) => {
@@ -174,8 +166,13 @@ describe("computeStats", () => {
174166
const stats = computeStats(null, percentilesByObjectId.get("unreadable"));
175167
// The graph breaks its line at these readings rather than drawing them.
176168
expect(accessorFor("unreadable")(data[4]!)).toBeNull();
177-
expect(stats.peak).toBe(UNREADABLE);
178-
expect(stats.current).toBe(UNREADABLE);
169+
expect(stats).toEqual({
170+
current: UNREADABLE,
171+
peak: UNREADABLE,
172+
p90: UNREADABLE,
173+
p99: UNREADABLE,
174+
p99_9: UNREADABLE,
175+
});
179176
});
180177

181178
it("counts a lag that started after the percentiles were fetched in pMAX", () => {
@@ -196,10 +193,10 @@ describe("computeStats", () => {
196193
// Unreadable once, hours ago, and healthy since: the worst thing in the
197194
// window, but not now and not one reading in ten.
198195
const stats = computeStats(400, {
199-
peak: UNREADABLE,
196+
peak: null,
200197
p90: 400,
201-
p99: UNREADABLE,
202-
p99_9: UNREADABLE,
198+
p99: null,
199+
p99_9: null,
203200
});
204201
const line = { key: "mv", yAccessor: () => null };
205202
const byKey = new Map([["mv", stats]]);

‎console/src/platform/freshness/freshnessRows.ts‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ export function computeStats(
115115
// The percentiles refetch once per bin and the latest reading once a
116116
// minute, so a lag that starts between percentile fetches is only in
117117
// `current`.
118-
peak: Math.max(percentiles.peak, current ?? -Infinity),
119-
p90: percentiles.p90,
120-
p99: percentiles.p99,
121-
p99_9: percentiles.p99_9,
118+
peak: Math.max(lagOrUnreadable(percentiles.peak), current ?? -Infinity),
119+
p90: lagOrUnreadable(percentiles.p90),
120+
p99: lagOrUnreadable(percentiles.p99),
121+
p99_9: lagOrUnreadable(percentiles.p99_9),
122122
};
123123
}
124124

@@ -131,9 +131,13 @@ export function computeStats(
131131
*/
132132
export const UNREADABLE = Infinity;
133133

134-
/** `null` is no reading at all, which is not the same as one that failed. */
134+
/** A lag from a query, where null is a reading that could not be taken. */
135+
export const lagOrUnreadable = (lag: number | null) =>
136+
lag === null ? UNREADABLE : lag;
137+
138+
/** Undefined is no reading at all, which is not the same as one that failed. */
135139
const currentFrom = (latest: number | null | undefined) =>
136-
latest === null ? UNREADABLE : (latest ?? null);
140+
latest === undefined ? null : lagOrUnreadable(latest);
137141

138142
const statFor = (stats: ObjectStats, predicate: Predicate) =>
139143
predicate === "current"

‎console/src/platform/freshness/queries.test.ts‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import { allObjects } from "~/store/allObjects";
2828
import { buildDatabaseObject, createProviderWrapper } from "~/test/utils";
2929

3030
import { FreshnessScope } from "./constants";
31-
import { UNREADABLE } from "./freshnessRows";
3231
import {
3332
freshnessQueryKeys,
3433
inFreshnessScope,
@@ -152,7 +151,7 @@ describe("freshness queries", () => {
152151
});
153152

154153
describe("useObjectLagPercentiles", () => {
155-
it("reads a NULL lag as unreadable and leaves out an object with no readings", async () => {
154+
it("passes a NULL lag through and leaves out an object with no readings", async () => {
156155
const params = { objectIds: ["u1", "u2", "u3"], lookbackMs: 3_600_000 };
157156
const interval = { type_oid: MzDataType.interval };
158157
server.use(
@@ -196,12 +195,8 @@ describe("useObjectLagPercentiles", () => {
196195
["u1", { peak: 9_000, p90: 430, p99: 9_000, p99_9: 9_000 }],
197196
[
198197
"u2",
199-
{
200-
peak: UNREADABLE,
201-
p90: 400,
202-
p99: UNREADABLE,
203-
p99_9: UNREADABLE,
204-
},
198+
// Unreadable readings arrive as null. `computeStats` marks them.
199+
{ peak: null, p90: 400, p99: null, p99_9: null },
205200
],
206201
]),
207202
),

‎console/src/platform/freshness/queries.ts‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import { isOnCluster } from "~/platform/clusters/queries";
2727
import { sumPostgresIntervalMs } from "~/util";
2828

2929
import { FreshnessScope, OBJECT_TYPE_FILTERS } from "./constants";
30-
import { UNREADABLE } from "./freshnessRows";
3130

3231
export interface HydrationCounts {
3332
hydratedReplicas: number;
@@ -121,18 +120,18 @@ export function useLatestLag(objectIds: string[]) {
121120
});
122121
}
123122

124-
/** Lags in milliseconds. `UNREADABLE` where the reading at that rank could not be taken. */
123+
/** Lags in milliseconds. Null where the reading at that rank could not be taken. */
125124
export interface LagPercentiles {
126-
peak: number;
127-
p90: number;
128-
p99: number;
129-
p99_9: number;
125+
peak: number | null;
126+
p90: number | null;
127+
p99: number | null;
128+
p99_9: number | null;
130129
}
131130

132131
type PercentileRow = Record<keyof LagPercentiles, IPostgresInterval | null>;
133132

134133
const lagMs = (lag: IPostgresInterval | null) =>
135-
lag === null ? UNREADABLE : sumPostgresIntervalMs(lag);
134+
lag === null ? null : sumPostgresIntervalMs(lag);
136135

137136
const toLagPercentiles = (row: PercentileRow): LagPercentiles => ({
138137
peak: lagMs(row.peak),

0 commit comments

Comments
 (0)