mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-11-26 10:06:31 +00:00
fix(webui): models page sort, request totals, panel polish
This commit is contained in:
@@ -45,6 +45,8 @@ export interface ModelCatalogRow {
|
||||
aliases: string[]
|
||||
/** Smart routers that target this model. */
|
||||
routers?: string[]
|
||||
/** False when the range ranking (capped by the server) has no entry: requests is then 0 by absence, not a measured zero. */
|
||||
ranked?: boolean
|
||||
requests: number
|
||||
tokensIn: number
|
||||
tokensOut: number
|
||||
@@ -86,6 +88,7 @@ export function buildModelCatalogRows(
|
||||
displayFree: m.displayFree,
|
||||
aliases: m.aliases ?? [],
|
||||
routers: m.routers ?? [],
|
||||
ranked: usage !== undefined,
|
||||
requests: usage?.requests ?? 0,
|
||||
tokensIn: usage?.tokensIn ?? 0,
|
||||
tokensOut: usage?.tokensOut ?? 0,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import { groupByProvider, priceStatus, selectModels } from '@/lib/models-view'
|
||||
import { groupByProvider, priceStatus, selectModels, sortThenSlice } from '@/lib/models-view'
|
||||
import type { ModelCatalogRow } from '@/lib/model-table-columns'
|
||||
|
||||
function row(id: string, over: Partial<ModelCatalogRow> = {}): ModelCatalogRow {
|
||||
@@ -68,3 +68,30 @@ 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)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
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'
|
||||
@@ -50,3 +51,31 @@ 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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user