Skip to content
Merged
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
31 changes: 6 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,11 @@ 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(
this.#logEvents,
(logEvent) => logEvent.timestamp.valueOf(),
timestamp
);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/typings/decoders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ interface Decoder {
* behaviour.
*
* @param timestamp
* @return The index of the log event L.
* @return The index of the log event L, or null if there are no log events.
*/
findNearestLogEventByTimestamp(timestamp: number): Nullable<number>;
}
Expand Down
46 changes: 46 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,50 @@ 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 last element in a sorted collection, `collection`, that is less than or equal to
* `upperBoundValue`. If all elements in the collection are greater than `upperBoundValue`, returns
* `0` (index of the first element in the collection).
*
* @param collection The collection sorted in ascending order.
* @param get Function to access an element (or its property) in the collection.
* @param upperBoundValue
* @return The index of the last element less than or equal to `upperBoundValue`, `0` if all
* elements are greater, or `null` if the collection is empty.
*/
const upperBoundBinarySearch = <T, U>(
collection: U[],
get: (arrayElement: U) => T,
upperBoundValue: T,
): Nullable<number> => {
if (0 === collection.length) {
return null;
}
let lowIdx = 0;
let highIdx = collection.length - 1;

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

// `mid` is guaranteed to be within bounds since `lowIdx <= highIdx`.
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
if (get(collection[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,
};
58 changes: 58 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,59 @@ 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 if collection is empty", () => {
const result = upperBoundBinarySearch(emptyArray, (arrayElement) => arrayElement, 5);
expect(result).toBeNull();
});
test("returns the first index if upperBoundValue < all elements in array", () => {
const result = upperBoundBinarySearch(array, (arrayElement) => arrayElement, -10);
expect(result).toBe(0);
});
test("returns the first index if upperBoundValue < element in singleElementArray", () => {
const result = upperBoundBinarySearch(
singleElementArray,
(arrayElement) => arrayElement,
-10
);

expect(result).toBe(0);
});
test("returns the last index if upperBoundValue > all elements in array", () => {
const result = upperBoundBinarySearch(array, (arrayElement) => arrayElement, 200);
expect(result).toBe(array.length - 1);
});
test("returns the last index if upperBoundValue > element in singleElementArray", () => {
const result = upperBoundBinarySearch(
singleElementArray,
(arrayElement) => arrayElement,
200
);

expect(result).toBe(0);
});
test("returns the correct index if upperBoundValue doesn't match any elements", () => {
const result = upperBoundBinarySearch(array, (arrayElement) => arrayElement, 2);
expect(result).toBe(4);
});
test("returns the correct index if upperBoundValue matches a unique element", () => {
const result = upperBoundBinarySearch(array, (arrayElement) => arrayElement, 0);
expect(result).toBe(2);
});
test("returns the last matched index if upperBoundValue matches a duplicated element", () => {
const result = upperBoundBinarySearch(array, (arrayElement) => arrayElement, 8);
expect(result).toBe(7);
});
test("returns index 0 if upperBoundValue matches the first element", () => {
const result = upperBoundBinarySearch(array, (arrayElement) => arrayElement, -5);
expect(result).toBe(0);
});
test("returns the last index if upperBoundValue matches the last element", () => {
const result = upperBoundBinarySearch(array, (arrayElement) => arrayElement, 100);
expect(result).toBe(array.length - 1);
});
});