fix: better support for custom timestamps

This commit is contained in:
Carl-Gerhard Lindesvärd
2025-11-17 13:56:55 +01:00
parent dcc0d0df18
commit 10726bf373
2 changed files with 12 additions and 3 deletions

View File

@@ -68,16 +68,25 @@ export function getTimestamp(
const clientTimestamp = new Date(userDefinedTimestamp); const clientTimestamp = new Date(userDefinedTimestamp);
const clientTimestampNumber = clientTimestamp.getTime(); const clientTimestampNumber = clientTimestamp.getTime();
// Constants for time validation
const ONE_MINUTE_MS = 60 * 1000;
const FIFTEEN_MINUTES_MS = 15 * ONE_MINUTE_MS;
// Use safeTimestamp if invalid or more than 1 minute in the future
if ( if (
Number.isNaN(clientTimestampNumber) || Number.isNaN(clientTimestampNumber) ||
clientTimestampNumber > safeTimestamp clientTimestampNumber > safeTimestamp + ONE_MINUTE_MS
) { ) {
return { timestamp: safeTimestamp, isTimestampFromThePast: false }; return { timestamp: safeTimestamp, isTimestampFromThePast: false };
} }
// isTimestampFromThePast is true only if timestamp is older than 1 hour
const isTimestampFromThePast =
clientTimestampNumber < safeTimestamp - FIFTEEN_MINUTES_MS;
return { return {
timestamp: clientTimestampNumber, timestamp: clientTimestampNumber,
isTimestampFromThePast: true, isTimestampFromThePast,
}; };
} }

View File

@@ -21,7 +21,7 @@ import type { EventsQueuePayloadIncomingEvent } from '@openpanel/queue';
import * as R from 'ramda'; import * as R from 'ramda';
import { v4 as uuid } from 'uuid'; import { v4 as uuid } from 'uuid';
const GLOBAL_PROPERTIES = ['__path', '__referrer']; const GLOBAL_PROPERTIES = ['__path', '__referrer', '__timestamp'];
// This function will merge two objects. // This function will merge two objects.
// First it will strip '' and undefined/null from B // First it will strip '' and undefined/null from B