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

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 4 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@mui/icons-material": "^6.4.1",
"@mui/joy": "^5.0.0-beta.51",
"axios": "^1.7.9",
"clp-ffi-js": "^0.3.4",
"clp-ffi-js": "^0.3.5",
"dayjs": "^1.11.13",
"monaco-editor": "0.50.0",
"react": "^19.0.0",
Expand Down
28 changes: 24 additions & 4 deletions src/contexts/StateContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,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 @@ -273,6 +273,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 @@ -460,6 +461,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 @@ -479,13 +485,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 @@ -524,6 +538,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
44 changes: 25 additions & 19 deletions src/services/LogFileManager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,26 +433,32 @@ class LogFileManager {
*/
#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 {
const eventIdx = this.#decoder.findNearestLogEventByTimestamp(args.timestamp);
if (null !== eventIdx) {
eventNum = eventIdx + 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 @@ class ClpIrDecoder implements Decoder {
return this.#streamReader.getFilteredLogEventMap();
}

findNearestLogEventByTimestamp (timestamp: number): Nullable<number> {
return this.#streamReader.findNearestLogEventByTimestamp(BigInt(timestamp));
}

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

Expand Down
30 changes: 28 additions & 2 deletions src/services/decoders/JsonlDecoder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,32 @@ class JsonlDecoder implements Decoder {
return results;
}

findNearestLogEventByTimestamp (timestamp: number): Nullable<number> {
let low = 0;
let high = this.#logEvents.length - 1;
if (high < low) {
return null;
}

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) {
low = mid + 1;
} else {
high = mid - 1;
}
}

// corner case: all log events have timestamps >= timestamp
if (0 > high) {
return 0;
}

return high;
}

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

/**
* Finds the log event, L, where if we assume:
*
* - the collection of log events is sorted in chronological order;
* - and we insert a marker log event, M, with timestamp `timestamp` into the collection (if log
* events with timestamp `timestamp` already exist in the collection, M should be inserted
* after them).
*
* L is the event just before M, if M is not the first event in the collection; otherwise L is
* the event just after M.
*
* NOTE: If the collection of log events isn't in chronological order, this method has undefined
* behaviour.
*
* @param timestamp
* @return The index of the log event L.
*/
findNearestLogEventByTimestamp(timestamp: number): Nullable<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
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