Skip to content

Commit 0d45d86

Browse files
committed
fix: quota display shows stale cumulative usage after daily/weekly reset
The quota reset mechanism is lazy — quota_daily_used/quota_weekly_used in the database are only reset on the next IncrementQuotaUsed call. The scheduling layer (IsQuotaExceeded) correctly checks period expiry before enforcing limits, so the account remains usable. However, the API response mapper reads the raw DB value without checking expiry, causing the frontend to display cumulative usage (e.g. 110%) even after the reset period has passed. Add IsDailyQuotaPeriodExpired/IsWeeklyQuotaPeriodExpired methods and use them in the mapper to return used=0 when the period has expired.
1 parent 94bba41 commit 0d45d86

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

backend/internal/handler/dto/mappers.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,17 @@ func AccountFromServiceShallow(a *service.Account) *Account {
276276
if limit := a.GetQuotaDailyLimit(); limit > 0 {
277277
out.QuotaDailyLimit = &limit
278278
used := a.GetQuotaDailyUsed()
279+
if a.IsDailyQuotaPeriodExpired() {
280+
used = 0
281+
}
279282
out.QuotaDailyUsed = &used
280283
}
281284
if limit := a.GetQuotaWeeklyLimit(); limit > 0 {
282285
out.QuotaWeeklyLimit = &limit
283286
used := a.GetQuotaWeeklyUsed()
287+
if a.IsWeeklyQuotaPeriodExpired() {
288+
used = 0
289+
}
284290
out.QuotaWeeklyUsed = &used
285291
}
286292
// 固定时间重置配置

backend/internal/service/account.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,6 +1543,24 @@ func isPeriodExpired(periodStart time.Time, dur time.Duration) bool {
15431543
return time.Since(periodStart) >= dur
15441544
}
15451545

1546+
// IsDailyQuotaPeriodExpired 检查日配额周期是否已过期(用于显示层判断是否需要将 used 归零)
1547+
func (a *Account) IsDailyQuotaPeriodExpired() bool {
1548+
start := a.getExtraTime("quota_daily_start")
1549+
if a.GetQuotaDailyResetMode() == "fixed" {
1550+
return a.isFixedDailyPeriodExpired(start)
1551+
}
1552+
return isPeriodExpired(start, 24*time.Hour)
1553+
}
1554+
1555+
// IsWeeklyQuotaPeriodExpired 检查周配额周期是否已过期(用于显示层判断是否需要将 used 归零)
1556+
func (a *Account) IsWeeklyQuotaPeriodExpired() bool {
1557+
start := a.getExtraTime("quota_weekly_start")
1558+
if a.GetQuotaWeeklyResetMode() == "fixed" {
1559+
return a.isFixedWeeklyPeriodExpired(start)
1560+
}
1561+
return isPeriodExpired(start, 7*24*time.Hour)
1562+
}
1563+
15461564
// IsQuotaExceeded 检查 API Key 账号配额是否已超限(任一维度超限即返回 true)
15471565
func (a *Account) IsQuotaExceeded() bool {
15481566
// 总额度

0 commit comments

Comments
 (0)