Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/daily-axis-date-ticks.md
Original file line number Diff line number Diff line change
@@ -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.
53 changes: 49 additions & 4 deletions packages/app/src/HDXMultiSeriesTimeChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
71 changes: 71 additions & 0 deletions packages/app/src/__tests__/HDXMultiSeriesTimeChart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
getSelectedLineData,
getVisibleLineData,
getVisibleTooltipRows,
getXAxisTickDateFormat,
getYAxisTicks,
HARD_LINES_LIMIT,
sameActiveClickSeries,
Expand Down Expand Up @@ -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');
});
});
8 changes: 7 additions & 1 deletion packages/app/src/useFormatTime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
102 changes: 102 additions & 0 deletions packages/app/tests/e2e/features/chart-explorer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
},
);
15 changes: 15 additions & 0 deletions packages/app/tests/e2e/page-objects/ChartExplorerPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]> {
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)
*/
Expand Down
11 changes: 11 additions & 0 deletions packages/common-utils/src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/common-utils/src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
},
) => {
Expand Down
Loading