Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(URL): Add support to query timestamp by URL #152

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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
28 changes: 24 additions & 4 deletions src/contexts/StateContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ const updateUrlIfEventOnPage = (
// eslint-disable-next-line max-lines-per-function, max-statements
const StateContextProvider = ({children}: StateContextProviderProps) => {
const {postPopUp} = useContext(NotificationContext);
const {filePath, logEventNum} = useContext(UrlContext);
const {filePath, logEventNum, timestamp} = useContext(UrlContext);

// States
const [exportProgress, setExportProgress] =
Expand All @@ -269,6 +269,7 @@ const StateContextProvider = ({children}: StateContextProviderProps) => {
const mainWorkerRef = useRef<null|Worker>(null);
const numPagesRef = useRef<number>(numPages);
const pageNumRef = useRef<number>(pageNum);
const timestampRef = useRef(timestamp);
const uiStateRef = useRef<UI_STATE>(uiState);

const handleMainWorkerResp = useCallback((ev: MessageEvent<MainWorkerRespMessage>) => {
Expand Down Expand Up @@ -462,6 +463,11 @@ const StateContextProvider = ({children}: StateContextProviderProps) => {
logEventNumRef.current = logEventNum;
}, [logEventNum]);

// Synchronize `timestampRef` with `timestamp`.
useEffect(() => {
timestampRef.current = timestamp;
}, [timestamp]);

// Synchronize `pageNumRef` with `pageNum`.
useEffect(() => {
pageNumRef.current = pageNum;
Expand All @@ -481,13 +487,21 @@ const StateContextProvider = ({children}: StateContextProviderProps) => {
}
}, [uiState]);

// On `logEventNum` update, clamp it then switch page if necessary or simply update the URL.
useEffect(() => {
if (null === mainWorkerRef.current) {
if (null === mainWorkerRef.current || null === timestamp) {
return;
}

if (URL_HASH_PARAMS_DEFAULT.logEventNum === logEventNum) {
loadPageByCursor(mainWorkerRef.current, {
code: CURSOR_CODE.TIMESTAMP,
args: {timestamp: timestamp},
});
updateWindowUrlHashParams({timestamp: null});
}, [timestamp]);

// On `logEventNum` update, clamp it then switch page if necessary or simply update the URL.
useEffect(() => {
if (null === mainWorkerRef.current || URL_HASH_PARAMS_DEFAULT.logEventNum === logEventNum) {
return;
}

Expand Down Expand Up @@ -526,6 +540,12 @@ const StateContextProvider = ({children}: StateContextProviderProps) => {
args: {eventNum: logEventNumRef.current},
};
}
if (URL_HASH_PARAMS_DEFAULT.timestamp !== timestampRef.current) {
cursor = {
code: CURSOR_CODE.TIMESTAMP,
args: {timestamp: timestampRef.current},
};
}
loadFile(filePath, cursor);
}, [
filePath,
Expand Down
18 changes: 12 additions & 6 deletions src/contexts/UrlContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const URL_SEARCH_PARAMS_DEFAULT = Object.freeze({
*/
const URL_HASH_PARAMS_DEFAULT = Object.freeze({
[HASH_PARAM_NAMES.LOG_EVENT_NUM]: null,
[HASH_PARAM_NAMES.TIMESTAMP]: null,
});

/**
Expand Down Expand Up @@ -204,12 +205,17 @@ const getWindowUrlHashParams = () => {
);
const hashParams = new URLSearchParams(window.location.hash.substring(1));

const logEventNum = hashParams.get(HASH_PARAM_NAMES.LOG_EVENT_NUM);
if (null !== logEventNum) {
const parsed = Number(logEventNum);
urlHashParams[HASH_PARAM_NAMES.LOG_EVENT_NUM] = Number.isNaN(parsed) ?
null :
parsed;
const numberHashParamNames = [HASH_PARAM_NAMES.LOG_EVENT_NUM,
HASH_PARAM_NAMES.TIMESTAMP];

for (const paramName of numberHashParamNames) {
const hashParam = hashParams.get(paramName);
if (null !== hashParam) {
const parsed = Number(hashParam);
urlHashParams[paramName] = Number.isNaN(parsed) ?
null :
parsed;
}
}

return urlHashParams;
Expand Down
40 changes: 21 additions & 19 deletions src/services/LogFileManager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,26 +411,28 @@
*/
#getCursorData (cursor: CursorType, numActiveEvents: number): CursorData {
const {code, args} = cursor;
switch (code) {
case CURSOR_CODE.PAGE_NUM:
return getPageNumCursorData(
args.pageNum,
args.eventPositionOnPage,
numActiveEvents,
this.#pageSize,
);
case CURSOR_CODE.LAST_EVENT:
return getLastEventCursorData(numActiveEvents, this.#pageSize);
case CURSOR_CODE.EVENT_NUM:
return getEventNumCursorData(
args.eventNum,
numActiveEvents,
this.#pageSize,
this.#decoder.getFilteredLogEventMap(),
);
default:
throw new Error(`Unsupported cursor type: ${code}`);
let eventNum: number = 0;
if (CURSOR_CODE.PAGE_NUM === code) {
return getPageNumCursorData(
args.pageNum,
args.eventPositionOnPage,
numActiveEvents,
this.#pageSize,
);
} else if (CURSOR_CODE.LAST_EVENT === code) {
return getLastEventCursorData(numActiveEvents, this.#pageSize);
} else if (CURSOR_CODE.EVENT_NUM === code) {
({eventNum} = args);
} else {
eventNum = this.#decoder.getLogEventIdxByTimestamp(args.timestamp) + 1;
}

return getEventNumCursorData(
eventNum,
numActiveEvents,
this.#pageSize,
this.#decoder.getFilteredLogEventMap(),
);
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/services/decoders/ClpIrDecoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@
return this.#streamReader.getFilteredLogEventMap();
}

getLogEventIdxByTimestamp (timestamp: number): number {
return this.#streamReader.getLogEventIdxByTimestamp(timestamp);

Check failure on line 81 in src/services/decoders/ClpIrDecoder.ts

View workflow job for this annotation

GitHub Actions / lint-check

Unsafe return of an error typed value

Check failure on line 81 in src/services/decoders/ClpIrDecoder.ts

View workflow job for this annotation

GitHub Actions / lint-check

Unsafe call of an `error` type typed value
}

setLogLevelFilter (logLevelFilter: LogLevelFilter): boolean {
this.#streamReader.filterLogEvents(logLevelFilter);

Expand Down
28 changes: 26 additions & 2 deletions src/services/decoders/JsonlDecoder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import {
} from "./utils";


const INVALID_LOG_EVENT_IDX = -1;

/**
* A decoder for JSONL (JSON lines) files that contain log events. See `DecoderOptions` for
* properties that are specific to log events (compared to generic JSON records).
Expand Down Expand Up @@ -122,6 +124,28 @@ class JsonlDecoder implements Decoder {
return results;
}

getLogEventIdxByTimestamp (timestamp: number): number {
let low = 0;
let high = this.#logEvents.length - 1;
let lastFoundLogEventIdx = INVALID_LOG_EVENT_IDX;

while (low <= high) {
const mid = Math.floor((low + high) / 2);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const midTimestamp = this.#logEvents[mid]!.timestamp.valueOf();
if (midTimestamp === timestamp) {
lastFoundLogEventIdx = mid;
low = mid + 1;
} else if (midTimestamp < timestamp) {
low = mid + 1;
} else {
high = mid - 1;
}
}

return lastFoundLogEventIdx;
}

/**
* Parses each line from the data array and buffers it internally.
*
Expand Down Expand Up @@ -214,7 +238,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 +255,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
8 changes: 8 additions & 0 deletions src/typings/decoders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ interface Decoder {
endIdx: number,
useFilter: boolean
): Nullable<DecodeResult[]>;

/**
* Finds the index of the last log event that matches the specified timestamp.
*
* @param timestamp
* @return The index of the matching log event, or -1 if no match is found.
*/
getLogEventIdxByTimestamp(timestamp: number): number;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we return null no such event is found?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just wanna make it consistant between the unstructured & structured getLogEventIdxByTimestamp, which return -1 for not found, since their return type is std::ptrdiff_t

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you think it is fine to make it to null, lmk

}

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
2 changes: 2 additions & 0 deletions src/typings/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ enum SEARCH_PARAM_NAMES {

enum HASH_PARAM_NAMES {
LOG_EVENT_NUM = "logEventNum",
TIMESTAMP = "timestamp",
}

interface UrlSearchParams {
Expand All @@ -15,6 +16,7 @@ interface UrlSearchParams {

interface UrlHashParams {
logEventNum: number,
timestamp: number,
}

type UrlSearchParamUpdatesType = {
Expand Down
Loading