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 6 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
22 changes: 21 additions & 1 deletion src/contexts/StateContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@
// 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 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 @@ -453,15 +454,28 @@
setUiState(UI_STATE.FAST_LOADING);
workerPostReq(mainWorkerRef.current, WORKER_REQ_CODE.SET_FILTER, {
cursor: {code: CURSOR_CODE.EVENT_NUM, args: {eventNum: logEventNumRef.current ?? 1}},
logLevelFilter: filter,

Check failure on line 457 in src/contexts/StateContextProvider.tsx

View workflow job for this annotation

GitHub Actions / lint-check

Unexpected 'fixme' comment: 'FIXME: is this necessary? Can we...'
});
}, []);

// Synchronize `logEventNumRef` with `logEventNum`.
// FIXME: is this necessary? Can we integrate it with the [numEvents, logEventNum] useEffect?
Henry8192 marked this conversation as resolved.
Show resolved Hide resolved
useEffect(() => {
logEventNumRef.current = logEventNum;
}, [logEventNum]);

useEffect(() => {
timestampRef.current = timestamp;

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

Henry8192 marked this conversation as resolved.
Show resolved Hide resolved
// Synchronize `pageNumRef` with `pageNum`.
useEffect(() => {
pageNumRef.current = pageNum;
Expand Down Expand Up @@ -526,6 +540,12 @@
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
20 changes: 13 additions & 7 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,13 +205,18 @@ 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 checkAndSetHashParam = (hashParamName: keyof UrlHashParams) => {
const hashParam = hashParams.get(hashParamName);
if (null !== hashParam) {
const parsed = Number(hashParam);
urlHashParams[hashParamName] = Number.isNaN(parsed) ?
null :
parsed;
}
};

checkAndSetHashParam(HASH_PARAM_NAMES.LOG_EVENT_NUM);
checkAndSetHashParam(HASH_PARAM_NAMES.TIMESTAMP);
Comment on lines +208 to +219
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would this be more concise and avoid the nested function declaration? (haven't tested

Suggested change
const checkAndSetHashParam = (hashParamName: keyof UrlHashParams) => {
const hashParam = hashParams.get(hashParamName);
if (null !== hashParam) {
const parsed = Number(hashParam);
urlHashParams[hashParamName] = Number.isNaN(parsed) ?
null :
parsed;
}
};
checkAndSetHashParam(HASH_PARAM_NAMES.LOG_EVENT_NUM);
checkAndSetHashParam(HASH_PARAM_NAMES.TIMESTAMP);
const numberHashParamNames = [HASH_PARAM_NAMES.LOG_EVENT_NUM, HASH_PARAM_NAMES.TIMESTAMP];
for (const paramName of numberHashParamNames) {
const hashParam = hashParams.get(paramName);
if (hashParam !== null) {
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
8 changes: 8 additions & 0 deletions src/services/MainWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ onmessage = async (ev: MessageEvent<MainWorkerReqMessage>) => {
args.isCaseSensitive
);
break;

Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we still need this?

/*
case WORKER_REQ_CODE.QUERY_TIMESTAMP:
if (null === LOG_FILE_MANAGER) {
throw new Error("Log file manager hasn't been initialized");
}
LOG_FILE_MANAGER.getLogEventIndexByTimestamp(timestamp);
*/
default:
console.error(`Unexpected ev.data: ${JSON.stringify(ev.data)}`);
break;
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
27 changes: 25 additions & 2 deletions src/services/decoders/JsonlDecoder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,29 @@ class JsonlDecoder implements Decoder {
return results;
}

getLogEventIdxByTimestamp (timestamp: number): number {
let low = 0;
let high = this.#logEvents.length - 1;
let result = -1;
Copy link
Collaborator

Choose a reason for hiding this comment

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

let's see if the suggestion at https://github.com/y-scope/yscope-log-viewer/pull/152/files#r1890816021 makes sense. If not (i.e., we want to keep the return type as number rather than Nullable<number>), let's create a const for -1 so we can avoid magic numbers.


while (low <= high) {
const mid = Math.floor((low + high) / 2);

// @ts-expect-error TS2532: Object is possibly 'undefined'.
const midTimestamp = this.#logEvents[mid].timestamp.valueOf();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since @ts-expect-error disables type-checking for the whole following line, can we extract a const midLogEvent = this.#logEvents[mid]; and only disable type-checking for that?

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 +237,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 +254,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: 9 additions & 0 deletions src/typings/decoders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ interface Decoder {
endIdx: number,
useFilter: boolean
): Nullable<DecodeResult[]>;

/**
* Retrieves the last index of the log event that matches the given timestamp.
* If no such log event exists, returns -1.
*
* @param timestamp
* @return
*/
Comment on lines +102 to +108
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
/**
* Retrieves the last index of the log event that matches the given timestamp.
* If no such log event exists, returns -1.
*
* @param timestamp
* @return
*/
/**
* 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?

}

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