mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 16:07:33 +00:00
fix(cn-quota): refresh usage cells with account snapshots
This commit is contained in:
1 parent
530c5e3a4f
commit
849aa593d4
2 files changed
+107
-9
No files matched your search
@@ -4,7 +4,7 @@
|
||||
data-test="cn-provider-quota"
|
||||
class="min-w-[220px] space-y-1"
|
||||
>
|
||||
<!-- Tier rows: 5h + weekly utilization bars (snapshot renders on mount).
|
||||
<!-- Tier rows: 5h + weekly utilization bars from the current snapshot/probe.
|
||||
复用账号页 UsageProgressBar:同阈值配色、同倒计时格式。 -->
|
||||
<div v-if="data?.success && data.tiers?.length" class="space-y-1">
|
||||
<UsageProgressBar
|
||||
@@ -78,7 +78,8 @@ const visible = computed(() => cnQuotaCellVisible(props.account))
|
||||
|
||||
const loading = ref(false)
|
||||
const error = ref<string | null>(null)
|
||||
const data = ref<CNProviderQuotaProbeResult | null>(null)
|
||||
const probeData = ref<CNProviderQuotaProbeResult | null>(null)
|
||||
let probeGeneration = 0
|
||||
|
||||
// 后端周期任务/手动探测写入的 extra 快照键(<provider>_ 前缀,与后端
|
||||
// cnQuotaExtraUpdates 对齐)。页面加载即有数据,无需等待探测。
|
||||
@@ -118,6 +119,18 @@ const snapshotData = computed<CNProviderQuotaProbeResult | null>(() => {
|
||||
return { success: true, tiers } as CNProviderQuotaProbeResult
|
||||
})
|
||||
|
||||
const data = computed(() => probeData.value ?? snapshotData.value)
|
||||
|
||||
// A list refresh replaces account.extra even when its quota snapshot is
|
||||
// unchanged. Keep a manual probe until the quota values or snapshot version
|
||||
// actually change, rather than reverting it on unrelated account updates.
|
||||
watch(
|
||||
[snapshotData, () => readExtraString(`${props.account.platform}_usage_updated_at`)],
|
||||
(next, previous) => {
|
||||
if (JSON.stringify(next) !== JSON.stringify(previous)) probeData.value = null
|
||||
}
|
||||
)
|
||||
|
||||
// 快照是否过期(无更新时间或超过 staleness 窗口)→ 挂载时需要自动探测。
|
||||
const snapshotIsStale = computed(() => {
|
||||
const updatedAt = readExtraString(`${props.account.platform}_usage_updated_at`)
|
||||
@@ -130,7 +143,6 @@ const snapshotIsStale = computed(() => {
|
||||
// 避免静默失败导致单元格空白无提示)。
|
||||
onMounted(() => {
|
||||
if (!visible.value) return
|
||||
data.value = snapshotData.value
|
||||
if (!snapshotIsStale.value) return
|
||||
// 模块级去抖:列表页每行一个实例,翻页/筛选/刷新会重复挂载;同一账号
|
||||
// 短时间内已自动探测过则跳过,避免对上游形成探测风暴。
|
||||
@@ -168,27 +180,31 @@ const windowLabel = (window: string) => {
|
||||
|
||||
const handleProbe = async () => {
|
||||
if (loading.value) return
|
||||
const generation = ++probeGeneration
|
||||
const accountID = props.account.id
|
||||
loading.value = true
|
||||
error.value = null
|
||||
try {
|
||||
const result = await adminAPI.cnProviders.queryQuota(props.account.id)
|
||||
const result = await adminAPI.cnProviders.queryQuota(accountID)
|
||||
if (generation !== probeGeneration) return
|
||||
// 失败时保留已渲染的快照条形图(仅显示错误行),成功才覆盖。
|
||||
if (result.success) {
|
||||
data.value = result
|
||||
probeData.value = result
|
||||
} else {
|
||||
error.value = result.error || t('common.error')
|
||||
}
|
||||
} catch (e) {
|
||||
error.value = extractErrorMessage(e)
|
||||
if (generation === probeGeneration) error.value = extractErrorMessage(e)
|
||||
} finally {
|
||||
loading.value = false
|
||||
if (generation === probeGeneration) loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
watch(
|
||||
() => props.account.id,
|
||||
[() => props.account.id, () => props.account.platform],
|
||||
() => {
|
||||
data.value = null
|
||||
probeGeneration++
|
||||
probeData.value = null
|
||||
error.value = null
|
||||
loading.value = false
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { flushPromises, mount } from '@vue/test-utils'
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { reactive } from 'vue'
|
||||
import CNProviderQuotaCell from '../CNProviderQuotaCell.vue'
|
||||
import UsageProgressBar from '../UsageProgressBar.vue'
|
||||
import type { Account } from '@/types'
|
||||
@@ -102,4 +103,85 @@ describe('CNProviderQuotaCell', () => {
|
||||
await flushPromises()
|
||||
expect(queryQuota).toHaveBeenCalledWith(account.id)
|
||||
})
|
||||
|
||||
it('renders replacement quota values and windows for the same account without probing', async () => {
|
||||
const wrapper = mount(CNProviderQuotaCell, { props: { account } })
|
||||
await wrapper.setProps({ account: {
|
||||
...account,
|
||||
extra: {
|
||||
...account.extra,
|
||||
zhipu_5h_used_percent: 52,
|
||||
zhipu_weekly_used_percent: null,
|
||||
zhipu_monthly_used_percent: 81,
|
||||
zhipu_monthly_reset_at: '2026-12-01T00:00:00Z'
|
||||
}
|
||||
} })
|
||||
const bars = wrapper.findAllComponents(UsageProgressBar)
|
||||
expect(bars.map(bar => bar.props('utilization'))).toEqual([52, 81])
|
||||
expect(bars[1].props('label')).toBe('admin.accounts.cnProviders.windowMonthly')
|
||||
expect(bars[1].props('resetsAt')).toBe('2026-12-01T00:00:00Z')
|
||||
expect(queryQuota).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('reacts to quota changes inside the existing extra object', async () => {
|
||||
const row = reactive({ ...account, extra: { ...account.extra } })
|
||||
const wrapper = mount(CNProviderQuotaCell, { props: { account: row } })
|
||||
row.extra.zhipu_5h_used_percent = 63
|
||||
await flushPromises()
|
||||
expect(wrapper.findAllComponents(UsageProgressBar)[0].props('utilization')).toBe(63)
|
||||
expect(queryQuota).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('removes bars when a refreshed snapshot clears all quota windows', async () => {
|
||||
const wrapper = mount(CNProviderQuotaCell, { props: { account } })
|
||||
await wrapper.setProps({ account: {
|
||||
...account,
|
||||
extra: { ...account.extra, zhipu_5h_used_percent: null, zhipu_weekly_used_percent: null }
|
||||
} })
|
||||
expect(wrapper.findAllComponents(UsageProgressBar)).toHaveLength(0)
|
||||
expect(queryQuota).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('keeps a manual result through unrelated refreshes, then adopts a changed snapshot', async () => {
|
||||
queryQuota.mockResolvedValue({ success: true, tiers: [{ window: '5h', used_percent: 44 }] })
|
||||
const wrapper = mount(CNProviderQuotaCell, { props: { account } })
|
||||
await wrapper.get('[data-test="cn-provider-quota-probe"]').trigger('click')
|
||||
await flushPromises()
|
||||
await wrapper.setProps({ account: {
|
||||
...account, extra: { ...account.extra, unrelated_field: true }
|
||||
} })
|
||||
expect(wrapper.findAllComponents(UsageProgressBar).map(bar => bar.props('utilization'))).toEqual([44])
|
||||
await wrapper.setProps({ account: {
|
||||
...account, extra: { ...account.extra, zhipu_5h_used_percent: 66 }
|
||||
} })
|
||||
expect(wrapper.findAllComponents(UsageProgressBar).map(bar => bar.props('utilization'))).toEqual([66, 27])
|
||||
expect(queryQuota).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('adopts a new snapshot version even if its quota values equal the old snapshot', async () => {
|
||||
queryQuota.mockResolvedValue({ success: true, tiers: [{ window: '5h', used_percent: 44 }] })
|
||||
const wrapper = mount(CNProviderQuotaCell, { props: { account } })
|
||||
await wrapper.get('[data-test="cn-provider-quota-probe"]').trigger('click')
|
||||
await flushPromises()
|
||||
await wrapper.setProps({ account: {
|
||||
...account,
|
||||
extra: { ...account.extra, zhipu_usage_updated_at: new Date(Date.now() + 1000).toISOString() }
|
||||
} })
|
||||
expect(wrapper.findAllComponents(UsageProgressBar).map(bar => bar.props('utilization'))).toEqual([0, 27])
|
||||
})
|
||||
|
||||
it('keeps the new account snapshot when the previous account probe finishes late', async () => {
|
||||
let resolveProbe!: (value: unknown) => void
|
||||
queryQuota.mockReturnValue(new Promise(resolve => { resolveProbe = resolve }))
|
||||
const wrapper = mount(CNProviderQuotaCell, { props: { account } })
|
||||
await wrapper.get('[data-test="cn-provider-quota-probe"]').trigger('click')
|
||||
await wrapper.setProps({ account: {
|
||||
...account, id: 8, extra: { ...account.extra, zhipu_5h_used_percent: 91 }
|
||||
} })
|
||||
expect(wrapper.findAllComponents(UsageProgressBar)[0].props('utilization')).toBe(91)
|
||||
resolveProbe({ success: true, tiers: [{ window: '5h', used_percent: 44 }] })
|
||||
await flushPromises()
|
||||
expect(wrapper.findAllComponents(UsageProgressBar)[0].props('utilization')).toBe(91)
|
||||
expect(wrapper.get('[data-test="cn-provider-quota-probe"]').attributes('disabled')).toBeUndefined()
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user