fix(webui): users page filter, labels, panel fetches

This commit is contained in:
2026-11-25 11:52:37 +00:00
parent a138b186b6
commit 19950c40f6
15 changed files with 202 additions and 50 deletions
+32
View File
@@ -334,3 +334,35 @@ describe('evictOldestDetails', () => {
expect(Object.keys(detail).sort()).toEqual(want)
})
})
describe('useConsumersStore.fetchUserModels', () => {
beforeEach(() => {
setActivePinia(createPinia())
mockedAdminFetch.mockReset()
useAuthStore().submit('test-admin-key')
})
it('fetches only the usermodel totals', async () => {
mockedAdminFetch.mockResolvedValue(emptyTotals)
const store = useConsumersStore()
await store.fetchUserModels('alice', 'day', 7)
expect(mockedAdminFetch).toHaveBeenCalledTimes(1)
expect(String(mockedAdminFetch.mock.calls[0]?.[0])).toContain('kind=usermodel')
expect(store.models.alice).toMatchObject({ loading: false, error: '', unavailable: false })
expect(store.models.alice?.totals).toEqual(emptyTotals)
})
it('a 404 marks the feature unavailable without an error', async () => {
mockedAdminFetch.mockRejectedValue(new AdminApiError('not found', 404))
const store = useConsumersStore()
await store.fetchUserModels('alice', 'day', 7)
expect(store.models.alice).toMatchObject({ unavailable: true, error: '', totals: null })
})
it('other failures surface as an error', async () => {
mockedAdminFetch.mockRejectedValue(new AdminApiError('boom', 500))
const store = useConsumersStore()
await store.fetchUserModels('alice', 'day', 7)
expect(store.models.alice?.error).not.toBe('')
})
})
+31
View File
@@ -43,6 +43,16 @@ export interface UserDetailState {
span: number | null
}
/** UserModelsState is one user's per-model totals alone (UserPanel), without the two series UserDetail also fetches. */
export interface UserModelsState {
loading: boolean
error: string
totals: AdminTotalsResponse | null
/** The usermodel call came back 404: the feature is off server-side. */
unavailable: boolean
reqId: number
}
/** MAX_CACHED_USER_DETAILS caps how many opened users' detail the store keeps in memory. */
export const MAX_CACHED_USER_DETAILS = 20
@@ -96,6 +106,7 @@ export const useConsumersStore = defineStore('consumers', {
error: '',
fetchedAt: null as number | null,
detail: {} as Record<string, UserDetailState>,
models: {} as Record<string, UserModelsState>,
}),
actions: {
/**
@@ -130,6 +141,26 @@ export const useConsumersStore = defineStore('consumers', {
if (this.data && this.fetchedAt !== null && Date.now() - this.fetchedAt < REFRESH_STALE_MS) return
await this.fetchConsumers()
},
/** fetchUserModels loads only the usermodel totals (latest request wins); a 404 sets `unavailable` instead of an error. */
async fetchUserModels(userId: string, window: HistoryWindow, span: number): Promise<void> {
const auth = useAuthStore()
if (!auth.isAuthenticated) return
const prev = this.models[userId]
const reqId = (prev?.reqId ?? 0) + 1
this.models[userId] = { loading: true, error: '', totals: prev?.totals ?? null, unavailable: false, reqId }
const clamped = dayOrMonthWindow(window, span)
try {
const totals = await adminFetch<AdminTotalsResponse>(
totalsUrl({ kind: 'usermodel', user: userId, window: clamped.window, span: clamped.span, metrics: ['req', 'cost'] }),
)
if (this.models[userId]?.reqId !== reqId) return
this.models[userId] = { loading: false, error: '', totals, unavailable: false, reqId }
} catch (err) {
if (this.models[userId]?.reqId !== reqId || isAuthRejection(err)) return
const unavailable = err instanceof AdminApiError && err.status === 404
this.models[userId] = { loading: false, error: unavailable ? '' : messageOf(err), totals: null, unavailable, reqId }
}
},
/**
* fetchUserDetail loads one user's timeline (req + cost series) and,
* only when `userModelStatsEnabled` is true (AdminFeaturesView.