From 2e8b1ad7fa294f06282903d609db5396a3336a25 Mon Sep 17 00:00:00 2001 From: Anderson Date: Tue, 20 Oct 2026 20:39:25 +0800 Subject: [PATCH] fix(antigravity): preserve fractional quota utilization --- .../internal/service/account_usage_service.go | 4 +-- .../service/antigravity_quota_fetcher.go | 2 +- .../service/antigravity_quota_fetcher_test.go | 32 ++++++++++++++++--- .../components/account/AccountUsageCell.vue | 4 +++ .../components/account/UsageProgressBar.vue | 4 +++ .../__tests__/AccountUsageCell.spec.ts | 15 +++++++++ .../__tests__/UsageProgressBar.spec.ts | 22 +++++++++++++ 7 files changed, 76 insertions(+), 7 deletions(-) diff --git a/backend/internal/service/account_usage_service.go b/backend/internal/service/account_usage_service.go index 746cb324a2..6859cfe5de 100644 --- a/backend/internal/service/account_usage_service.go +++ b/backend/internal/service/account_usage_service.go @@ -156,8 +156,8 @@ type UsageProgress struct { // AntigravityModelQuota Antigravity 单个模型的配额信息 type AntigravityModelQuota struct { - Utilization int `json:"utilization"` // 使用率 0-100 - ResetTime string `json:"reset_time"` // 重置时间 ISO8601 + Utilization float64 `json:"utilization"` // 使用率 0-100 + ResetTime string `json:"reset_time"` // 重置时间 ISO8601 } // AntigravityModelDetail Antigravity 单个模型的详细能力信息 diff --git a/backend/internal/service/antigravity_quota_fetcher.go b/backend/internal/service/antigravity_quota_fetcher.go index e296654e6d..7275f81794 100644 --- a/backend/internal/service/antigravity_quota_fetcher.go +++ b/backend/internal/service/antigravity_quota_fetcher.go @@ -145,7 +145,7 @@ func (f *AntigravityQuotaFetcher) buildUsageInfo(modelsResp *antigravity.FetchAv } // remainingFraction 是剩余比例 (0.0-1.0),转换为使用率百分比 - utilization := int((1.0 - modelInfo.QuotaInfo.RemainingFraction) * 100) + utilization := (1.0 - modelInfo.QuotaInfo.RemainingFraction) * 100 info.AntigravityQuota[modelName] = &AntigravityModelQuota{ Utilization: utilization, diff --git a/backend/internal/service/antigravity_quota_fetcher_test.go b/backend/internal/service/antigravity_quota_fetcher_test.go index a5463ec007..61654ae41f 100644 --- a/backend/internal/service/antigravity_quota_fetcher_test.go +++ b/backend/internal/service/antigravity_quota_fetcher_test.go @@ -98,12 +98,12 @@ func TestBuildUsageInfo_BasicModels(t *testing.T) { sonnetQuota := info.AntigravityQuota["claude-sonnet-4-20250514"] require.NotNil(t, sonnetQuota) - require.Equal(t, 25, sonnetQuota.Utilization) // (1 - 0.75) * 100 = 25 + require.Equal(t, 25.0, sonnetQuota.Utilization) // (1 - 0.75) * 100 = 25 require.Equal(t, "2026-03-08T12:00:00Z", sonnetQuota.ResetTime) geminiQuota := info.AntigravityQuota["gemini-2.5-pro"] require.NotNil(t, geminiQuota) - require.Equal(t, 50, geminiQuota.Utilization) // (1 - 0.50) * 100 = 50 + require.Equal(t, 50.0, geminiQuota.Utilization) // (1 - 0.50) * 100 = 50 require.Equal(t, "2026-03-08T15:00:00Z", geminiQuota.ResetTime) // AntigravityQuotaDetails @@ -347,7 +347,7 @@ func TestBuildUsageInfo_FullUtilization(t *testing.T) { quota := info.AntigravityQuota["claude-sonnet-4-20250514"] require.NotNil(t, quota) - require.Equal(t, 100, quota.Utilization) + require.Equal(t, 100.0, quota.Utilization) } func TestBuildUsageInfo_ZeroUtilization(t *testing.T) { @@ -366,7 +366,31 @@ func TestBuildUsageInfo_ZeroUtilization(t *testing.T) { info := fetcher.buildUsageInfo(modelsResp, "", "", nil) quota := info.AntigravityQuota["claude-sonnet-4-20250514"] require.NotNil(t, quota) - require.Equal(t, 0, quota.Utilization) + require.Equal(t, 0.0, quota.Utilization) +} + +func TestBuildUsageInfo_PreservesQuotaPrecision(t *testing.T) { + for _, tt := range []struct { + name string + remaining float64 + used float64 + }{ + {name: "below one percent", remaining: 0.99978864, used: 0.021136}, + {name: "fractional percent", remaining: 0.98016745, used: 1.983255}, + {name: "below display precision", remaining: 0.99999999, used: 0.000001}, + } { + t.Run(tt.name, func(t *testing.T) { + fetcher := &AntigravityQuotaFetcher{} + info := fetcher.buildUsageInfo(&antigravity.FetchAvailableModelsResponse{ + Models: map[string]antigravity.ModelInfo{ + "claude-sonnet-4-20250514": {QuotaInfo: &antigravity.ModelQuotaInfo{RemainingFraction: tt.remaining}}, + }, + }, "", "", nil) + quota := info.AntigravityQuota["claude-sonnet-4-20250514"] + require.NotNil(t, quota) + require.InDelta(t, tt.used, quota.Utilization, 0.00000001) + }) + } } func TestBuildUsageInfo_AICredits(t *testing.T) { diff --git a/frontend/src/components/account/AccountUsageCell.vue b/frontend/src/components/account/AccountUsageCell.vue index 6bf5f12af5..f313003ab1 100644 --- a/frontend/src/components/account/AccountUsageCell.vue +++ b/frontend/src/components/account/AccountUsageCell.vue @@ -301,6 +301,7 @@ v-if="antigravity3ProUsageFromAPI !== null" :label="t('admin.accounts.usageWindow.gemini3Pro')" :utilization="antigravity3ProUsageFromAPI.utilization" + precise-percent :resets-at="antigravity3ProUsageFromAPI.resetTime" color="indigo" /> @@ -310,6 +311,7 @@ v-if="antigravity3FlashUsageFromAPI !== null" :label="t('admin.accounts.usageWindow.gemini3Flash')" :utilization="antigravity3FlashUsageFromAPI.utilization" + precise-percent :resets-at="antigravity3FlashUsageFromAPI.resetTime" color="emerald" /> @@ -319,6 +321,7 @@ v-if="antigravity3ImageUsageFromAPI !== null" :label="t('admin.accounts.usageWindow.gemini3Image')" :utilization="antigravity3ImageUsageFromAPI.utilization" + precise-percent :resets-at="antigravity3ImageUsageFromAPI.resetTime" color="purple" /> @@ -328,6 +331,7 @@ v-if="antigravityClaudeUsageFromAPI !== null" :label="t('admin.accounts.usageWindow.claude')" :utilization="antigravityClaudeUsageFromAPI.utilization" + precise-percent :resets-at="antigravityClaudeUsageFromAPI.resetTime" color="amber" /> diff --git a/frontend/src/components/account/UsageProgressBar.vue b/frontend/src/components/account/UsageProgressBar.vue index bf08127185..3bccdda6c0 100644 --- a/frontend/src/components/account/UsageProgressBar.vue +++ b/frontend/src/components/account/UsageProgressBar.vue @@ -80,6 +80,7 @@ const props = withDefaults( remainingCapacity?: boolean /** fixed: 定宽居中徽章(账号页纵向对齐);auto: 限宽截断左对齐(监控页组合标签) */ labelWidth?: 'fixed' | 'auto' + precisePercent?: boolean }>(), { labelWidth: 'fixed' } ) @@ -178,6 +179,9 @@ const barWidth = computed(() => { // Display percentage (cap at 999% for readability) const displayPercent = computed(() => { + if (props.precisePercent && props.utilization > 0 && props.utilization < 100) { + return props.utilization < 0.01 ? '<0.01%' : `${props.utilization.toFixed(2)}%` + } const percent = Math.round( props.remainingCapacity ? Math.min(Math.max(props.utilization, 0), 100) diff --git a/frontend/src/components/account/__tests__/AccountUsageCell.spec.ts b/frontend/src/components/account/__tests__/AccountUsageCell.spec.ts index d595102f66..fbc4cb0260 100644 --- a/frontend/src/components/account/__tests__/AccountUsageCell.spec.ts +++ b/frontend/src/components/account/__tests__/AccountUsageCell.spec.ts @@ -113,6 +113,21 @@ const cnUsageCellStubs = { } describe('AccountUsageCell', () => { + it('shows small nonzero Antigravity quota usage', async () => { + getUsage.mockResolvedValue({ + antigravity_quota: { + 'claude-sonnet-4-5': { utilization: 0.021136, reset_time: '2026-10-21T01:00:00Z' } + } + }) + const wrapper = mount(AccountUsageCell, { + props: { account: makeAccount({ id: 6401 }) }, + global: { stubs: { AccountQuotaInfo: true } } + }) + await flushPromises() + expect(wrapper.text()).toContain('0.02%') + wrapper.unmount() + }) + beforeEach(() => { getUsage.mockReset() Object.defineProperty(window, 'matchMedia', { diff --git a/frontend/src/components/account/__tests__/UsageProgressBar.spec.ts b/frontend/src/components/account/__tests__/UsageProgressBar.spec.ts index 2b954c748f..d3f74bbd17 100644 --- a/frontend/src/components/account/__tests__/UsageProgressBar.spec.ts +++ b/frontend/src/components/account/__tests__/UsageProgressBar.spec.ts @@ -13,6 +13,28 @@ vi.mock('vue-i18n', async () => { }) describe('UsageProgressBar', () => { + it.each([ + [0.021136, '0.02%'], + [1.983255, '1.98%'], + [0.000001, '<0.01%'], + [0, '0%'], + [100, '100%'], + ])('preserves quota utilization %s with precisePercent', (utilization, expected) => { + const wrapper = mount(UsageProgressBar, { + props: { label: 'Claude', utilization, color: 'amber', precisePercent: true } + }) + expect(wrapper.text()).toContain(expected) + wrapper.unmount() + }) + + it('keeps default percentage rounding for other usage bars', () => { + const wrapper = mount(UsageProgressBar, { + props: { label: '5h', utilization: 0.021136, color: 'amber' } + }) + expect(wrapper.text()).toContain('0%') + wrapper.unmount() + }) + beforeEach(() => { vi.useFakeTimers() vi.setSystemTime(new Date('2026-03-17T00:00:00Z'))