Skip to content
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
72 changes: 72 additions & 0 deletions web/src/__tests__/numeric-comparator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { 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),
);
// Direction multiplier is applied to fallback, so order is reversed
expect(sortedDesc.map((l) => l.logIndex)).toEqual([2, 1, 0]);
});
});
21 changes: 10 additions & 11 deletions web/src/components/logs-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from '../logs.types';
import { parseName, parseResources, ResourceLabel } from '../parse-resources';
import { severityFromString } from '../severity';
import { numericComparator } from '../sort-utils';
import { TestIds } from '../test-ids';
import { LogDetail } from './log-detail';
import './logs-table.css';
Expand All @@ -38,8 +39,6 @@ interface LogsTableProps {
schema: Schema;
}

type TableCellValue = string | number | Resource | Array<Resource>;

const isJSONObject = (value: string): boolean => {
const trimmedValue = value.trim();

Expand Down Expand Up @@ -96,13 +95,6 @@ const getSeverityClass = (severity: string) => {
return severity ? `lv-plugin__table__severity-${severity}` : '';
};

// sort with an appropriate numeric comparator for big floats
const numericComparator = <T extends TableCellValue>(
a: T,
b: T,
directionMultiplier: number,
): number => (a < b ? -1 : a > b ? 1 : 0) * directionMultiplier;

const columns: Array<TableColumn<LogTableData>> = [
{
id: 'expand',
Expand All @@ -119,7 +111,12 @@ const columns: Array<TableColumn<LogTableData>> = [
},
sort: (data, sortDirection) =>
data.sort((a, b) =>
numericComparator(a.timestamp, b.timestamp, sortDirection === 'asc' ? 1 : -1),
numericComparator(
a.timestamp,
b.timestamp,
sortDirection === 'asc' ? 1 : -1,
a.logIndex - b.logIndex,
),
),
},
{
Expand Down Expand Up @@ -306,7 +303,9 @@ export const LogsTable: React.FC<LogsTableProps> = ({
}
}

return tableData.sort((a, b) => numericComparator(a.timestamp, b.timestamp, -1));
return tableData.sort((a, b) =>
numericComparator(a.timestamp, b.timestamp, -1, a.logIndex - b.logIndex),
);
}, [tableData, columns, sortBy]);

const dataIsEmpty = sortedData.length === 0;
Expand Down
15 changes: 15 additions & 0 deletions web/src/sort-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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;
Comment on lines +11 to +12
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Don't apply directionMultiplier to a stability fallback.

Line 12 re-scales fallbackComparison, but the current callers use a.logIndex - b.logIndex to preserve input/backend order when the primary timestamp comparison ties. In descending mode that reverses those tied rows instead of keeping their natural order.

Suggested fix
 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;
+    return fallbackComparison;
   }
   return result * directionMultiplier;
 };

Downstream, the descending fallback expectations in web/src/__tests__/numeric-comparator.spec.ts should preserve input order rather than reverse it.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (result === 0 && fallbackComparison !== undefined) {
return fallbackComparison * directionMultiplier;
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;
}
return result * directionMultiplier;
};
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@web/src/sort-utils.ts` around lines 11 - 12, The stability fallback currently
multiplies fallbackComparison by directionMultiplier which reverses natural
order for tied timestamps in descending mode; change the return so that when
result === 0 and fallbackComparison !== undefined you return fallbackComparison
(without applying directionMultiplier) to preserve input/backend order, updating
any tests (e.g., in web/src/__tests__/numeric-comparator.spec.ts) that expected
reversed order to now expect the original input order; refer to the variables
result, fallbackComparison, and directionMultiplier in sort-utils.ts when making
this change.

}
return result * directionMultiplier;
};