Skip to content

Commit 1cc4651

Browse files
jdonelsonclaude
andcommitted
console: Debounce the freshness threshold field
Committing per keystroke meant passing through every state on the way to the intended one. The worst is the empty field: Number("") is 0, so clearing the field to retype marked every object as exceeding until the first digit landed. The field now shows what is typed immediately and commits once typing stops. Whatever has settled is taken at face value, so a field left empty commits 0, which is a readable state and what clearing it asks for. The status dot and the graph swatch use spacing tokens rather than px. The comment on OBJECT_TYPE_FILTERS claimed a cluster-scoped page can never list a table. The NULL cluster_id is real, but the conclusion was not: a table created FROM SOURCE is ingested on its source's cluster and does report lag, so it belongs here and is missing. Reworded to what is observed, with a TODO. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent 429b785 commit 1cc4651

4 files changed

Lines changed: 157 additions & 22 deletions

File tree

‎console/src/platform/freshness/FreshnessPage.tsx‎

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
useTheme,
2222
VStack,
2323
} from "@chakra-ui/react";
24+
import debounce from "lodash.debounce";
2425
import React from "react";
2526

2627
import { isSystemCluster } from "~/api/materialize";
@@ -48,6 +49,7 @@ import { formatDurationForAxis } from "~/utils/format";
4849

4950
import {
5051
OBJECT_TYPE_FILTERS,
52+
THRESHOLD_INPUT_SETTLE_MS,
5153
THRESHOLD_STEP_MS,
5254
TIME_PERIOD_OPTIONS,
5355
} from "./constants";
@@ -90,6 +92,58 @@ const SectionHeader = ({
9092
);
9193
};
9294

95+
/**
96+
* The threshold in seconds, as a text field.
97+
*
98+
* Holds what is being typed separately from the committed threshold, and
99+
* commits once typing stops. Committing per keystroke means passing through
100+
* every intermediate state on the way to the intended one, and the worst of
101+
* those is the empty field: `Number("")` is `0`, which marks every object as
102+
* exceeding.
103+
*
104+
* Whatever has settled is then taken at face value, including an empty or
105+
* unparseable field, which commits 0. A reader who clears this and stops has
106+
* asked for no threshold, and the page is readable in that state.
107+
*/
108+
export interface ThresholdInputProps {
109+
valueMs: number;
110+
onChange: (ms: number) => void;
111+
}
112+
113+
export const ThresholdInput = ({ valueMs, onChange }: ThresholdInputProps) => {
114+
const [draft, setDraft] = React.useState<string | undefined>(undefined);
115+
116+
const commit = React.useMemo(
117+
() =>
118+
debounce((raw: string) => {
119+
const seconds = Number(raw);
120+
onChange(Number.isFinite(seconds) && seconds >= 0 ? seconds * 1000 : 0);
121+
// Cleared alongside the commit so both land in one render, and the
122+
// field goes back to following `valueMs`.
123+
setDraft(undefined);
124+
}, THRESHOLD_INPUT_SETTLE_MS),
125+
[onChange],
126+
);
127+
128+
React.useEffect(() => commit.cancel, [commit]);
129+
130+
return (
131+
<Input
132+
type="number"
133+
size="sm"
134+
width="20"
135+
min={0}
136+
step={THRESHOLD_STEP_MS / 1000}
137+
aria-label="Freshness threshold in seconds"
138+
value={draft ?? (valueMs / 1000).toString()}
139+
onChange={(e) => {
140+
setDraft(e.target.value);
141+
commit(e.target.value);
142+
}}
143+
/>
144+
);
145+
};
146+
93147
const FreshnessContent = ({
94148
clusterId,
95149
lookbackMs,
@@ -223,8 +277,7 @@ const FreshnessContent = ({
223277
<VStack alignItems="stretch" width="100%" spacing="4">
224278
<HStack spacing="2" alignItems="center">
225279
<Box
226-
width="9px"
227-
height="9px"
280+
boxSize="2.5"
228281
borderRadius="full"
229282
background={ok ? colors.accent.green : colors.accent.red}
230283
/>
@@ -349,20 +402,9 @@ const FreshnessPage = () => {
349402
<Text textStyle="text-ui-reg" color={colors.foreground.secondary}>
350403
Highlight objects that exceeded
351404
</Text>
352-
<Input
353-
type="number"
354-
size="sm"
355-
width="20"
356-
min={0}
357-
step={THRESHOLD_STEP_MS / 1000}
358-
aria-label="Freshness threshold in seconds"
359-
value={(liveThreshold / 1000).toString()}
360-
onChange={(e) => {
361-
const seconds = Number(e.target.value);
362-
if (Number.isFinite(seconds) && seconds >= 0) {
363-
onThresholdChange(seconds * 1000);
364-
}
365-
}}
405+
<ThresholdInput
406+
valueMs={liveThreshold}
407+
onChange={onThresholdChange}
366408
/>
367409
<Text textStyle="text-ui-reg" color={colors.foreground.secondary}>
368410
seconds

‎console/src/platform/freshness/FreshnessTable.tsx‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,8 @@ const FreshnessTableInner = ({
173173
<Td py="2" pr="0">
174174
{row.color && (
175175
<Box
176-
width="10px"
177-
height="10px"
178-
borderRadius="2px"
176+
boxSize="2.5"
177+
borderRadius="sm"
179178
background={row.color}
180179
role="img"
181180
aria-label="Shown on the graph"
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright Materialize, Inc. and contributors. All rights reserved.
2+
//
3+
// Use of this software is governed by the Business Source License
4+
// included in the LICENSE file.
5+
//
6+
// As of the Change Date specified in that file, in accordance with
7+
// the Business Source License, use of this software will be governed
8+
// by the Apache License, Version 2.0.
9+
10+
import { screen, waitFor } from "@testing-library/react";
11+
import userEvent from "@testing-library/user-event";
12+
import React from "react";
13+
import { describe, expect, it, vi } from "vitest";
14+
15+
import { renderComponent } from "~/test/utils";
16+
17+
import { ThresholdInput } from "./FreshnessPage";
18+
19+
const field = () => screen.getByLabelText("Freshness threshold in seconds");
20+
21+
describe("ThresholdInput", () => {
22+
it("shows the committed threshold in seconds", async () => {
23+
await renderComponent(
24+
<ThresholdInput valueMs={2_300} onChange={vi.fn()} />,
25+
);
26+
expect(field()).toHaveValue(2.3);
27+
});
28+
29+
it("commits once typing stops", async () => {
30+
const onChange = vi.fn();
31+
await renderComponent(
32+
<ThresholdInput valueMs={2_000} onChange={onChange} />,
33+
);
34+
35+
await userEvent.clear(field());
36+
await userEvent.type(field(), "25");
37+
38+
await waitFor(() => expect(onChange).toHaveBeenCalledWith(25_000));
39+
});
40+
41+
it("does not commit the states passed through on the way", async () => {
42+
// Clearing the field and typing "25" passes through "" and "2". `Number("")`
43+
// is 0, which marks every object as exceeding, and 2 is a threshold nobody
44+
// asked for.
45+
const onChange = vi.fn();
46+
await renderComponent(
47+
<ThresholdInput valueMs={2_000} onChange={onChange} />,
48+
);
49+
50+
await userEvent.clear(field());
51+
await userEvent.type(field(), "25");
52+
53+
await waitFor(() => expect(onChange).toHaveBeenCalledWith(25_000));
54+
expect(onChange).toHaveBeenCalledTimes(1);
55+
});
56+
57+
it("takes a field left empty as no threshold", async () => {
58+
const onChange = vi.fn();
59+
await renderComponent(
60+
<ThresholdInput valueMs={2_000} onChange={onChange} />,
61+
);
62+
63+
await userEvent.clear(field());
64+
65+
await waitFor(() => expect(onChange).toHaveBeenCalledWith(0));
66+
});
67+
68+
it("keeps the field responsive while the commit is pending", async () => {
69+
await renderComponent(
70+
<ThresholdInput valueMs={2_000} onChange={vi.fn()} />,
71+
);
72+
73+
await userEvent.clear(field());
74+
await userEvent.type(field(), "7");
75+
76+
// Shown immediately, whatever the debounce is doing.
77+
expect(field()).toHaveValue(7);
78+
});
79+
});

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,25 @@ export const THRESHOLD_STEP_MS = 100;
5555
export const THRESHOLD_SETTLE_MS = 120;
5656

5757
/**
58-
* Type filter chips.
58+
* How long the threshold field waits after a keystroke before committing.
5959
*
60-
* Tables are absent on purpose: a table has no cluster, so a cluster-scoped
61-
* page can never list one. `mz_relations` gives `'table'` a NULL `cluster_id`.
60+
* Longer than `THRESHOLD_SETTLE_MS`, which paces a drag: a pointer emits
61+
* continuously and 120ms of stillness means the reader has stopped, while
62+
* typing has gaps that long between digits. Committing inside one would read
63+
* "2" on the way to "25".
64+
*/
65+
export const THRESHOLD_INPUT_SETTLE_MS = 400;
66+
67+
/**
68+
* The object types this page offers to filter by.
69+
*
70+
* Tables are not among them. A table carries a NULL `cluster_id`, so the
71+
* cluster-scoped lookup this page does never reaches one, including a table
72+
* created `FROM SOURCE`, which is ingested on its source's cluster and does
73+
* report lag. Such a table is missing from this page today.
74+
*
75+
* TODO: Reach those tables through their source rather than through
76+
* `cluster_id`. Cluster Overview has the same gap.
6277
*/
6378
export const OBJECT_TYPE_FILTERS: { label: string; value: string }[] = [
6479
{ label: "Sources", value: "source" },

0 commit comments

Comments
 (0)