fix(webui): sort users and breaker tables before paging

This commit is contained in:
2026-11-25 11:52:37 +00:00
parent f7cf402564
commit 7b660363e7
7 changed files with 90 additions and 64 deletions
+1 -28
View File
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest'
import { groupByProvider, priceStatus, selectModels, sortThenSlice } from '@/lib/models-view'
import { groupByProvider, priceStatus, selectModels } from '@/lib/models-view'
import type { ModelCatalogRow } from '@/lib/model-table-columns'
function row(id: string, over: Partial<ModelCatalogRow> = {}): ModelCatalogRow {
@@ -68,30 +68,3 @@ describe('groupByProvider', () => {
expect(g!.rows.map((r) => r.id)).toEqual(['a/c', 'a/a', 'a/b'])
})
})
describe('sortThenSlice', () => {
const rows = [
{ id: 'a', requests: 5, name: 'b' },
{ id: 'b', requests: null, name: 'a' },
{ id: 'c', requests: 9, name: 'c' },
{ id: 'd', requests: 1, name: 'd' },
]
it('orders the whole list before taking the page', () => {
const p0 = sortThenSlice(rows, { key: 'requests', direction: 'asc' }, 2, 0)
const p1 = sortThenSlice(rows, { key: 'requests', direction: 'asc' }, 2, 1)
expect(p0.map((r) => r.id)).toEqual(['d', 'a'])
expect(p1.map((r) => r.id)).toEqual(['c', 'b'])
})
it('keeps missing values last in both directions', () => {
expect(sortThenSlice(rows, { key: 'requests', direction: 'desc' }, 4, 0).map((r) => r.id)).toEqual(['c', 'a', 'd', 'b'])
})
it('sorts text and keeps input order without a sort', () => {
expect(sortThenSlice(rows, { key: 'name', direction: 'asc' }, 4, 0).map((r) => r.id)).toEqual(['b', 'a', 'c', 'd'])
expect(sortThenSlice(rows, null, 2, 1).map((r) => r.id)).toEqual(['c', 'd'])
})
it('clamps an out-of-range page and does not mutate the input', () => {
const copy = [...rows]
expect(sortThenSlice(rows, null, 3, 9).map((r) => r.id)).toEqual(['d'])
expect(rows).toEqual(copy)
})
})
-29
View File
@@ -1,5 +1,4 @@
import { filterModelRows } from '@/lib/model-filter'
import { pageWindow } from '@/lib/paging'
import type { ModelCatalogRow } from '@/lib/model-table-columns'
export type PriceStatus = 'priced' | 'unpriced' | 'free' | 'none'
@@ -51,31 +50,3 @@ export function groupByProvider(rows: ModelCatalogRow[]): ProviderGroup[] {
for (const g of groups.values()) g.rows.sort(byTraffic)
return [...groups.values()]
}
export interface SortSpec {
key: string
direction: 'asc' | 'desc'
}
function compareCells(a: unknown, b: unknown): number {
if (typeof a === 'number' && typeof b === 'number') return a - b
return String(a).localeCompare(String(b))
}
/** Sorts the whole list by `sort` (missing values last either way), then returns page `pageIndex` of it. */
export function sortThenSlice<T extends object>(rows: readonly T[], sort: SortSpec | null, pageSize: number, pageIndex: number): T[] {
let ordered = [...rows]
if (sort) {
const sign = sort.direction === 'asc' ? 1 : -1
const cell = (r: T): unknown => (r as Record<string, unknown>)[sort.key]
const missing = (v: unknown): boolean => v === null || v === undefined || v === ''
ordered = ordered.sort((a, b) => {
const x = cell(a)
const y = cell(b)
if (missing(x) || missing(y)) return Number(missing(x)) - Number(missing(y))
return sign * compareCells(x, y)
})
}
const w = pageWindow(ordered.length, pageSize, pageIndex)
return ordered.slice(w.from, w.to)
}
+30
View File
@@ -0,0 +1,30 @@
import { describe, expect, it } from 'vitest'
import { sortThenSlice } from '@/lib/table-sort'
describe('sortThenSlice', () => {
const rows = [
{ id: 'a', requests: 5, name: 'b' },
{ id: 'b', requests: null, name: 'a' },
{ id: 'c', requests: 9, name: 'c' },
{ id: 'd', requests: 1, name: 'd' },
]
it('orders the whole list before taking the page', () => {
const p0 = sortThenSlice(rows, { key: 'requests', direction: 'asc' }, 2, 0)
const p1 = sortThenSlice(rows, { key: 'requests', direction: 'asc' }, 2, 1)
expect(p0.map((r) => r.id)).toEqual(['d', 'a'])
expect(p1.map((r) => r.id)).toEqual(['c', 'b'])
})
it('keeps missing values last in both directions', () => {
expect(sortThenSlice(rows, { key: 'requests', direction: 'desc' }, 4, 0).map((r) => r.id)).toEqual(['c', 'a', 'd', 'b'])
})
it('sorts text and keeps input order without a sort', () => {
expect(sortThenSlice(rows, { key: 'name', direction: 'asc' }, 4, 0).map((r) => r.id)).toEqual(['b', 'a', 'c', 'd'])
expect(sortThenSlice(rows, null, 2, 1).map((r) => r.id)).toEqual(['c', 'd'])
})
it('clamps an out-of-range page and does not mutate the input', () => {
const copy = [...rows]
expect(sortThenSlice(rows, null, 3, 9).map((r) => r.id)).toEqual(['d'])
expect(rows).toEqual(copy)
})
})
+29
View File
@@ -0,0 +1,29 @@
import { pageWindow } from '@/lib/paging'
export interface SortSpec {
key: string
direction: 'asc' | 'desc'
}
function compareCells(a: unknown, b: unknown): number {
if (typeof a === 'number' && typeof b === 'number') return a - b
return String(a).localeCompare(String(b))
}
/** Sorts the whole list by `sort` (missing values last either way), then returns page `pageIndex` of it. */
export function sortThenSlice<T extends object>(rows: readonly T[], sort: SortSpec | null, pageSize: number, pageIndex: number): T[] {
let ordered = [...rows]
if (sort) {
const sign = sort.direction === 'asc' ? 1 : -1
const cell = (r: T): unknown => (r as Record<string, unknown>)[sort.key]
const missing = (v: unknown): boolean => v === null || v === undefined || v === ''
ordered = ordered.sort((a, b) => {
const x = cell(a)
const y = cell(b)
if (missing(x) || missing(y)) return Number(missing(x)) - Number(missing(y))
return sign * compareCells(x, y)
})
}
const w = pageWindow(ordered.length, pageSize, pageIndex)
return ordered.slice(w.from, w.to)
}
+4 -2
View File
@@ -5,13 +5,15 @@ import { computed, onMounted, reactive, watch } from 'vue'
import { formatCompactCount, formatContextWindow, formatModelCostHover } from '@/lib/format'
import { loadState } from '@/lib/load-state'
import { NO_KIND, groupByProvider, priceStatus, selectModels, sortThenSlice } from '@/lib/models-view'
import type { ModelSelection, PriceStatus, SortSpec } from '@/lib/models-view'
import { NO_KIND, groupByProvider, priceStatus, selectModels } from '@/lib/models-view'
import type { ModelSelection, PriceStatus } from '@/lib/models-view'
import { pageWindow } from '@/lib/paging'
import { resolveSel } from '@/lib/panel-selection'
import { breakerRows, providerRateStatus, providerSuccessRate } from '@/lib/provider-rate'
import type { BreakerRow } from '@/lib/provider-rate'
import { routersByModel } from '@/lib/router-index'
import { sortThenSlice } from '@/lib/table-sort'
import type { SortSpec } from '@/lib/table-sort'
import { wbColumns } from '@/lib/wb-columns'
import ModelPanel from '@/panels/ModelPanel.vue'
import ProviderPanel from '@/panels/ProviderPanel.vue'
+12 -3
View File
@@ -7,6 +7,8 @@ import { thinLabels } from '@/lib/chart-labels'
import { formatBucketLabel, formatCompactCount, formatUntil } from '@/lib/format'
import { loadState } from '@/lib/load-state'
import { pageWindow } from '@/lib/paging'
import { sortThenSlice } from '@/lib/table-sort'
import type { SortSpec } from '@/lib/table-sort'
import { breakerRows, ratePercentSeries } from '@/lib/provider-rate'
import type { BreakerRow } from '@/lib/provider-rate'
import { scopeId } from '@/lib/scope'
@@ -121,8 +123,8 @@ const ttfbState = computed<ViewState>(() => {
// --- Breaker table ----------------------------------------------------------
const breakerColumns = wbColumns<BreakerView>([
{ key: 'provider', header: 'Provider', kind: 'text', sortable: false },
{ key: 'status', header: 'State', kind: 'status', sortable: false },
{ key: 'provider', header: 'Provider', kind: 'text' },
{ key: 'status', header: 'State', kind: 'status', sortable: true },
{ key: 'until', header: 'Opens until', kind: 'time', sortable: false },
{ key: 'lastErr', header: 'Last error', kind: 'text', sortable: false },
])
@@ -145,7 +147,12 @@ const onPhone = (e: MediaQueryListEvent): void => {
const breakerPageSize = computed(() => (phone.value ? BREAKER_PAGE_SIZE_PHONE : BREAKER_PAGE_SIZE))
const breakerPage = ref(1)
const breakerWindow = computed(() => pageWindow(breakers.value.length, breakerPageSize.value, breakerPage.value - 1))
const breakerPaged = computed(() => breakers.value.slice(breakerWindow.value.from, breakerWindow.value.to))
const breakerSort = ref<SortSpec | null>(null)
const breakerPaged = computed(() => sortThenSlice(breakers.value, breakerSort.value, breakerPageSize.value, breakerWindow.value.index))
function onBreakerSort(next: SortSpec | null): void {
breakerSort.value = next
breakerPage.value = 1
}
const breakerState = computed<ViewState>(() => (overview.value === null ? 'loading' : breakers.value.length === 0 ? 'empty' : 'ready'))
const openCount = computed(() => breakers.value.filter((r) => r.state !== 'closed').length)
const degradedCount = computed(() => breakers.value.filter((r) => r.degraded).length)
@@ -276,8 +283,10 @@ const windowUsed = computed(() => hourOrDayWindow(filters.window, filters.span))
class="wb-clip"
:columns="breakerColumns"
:rows="breakerPaged"
:sort="breakerSort"
row-key="provider"
label="Provider circuit breakers"
@update:sort="onBreakerSort"
@row-activate="(row: BreakerView) => nav.openPanel('provider', row.provider)"
>
<template #cell-status="{ value }">
+14 -2
View File
@@ -8,6 +8,8 @@ import { formatLastSeen } from '@/lib/last-seen'
import { loadState } from '@/lib/load-state'
import { isHot, primaryLimit } from '@/lib/meters'
import { pageWindow } from '@/lib/paging'
import { sortThenSlice } from '@/lib/table-sort'
import type { SortSpec } from '@/lib/table-sort'
import { resolveSel } from '@/lib/panel-selection'
import { wbColumns } from '@/lib/wb-columns'
import GroupPanel from '@/panels/GroupPanel.vue'
@@ -111,9 +113,17 @@ const groupColumns = wbColumns<Row>([
const columns = computed(() => (tab.value === 'users' ? userColumns : groupColumns))
const page = ref(1)
watch([tab, needle], () => (page.value = 1))
watch([tab, needle], () => {
page.value = 1
sort.value = null
})
const window_ = computed(() => pageWindow(rows.value.length, PAGE_SIZE, page.value - 1))
const paged = computed(() => rows.value.slice(window_.value.from, window_.value.to))
const sort = ref<SortSpec | null>(null)
const paged = computed(() => sortThenSlice(rows.value, sort.value, PAGE_SIZE, window_.value.index))
function onSort(next: SortSpec | null): void {
sort.value = next
page.value = 1
}
const state = computed<ViewState>(() => {
const s = loadState({ loading: dashboard.lastUpdated === null, hasData: usage.value !== null, error: dashboard.error })
@@ -168,9 +178,11 @@ onUnmounted(stopClock)
class="wb-clip"
:columns="columns"
:rows="paged"
:sort="sort"
row-key="id"
:active-key="activeKey"
:label="tab === 'users' ? 'Users' : 'Groups'"
@update:sort="onSort"
@row-activate="open"
>
<template #cell-members="{ value }">{{ formatCompactCount(value as number) }}</template>