From 040649d2e484ba775e1e002e5509d75da7447b52 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 20 Nov 2026 18:46:49 +0000 Subject: [PATCH 1/4] feat(app): let users pick the dashboard live refresh interval Live mode refreshed at the date range's auto granularity only. The Live button is now a split button whose menu offers 30s up to that default. Co-authored-by: Mike Shi --- .changeset/dashboard-live-refresh-interval.md | 5 + packages/app/src/DBDashboardPage.tsx | 45 +++--- .../src/components/DashboardLiveButton.tsx | 90 ++++++++++++ .../__tests__/useDashboardRefresh.test.tsx | 136 ++++++++++++++++++ .../app/src/hooks/useDashboardRefresh.tsx | 66 ++++++++- 5 files changed, 321 insertions(+), 21 deletions(-) create mode 100644 .changeset/dashboard-live-refresh-interval.md create mode 100644 packages/app/src/components/DashboardLiveButton.tsx create mode 100644 packages/app/src/hooks/__tests__/useDashboardRefresh.test.tsx diff --git a/.changeset/dashboard-live-refresh-interval.md b/.changeset/dashboard-live-refresh-interval.md new file mode 100644 index 0000000000..1d61cc7182 --- /dev/null +++ b/.changeset/dashboard-live-refresh-interval.md @@ -0,0 +1,5 @@ +--- +'@hyperdx/app': patch +--- + +feat: Pick the dashboard live mode refresh interval, from 30s up to the time range's default diff --git a/packages/app/src/DBDashboardPage.tsx b/packages/app/src/DBDashboardPage.tsx index 4296121918..33f741a2d3 100644 --- a/packages/app/src/DBDashboardPage.tsx +++ b/packages/app/src/DBDashboardPage.tsx @@ -153,6 +153,7 @@ import { type DragHandleProps, } from '@/components/DashboardDndContext'; import { DashboardKioskHeader } from '@/components/DashboardKioskHeader'; +import { DashboardLiveButton } from '@/components/DashboardLiveButton'; import DashboardTableOfContents from '@/components/DashboardTableOfContents'; import EditTimeChartForm from '@/components/DBEditTimeChartForm'; import DBNumberChart from '@/components/DBNumberChart'; @@ -2131,6 +2132,9 @@ function DBDashboardPage({ }; const [isLive, setIsLive] = useState(false); + const [selectedRefreshInterval, setSelectedRefreshInterval] = useState< + Granularity | undefined + >(); const { control, setValue, getValues, handleSubmit } = useForm<{ granularity: SQLInterval | 'auto'; @@ -2173,12 +2177,26 @@ function DBDashboardPage({ isRefreshEnabled, manualRefreshCooloff, refresh, + refreshInterval, + refreshIntervalOptions, } = useDashboardRefresh({ searchedTimeRange, onTimeRangeSelect, isLive: isLive || isKioskMode, + selectedRefreshInterval, }); + const onRefreshIntervalChange = useCallback( + (interval: Granularity) => { + // Picking the default follows the date range again as it changes. + setSelectedRefreshInterval( + interval === granularityOverride ? undefined : interval, + ); + setIsLive(true); + }, + [granularityOverride], + ); + const onSubmit = useCallback(() => { onSearch(displayedTimeInputValue); handleSubmit(data => { @@ -3290,25 +3308,14 @@ function DBDashboardPage({ }} /> - - - + setIsLive(prev => !prev)} + refreshInterval={refreshInterval} + defaultRefreshInterval={granularityOverride} + refreshIntervalOptions={refreshIntervalOptions} + onRefreshIntervalChange={onRefreshIntervalChange} + /> void; + refreshInterval: Granularity; + defaultRefreshInterval: Granularity; + refreshIntervalOptions: Granularity[]; + onRefreshIntervalChange: (interval: Granularity) => void; +}) { + const variant = isLive ? 'primary' : 'secondary'; + const intervalLabel = mapGranularityToExternalFormat(refreshInterval); + + return ( + + + + + + + + + + Refresh every + {refreshIntervalOptions.map(interval => ( + onRefreshIntervalChange(interval)} + leftSection={ + interval === refreshInterval ? ( + + ) : ( + + ) + } + rightSection={ + interval === defaultRefreshInterval ? ( + + Default + + ) : null + } + > + {mapGranularityToExternalFormat(interval)} + + ))} + + + + ); +} diff --git a/packages/app/src/hooks/__tests__/useDashboardRefresh.test.tsx b/packages/app/src/hooks/__tests__/useDashboardRefresh.test.tsx new file mode 100644 index 0000000000..81338ec705 --- /dev/null +++ b/packages/app/src/hooks/__tests__/useDashboardRefresh.test.tsx @@ -0,0 +1,136 @@ +import { Granularity } from '@hyperdx/common-utils/dist/core/utils'; +import { act, renderHook } from '@testing-library/react'; + +import { + getRefreshIntervalOptions, + resolveRefreshInterval, + useDashboardRefresh, +} from '@/hooks/useDashboardRefresh'; + +const HOUR_MS = 60 * 60 * 1000; + +function lastHours(hours: number): [Date, Date] { + const end = new Date('2026-01-01T12:00:00Z'); + return [new Date(end.getTime() - hours * HOUR_MS), end]; +} + +describe('getRefreshIntervalOptions', () => { + it('offers 30s up to the default interval', () => { + expect(getRefreshIntervalOptions(Granularity.ThirtyMinute)).toEqual([ + Granularity.ThirtySecond, + Granularity.OneMinute, + Granularity.FiveMinute, + Granularity.FifteenMinute, + Granularity.ThirtyMinute, + ]); + }); + + it('offers only 30s when that is the default', () => { + expect(getRefreshIntervalOptions(Granularity.ThirtySecond)).toEqual([ + Granularity.ThirtySecond, + ]); + }); + + it('keeps a default faster than 30s as the only choice', () => { + expect(getRefreshIntervalOptions(Granularity.FifteenSecond)).toEqual([ + Granularity.FifteenSecond, + ]); + }); +}); + +describe('resolveRefreshInterval', () => { + it('uses the default when nothing is selected', () => { + expect(resolveRefreshInterval(undefined, Granularity.FiveMinute)).toBe( + Granularity.FiveMinute, + ); + }); + + it('uses a faster selection', () => { + expect( + resolveRefreshInterval(Granularity.OneMinute, Granularity.FiveMinute), + ).toBe(Granularity.OneMinute); + }); + + it('falls back to the default when the selection is slower than it', () => { + expect( + resolveRefreshInterval(Granularity.OneHour, Granularity.FiveMinute), + ).toBe(Granularity.FiveMinute); + }); +}); + +describe('useDashboardRefresh', () => { + beforeEach(() => { + jest.useFakeTimers(); + }); + + afterEach(() => { + jest.useRealTimers(); + }); + + it('refreshes at the selected interval', () => { + const onTimeRangeSelect = jest.fn(); + const { result } = renderHook(() => + useDashboardRefresh({ + searchedTimeRange: lastHours(24), + onTimeRangeSelect, + isLive: true, + selectedRefreshInterval: Granularity.ThirtySecond, + }), + ); + + expect(result.current.granularityOverride).toBe(Granularity.ThirtyMinute); + expect(result.current.refreshInterval).toBe(Granularity.ThirtySecond); + + act(() => { + jest.advanceTimersByTime(29_000); + }); + expect(onTimeRangeSelect).not.toHaveBeenCalled(); + act(() => { + jest.advanceTimersByTime(1_000); + }); + expect(onTimeRangeSelect).toHaveBeenCalledTimes(1); + }); + + it('refreshes at the default interval without a selection', () => { + const onTimeRangeSelect = jest.fn(); + const { result } = renderHook(() => + useDashboardRefresh({ + searchedTimeRange: lastHours(1), + onTimeRangeSelect, + isLive: true, + }), + ); + + expect(result.current.refreshInterval).toBe(Granularity.OneMinute); + expect(result.current.refreshIntervalOptions).toEqual([ + Granularity.ThirtySecond, + Granularity.OneMinute, + ]); + + act(() => { + jest.advanceTimersByTime(59_000); + }); + expect(onTimeRangeSelect).not.toHaveBeenCalled(); + act(() => { + jest.advanceTimersByTime(1_000); + }); + expect(onTimeRangeSelect).toHaveBeenCalledTimes(1); + }); + + it('does not refresh when not live', () => { + const onTimeRangeSelect = jest.fn(); + renderHook(() => + useDashboardRefresh({ + searchedTimeRange: lastHours(1), + onTimeRangeSelect, + isLive: false, + selectedRefreshInterval: Granularity.ThirtySecond, + }), + ); + + act(() => { + jest.advanceTimersByTime(10 * 60_000); + }); + expect(onTimeRangeSelect).not.toHaveBeenCalled(); + }); +}); diff --git a/packages/app/src/hooks/useDashboardRefresh.tsx b/packages/app/src/hooks/useDashboardRefresh.tsx index 04e17d505f..b28dc4e54f 100644 --- a/packages/app/src/hooks/useDashboardRefresh.tsx +++ b/packages/app/src/hooks/useDashboardRefresh.tsx @@ -4,17 +4,68 @@ import React from 'react'; import { convertDateRangeToGranularityString, convertGranularityToSeconds, + Granularity, } from '@hyperdx/common-utils/dist/core/utils'; import { useDocumentVisibility } from '@mantine/hooks'; +const MIN_REFRESH_INTERVAL_SECONDS = 30; + +// Every interval convertDateRangeToGranularityString can return at or above the +// minimum, so the date range's default interval is always one of the choices. +const REFRESH_INTERVAL_CHOICES = [ + Granularity.ThirtySecond, + Granularity.OneMinute, + Granularity.FiveMinute, + Granularity.FifteenMinute, + Granularity.ThirtyMinute, + Granularity.OneHour, + Granularity.TwoHour, + Granularity.SixHour, + Granularity.TwelveHour, + Granularity.OneDay, + Granularity.TwoDay, + Granularity.SevenDay, + Granularity.ThirtyDay, +]; + +/** + * Refresh intervals a live dashboard offers, fastest first: from 30s up to the + * date range's default interval. A default faster than 30s is the only choice. + */ +export function getRefreshIntervalOptions( + defaultInterval: Granularity, +): Granularity[] { + const maxSeconds = convertGranularityToSeconds(defaultInterval); + if (maxSeconds < MIN_REFRESH_INTERVAL_SECONDS) { + return [defaultInterval]; + } + return REFRESH_INTERVAL_CHOICES.filter( + interval => convertGranularityToSeconds(interval) <= maxSeconds, + ); +} + +/** The chosen interval while it is still offered, otherwise the default. */ +export function resolveRefreshInterval( + selected: Granularity | undefined, + defaultInterval: Granularity, +): Granularity { + return selected != null && + getRefreshIntervalOptions(defaultInterval).includes(selected) + ? selected + : defaultInterval; +} + export const useDashboardRefresh = ({ searchedTimeRange, onTimeRangeSelect, isLive, + selectedRefreshInterval, }: { onTimeRangeSelect: (start: Date, end: Date) => void; searchedTimeRange: [Date, Date]; isLive: boolean; + /** Overrides the date range's default refresh interval while it is offered. */ + selectedRefreshInterval?: Granularity; }) => { const [manualRefreshCooloff, setManualRefreshCooloff] = React.useState(false); @@ -41,6 +92,15 @@ export const useDashboardRefresh = ({ const granularityOverride = convertDateRangeToGranularityString(searchedTimeRange); + const refreshIntervalOptions = React.useMemo( + () => getRefreshIntervalOptions(granularityOverride), + [granularityOverride], + ); + const refreshInterval = resolveRefreshInterval( + selectedRefreshInterval, + granularityOverride, + ); + // Auto-refresh interval const intervalRef = React.useRef(null); React.useEffect(() => { @@ -52,7 +112,7 @@ export const useDashboardRefresh = ({ () => { refresh(); }, - convertGranularityToSeconds(granularityOverride) * 1000, + convertGranularityToSeconds(refreshInterval) * 1000, ); } else { if (intervalRef.current) { @@ -64,12 +124,14 @@ export const useDashboardRefresh = ({ window.clearInterval(intervalRef.current); } }; - }, [isRefreshEnabled, granularityOverride, refresh]); + }, [isRefreshEnabled, refreshInterval, refresh]); return { granularityOverride, isRefreshEnabled, manualRefreshCooloff, refresh, + refreshInterval, + refreshIntervalOptions, }; }; From 767631d7bc847573e87345bc5227c3968497cd48 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 20 Nov 2026 18:47:18 +0000 Subject: [PATCH 2/4] chore: format changeset Co-authored-by: Mike Shi --- .changeset/dashboard-live-refresh-interval.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.changeset/dashboard-live-refresh-interval.md b/.changeset/dashboard-live-refresh-interval.md index 1d61cc7182..bdce514a3c 100644 --- a/.changeset/dashboard-live-refresh-interval.md +++ b/.changeset/dashboard-live-refresh-interval.md @@ -2,4 +2,5 @@ '@hyperdx/app': patch --- -feat: Pick the dashboard live mode refresh interval, from 30s up to the time range's default +feat: Pick the dashboard live mode refresh interval, from 30s up to the time +range's default From 42f492b64ce636c26399c1931b42eaaecdd4782f Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 20 Nov 2026 19:02:58 +0000 Subject: [PATCH 3/4] feat(app): add an auto refresh interval that follows the date range Every listed interval, including the date range's default, is now a fixed choice. Auto is a separate first item. Co-authored-by: Mike Shi --- packages/app/src/DBDashboardPage.tsx | 11 ++--- .../src/components/DashboardLiveButton.tsx | 41 ++++++++++------ .../__tests__/useDashboardRefresh.test.tsx | 49 +++++++++++++++++++ .../app/src/hooks/useDashboardRefresh.tsx | 9 +++- 4 files changed, 87 insertions(+), 23 deletions(-) diff --git a/packages/app/src/DBDashboardPage.tsx b/packages/app/src/DBDashboardPage.tsx index 33f741a2d3..aca473685d 100644 --- a/packages/app/src/DBDashboardPage.tsx +++ b/packages/app/src/DBDashboardPage.tsx @@ -2179,6 +2179,7 @@ function DBDashboardPage({ refresh, refreshInterval, refreshIntervalOptions, + isAutoRefreshInterval, } = useDashboardRefresh({ searchedTimeRange, onTimeRangeSelect, @@ -2187,14 +2188,11 @@ function DBDashboardPage({ }); const onRefreshIntervalChange = useCallback( - (interval: Granularity) => { - // Picking the default follows the date range again as it changes. - setSelectedRefreshInterval( - interval === granularityOverride ? undefined : interval, - ); + (interval: Granularity | undefined) => { + setSelectedRefreshInterval(interval); setIsLive(true); }, - [granularityOverride], + [], ); const onSubmit = useCallback(() => { @@ -3312,6 +3310,7 @@ function DBDashboardPage({ isLive={isLive} onToggleLive={() => setIsLive(prev => !prev)} refreshInterval={refreshInterval} + isAutoRefreshInterval={isAutoRefreshInterval} defaultRefreshInterval={granularityOverride} refreshIntervalOptions={refreshIntervalOptions} onRefreshIntervalChange={onRefreshIntervalChange} diff --git a/packages/app/src/components/DashboardLiveButton.tsx b/packages/app/src/components/DashboardLiveButton.tsx index f532ba32df..97ee14ee32 100644 --- a/packages/app/src/components/DashboardLiveButton.tsx +++ b/packages/app/src/components/DashboardLiveButton.tsx @@ -1,12 +1,17 @@ import { Granularity } from '@hyperdx/common-utils/dist/core/utils'; import { mapGranularityToExternalFormat } from '@hyperdx/common-utils/dist/externalSource'; -import { Box, Button, Menu, Text, Tooltip } from '@mantine/core'; +import { Box, Button, Menu, Tooltip } from '@mantine/core'; import { IconCheck, IconChevronDown } from '@tabler/icons-react'; +function CheckSlot({ checked }: { checked: boolean }) { + return checked ? : ; +} + export function DashboardLiveButton({ isLive, onToggleLive, refreshInterval, + isAutoRefreshInterval, defaultRefreshInterval, refreshIntervalOptions, onRefreshIntervalChange, @@ -14,12 +19,16 @@ export function DashboardLiveButton({ isLive: boolean; onToggleLive: () => void; refreshInterval: Granularity; + isAutoRefreshInterval: boolean; defaultRefreshInterval: Granularity; refreshIntervalOptions: Granularity[]; - onRefreshIntervalChange: (interval: Granularity) => void; + /** Undefined selects auto, which follows the date range's default. */ + onRefreshIntervalChange: (interval: Granularity | undefined) => void; }) { const variant = isLive ? 'primary' : 'secondary'; const intervalLabel = mapGranularityToExternalFormat(refreshInterval); + const autoLabel = `Auto (${mapGranularityToExternalFormat(defaultRefreshInterval)})`; + const triggerLabel = isAutoRefreshInterval ? autoLabel : intervalLabel; return ( @@ -50,34 +59,34 @@ export function DashboardLiveButton({ variant={variant} px="xs" rightSection={} - aria-label={`Refresh interval ${intervalLabel}`} + aria-label={`Refresh interval ${triggerLabel}`} data-testid="dashboard-refresh-interval-button" style={{ borderInlineStart: '1px solid var(--color-border)', }} > - {intervalLabel} + {triggerLabel} Refresh every + onRefreshIntervalChange(undefined)} + leftSection={} + > + {autoLabel} + + {refreshIntervalOptions.map(interval => ( onRefreshIntervalChange(interval)} leftSection={ - interval === refreshInterval ? ( - - ) : ( - - ) - } - rightSection={ - interval === defaultRefreshInterval ? ( - - Default - - ) : null + } > {mapGranularityToExternalFormat(interval)} diff --git a/packages/app/src/hooks/__tests__/useDashboardRefresh.test.tsx b/packages/app/src/hooks/__tests__/useDashboardRefresh.test.tsx index 81338ec705..073c9a345e 100644 --- a/packages/app/src/hooks/__tests__/useDashboardRefresh.test.tsx +++ b/packages/app/src/hooks/__tests__/useDashboardRefresh.test.tsx @@ -133,4 +133,53 @@ describe('useDashboardRefresh', () => { }); expect(onTimeRangeSelect).not.toHaveBeenCalled(); }); + + it('keeps a fixed interval when the date range widens, unlike auto', () => { + const { result, rerender } = renderHook( + ({ + hours, + selected, + }: { + hours: number; + selected: Granularity | undefined; + }) => + useDashboardRefresh({ + searchedTimeRange: lastHours(hours), + onTimeRangeSelect: jest.fn(), + isLive: true, + selectedRefreshInterval: selected, + }), + { + initialProps: { + hours: 3, + selected: Granularity.FiveMinute as Granularity | undefined, + }, + }, + ); + + expect(result.current.refreshInterval).toBe(Granularity.FiveMinute); + expect(result.current.isAutoRefreshInterval).toBe(false); + + rerender({ hours: 24, selected: Granularity.FiveMinute }); + expect(result.current.refreshInterval).toBe(Granularity.FiveMinute); + expect(result.current.isAutoRefreshInterval).toBe(false); + + rerender({ hours: 24, selected: undefined }); + expect(result.current.refreshInterval).toBe(Granularity.ThirtyMinute); + expect(result.current.isAutoRefreshInterval).toBe(true); + }); + + it('reports auto while a fixed interval is slower than the default', () => { + const { result } = renderHook(() => + useDashboardRefresh({ + searchedTimeRange: lastHours(1), + onTimeRangeSelect: jest.fn(), + isLive: true, + selectedRefreshInterval: Granularity.ThirtyMinute, + }), + ); + + expect(result.current.refreshInterval).toBe(Granularity.OneMinute); + expect(result.current.isAutoRefreshInterval).toBe(true); + }); }); diff --git a/packages/app/src/hooks/useDashboardRefresh.tsx b/packages/app/src/hooks/useDashboardRefresh.tsx index b28dc4e54f..6343c2880e 100644 --- a/packages/app/src/hooks/useDashboardRefresh.tsx +++ b/packages/app/src/hooks/useDashboardRefresh.tsx @@ -64,7 +64,10 @@ export const useDashboardRefresh = ({ onTimeRangeSelect: (start: Date, end: Date) => void; searchedTimeRange: [Date, Date]; isLive: boolean; - /** Overrides the date range's default refresh interval while it is offered. */ + /** + * Fixed refresh interval, used while the date range still offers it. + * Undefined follows the date range's default. + */ selectedRefreshInterval?: Granularity; }) => { const [manualRefreshCooloff, setManualRefreshCooloff] = React.useState(false); @@ -100,6 +103,9 @@ export const useDashboardRefresh = ({ selectedRefreshInterval, granularityOverride, ); + const isAutoRefreshInterval = + selectedRefreshInterval == null || + !refreshIntervalOptions.includes(selectedRefreshInterval); // Auto-refresh interval const intervalRef = React.useRef(null); @@ -133,5 +139,6 @@ export const useDashboardRefresh = ({ refresh, refreshInterval, refreshIntervalOptions, + isAutoRefreshInterval, }; }; From 726b2e4006fcfce4f1218cd2fb9782045aa6b756 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 20 Nov 2026 19:14:37 +0000 Subject: [PATCH 4/4] fix(app): drop the sub-30s fixed interval and cap the refresh timer delay Ranges of 15 minutes or less now offer only Auto. A 30d interval exceeded setInterval's maximum delay and refreshed in a tight loop. Also derives auto vs fixed once and adds DashboardLiveButton tests. Co-authored-by: Mike Shi --- .../src/components/DashboardLiveButton.tsx | 46 ++++--- .../__tests__/DashboardLiveButton.test.tsx | 112 ++++++++++++++++++ .../__tests__/useDashboardRefresh.test.tsx | 50 ++++---- .../app/src/hooks/useDashboardRefresh.tsx | 47 ++++---- 4 files changed, 184 insertions(+), 71 deletions(-) create mode 100644 packages/app/src/components/__tests__/DashboardLiveButton.test.tsx diff --git a/packages/app/src/components/DashboardLiveButton.tsx b/packages/app/src/components/DashboardLiveButton.tsx index 97ee14ee32..30fb69008a 100644 --- a/packages/app/src/components/DashboardLiveButton.tsx +++ b/packages/app/src/components/DashboardLiveButton.tsx @@ -3,8 +3,24 @@ import { mapGranularityToExternalFormat } from '@hyperdx/common-utils/dist/exter import { Box, Button, Menu, Tooltip } from '@mantine/core'; import { IconCheck, IconChevronDown } from '@tabler/icons-react'; -function CheckSlot({ checked }: { checked: boolean }) { - return checked ? : ; +function IntervalItem({ + label, + checked, + onClick, +}: { + label: string; + checked: boolean; + onClick: () => void; +}) { + return ( + : } + > + {label} + + ); } export function DashboardLiveButton({ @@ -70,27 +86,19 @@ export function DashboardLiveButton({ Refresh every - onRefreshIntervalChange(undefined)} - leftSection={} - > - {autoLabel} - - + /> + {refreshIntervalOptions.length > 0 && } {refreshIntervalOptions.map(interval => ( - onRefreshIntervalChange(interval)} - leftSection={ - - } - > - {mapGranularityToExternalFormat(interval)} - + /> ))} diff --git a/packages/app/src/components/__tests__/DashboardLiveButton.test.tsx b/packages/app/src/components/__tests__/DashboardLiveButton.test.tsx new file mode 100644 index 0000000000..cb4a1d39cd --- /dev/null +++ b/packages/app/src/components/__tests__/DashboardLiveButton.test.tsx @@ -0,0 +1,112 @@ +import React from 'react'; +import { Granularity } from '@hyperdx/common-utils/dist/core/utils'; +import { fireEvent, screen } from '@testing-library/react'; + +import { DashboardLiveButton } from '@/components/DashboardLiveButton'; + +const OPTIONS = [ + Granularity.ThirtySecond, + Granularity.OneMinute, + Granularity.FiveMinute, +]; + +function renderButton( + props: Partial> = {}, +) { + const onToggleLive = jest.fn(); + const onRefreshIntervalChange = jest.fn(); + renderWithMantine( + , + ); + return { onToggleLive, onRefreshIntervalChange }; +} + +async function openMenu() { + fireEvent.click(screen.getByTestId('dashboard-refresh-interval-button')); + await screen.findByTestId('dashboard-refresh-interval-menu'); +} + +describe('DashboardLiveButton', () => { + it('toggles live mode', () => { + const { onToggleLive } = renderButton(); + + fireEvent.click(screen.getByTestId('dashboard-live-button')); + + expect(onToggleLive).toHaveBeenCalledTimes(1); + }); + + it('shows auto as the first, checked item', async () => { + renderButton(); + + expect( + screen.getByTestId('dashboard-refresh-interval-button'), + ).toHaveTextContent('Auto (5m)'); + + await openMenu(); + const items = screen.getAllByRole('menuitem', { hidden: true }); + expect(items.map(item => item.textContent)).toEqual([ + 'Auto (5m)', + '30s', + '1m', + '5m', + ]); + expect(items[0]).toHaveAttribute('data-checked', 'true'); + expect(items[3]).toHaveAttribute('data-checked', 'false'); + }); + + it('checks a fixed interval equal to auto without checking auto', async () => { + renderButton({ isAutoRefreshInterval: false }); + + expect( + screen.getByTestId('dashboard-refresh-interval-button'), + ).toHaveTextContent(/^5m$/); + + await openMenu(); + expect( + screen.getByRole('menuitem', { name: 'Auto (5m)', hidden: true }), + ).toHaveAttribute('data-checked', 'false'); + expect( + screen.getByRole('menuitem', { name: '5m', hidden: true }), + ).toHaveAttribute('data-checked', 'true'); + }); + + it('selects a fixed interval or auto', async () => { + const { onRefreshIntervalChange } = renderButton(); + + await openMenu(); + fireEvent.click(screen.getByRole('menuitem', { name: '1m', hidden: true })); + expect(onRefreshIntervalChange).toHaveBeenLastCalledWith( + Granularity.OneMinute, + ); + + await openMenu(); + fireEvent.click( + screen.getByRole('menuitem', { name: 'Auto (5m)', hidden: true }), + ); + expect(onRefreshIntervalChange).toHaveBeenLastCalledWith(undefined); + }); + + it('offers only auto when there are no fixed intervals', async () => { + renderButton({ + refreshInterval: Granularity.FifteenSecond, + defaultRefreshInterval: Granularity.FifteenSecond, + refreshIntervalOptions: [], + }); + + await openMenu(); + expect( + screen + .getAllByRole('menuitem', { hidden: true }) + .map(item => item.textContent), + ).toEqual(['Auto (15s)']); + }); +}); diff --git a/packages/app/src/hooks/__tests__/useDashboardRefresh.test.tsx b/packages/app/src/hooks/__tests__/useDashboardRefresh.test.tsx index 073c9a345e..615deb8288 100644 --- a/packages/app/src/hooks/__tests__/useDashboardRefresh.test.tsx +++ b/packages/app/src/hooks/__tests__/useDashboardRefresh.test.tsx @@ -3,7 +3,6 @@ import { act, renderHook } from '@testing-library/react'; import { getRefreshIntervalOptions, - resolveRefreshInterval, useDashboardRefresh, } from '@/hooks/useDashboardRefresh'; @@ -31,30 +30,8 @@ describe('getRefreshIntervalOptions', () => { ]); }); - it('keeps a default faster than 30s as the only choice', () => { - expect(getRefreshIntervalOptions(Granularity.FifteenSecond)).toEqual([ - Granularity.FifteenSecond, - ]); - }); -}); - -describe('resolveRefreshInterval', () => { - it('uses the default when nothing is selected', () => { - expect(resolveRefreshInterval(undefined, Granularity.FiveMinute)).toBe( - Granularity.FiveMinute, - ); - }); - - it('uses a faster selection', () => { - expect( - resolveRefreshInterval(Granularity.OneMinute, Granularity.FiveMinute), - ).toBe(Granularity.OneMinute); - }); - - it('falls back to the default when the selection is slower than it', () => { - expect( - resolveRefreshInterval(Granularity.OneHour, Granularity.FiveMinute), - ).toBe(Granularity.FiveMinute); + it('offers no fixed interval when the default is faster than 30s', () => { + expect(getRefreshIntervalOptions(Granularity.FifteenSecond)).toEqual([]); }); }); @@ -182,4 +159,27 @@ describe('useDashboardRefresh', () => { expect(result.current.refreshInterval).toBe(Granularity.OneMinute); expect(result.current.isAutoRefreshInterval).toBe(true); }); + + it('caps a 30d interval at the longest timer delay', () => { + const onTimeRangeSelect = jest.fn(); + const { result } = renderHook(() => + useDashboardRefresh({ + searchedTimeRange: lastHours(2 * 365 * 24), + onTimeRangeSelect, + isLive: true, + }), + ); + + expect(result.current.refreshInterval).toBe(Granularity.ThirtyDay); + + act(() => { + jest.advanceTimersByTime(60_000); + }); + expect(onTimeRangeSelect).not.toHaveBeenCalled(); + + act(() => { + jest.advanceTimersByTime(2 ** 31 - 1); + }); + expect(onTimeRangeSelect).toHaveBeenCalledTimes(1); + }); }); diff --git a/packages/app/src/hooks/useDashboardRefresh.tsx b/packages/app/src/hooks/useDashboardRefresh.tsx index 6343c2880e..d90451bacd 100644 --- a/packages/app/src/hooks/useDashboardRefresh.tsx +++ b/packages/app/src/hooks/useDashboardRefresh.tsx @@ -8,10 +8,12 @@ import { } from '@hyperdx/common-utils/dist/core/utils'; import { useDocumentVisibility } from '@mantine/hooks'; -const MIN_REFRESH_INTERVAL_SECONDS = 30; +// setInterval treats longer delays as 0 and would refresh in a tight loop. +const MAX_TIMER_DELAY_MS = 2 ** 31 - 1; -// Every interval convertDateRangeToGranularityString can return at or above the -// minimum, so the date range's default interval is always one of the choices. +// The intervals convertDateRangeToGranularityString can return, minus 15s +// (refreshes stay at 30s or slower), so a default of 30s or more is always one +// of the choices. const REFRESH_INTERVAL_CHOICES = [ Granularity.ThirtySecond, Granularity.OneMinute, @@ -29,32 +31,18 @@ const REFRESH_INTERVAL_CHOICES = [ ]; /** - * Refresh intervals a live dashboard offers, fastest first: from 30s up to the - * date range's default interval. A default faster than 30s is the only choice. + * Fixed refresh intervals a live dashboard offers, fastest first: from 30s up + * to the date range's default interval. Empty when the default is under 30s. */ export function getRefreshIntervalOptions( defaultInterval: Granularity, ): Granularity[] { const maxSeconds = convertGranularityToSeconds(defaultInterval); - if (maxSeconds < MIN_REFRESH_INTERVAL_SECONDS) { - return [defaultInterval]; - } return REFRESH_INTERVAL_CHOICES.filter( interval => convertGranularityToSeconds(interval) <= maxSeconds, ); } -/** The chosen interval while it is still offered, otherwise the default. */ -export function resolveRefreshInterval( - selected: Granularity | undefined, - defaultInterval: Granularity, -): Granularity { - return selected != null && - getRefreshIntervalOptions(defaultInterval).includes(selected) - ? selected - : defaultInterval; -} - export const useDashboardRefresh = ({ searchedTimeRange, onTimeRangeSelect, @@ -99,13 +87,15 @@ export const useDashboardRefresh = ({ () => getRefreshIntervalOptions(granularityOverride), [granularityOverride], ); - const refreshInterval = resolveRefreshInterval( - selectedRefreshInterval, - granularityOverride, - ); - const isAutoRefreshInterval = - selectedRefreshInterval == null || - !refreshIntervalOptions.includes(selectedRefreshInterval); + // A fixed interval the date range no longer offers falls back to auto but + // stays selected, so widening the range restores it. + const fixedRefreshInterval = + selectedRefreshInterval != null && + refreshIntervalOptions.includes(selectedRefreshInterval) + ? selectedRefreshInterval + : undefined; + const isAutoRefreshInterval = fixedRefreshInterval == null; + const refreshInterval = fixedRefreshInterval ?? granularityOverride; // Auto-refresh interval const intervalRef = React.useRef(null); @@ -118,7 +108,10 @@ export const useDashboardRefresh = ({ () => { refresh(); }, - convertGranularityToSeconds(refreshInterval) * 1000, + Math.min( + convertGranularityToSeconds(refreshInterval) * 1000, + MAX_TIMER_DELAY_MS, + ), ); } else { if (intervalRef.current) {