Align Codex usage stats with reset windows

This commit is contained in:
paraline
2026-06-01 11:37:45 +00:00
parent aa69e3947d
commit bf1a2d6dc2
@@ -528,14 +528,14 @@ func (s *AccountUsageService) getOpenAIUsage(ctx context.Context, account *Accou
return usage, nil
}
if stats, err := s.usageLogRepo.GetAccountWindowStats(ctx, account.ID, now.Add(-5*time.Hour)); err == nil {
if stats, err := s.usageLogRepo.GetAccountWindowStats(ctx, account.ID, codexWindowStatsStart(usage.FiveHour, 5*time.Hour, now)); err == nil {
if usage.FiveHour == nil {
usage.FiveHour = &UsageProgress{Utilization: 0}
}
usage.FiveHour.WindowStats = windowStatsFromAccountStats(stats)
}
if stats, err := s.usageLogRepo.GetAccountWindowStats(ctx, account.ID, now.Add(-7*24*time.Hour)); err == nil {
if stats, err := s.usageLogRepo.GetAccountWindowStats(ctx, account.ID, codexWindowStatsStart(usage.SevenDay, 7*24*time.Hour, now)); err == nil {
if usage.SevenDay == nil {
usage.SevenDay = &UsageProgress{Utilization: 0}
}
@@ -1120,6 +1120,13 @@ func buildCodexUsageProgressFromExtra(extra map[string]any, window string, now t
return progress
}
func codexWindowStatsStart(progress *UsageProgress, fallbackWindow time.Duration, now time.Time) time.Time {
if progress != nil && progress.ResetsAt != nil && now.Before(*progress.ResetsAt) {
return progress.ResetsAt.Add(-fallbackWindow)
}
return now.Add(-fallbackWindow)
}
func (s *AccountUsageService) GetAccountUsageStats(ctx context.Context, accountID int64, startTime, endTime time.Time) (*usagestats.AccountUsageStatsResponse, error) {
stats, err := s.usageLogRepo.GetAccountUsageStats(ctx, accountID, startTime, endTime)
if err != nil {