feat: show current-cycle daily token peak

This commit is contained in:
2026-08-14 14:50:02 +08:00
parent 915d26d51d
commit c1985852e1
7 changed files with 48 additions and 5 deletions
+24
View File
@@ -533,6 +533,7 @@ func (a *App) syncAccount(ctx context.Context, id int64) error {
_, _ = a.store.DB.Exec("INSERT INTO daily_usage(account_id,date,total_tokens,fetched_at) VALUES(?,?,?,?) ON CONFLICT(account_id,date) DO UPDATE SET total_tokens=excluded.total_tokens,fetched_at=excluded.fetched_at", id, x.StartDate, x.Tokens, d.FetchedAt)
}
d.CurrentCycle = currentTokenCycle(d.Limits, d.Usage, d.FetchedAt)
d.Summary.PeakDailyTokens = peakDailyTokensForCycle(d.CurrentCycle, d.Usage, d.FetchedAt)
}
resetDetected, e := a.storeLimitSnapshots(d)
if e != nil {
@@ -642,6 +643,29 @@ func currentTokenCycle(limits []LimitBucket, usage []UsagePoint, fetchedAt int64
}
}
func peakDailyTokensForCycle(cycle *TokenCycle, usage []UsagePoint, fetchedAt int64) *int64 {
if cycle == nil {
return nil
}
startDate := time.Unix(cycle.StartedAt, 0).UTC().Format("2006-01-02")
endDate := time.Unix(fetchedAt, 0).UTC().Format("2006-01-02")
var peak int64
found := false
for _, point := range usage {
if _, err := time.Parse("2006-01-02", point.Date); err != nil || point.Date < startDate || point.Date > endDate {
continue
}
if !found || point.TotalTokens > peak {
peak = point.TotalTokens
found = true
}
}
if !found {
return nil
}
return &peak
}
func betterTokenCycleLimit(candidate, current LimitBucket) bool {
if candidate.WindowDurationMinutes != current.WindowDurationMinutes {
return candidate.WindowDurationMinutes > current.WindowDurationMinutes