Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
32 changes: 7 additions & 25 deletions src/services/decoders/JsonlDecoder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
LogLevelFilter,
} from "../../../typings/logs";
import {getNestedJsonValue} from "../../../utils/js";
import {upperBoundBinarySearch} from "../../../utils/math";
import YscopeFormatter from "../../formatters/YscopeFormatter";
import {parseFilterKeys} from "../utils";
import {
Expand Down Expand Up @@ -139,31 +140,12 @@ class JsonlDecoder implements Decoder {
}

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);

// `mid` is guaranteed to be within bounds since `low <= high`.
// 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;
return upperBoundBinarySearch(
(idx) => this.#logEvents[idx]?.timestamp.valueOf(),
0,
this.#logEvents.length - 1,
timestamp
);
}

/**
Expand Down
44 changes: 44 additions & 0 deletions src/utils/math.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import {Nullable} from "../typings/common";


/**
* Clamps a number to the given range. E.g. If the number is greater than the range's upper bound,
* the range's upper bound is returned.
Expand All @@ -20,7 +23,48 @@ const clamp = (num: number, min: number, max: number) => Math.min(Math.max(num,
const getChunkNum =
(itemNum: number, chunkSize: number) => Math.max(1, Math.ceil(itemNum / chunkSize));

/**
* Finds the index of the last element in a sorted collection that is less than or equal to
* `upperboundValue`. If all elements in the collection are greater than `upperboundValue`, return
* the first index of the collection (i.e., `0`).
*
* @param get
* @param lowIdx
* @param highIdx
* @param upperboundValue
* @return
*/
const upperBoundBinarySearch = <T>(
get: (index: number) => T,
lowIdx: number,
highIdx: number,
upperboundValue: T,
): Nullable<number> => {
if (highIdx < lowIdx || "undefined" === typeof (get(highIdx))) {
return null;
}

while (lowIdx <= highIdx) {
const mid = Math.floor((lowIdx + highIdx) / 2);

// `mid` is guaranteed to be within bounds since `low <= high`.
if (get(mid) <= upperboundValue) {
lowIdx = mid + 1;
} else {
highIdx = mid - 1;
}
}

// corner case: all values >= upperboundValue
if (0 > highIdx) {
return 0;
}

return highIdx;
};

export {
clamp,
getChunkNum,
upperBoundBinarySearch,
};
52 changes: 52 additions & 0 deletions test/utils/math.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint-disable @stylistic/array-element-newline */
import {
clamp,
getChunkNum,
upperBoundBinarySearch,
} from "../../src/utils/math";


Expand Down Expand Up @@ -49,3 +51,53 @@ describe("getChunkNum", () => {
expect(getChunkNum(20, 4)).toBe(5);
});
});

describe("upperBoundBinarySearch", () => {
const emptyArray: number[] = [];
const singleElementArray = [3];
const array = [-5, -3, 0, 1, 1, 8, 8, 8, 100];
test("returns null for empty collection with fake highIdx", () => {
const result = upperBoundBinarySearch((idx) => emptyArray[idx], 0, 1, 5);
expect(result).toBeNull();
});
test("returns null for empty collection with valid highIdx", () => {
const result = upperBoundBinarySearch((idx) => emptyArray[idx], 0, -1, 5);
expect(result).toBeNull();
});
test("returns the first index for upperboundValue < all elements in array", () => {
const result = upperBoundBinarySearch((idx) => array[idx], 0, array.length - 1, -10);
expect(result).toBe(0);
});
test("returns the first index for upperboundValue < element in singleElementArray", () => {
const result = upperBoundBinarySearch((idx) => singleElementArray[idx], 0, 0, -10);
expect(result).toBe(0);
});
test("returns the last index for upperboundValue > all elements in array", () => {
const result = upperBoundBinarySearch((idx) => array[idx], 0, array.length - 1, 200);
expect(result).toBe(array.length - 1);
});
test("returns the last index for upperboundValue > element in singleElementArray", () => {
const result = upperBoundBinarySearch((idx) => singleElementArray[idx], 0, 0, 200);
expect(result).toBe(0);
});
test("returns the index for upperboundValue doesn't exactly match any elements", () => {
const result = upperBoundBinarySearch((idx) => array[idx], 0, array.length - 1, 2);
expect(result).toBe(4);
});
test("returns the index for upperboundValue matches a unique element", () => {
const result = upperBoundBinarySearch((idx) => array[idx], 0, array.length - 1, 0);
expect(result).toBe(2);
});
test("returns the index for upperboundValue matches a duplicated element", () => {
const result = upperBoundBinarySearch((idx) => array[idx], 0, array.length - 1, 8);
expect(result).toBe(7);
});
test("returns the first element index", () => {
const result = upperBoundBinarySearch((idx) => array[idx], 0, array.length - 1, -5);
expect(result).toBe(0);
});
test("returns the last element index", () => {
const result = upperBoundBinarySearch((idx) => array[idx], 0, array.length - 1, 100);
expect(result).toBe(8);
});
});
Loading