Skip to content

Commit 553a486

Browse files
authored
Merge pull request Wei-Shaw#1171 from wucm667/fix/quota-display-stale-after-reset
fix: quota display shows stale cumulative usage after daily/weekly reset
2 parents c73374a + 0d45d86 commit 553a486

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)