mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-24 23:21:18 +00:00
perf(usage): 优化 /admin/usage 打开速度与刷新响应
根因:页面 mount 并发 6 个请求,其中 5 个在原始 usage_logs 上 live 聚合, 且有 1 个重复 getModelStats。优化(不引入预聚合): 前端 - UsageView 经 :model-options 下传 model 列表,移除 UsageFilters 重复的 getModelStats(mount 请求 6→5,少一次 usage_logs 全表 GROUP BY model) - 刷新/换筛选保留旧模型数据(invalidateModelStatsCache 只失效标记不清空数据), 图表不再闪空,刷新期间页面保持可交互 后端 - GetStatsWithFilters 4 条聚合 errgroup 并行(仅 *sql.DB 连接池路径,ent.Tx 顺序回退以保事务内不并发),endpoint 明细 best-effort;抑制取消级联噪声日志 - /admin/usage/stats 复用 dashboard 的 newSnapshotCache 30s 处理器层缓存,按 filters+窗口为 key;前端手动刷新带 nocache=1 强制回源(刷新=最新) 注:自合并提交 4c8396c 迁移而来,仅取"列表打开速度与刷新响应"部分;原提交的 "审计查看弹窗大 body 渲染"改动(AuditLogModal / audit-log-format / 审计 i18n) 依赖尚未迁移的审计功能(d8389ade),本次已排除。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
b60d8bb4cc
commit
bf24b61139
10 files changed
+325
-74
No files matched your search
@@ -3,7 +3,7 @@ import { flushPromises, mount } from '@vue/test-utils'
|
||||
|
||||
import UsageView from '../UsageView.vue'
|
||||
|
||||
const { list, getStats, getSnapshotV2, getById } = vi.hoisted(() => {
|
||||
const { list, getStats, getSnapshotV2, getById, getModelStats } = vi.hoisted(() => {
|
||||
vi.stubGlobal('localStorage', {
|
||||
getItem: vi.fn(() => null),
|
||||
setItem: vi.fn(),
|
||||
@@ -15,6 +15,7 @@ const { list, getStats, getSnapshotV2, getById } = vi.hoisted(() => {
|
||||
getStats: vi.fn(),
|
||||
getSnapshotV2: vi.fn(),
|
||||
getById: vi.fn(),
|
||||
getModelStats: vi.fn(),
|
||||
}
|
||||
})
|
||||
|
||||
@@ -40,6 +41,7 @@ vi.mock('@/api/admin', () => ({
|
||||
},
|
||||
dashboard: {
|
||||
getSnapshotV2,
|
||||
getModelStats,
|
||||
},
|
||||
users: {
|
||||
getById,
|
||||
@@ -116,6 +118,7 @@ describe('admin UsageView distribution metric toggles', () => {
|
||||
getStats.mockReset()
|
||||
getSnapshotV2.mockReset()
|
||||
getById.mockReset()
|
||||
getModelStats.mockReset()
|
||||
|
||||
list.mockResolvedValue({
|
||||
items: [],
|
||||
@@ -137,12 +140,44 @@ describe('admin UsageView distribution metric toggles', () => {
|
||||
models: [],
|
||||
groups: [],
|
||||
})
|
||||
getModelStats.mockResolvedValue({ models: [] })
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers()
|
||||
})
|
||||
|
||||
it('keeps previous model stats visible during refresh until new data arrives', async () => {
|
||||
// 首次加载返回 A
|
||||
getModelStats.mockResolvedValueOnce({ models: [{ model: 'A', total_tokens: 10 }] })
|
||||
|
||||
const wrapper = mount(UsageView, {
|
||||
global: { stubs: {
|
||||
AppLayout: AppLayoutStub, UsageStatsCards: true, UsageFilters: UsageFiltersStub,
|
||||
UsageTable: true, UsageExportProgress: true, UsageCleanupDialog: true,
|
||||
UserBalanceHistoryModal: true, AuditLogModal: true, Pagination: true, Select: true,
|
||||
DateRangePicker: true, Icon: true, TokenUsageTrend: true,
|
||||
ModelDistributionChart: ModelDistributionChartStub, GroupDistributionChart: GroupDistributionChartStub,
|
||||
EndpointDistributionChart: true,
|
||||
} },
|
||||
})
|
||||
vi.advanceTimersByTime(120)
|
||||
await flushPromises()
|
||||
expect((wrapper.vm as any).requestedModelStats).toEqual([{ model: 'A', total_tokens: 10 }])
|
||||
|
||||
// 刷新:让第二次 getModelStats 处于 pending,断言旧数据 A 仍在(不被清空成 [])
|
||||
let resolveSecond: (v: any) => void = () => {}
|
||||
getModelStats.mockReturnValueOnce(new Promise((res) => { resolveSecond = res }))
|
||||
;(wrapper.vm as any).refreshData()
|
||||
await flushPromises()
|
||||
expect((wrapper.vm as any).requestedModelStats).toEqual([{ model: 'A', total_tokens: 10 }])
|
||||
|
||||
// 新数据到达后替换为 B
|
||||
resolveSecond({ models: [{ model: 'B', total_tokens: 20 }] })
|
||||
await flushPromises()
|
||||
expect((wrapper.vm as any).requestedModelStats).toEqual([{ model: 'B', total_tokens: 20 }])
|
||||
})
|
||||
|
||||
it('keeps model and group metric toggles independent without refetching chart data', async () => {
|
||||
const wrapper = mount(UsageView, {
|
||||
global: {
|
||||
|
||||
Reference in new issue
Block a user