From 54ef03cf13b54844844ae835c2dadfa2066464d6 Mon Sep 17 00:00:00 2001 From: Henry8192 <50559854+Henry8192@users.noreply.github.com> Date: Mon, 9 Dec 2024 13:47:26 -0500 Subject: [PATCH] implementing JSONL getLogEventIdxByTimestamp, fix timestamp type to bigint --- src/services/decoders/JsonlDecoder/index.ts | 26 +++++++++++++++++++-- src/typings/decoders.ts | 9 ++++++- src/typings/logs.ts | 2 +- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/services/decoders/JsonlDecoder/index.ts b/src/services/decoders/JsonlDecoder/index.ts index 1ff3f4d7..5ea0f626 100644 --- a/src/services/decoders/JsonlDecoder/index.ts +++ b/src/services/decoders/JsonlDecoder/index.ts @@ -122,6 +122,28 @@ class JsonlDecoder implements Decoder { return results; } + getLogEventIdxByTimestamp (timestamp: bigint): number { + let low = 0; + let high = this.#logEvents.length - 1; + let result = -1; + + while (low <= high) { + const mid = Math.floor((low + high) / 2); + const midTimestamp = BigInt(this.#logEvents[mid].timestamp.valueOf()); + + if (midTimestamp === timestamp) { + result = mid; + low = mid + 1; + } else if (midTimestamp < timestamp) { + low = mid + 1; + } else { + high = mid - 1; + } + } + + return result; + } + /** * Parses each line from the data array and buffers it internally. * @@ -214,7 +236,7 @@ class JsonlDecoder implements Decoder { * @return The decoded log event. */ #decodeLogEvent = (logEventIdx: number): DecodeResult => { - let timestamp: number; + let timestamp: bigint; let message: string; let logLevel: LOG_LEVEL; @@ -231,7 +253,7 @@ class JsonlDecoder implements Decoder { const logEvent = this.#logEvents[logEventIdx] as LogEvent; logLevel = logEvent.level; message = this.#formatter.formatLogEvent(logEvent); - timestamp = logEvent.timestamp.valueOf(); + timestamp = BigInt(logEvent.timestamp.valueOf()); } return [ diff --git a/src/typings/decoders.ts b/src/typings/decoders.ts index 392d9c62..b8c7afd5 100644 --- a/src/typings/decoders.ts +++ b/src/typings/decoders.ts @@ -99,7 +99,14 @@ interface Decoder { useFilter: boolean ): Nullable; - getLogEventIdxByTimestamp(timestamp: number): number; + /** + * Retrieves the last index of the log event that matches the given timestamp. + * If no such log event exists, returns -1. + * + * @param timestamp + * @return + */ + getLogEventIdxByTimestamp(timestamp: bigint): number; } export type { diff --git a/src/typings/logs.ts b/src/typings/logs.ts index e5cd333f..93d8c2b7 100644 --- a/src/typings/logs.ts +++ b/src/typings/logs.ts @@ -43,7 +43,7 @@ const LOG_LEVEL_VALUES = Object.freeze( const MAX_LOG_LEVEL = Math.max(...LOG_LEVEL_VALUES); -const INVALID_TIMESTAMP_VALUE = 0; +const INVALID_TIMESTAMP_VALUE = 0n; export type {