Skip to content
Open
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
32 changes: 32 additions & 0 deletions packages/app/src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
getMetricTableName,
mapKeyBy,
orderByStringToSortingState,
parseTimestampToMs,
sortingStateToOrderByString,
stripTrailingSlash,
useQueryHistory,
Expand Down Expand Up @@ -1123,3 +1124,34 @@ describe('mapKeyBy', () => {
expect(result.get('a')).toBe(data.at(1));
});
});

describe('parseTimestampToMs', () => {
it('returns integer ms when there are no sub-millisecond digits', () => {
const result = parseTimestampToMs('2024-01-01T00:00:01.000000000Z');
expect(result).toBe(new Date('2024-01-01T00:00:01.000Z').getTime());
});

it('preserves sub-millisecond precision as a fractional ms', () => {
const base = new Date('2024-01-01T00:00:01.000Z').getTime();
const result = parseTimestampToMs('2024-01-01T00:00:01.000500000Z');
expect(result).toBeCloseTo(base + 0.5, 4);
});

it('preserves whole-millisecond component when sub-ms digits are also present', () => {
const base = new Date('2024-01-01T00:00:01.500Z').getTime();
const result = parseTimestampToMs('2024-01-01T00:00:01.500500000Z');
expect(result).toBeCloseTo(base + 0.5, 4);
});

it('handles max sub-millisecond value (999 µs + 999 ns)', () => {
const base = new Date('2024-01-01T00:00:01.000Z').getTime();
const result = parseTimestampToMs('2024-01-01T00:00:01.000999999Z');
expect(result).toBeCloseTo(base + 0.999999, 3);
});

it('orders two timestamps within the same millisecond correctly', () => {
const earlier = parseTimestampToMs('2024-01-01T00:00:01.000400000Z');
const later = parseTimestampToMs('2024-01-01T00:00:01.000800000Z');
expect(earlier).toBeLessThan(later);
});
});
22 changes: 8 additions & 14 deletions packages/app/src/components/DBTraceWaterfallChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { ReactNode, useCallback, useEffect, useMemo, useState } from 'react';
import _, { omit } from 'lodash';
import { useForm } from 'react-hook-form';
import SqlString from 'sqlstring';
import TimestampNano from 'timestamp-nano';
import { tcFromSource } from '@hyperdx/common-utils/dist/core/metadata';
import {
ChartConfig,
Expand Down Expand Up @@ -53,6 +52,7 @@ import {
getChartColorSuccessHighlight,
getChartColorWarning,
getChartColorWarningHighlight,
parseTimestampToMs,
} from '@/utils';
import {
getHighlightedAttributesFromData,
Expand Down Expand Up @@ -515,16 +515,10 @@ export function DBTraceWaterfallChartContainer({
...traceRowsData,
...logRowsData,
];
nextRows.sort((a, b) => {
const aDate = TimestampNano.fromString(a.Timestamp);
const bDate = TimestampNano.fromString(b.Timestamp);
const secDiff = aDate.getTimeT() - bDate.getTimeT();
if (secDiff === 0) {
return aDate.getNano() - bDate.getNano();
} else {
return secDiff;
}
});
nextRows.sort(
(a, b) =>
parseTimestampToMs(a.Timestamp) - parseTimestampToMs(b.Timestamp),
);

return nextRows;
}, [traceRowsData, logRowsData]);
Expand Down Expand Up @@ -753,7 +747,7 @@ export function DBTraceWaterfallChartContainer({
// All units in ms!
const foundMinOffset =
rows?.reduce((acc, result) => {
return Math.min(acc, new Date(result.Timestamp).getTime());
return Math.min(acc, parseTimestampToMs(result.Timestamp));
}, Number.MAX_SAFE_INTEGER) ?? 0;
const minOffset =
foundMinOffset === Number.MAX_SAFE_INTEGER ? 0 : foundMinOffset;
Expand All @@ -765,7 +759,7 @@ export function DBTraceWaterfallChartContainer({
() =>
flattenedNodes.map((result, i) => {
const tookMs = (result.Duration || 0) * 1000;
const startOffset = new Date(result.Timestamp).getTime();
const startOffset = parseTimestampToMs(result.Timestamp);
const start = startOffset - minOffset;
const end = start + tookMs;

Expand Down Expand Up @@ -799,7 +793,7 @@ export function DBTraceWaterfallChartContainer({
const markers =
showSpanEvents && result.SpanEvents
? result.SpanEvents.map(spanEvent => ({
timestamp: new Date(spanEvent.Timestamp).getTime() - minOffset,
timestamp: parseTimestampToMs(spanEvent.Timestamp) - minOffset,
name: spanEvent.Name,
attributes: spanEvent.Attributes || {},
}))
Expand Down
6 changes: 6 additions & 0 deletions packages/app/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useRouter } from 'next/router';
import { formatDistanceToNowStrict } from 'date-fns';
import numbro from 'numbro';
import type { SetStateAction } from 'react';
import TimestampNano from 'timestamp-nano';
import { TableConnection } from '@hyperdx/common-utils/dist/core/metadata';
import {
NumericUnit,
Expand Down Expand Up @@ -1113,3 +1114,8 @@ export const isElementClickable = (el: HTMLElement): boolean => {
// or if the element at point is a descendant of the element passed in
return el === elementAtPoint || el.contains(elementAtPoint);
};

export function parseTimestampToMs(isoString: string): number {
const ts = TimestampNano.fromString(isoString);
return ts.toDate().getTime() + (ts.getNano() % 1_000_000) / 1_000_000;
}
Loading