Skip to content

Commit

Permalink
new interface:
Browse files Browse the repository at this point in the history
  • Loading branch information
davemarco committed Sep 18, 2024
1 parent baa0a32 commit b70bdb6
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 27 deletions.
30 changes: 26 additions & 4 deletions new-log-viewer/src/services/LogFileManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class LogFileManager {
}

this.#numEvents = decoder.getEstimatedNumEvents();
this.#computePageBoundaries();
this.#computeUnfilteredPageBoundaries();
console.log(
`Found ${this.#numEvents} log events.`,
);
Expand Down Expand Up @@ -183,7 +183,7 @@ class LogFileManager {
console.debug(`loadPage: cursor=${JSON.stringify(cursor)}`);

const {beginLogEventNum, endLogEventNum} = this.#getCursorRange(cursor);
const results = this.#decoder.decode(beginLogEventNum - 1, endLogEventNum);
const results = this.#decoder.decodeFilteredRange(beginLogEventNum - 1, endLogEventNum);
if (null === results) {
throw new Error("Error occurred during decoding. " +
`beginLogEventNum=${beginLogEventNum}, ` +
Expand Down Expand Up @@ -226,22 +226,23 @@ class LogFileManager {
throw new Error("Error changing log level filter");
}

this.#computePageBoundaries();
logLevelFilter ? this.#computeFilteredPageBoundaries() : this.#computeUnfilteredPageBoundaries()

Check warning on line 229 in new-log-viewer/src/services/LogFileManager.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

new-log-viewer/src/services/LogFileManager.ts#L229

[@stylistic/js/max-len] This line has a length of 104. Maximum allowed is 100.

Check failure on line 229 in new-log-viewer/src/services/LogFileManager.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

new-log-viewer/src/services/LogFileManager.ts#L229

[no-unused-expressions] Expected an assignment or function call and instead saw an expression.

Check failure on line 229 in new-log-viewer/src/services/LogFileManager.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

new-log-viewer/src/services/LogFileManager.ts#L229

[@stylistic/js/multiline-ternary] Expected newline between test and consequent of ternary expression.

Check failure on line 229 in new-log-viewer/src/services/LogFileManager.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

new-log-viewer/src/services/LogFileManager.ts#L229

[@stylistic/js/multiline-ternary] Expected newline between consequent and alternate of ternary expression.
}

Check failure on line 230 in new-log-viewer/src/services/LogFileManager.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

new-log-viewer/src/services/LogFileManager.ts#L229-L230

[@stylistic/js/semi] Missing semicolon.

/**
* Computes logEventNum page boundaries based on current filter. Sets two arrays of page
* boundaries. The first array contains the number of first log event on each page. The
* second array contains the number last log event on each page.
*/
#computePageBoundaries () {
#computeFilteredPageBoundaries () {
this.#firstLogEventNumPerPage.length = 0;
this.#lastLogEventNumPerPage.length = 0;

const filteredLogEventsIndices: number[] = this.#decoder.getFilteredLogEvents();
this.#numFilteredEvents = filteredLogEventsIndices.length;

for (let i = 0; i < this.#numFilteredEvents; i += this.#pageSize) {

const firstLogEventOnPageIdx: number = filteredLogEventsIndices[i] as number;

Check failure on line 246 in new-log-viewer/src/services/LogFileManager.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

new-log-viewer/src/services/LogFileManager.ts#L244-L246

[@stylistic/js/padded-blocks] Block must not be padded by blank lines.
this.#firstLogEventNumPerPage.push(1 + firstLogEventOnPageIdx);

Expand All @@ -258,6 +259,27 @@ class LogFileManager {
}
}

#computeUnfilteredPageBoundaries () {
this.#firstLogEventNumPerPage.length = 0;
this.#lastLogEventNumPerPage.length = 0;

this.#numFilteredEvents = this.#numEvents;

for (let i = 0; i < this.#numFilteredEvents; i += this.#pageSize) {
this.#firstLogEventNumPerPage.push(1 + i);

// Need to minus one from page size to get correct index into filtered log events.
let lastPageIdx: number = i + this.#pageSize - 1;

// Guard to prevent indexing out of array on last page.
if (lastPageIdx >= this.#numFilteredEvents) {
lastPageIdx = this.#numFilteredEvents - 1;
}

this.#lastLogEventNumPerPage.push(1 + lastPageIdx);
}
}

/**
* Gets the range of log event numbers for the page containing the given cursor.
*
Expand Down
53 changes: 32 additions & 21 deletions new-log-viewer/src/services/decoders/JsonlDecoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
LogLevelFilter,
} from "../../typings/logs";
import LogbackFormatter from "../formatters/LogbackFormatter";
import { number } from "prop-types";

Check warning on line 23 in new-log-viewer/src/services/decoders/JsonlDecoder.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

new-log-viewer/src/services/decoders/JsonlDecoder.ts#L1-L23

[simple-import-sort/imports] Run autofix to sort these imports!

Check failure on line 23 in new-log-viewer/src/services/decoders/JsonlDecoder.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

new-log-viewer/src/services/decoders/JsonlDecoder.ts#L23

[@stylistic/js/object-curly-spacing] There should be no space after '{'.

Check failure on line 23 in new-log-viewer/src/services/decoders/JsonlDecoder.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

new-log-viewer/src/services/decoders/JsonlDecoder.ts#L23

[@typescript-eslint/no-unused-vars] 'number' is defined but never used.

Check failure on line 23 in new-log-viewer/src/services/decoders/JsonlDecoder.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

new-log-viewer/src/services/decoders/JsonlDecoder.ts#L23

[@stylistic/js/object-curly-spacing] There should be no space before '}'.


/**
Expand Down Expand Up @@ -53,6 +54,8 @@ class JsonlDecoder implements Decoder {

#logEvents: JsonLogEvent[] = [];

#isFiltered: boolean;

#filteredLogIndices: number[];

#invalidLogEventIdxToRawLine: Map<number, string> = new Map();
Expand All @@ -66,11 +69,11 @@ class JsonlDecoder implements Decoder {
*/
constructor (dataArray: Uint8Array, decoderOptions: JsonlDecoderOptions) {
this.#filteredLogIndices = [];

this.#logLevelKey = decoderOptions.logLevelKey;
this.#timestampKey = decoderOptions.timestampKey;
this.#formatter = new LogbackFormatter(decoderOptions);
this.#dataArray = dataArray;
this.#isFiltered = false;
}

getEstimatedNumEvents (): number {
Expand All @@ -87,7 +90,6 @@ class JsonlDecoder implements Decoder {
}

this.#deserialize();
this.#filterLogs(null);

const numInvalidEvents = Array.from(this.#invalidLogEventIdxToRawLine.keys()).length;

Expand All @@ -99,37 +101,48 @@ class JsonlDecoder implements Decoder {

changeLogLevelFilter (logLevelFilter: LogLevelFilter): boolean {
this.#filterLogs(logLevelFilter);

this.#isFiltered = logLevelFilter ? true : false;

Check failure on line 104 in new-log-viewer/src/services/decoders/JsonlDecoder.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

new-log-viewer/src/services/decoders/JsonlDecoder.ts#L104

[no-unneeded-ternary] Unnecessary use of boolean literals in conditional expression.

Check failure on line 104 in new-log-viewer/src/services/decoders/JsonlDecoder.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

new-log-viewer/src/services/decoders/JsonlDecoder.ts#L104

[@stylistic/js/multiline-ternary] Expected newline between test and consequent of ternary expression.

Check failure on line 104 in new-log-viewer/src/services/decoders/JsonlDecoder.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

new-log-viewer/src/services/decoders/JsonlDecoder.ts#L104

[@stylistic/js/multiline-ternary] Expected newline between consequent and alternate of ternary expression.
return true;

Check warning on line 105 in new-log-viewer/src/services/decoders/JsonlDecoder.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

new-log-viewer/src/services/decoders/JsonlDecoder.ts#L105

[@stylistic/js/padding-line-between-statements] Expected blank line before this statement.
}

decode (beginIdx: number, endIdx: number): Nullable<DecodeResultType[]> {
if (0 > beginIdx || this.#filteredLogIndices.length < endIdx) {
decodeRange (beginIdx: number, endIdx: number): Nullable<DecodeResultType[]> {
return this.#decodeAnyRange(beginIdx,endIdx,false)

Check failure on line 109 in new-log-viewer/src/services/decoders/JsonlDecoder.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

new-log-viewer/src/services/decoders/JsonlDecoder.ts#L109

[@stylistic/js/comma-spacing] A space is required after ','.

Check failure on line 109 in new-log-viewer/src/services/decoders/JsonlDecoder.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

new-log-viewer/src/services/decoders/JsonlDecoder.ts#L109

[@stylistic/js/comma-spacing] A space is required after ','.
}

Check failure on line 110 in new-log-viewer/src/services/decoders/JsonlDecoder.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

new-log-viewer/src/services/decoders/JsonlDecoder.ts#L109-L110

[@stylistic/js/semi] Missing semicolon.

decodeFilteredRange (beginIdx: number, endIdx: number): Nullable<DecodeResultType[]> {
return this.#decodeAnyRange(beginIdx,endIdx,this.#isFiltered)

Check failure on line 113 in new-log-viewer/src/services/decoders/JsonlDecoder.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

new-log-viewer/src/services/decoders/JsonlDecoder.ts#L113

[@stylistic/js/comma-spacing] A space is required after ','.

Check failure on line 113 in new-log-viewer/src/services/decoders/JsonlDecoder.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

new-log-viewer/src/services/decoders/JsonlDecoder.ts#L113

[@stylistic/js/comma-spacing] A space is required after ','.
}

Check failure on line 114 in new-log-viewer/src/services/decoders/JsonlDecoder.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

new-log-viewer/src/services/decoders/JsonlDecoder.ts#L113-L114

[@stylistic/js/semi] Missing semicolon.

#decodeAnyRange (beginIdx: number, endIdx: number, useFilter: boolean): Nullable<DecodeResultType[]> {

Check warning on line 116 in new-log-viewer/src/services/decoders/JsonlDecoder.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

new-log-viewer/src/services/decoders/JsonlDecoder.ts#L116

[@stylistic/js/max-len] This line has a length of 106. Maximum allowed is 100.
let length: number = useFilter ? this.#filteredLogIndices.length : this.#logEvents.length

Check failure on line 117 in new-log-viewer/src/services/decoders/JsonlDecoder.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

new-log-viewer/src/services/decoders/JsonlDecoder.ts#L117

[@stylistic/js/multiline-ternary] Expected newline between test and consequent of ternary expression.

Check failure on line 117 in new-log-viewer/src/services/decoders/JsonlDecoder.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

new-log-viewer/src/services/decoders/JsonlDecoder.ts#L117

[@stylistic/js/multiline-ternary] Expected newline between consequent and alternate of ternary expression.

Check failure on line 117 in new-log-viewer/src/services/decoders/JsonlDecoder.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

new-log-viewer/src/services/decoders/JsonlDecoder.ts#L117

[prefer-const] 'length' is never reassigned. Use 'const' instead.

Check failure on line 118 in new-log-viewer/src/services/decoders/JsonlDecoder.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

new-log-viewer/src/services/decoders/JsonlDecoder.ts#L117-L118

[@stylistic/js/semi] Missing semicolon.
if (0 > beginIdx || length < endIdx) {
return null;
}

// eslint-disable-next-line no-warning-comments
// TODO We could probably optimize this to avoid checking `#invalidLogEventIdxToRawLine` on
// every iteration.
const results: DecodeResultType[] = [];
for (let filteredLogEventIdx = beginIdx;
filteredLogEventIdx < endIdx;
filteredLogEventIdx++) {
for (let logEventIdx = beginIdx; logEventIdx < endIdx; logEventIdx++) {
let timestamp: number;
let message: string;
let logLevel: LOG_LEVEL;

// Explicit cast since typescript thinks `#filteredLogIndices[filteredLogEventIdx]` can
// be undefined, but it shouldn't be since we performed a bounds check at the beginning
// of the method.
const logEventIdx = this.#filteredLogIndices[filteredLogEventIdx] as number;
let logLevel: LOG_LEVEL;
let message: string;
let timestamp: number;
if (this.#invalidLogEventIdxToRawLine.has(logEventIdx)) {
logLevel = LOG_LEVEL.NONE;
message = `${this.#invalidLogEventIdxToRawLine.get(logEventIdx)}\n`;
let filteredIdx: number = useFilter ?

Check failure on line 134 in new-log-viewer/src/services/decoders/JsonlDecoder.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

new-log-viewer/src/services/decoders/JsonlDecoder.ts#L134

[prefer-const] 'filteredIdx' is never reassigned. Use 'const' instead.
this.#filteredLogIndices[logEventIdx] as number :
logEventIdx

Check failure on line 137 in new-log-viewer/src/services/decoders/JsonlDecoder.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

new-log-viewer/src/services/decoders/JsonlDecoder.ts#L136-L137

[@stylistic/js/semi] Missing semicolon.
if (this.#invalidLogEventIdxToRawLine.has(filteredIdx)) {
timestamp = INVALID_TIMESTAMP_VALUE;
message = `${this.#invalidLogEventIdxToRawLine.get(filteredIdx)}\n`;
logLevel = LOG_LEVEL.NONE;
} else {
// Explicit cast since typescript thinks `#logEvents[logEventIdx]` can be undefined,
// but it shouldn't be since the index comes from a class-internal filter.
const logEvent: JsonLogEvent = this.#logEvents[logEventIdx] as JsonLogEvent;
const logEvent: JsonLogEvent = this.#logEvents[filteredIdx] as JsonLogEvent;
logLevel = logEvent.level;
message = this.#formatter.formatLogEvent(logEvent);
timestamp = logEvent.timestamp.valueOf();
Expand All @@ -139,7 +152,7 @@ class JsonlDecoder implements Decoder {
message,
timestamp,
logLevel,
logEventIdx + 1,
filteredIdx + 1,
]);
}

Expand Down Expand Up @@ -204,9 +217,7 @@ class JsonlDecoder implements Decoder {
#filterLogs (logLevelFilter: LogLevelFilter) {
this.#filteredLogIndices.length = 0;

if (!logLevelFilter || 0 === logLevelFilter.length) {
this.#filteredLogIndices = createIndicesArray(this.#logEvents.length);

if (!logLevelFilter) {
return;
}

Expand Down
15 changes: 13 additions & 2 deletions new-log-viewer/src/typings/decoders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,25 @@ interface Decoder {
buildIdx(beginIdx: number, endIdx: number): Nullable<LogEventCount>;

/**
* Decodes the log events in the range `[beginIdx, endIdx)`.
* Decodes filtered log events (i.e. only includes events are included in current filter) in
* the range `[beginIdx, endIdx)`.
*
* @param beginIdx
* @param endIdx
* @return The decoded log events on success or null if any log event in the range doesn't exist
* (e.g., the range exceeds the number of log events in the file).
*/
decode(beginIdx: number, endIdx: number): Nullable<DecodeResultType[]>;
decodeFilteredRange(beginIdx: number, endIdx: number): Nullable<DecodeResultType[]>;

/**
* Decodes the all log events in the range `[beginIdx, endIdx)` irrespective of filter applied.
*
* @param beginIdx
* @param endIdx
* @return The decoded log events on success or null if any log event in the range doesn't exist
* (e.g., the range exceeds the number of log events in the file).
*/
decodeRange(beginIdx: number, endIdx: number): Nullable<DecodeResultType[]>;
}

/**
Expand Down

0 comments on commit b70bdb6

Please sign in to comment.