diff --git a/.changeset/dashboard-live-refresh-interval.md b/.changeset/dashboard-live-refresh-interval.md new file mode 100644 index 000000000..bdce514a3 --- /dev/null +++ b/.changeset/dashboard-live-refresh-interval.md @@ -0,0 +1,6 @@ +--- +'@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 429612191..aca473685 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,24 @@ function DBDashboardPage({ isRefreshEnabled, manualRefreshCooloff, refresh, + refreshInterval, + refreshIntervalOptions, + isAutoRefreshInterval, } = useDashboardRefresh({ searchedTimeRange, onTimeRangeSelect, isLive: isLive || isKioskMode, + selectedRefreshInterval, }); + const onRefreshIntervalChange = useCallback( + (interval: Granularity | undefined) => { + setSelectedRefreshInterval(interval); + setIsLive(true); + }, + [], + ); + const onSubmit = useCallback(() => { onSearch(displayedTimeInputValue); handleSubmit(data => { @@ -3290,25 +3306,15 @@ function DBDashboardPage({ }} /> - - - + setIsLive(prev => !prev)} + refreshInterval={refreshInterval} + isAutoRefreshInterval={isAutoRefreshInterval} + defaultRefreshInterval={granularityOverride} + refreshIntervalOptions={refreshIntervalOptions} + onRefreshIntervalChange={onRefreshIntervalChange} + /> void; +}) { + return ( + : } + > + {label} + + ); +} + +export function DashboardLiveButton({ + isLive, + onToggleLive, + refreshInterval, + isAutoRefreshInterval, + defaultRefreshInterval, + refreshIntervalOptions, + onRefreshIntervalChange, +}: { + isLive: boolean; + onToggleLive: () => void; + refreshInterval: Granularity; + isAutoRefreshInterval: boolean; + defaultRefreshInterval: Granularity; + refreshIntervalOptions: Granularity[]; + /** 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 ( + + + + + + + + + + Refresh every + onRefreshIntervalChange(undefined)} + /> + {refreshIntervalOptions.length > 0 && } + {refreshIntervalOptions.map(interval => ( + onRefreshIntervalChange(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 000000000..cb4a1d39c --- /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 new file mode 100644 index 000000000..615deb828 --- /dev/null +++ b/packages/app/src/hooks/__tests__/useDashboardRefresh.test.tsx @@ -0,0 +1,185 @@ +import { Granularity } from '@hyperdx/common-utils/dist/core/utils'; +import { act, renderHook } from '@testing-library/react'; + +import { + getRefreshIntervalOptions, + 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('offers no fixed interval when the default is faster than 30s', () => { + expect(getRefreshIntervalOptions(Granularity.FifteenSecond)).toEqual([]); + }); +}); + +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(); + }); + + 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); + }); + + 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 04e17d505..d90451bac 100644 --- a/packages/app/src/hooks/useDashboardRefresh.tsx +++ b/packages/app/src/hooks/useDashboardRefresh.tsx @@ -4,17 +4,59 @@ import React from 'react'; import { convertDateRangeToGranularityString, convertGranularityToSeconds, + Granularity, } from '@hyperdx/common-utils/dist/core/utils'; import { useDocumentVisibility } from '@mantine/hooks'; +// setInterval treats longer delays as 0 and would refresh in a tight loop. +const MAX_TIMER_DELAY_MS = 2 ** 31 - 1; + +// 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, + Granularity.FiveMinute, + Granularity.FifteenMinute, + Granularity.ThirtyMinute, + Granularity.OneHour, + Granularity.TwoHour, + Granularity.SixHour, + Granularity.TwelveHour, + Granularity.OneDay, + Granularity.TwoDay, + Granularity.SevenDay, + Granularity.ThirtyDay, +]; + +/** + * 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); + return REFRESH_INTERVAL_CHOICES.filter( + interval => convertGranularityToSeconds(interval) <= maxSeconds, + ); +} + export const useDashboardRefresh = ({ searchedTimeRange, onTimeRangeSelect, isLive, + selectedRefreshInterval, }: { onTimeRangeSelect: (start: Date, end: Date) => void; searchedTimeRange: [Date, Date]; isLive: boolean; + /** + * 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); @@ -41,6 +83,20 @@ export const useDashboardRefresh = ({ const granularityOverride = convertDateRangeToGranularityString(searchedTimeRange); + const refreshIntervalOptions = React.useMemo( + () => getRefreshIntervalOptions(granularityOverride), + [granularityOverride], + ); + // 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); React.useEffect(() => { @@ -52,7 +108,10 @@ export const useDashboardRefresh = ({ () => { refresh(); }, - convertGranularityToSeconds(granularityOverride) * 1000, + Math.min( + convertGranularityToSeconds(refreshInterval) * 1000, + MAX_TIMER_DELAY_MS, + ), ); } else { if (intervalRef.current) { @@ -64,12 +123,15 @@ export const useDashboardRefresh = ({ window.clearInterval(intervalRef.current); } }; - }, [isRefreshEnabled, granularityOverride, refresh]); + }, [isRefreshEnabled, refreshInterval, refresh]); return { granularityOverride, isRefreshEnabled, manualRefreshCooloff, refresh, + refreshInterval, + refreshIntervalOptions, + isAutoRefreshInterval, }; };