-
Notifications
You must be signed in to change notification settings - Fork 21
LOG-9015: fix: add fallback to numeric comparator to respect natural sorting #357
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
Open
jgbernalp
wants to merge
1
commit into
openshift:main
Choose a base branch
from
jgbernalp:fix-numeric-comparator-toaccount-for-natural-sorting
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import { TestIds } from '../../../src/test-ids'; | ||
|
|
||
| const LOGS_PAGE_URL = '/monitoring/logs'; | ||
| const QUERY_RANGE_STREAMS_URL_MATCH = | ||
| '/api/proxy/plugin/logging-view-plugin/backend/api/logs/v1/application/loki/api/v1/query_range?query=%7B*'; | ||
| const QUERY_RANGE_MATRIX_URL_MATCH = | ||
| '/api/proxy/plugin/logging-view-plugin/backend/api/logs/v1/application/loki/api/v1/query_range?query=sum*'; | ||
|
|
||
| const sameTimestampStreamsResponse = () => { | ||
| const msTimestamp = BigInt(Date.now()); | ||
| const nanosTimestamp = msTimestamp * 1000000n; | ||
| const nanosString = String(nanosTimestamp); | ||
|
|
||
| return { | ||
| status: 'success', | ||
| data: { | ||
| resultType: 'streams', | ||
| result: [ | ||
| { | ||
| stream: { | ||
| filename: '/var/log/out.log', | ||
| job: 'varlogs', | ||
| level: 'info', | ||
| observedTimestamp: String(nanosTimestamp + 200n), | ||
| }, | ||
| values: [[nanosString, 'log-line-B']], | ||
| }, | ||
| { | ||
| stream: { | ||
| filename: '/var/log/out.log', | ||
| job: 'varlogs', | ||
| level: 'info', | ||
| observedTimestamp: String(nanosTimestamp + 300n), | ||
| }, | ||
| values: [[nanosString, 'log-line-C']], | ||
| }, | ||
| { | ||
| stream: { | ||
| filename: '/var/log/out.log', | ||
| job: 'varlogs', | ||
| level: 'info', | ||
| observedTimestamp: String(nanosTimestamp + 100n), | ||
| }, | ||
| values: [[nanosString, 'log-line-A']], | ||
| }, | ||
| ], | ||
| stats: { | ||
| summary: { | ||
| bytesProcessedPerSecond: 0, | ||
| linesProcessedPerSecond: 0, | ||
| totalBytesProcessed: 0, | ||
| totalLinesProcessed: 0, | ||
| execTime: 0, | ||
| queueTime: 0, | ||
| subqueries: 0, | ||
| }, | ||
| querier: { | ||
| store: { | ||
| totalChunksRef: 0, | ||
| totalChunksDownloaded: 0, | ||
| chunksDownloadTime: 0, | ||
| chunk: { | ||
| headChunkBytes: 0, | ||
| headChunkLines: 0, | ||
| decompressedBytes: 0, | ||
| decompressedLines: 0, | ||
| compressedBytes: 0, | ||
| totalDuplicates: 0, | ||
| }, | ||
| }, | ||
| }, | ||
| ingester: { | ||
| totalReached: 0, | ||
| totalChunksMatched: 0, | ||
| totalBatches: 0, | ||
| totalLinesSent: 0, | ||
| store: { | ||
| totalChunksRef: 0, | ||
| totalChunksDownloaded: 0, | ||
| chunksDownloadTime: 0, | ||
| chunk: { | ||
| headChunkBytes: 0, | ||
| headChunkLines: 0, | ||
| decompressedBytes: 0, | ||
| decompressedLines: 0, | ||
| compressedBytes: 0, | ||
| totalDuplicates: 0, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
| }; | ||
|
|
||
| describe('Logs Table Sorting', () => { | ||
| it('sorts same-timestamp logs by observedTimestamp', () => { | ||
| cy.intercept(QUERY_RANGE_STREAMS_URL_MATCH, sameTimestampStreamsResponse()).as( | ||
| 'queryRangeStreams', | ||
| ); | ||
| cy.intercept(QUERY_RANGE_MATRIX_URL_MATCH, { statusCode: 200, body: {} }); | ||
|
|
||
| cy.visit(LOGS_PAGE_URL); | ||
|
|
||
| cy.wait('@queryRangeStreams'); | ||
|
|
||
| // Default direction is 'backward' (desc) — largest observedTimestamp first | ||
| // observedTimestamp order: C(+300) > B(+200) > A(+100) | ||
| cy.byTestID(TestIds.LogsTable) | ||
| .should('exist') | ||
| .within(() => { | ||
| cy.get('td[data-label="message"]').should('have.length', 3); | ||
| cy.get('td[data-label="message"]').eq(0).should('contain', 'log-line-C'); | ||
| cy.get('td[data-label="message"]').eq(1).should('contain', 'log-line-B'); | ||
| cy.get('td[data-label="message"]').eq(2).should('contain', 'log-line-A'); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import { bigIntDifference, numericComparator } from '../sort-utils'; | ||
|
|
||
| describe('numericComparator', () => { | ||
| it('should return -1 when a < b with positive multiplier', () => { | ||
| expect(numericComparator(1, 2, 1)).toBe(-1); | ||
| }); | ||
|
|
||
| it('should return 1 when a > b with positive multiplier', () => { | ||
| expect(numericComparator(2, 1, 1)).toBe(1); | ||
| }); | ||
|
|
||
| it('should return 0 when a === b', () => { | ||
| expect(numericComparator(1, 1, 1)).toBe(0); | ||
| }); | ||
|
|
||
| it('should invert result with negative multiplier (descending sort)', () => { | ||
| expect(numericComparator(1, 2, -1)).toBe(1); | ||
| expect(numericComparator(2, 1, -1)).toBe(-1); | ||
| }); | ||
|
|
||
| it('should use fallback comparison when values are equal', () => { | ||
| expect(numericComparator(5, 5, 1, 3)).toBe(3); | ||
| expect(numericComparator(5, 5, 1, -2)).toBe(-2); | ||
| }); | ||
|
|
||
| it('should apply direction multiplier to fallback comparison', () => { | ||
| expect(numericComparator(5, 5, -1, 3)).toBe(-3); | ||
| expect(numericComparator(5, 5, -1, -2)).toBe(2); | ||
| }); | ||
|
|
||
| it('should ignore fallback when values are not equal', () => { | ||
| expect(numericComparator(1, 2, 1, 100)).toBe(-1); | ||
| expect(numericComparator(2, 1, 1, 100)).toBe(1); | ||
| }); | ||
|
|
||
| it('should work with timestamps', () => { | ||
| const timestamp1 = 1679000000000; | ||
| const timestamp2 = 1679000000001; | ||
| expect(numericComparator(timestamp1, timestamp2, 1)).toBe(-1); | ||
| expect(numericComparator(timestamp2, timestamp1, 1)).toBe(1); | ||
| expect(numericComparator(timestamp1, timestamp1, 1)).toBe(0); | ||
| }); | ||
|
|
||
| it('should use logIndex as tiebreaker for equal timestamps in ascending sort', () => { | ||
| const timestamp = 1679000000000; | ||
| const logs = [ | ||
| { timestamp, logIndex: 0 }, | ||
| { timestamp, logIndex: 1 }, | ||
| { timestamp, logIndex: 2 }, | ||
| ]; | ||
|
|
||
| const sortedAsc = [...logs].sort((a, b) => | ||
| numericComparator(a.timestamp, b.timestamp, 1, a.logIndex - b.logIndex), | ||
| ); | ||
| expect(sortedAsc.map((l) => l.logIndex)).toEqual([0, 1, 2]); | ||
| }); | ||
|
|
||
| it('should use logIndex as tiebreaker for equal timestamps in descending sort', () => { | ||
| const timestamp = 1679000000000; | ||
| const logs = [ | ||
| { timestamp, logIndex: 0 }, | ||
| { timestamp, logIndex: 1 }, | ||
| { timestamp, logIndex: 2 }, | ||
| ]; | ||
|
|
||
| const sortedDesc = [...logs].sort((a, b) => | ||
| numericComparator(a.timestamp, b.timestamp, -1, a.logIndex - b.logIndex), | ||
| ); | ||
| expect(sortedDesc.map((l) => l.logIndex)).toEqual([2, 1, 0]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('observedTimestampDifference', () => { | ||
| it('should return the numeric difference for small bigints', () => { | ||
| expect(bigIntDifference(10n, 3n)).toBe(7); | ||
| expect(bigIntDifference(3n, 10n)).toBe(-7); | ||
| }); | ||
|
|
||
| it('should return 0 when both values are equal', () => { | ||
| expect(bigIntDifference(42n, 42n)).toBe(0); | ||
| }); | ||
|
|
||
| it('should clamp to MIN_SAFE_INTEGER when difference is too negative', () => { | ||
| const a = 0n; | ||
| const b = BigInt(Number.MAX_SAFE_INTEGER) + 1n; | ||
| expect(bigIntDifference(a, b)).toBe(Number.MIN_SAFE_INTEGER); | ||
| }); | ||
|
|
||
| it('should clamp to MAX_SAFE_INTEGER when difference is too positive', () => { | ||
| const a = BigInt(Number.MAX_SAFE_INTEGER) + 1n; | ||
| const b = 0n; | ||
| expect(bigIntDifference(a, b)).toBe(Number.MAX_SAFE_INTEGER); | ||
| }); | ||
|
|
||
| it('should not clamp when difference is exactly MAX_SAFE_INTEGER', () => { | ||
| const maxSafe = BigInt(Number.MAX_SAFE_INTEGER); | ||
| expect(bigIntDifference(maxSafe, 0n)).toBe(Number.MAX_SAFE_INTEGER); | ||
| }); | ||
|
|
||
| it('should not clamp when difference is exactly MIN_SAFE_INTEGER', () => { | ||
| const minSafe = BigInt(Number.MIN_SAFE_INTEGER); | ||
| expect(bigIntDifference(minSafe, 0n)).toBe(Number.MIN_SAFE_INTEGER); | ||
| }); | ||
|
|
||
| it('should clamp when difference is one past MAX_SAFE_INTEGER', () => { | ||
| const pastMax = BigInt(Number.MAX_SAFE_INTEGER) + 1n; | ||
| expect(bigIntDifference(pastMax, 0n)).toBe(Number.MAX_SAFE_INTEGER); | ||
| }); | ||
|
|
||
| it('should clamp when difference is one past MIN_SAFE_INTEGER', () => { | ||
| const pastMin = BigInt(Number.MIN_SAFE_INTEGER) - 1n; | ||
| expect(bigIntDifference(pastMin, 0n)).toBe(Number.MIN_SAFE_INTEGER); | ||
| }); | ||
|
|
||
| it('should handle realistic nanosecond timestamp differences', () => { | ||
| const ts1 = 1679000000000000000n; | ||
| const ts2 = 1679000000000000001n; | ||
| expect(bigIntDifference(ts2, ts1)).toBe(1); | ||
| expect(bigIntDifference(ts1, ts2)).toBe(-1); | ||
| }); | ||
|
|
||
| it('should clamp when nanosecond timestamps are far apart', () => { | ||
| const ts1 = 1679000000000000000n; | ||
| const ts2 = 1579000000000000000n; | ||
| expect(bigIntDifference(ts1, ts2)).toBe(Number.MAX_SAFE_INTEGER); | ||
| expect(bigIntDifference(ts2, ts1)).toBe(Number.MIN_SAFE_INTEGER); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| type SortableValue = string | number; | ||
|
|
||
| // sort with an appropriate numeric comparator for big floats | ||
| export const numericComparator = <T extends SortableValue>( | ||
| a: T, | ||
| b: T, | ||
| directionMultiplier: number, | ||
| fallbackComparison?: number, | ||
| ): number => { | ||
| const result = a < b ? -1 : a > b ? 1 : 0; | ||
| if (result === 0 && fallbackComparison !== undefined) { | ||
| return fallbackComparison * directionMultiplier; | ||
|
jgbernalp marked this conversation as resolved.
|
||
| } | ||
| return result * directionMultiplier; | ||
| }; | ||
|
|
||
| const MIN_SAFE_BIGINT = BigInt(Number.MIN_SAFE_INTEGER); | ||
| const MAX_SAFE_BIGINT = BigInt(Number.MAX_SAFE_INTEGER); | ||
|
|
||
| export const bigIntDifference = (a: bigint | undefined, b: bigint | undefined): number => { | ||
| if (a === undefined || b === undefined) { | ||
| return 0; | ||
| } | ||
| const difference = a - b; | ||
| if (difference < MIN_SAFE_BIGINT) return Number.MIN_SAFE_INTEGER; | ||
| if (difference > MAX_SAFE_BIGINT) return Number.MAX_SAFE_INTEGER; | ||
| return Number(difference); | ||
| }; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.