Skip to content
Draft
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
24 changes: 15 additions & 9 deletions frontend/public/components/monitoring/metrics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import KebabDropdown from './kebab-dropdown';
import IntervalDropdown from './poll-interval-dropdown';
import TablePagination from './table-pagination';
import { PrometheusAPIError } from './types';
import { naturalSort } from './utils';

// Stores information about the currently focused query input
let focusedQuery;
Expand Down Expand Up @@ -432,16 +433,21 @@ export const QueryTable: React.FC<QueryTableProps> = ({ index, namespace }) => {

rows = _.map(result, rowMapper);
if (sortBy) {
// Sort Values column numerically and sort all the other columns alphabetically
// Sort all columns naturally (alphanumeric)
const valuesColIndex = allLabelKeys.length + 1;
const sort =
sortBy.index === valuesColIndex
? (cells) => {
const v = Number(cells[valuesColIndex]);
return Number.isNaN(v) ? 0 : v;
}
: `${sortBy.index}`;
rows = _.orderBy(rows, [sort], [sortBy.direction]);
if (sortBy.index === valuesColIndex) {
rows = rows.sort((a, b) => {
const aVal = Number(a[valuesColIndex]);
const bVal = Number(b[valuesColIndex]);
const aNum = Number.isNaN(aVal) ? 0 : aVal;
const bNum = Number.isNaN(bVal) ? 0 : bVal;
return (aNum - bNum) * (sortBy.direction === 'asc' ? 1 : -1);
});
} else {
rows = rows.sort((a, b) =>
naturalSort(String(a[sortBy.index] ?? ''), String(b[sortBy.index] ?? ''), sortBy.direction),
);
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions frontend/public/components/monitoring/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,11 @@ export const targetSource = (target: Target): AlertSource =>
target.labels?.prometheus === 'openshift-monitoring/k8s'
? AlertSource.Platform
: AlertSource.User;

export const naturalSort = (
a: string | undefined | null,
b: string | undefined | null,
direction: 'asc' | 'desc',
): number =>
(a ?? '').localeCompare(b ?? '', undefined, { sensitivity: 'base', numeric: true }) *
(direction === 'asc' ? 1 : -1) || 0;