Skip to content

Commit

Permalink
implementing JSONL getLogEventIdxByTimestamp, fix timestamp type to b…
Browse files Browse the repository at this point in the history
…igint
  • Loading branch information
Henry8192 committed Dec 9, 2024
1 parent 7f5cc7f commit 54ef03c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
26 changes: 24 additions & 2 deletions src/services/decoders/JsonlDecoder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;

Expand All @@ -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 [
Expand Down
9 changes: 8 additions & 1 deletion src/typings/decoders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,14 @@ interface Decoder {
useFilter: boolean
): Nullable<DecodeResult[]>;

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 {
Expand Down
2 changes: 1 addition & 1 deletion src/typings/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 54ef03c

Please sign in to comment.