diff --git a/.changeset/daily-axis-date-ticks.md b/.changeset/daily-axis-date-ticks.md new file mode 100644 index 000000000..7dd868979 --- /dev/null +++ b/.changeset/daily-axis-date-ticks.md @@ -0,0 +1,9 @@ +--- +'@hyperdx/app': patch +'@hyperdx/common-utils': patch +--- + +fix: show the date on every x-axis tick of time charts at daily granularity or +coarser, instead of repeating the time (e.g. `00:00:00`) after the first tick. +When a daily bucket doesn't start at midnight in the display time zone, the tick +shows the date and time. diff --git a/packages/app/src/HDXMultiSeriesTimeChart.tsx b/packages/app/src/HDXMultiSeriesTimeChart.tsx index 726f1b5af..e048a243f 100644 --- a/packages/app/src/HDXMultiSeriesTimeChart.tsx +++ b/packages/app/src/HDXMultiSeriesTimeChart.tsx @@ -30,7 +30,10 @@ import { import { getTickValuesFixedDomain } from 'recharts/lib/util/scale/getNiceTickValues'; import { CurveType } from 'recharts/types/shape/Curve'; import { AxisDomain, XAxisTickContentProps } from 'recharts/types/util/types'; -import { convertGranularityToSeconds } from '@hyperdx/common-utils/dist/core/utils'; +import { + convertGranularityToSeconds, + toWallClock, +} from '@hyperdx/common-utils/dist/core/utils'; import { DisplayType, getDefaultLineInterpolation, @@ -74,7 +77,9 @@ import { MAX_TIME_CHART_SERIES, toStartOfInterval, } from './ChartUtils'; -import { useFormatTime } from './useFormatTime'; +import { getLocalTimeZone } from './timeZone'; +import { useDisplayTimeZone } from './useDisplayTimeZone'; +import { type DateFormat, useFormatTime } from './useFormatTime'; import { useContentFontSize } from './useUserPreferences'; import styles from '@styles/HDXLineChart.module.scss'; @@ -1394,6 +1399,40 @@ export function compactTickAnchor( return 'middle'; } +const SECONDS_PER_DAY = 24 * 60 * 60; + +/** + * Format for an x-axis tick label. Below daily granularity the first tick + * carries the date and the rest only the time. At daily granularity or coarser + * every tick carries the date, plus the time when the bucket does not start at + * midnight in `timeZone` (buckets are UTC-aligned, so this happens in most + * zones). An undefined `timeZone` means the browser's zone. + */ +export function getXAxisTickDateFormat({ + tickSeconds, + index, + granularity, + timeZone, +}: { + tickSeconds: number; + index: number; + granularity: string; + timeZone: string | undefined; +}): DateFormat { + if (convertGranularityToSeconds(granularity) < SECONDS_PER_DAY) { + return index === 0 ? 'normal' : 'time'; + } + const wallClock = toWallClock( + new Date(tickSeconds * 1000), + timeZone ?? getLocalTimeZone(), + ); + const isMidnight = + wallClock.getUTCHours() === 0 && + wallClock.getUTCMinutes() === 0 && + wallClock.getUTCSeconds() === 0; + return isMidnight ? 'date' : 'short'; +} + export const MemoChart = memo(function MemoChart({ graphResults, setIsClickActive, @@ -1743,13 +1782,19 @@ export const MemoChart = memo(function MemoChart({ }, [displayType, graphResults.length, containerWidth, yAxisWidth]); const formatTime = useFormatTime(); + const displayTimeZone = useDisplayTimeZone(); const xTickFormatter = useCallback( (value: number, index: number) => { return formatTime(value * 1000, { - format: index === 0 ? 'normal' : 'time', + format: getXAxisTickDateFormat({ + tickSeconds: value, + index, + granularity, + timeZone: displayTimeZone, + }), }); }, - [formatTime], + [formatTime, granularity, displayTimeZone], ); // Compact mode: anchor the first label to the start and the last to the end diff --git a/packages/app/src/__tests__/HDXMultiSeriesTimeChart.test.ts b/packages/app/src/__tests__/HDXMultiSeriesTimeChart.test.ts index 8cceb459d..23fe5c85c 100644 --- a/packages/app/src/__tests__/HDXMultiSeriesTimeChart.test.ts +++ b/packages/app/src/__tests__/HDXMultiSeriesTimeChart.test.ts @@ -23,6 +23,7 @@ import { getSelectedLineData, getVisibleLineData, getVisibleTooltipRows, + getXAxisTickDateFormat, getYAxisTicks, HARD_LINES_LIMIT, sameActiveClickSeries, @@ -1367,3 +1368,73 @@ describe('compactTickAnchor', () => { expect(compactTickAnchor(0, 1)).toBe('start'); }); }); + +describe('getXAxisTickDateFormat', () => { + const seconds = (iso: string) => new Date(iso).getTime() / 1000; + const utcMidnight = seconds('2026-10-27T00:00:00Z'); + + it('keeps the date on the first tick only below daily granularity', () => { + const format = (index: number) => + getXAxisTickDateFormat({ + tickSeconds: utcMidnight, + index, + granularity: '12 hour', + timeZone: 'Etc/UTC', + }); + expect(format(0)).toBe('normal'); + expect(format(1)).toBe('time'); + }); + + it.each(['1 day', '7 day', '30 day'])( + 'shows only the date on every midnight tick at %s granularity', + granularity => { + for (const index of [0, 1, 5]) { + expect( + getXAxisTickDateFormat({ + tickSeconds: utcMidnight, + index, + granularity, + timeZone: 'Etc/UTC', + }), + ).toBe('date'); + } + }, + ); + + it('uses midnight in the display time zone, not UTC', () => { + expect( + getXAxisTickDateFormat({ + tickSeconds: seconds('2026-10-27T04:00:00Z'), + index: 1, + granularity: '1 day', + timeZone: 'America/New_York', + }), + ).toBe('date'); + }); + + it('adds the time when a daily bucket does not start at midnight', () => { + // UTC-aligned buckets start at 8 PM in New York before the Nov 1 DST + // change and 7 PM after it. + for (const iso of ['2026-10-27T00:00:00Z', '2026-11-05T00:00:00Z']) { + expect( + getXAxisTickDateFormat({ + tickSeconds: seconds(iso), + index: 1, + granularity: '1 day', + timeZone: 'America/New_York', + }), + ).toBe('short'); + } + }); + + it('falls back to the sub-day formats for an unrecognized granularity', () => { + expect( + getXAxisTickDateFormat({ + tickSeconds: utcMidnight, + index: 1, + granularity: 'auto', + timeZone: 'Etc/UTC', + }), + ).toBe('time'); + }); +}); diff --git a/packages/app/src/useFormatTime.tsx b/packages/app/src/useFormatTime.tsx index 59019a065..85da2fa38 100644 --- a/packages/app/src/useFormatTime.tsx +++ b/packages/app/src/useFormatTime.tsx @@ -6,7 +6,13 @@ import { useUserPreferences } from './useUserPreferences'; type DateLike = number | string | Date; -type DateFormat = 'normal' | 'short' | 'withMs' | 'time' | 'withYear'; +export type DateFormat = + | 'normal' + | 'short' + | 'withMs' + | 'time' + | 'withYear' + | 'date'; const parse = (time: DateLike) => { if (time instanceof Date) { diff --git a/packages/app/tests/e2e/features/chart-explorer.spec.ts b/packages/app/tests/e2e/features/chart-explorer.spec.ts index b320271b6..5c8713282 100644 --- a/packages/app/tests/e2e/features/chart-explorer.spec.ts +++ b/packages/app/tests/e2e/features/chart-explorer.spec.ts @@ -477,3 +477,105 @@ test.describe('Chart Explorer Functionality', { tag: ['@charts'] }, () => { }, ); }); + +const DAY_MS = 24 * 60 * 60 * 1000; +// "Oct 27" +const DATE_ONLY_LABEL = /^[A-Z][a-z]{2} \d{1,2}$/; +// "Oct 26 7:00PM" +const DATE_AND_SHORT_TIME_LABEL = + /^[A-Z][a-z]{2} \d{1,2} \d{1,2}:\d{2}(AM|PM)$/; +// "Oct 26 7:00:00 PM" +const DATE_AND_TIME_LABEL = + /^[A-Z][a-z]{2} \d{1,2} \d{1,2}:\d{2}:\d{2} (AM|PM)$/; +// "7:00:00 PM" +const TIME_ONLY_LABEL = /^\d{1,2}:\d{2}:\d{2} (AM|PM)$/; + +async function runTimeChart( + chartExplorerPage: ChartExplorerPage, + rangeDays: number, + granularityLabel: string, +) { + const to = new Date(); + await chartExplorerPage.gotoWithTimeRange( + new Date(to.getTime() - rangeDays * DAY_MS), + to, + ); + await chartExplorerPage.chartEditor.waitForDataToLoad(); + await chartExplorerPage.chartEditor.setGranularity(granularityLabel); + await chartExplorerPage.chartEditor.runQuery(); +} + +test.describe( + 'Chart Explorer x-axis labels in UTC', + { tag: ['@charts'] }, + () => { + test.use({ timezoneId: 'UTC' }); + + test('shows the date on every tick at daily granularity', async ({ + page, + }) => { + const chartExplorerPage = new ChartExplorerPage(page); + await runTimeChart(chartExplorerPage, 30, '1 Day Granularity'); + + await expect + .poll( + async () => (await chartExplorerPage.getTimeChartXAxisLabels())[1], + { timeout: 15000 }, + ) + .toMatch(DATE_ONLY_LABEL); + const labels = await chartExplorerPage.getTimeChartXAxisLabels(); + expect(labels.length).toBeGreaterThan(1); + for (const label of labels) { + expect(label).toMatch(DATE_ONLY_LABEL); + } + expect(new Set(labels).size).toBe(labels.length); + }); + + test('keeps time-only ticks after the first below daily granularity', async ({ + page, + }) => { + const chartExplorerPage = new ChartExplorerPage(page); + await runTimeChart(chartExplorerPage, 2, '1 Hour Granularity'); + + await expect + .poll( + async () => (await chartExplorerPage.getTimeChartXAxisLabels())[1], + { timeout: 15000 }, + ) + .toMatch(TIME_ONLY_LABEL); + const [first, ...rest] = + await chartExplorerPage.getTimeChartXAxisLabels(); + expect(first).toMatch(DATE_AND_TIME_LABEL); + for (const label of rest) { + expect(label).toMatch(TIME_ONLY_LABEL); + } + }); + }, +); + +test.describe( + 'Chart Explorer x-axis labels outside UTC', + { tag: ['@charts'] }, + () => { + test.use({ timezoneId: 'America/New_York' }); + + test('shows the date and time when daily buckets do not start at local midnight', async ({ + page, + }) => { + const chartExplorerPage = new ChartExplorerPage(page); + await runTimeChart(chartExplorerPage, 30, '1 Day Granularity'); + + await expect + .poll( + async () => (await chartExplorerPage.getTimeChartXAxisLabels())[1], + { timeout: 15000 }, + ) + .toMatch(DATE_AND_SHORT_TIME_LABEL); + const labels = await chartExplorerPage.getTimeChartXAxisLabels(); + expect(labels.length).toBeGreaterThan(1); + for (const label of labels) { + expect(label).toMatch(DATE_AND_SHORT_TIME_LABEL); + } + }); + }, +); diff --git a/packages/app/tests/e2e/page-objects/ChartExplorerPage.ts b/packages/app/tests/e2e/page-objects/ChartExplorerPage.ts index cb2dc2cb9..d54b98655 100644 --- a/packages/app/tests/e2e/page-objects/ChartExplorerPage.ts +++ b/packages/app/tests/e2e/page-objects/ChartExplorerPage.ts @@ -24,6 +24,21 @@ export class ChartExplorerPage { await this.page.goto('/chart'); } + /** Navigate to the chart explorer with an absolute time range. */ + async gotoWithTimeRange(from: Date, to: Date) { + await this.page.goto(`/chart?from=${from.getTime()}&to=${to.getTime()}`); + } + + /** Read the time chart's x-axis tick labels, left to right. */ + async getTimeChartXAxisLabels(): Promise { + const ticks = this.getFirstChart().locator( + '.recharts-xAxis-tick-labels .recharts-cartesian-axis-tick-value', + ); + await ticks.first().waitFor({ state: 'visible', timeout: 15000 }); + const labels = await ticks.allTextContents(); + return labels.map(l => l.trim()); + } + /** * Get chart containers (recharts) */ diff --git a/packages/common-utils/src/__tests__/utils.test.ts b/packages/common-utils/src/__tests__/utils.test.ts index 8e553e04d..4e989bb79 100644 --- a/packages/common-utils/src/__tests__/utils.test.ts +++ b/packages/common-utils/src/__tests__/utils.test.ts @@ -103,6 +103,17 @@ describe('utils', () => { formatDate(date, { clock: '24h', timeZone: 'Asia/Tokyo' }), ).toEqual('Jan 1 21:00:00'); }); + + it('formats only the date, in the given time zone', () => { + const date = new Date('2021-01-01T20:00:00Z'); + expect( + formatDate(date, { + clock: '12h', + timeZone: 'Asia/Tokyo', + format: 'date', + }), + ).toEqual('Jan 2'); + }); }); describe('toWallClock / fromWallClock', () => { diff --git a/packages/common-utils/src/core/utils.ts b/packages/common-utils/src/core/utils.ts index b526c9d59..fc47205aa 100644 --- a/packages/common-utils/src/core/utils.ts +++ b/packages/common-utils/src/core/utils.ts @@ -845,6 +845,10 @@ const TIME_TOKENS = { '12h': 'h:mm:ss a', '24h': 'HH:mm:ss', }, + date: { + '12h': 'MMM d', + '24h': 'MMM d', + }, }; export const UTC_TIME_ZONE = 'Etc/UTC'; @@ -858,7 +862,7 @@ export const formatDate = ( clock = '12h', }: { timeZone?: string; - format?: 'normal' | 'short' | 'withMs' | 'time' | 'withYear'; + format?: 'normal' | 'short' | 'withMs' | 'time' | 'withYear' | 'date'; clock?: '12h' | '24h'; }, ) => {