-
Notifications
You must be signed in to change notification settings - Fork 17
fix: resolve #56 - 支持 Quota #334
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| package quota | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/awsl-project/maxx/internal/domain" | ||
| "github.com/awsl-project/maxx/internal/repository" | ||
| ) | ||
|
|
||
| const defaultWarningThreshold = 80 | ||
|
|
||
| type UsageSummaryProvider interface { | ||
| GetSummary(tenantID uint64, filter repository.UsageStatsFilter) (*domain.UsageStatsSummary, error) | ||
| } | ||
|
|
||
| type TimezoneSettingProvider interface { | ||
| Get(key string) (string, error) | ||
| } | ||
|
|
||
| // EvaluateProviderQuota evaluates provider-level quota usage for the current period. | ||
| func EvaluateProviderQuota( | ||
| provider *domain.Provider, | ||
| tenantID uint64, | ||
| usageRepo UsageSummaryProvider, | ||
| settingRepo TimezoneSettingProvider, | ||
| now time.Time, | ||
| ) (*domain.ProviderQuotaStatus, error) { | ||
| if provider == nil || provider.Config == nil || provider.Config.Quota == nil { | ||
| return nil, nil | ||
| } | ||
|
|
||
| cfg := provider.Config.Quota | ||
| period := normalizePeriod(cfg.Period) | ||
| warningThreshold := normalizeWarningThreshold(cfg.WarningThresholdPercent) | ||
| hasAnyLimit := cfg.RequestLimit > 0 || cfg.TokenLimit > 0 || cfg.CostLimit > 0 | ||
|
|
||
| loc, timezoneName := resolveLocation(settingRepo) | ||
| periodStart, periodEnd := periodBounds(now, period, loc) | ||
|
|
||
| status := &domain.ProviderQuotaStatus{ | ||
| Enabled: cfg.Enabled, | ||
| Period: period, | ||
| Timezone: timezoneName, | ||
| WarningThresholdPercent: warningThreshold, | ||
| PeriodStart: periodStart.UTC(), | ||
| PeriodEnd: periodEnd.UTC(), | ||
| HasAnyLimit: hasAnyLimit, | ||
| } | ||
|
|
||
| if !cfg.Enabled || !hasAnyLimit || usageRepo == nil { | ||
| return status, nil | ||
| } | ||
|
|
||
| providerID := provider.ID | ||
| filter := repository.UsageStatsFilter{ | ||
| Granularity: domain.GranularityMinute, | ||
| StartTime: ptrTime(periodStart.UTC()), | ||
| EndTime: ptrTime(periodEnd.UTC()), | ||
| ProviderID: &providerID, | ||
| } | ||
|
|
||
| summary, err := usageRepo.GetSummary(tenantID, filter) | ||
| if err != nil { | ||
| return status, fmt.Errorf("get provider quota summary: %w", err) | ||
| } | ||
| if summary == nil { | ||
| summary = &domain.UsageStatsSummary{} | ||
| } | ||
|
|
||
| usedTokens := summary.TotalInputTokens + summary.TotalOutputTokens | ||
| status.Requests = buildMetric(cfg.RequestLimit, summary.TotalRequests, warningThreshold) | ||
| status.Tokens = buildMetric(cfg.TokenLimit, usedTokens, warningThreshold) | ||
| status.Cost = buildMetric(cfg.CostLimit, summary.TotalCost, warningThreshold) | ||
|
|
||
| if status.Requests != nil { | ||
| status.Warning = status.Warning || status.Requests.Warning | ||
| status.Exceeded = status.Exceeded || status.Requests.Exceeded | ||
| } | ||
| if status.Tokens != nil { | ||
| status.Warning = status.Warning || status.Tokens.Warning | ||
| status.Exceeded = status.Exceeded || status.Tokens.Exceeded | ||
| } | ||
| if status.Cost != nil { | ||
| status.Warning = status.Warning || status.Cost.Warning | ||
| status.Exceeded = status.Exceeded || status.Cost.Exceeded | ||
| } | ||
|
|
||
| return status, nil | ||
| } | ||
|
|
||
| func buildMetric(limit uint64, used uint64, warningThreshold int) *domain.ProviderQuotaMetricStatus { | ||
| if limit == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| remaining := uint64(0) | ||
| if used < limit { | ||
| remaining = limit - used | ||
| } | ||
|
|
||
| usagePercent := float64(used) / float64(limit) * 100 | ||
| warning := usagePercent >= float64(warningThreshold) | ||
| exceeded := used >= limit | ||
|
|
||
| return &domain.ProviderQuotaMetricStatus{ | ||
| Limit: limit, | ||
| Used: used, | ||
| Remaining: remaining, | ||
| UsagePercent: usagePercent, | ||
| Warning: warning, | ||
| Exceeded: exceeded, | ||
| } | ||
| } | ||
|
|
||
| func normalizePeriod(period domain.ProviderQuotaPeriod) domain.ProviderQuotaPeriod { | ||
| switch period { | ||
| case domain.ProviderQuotaPeriodWeek, domain.ProviderQuotaPeriodMonth: | ||
| return period | ||
| default: | ||
| return domain.ProviderQuotaPeriodDay | ||
| } | ||
| } | ||
|
|
||
| func normalizeWarningThreshold(v int) int { | ||
| if v <= 0 || v > 100 { | ||
| return defaultWarningThreshold | ||
| } | ||
| return v | ||
| } | ||
|
|
||
| func resolveLocation(settingRepo TimezoneSettingProvider) (*time.Location, string) { | ||
| if settingRepo == nil { | ||
| return time.UTC, "UTC" | ||
| } | ||
|
|
||
| name, err := settingRepo.Get(domain.SettingKeyTimezone) | ||
| if err != nil { | ||
| return time.UTC, "UTC" | ||
| } | ||
| name = strings.TrimSpace(name) | ||
| if name == "" { | ||
| return time.UTC, "UTC" | ||
| } | ||
|
|
||
| loc, err := time.LoadLocation(name) | ||
| if err != nil { | ||
| return time.UTC, "UTC" | ||
| } | ||
| return loc, name | ||
| } | ||
|
|
||
| func periodBounds(now time.Time, period domain.ProviderQuotaPeriod, loc *time.Location) (time.Time, time.Time) { | ||
| localNow := now.In(loc) | ||
| year, month, day := localNow.Date() | ||
| startOfDay := time.Date(year, month, day, 0, 0, 0, 0, loc) | ||
|
|
||
| switch period { | ||
| case domain.ProviderQuotaPeriodWeek: | ||
| weekday := int(startOfDay.Weekday()) | ||
| if weekday == 0 { | ||
| weekday = 7 | ||
| } | ||
| start := startOfDay.AddDate(0, 0, -(weekday - 1)) | ||
| return start, start.AddDate(0, 0, 7) | ||
| case domain.ProviderQuotaPeriodMonth: | ||
| start := time.Date(year, month, 1, 0, 0, 0, 0, loc) | ||
| return start, start.AddDate(0, 1, 0) | ||
| default: | ||
| return startOfDay, startOfDay.AddDate(0, 0, 1) | ||
| } | ||
| } | ||
|
|
||
| func ptrTime(t time.Time) *time.Time { | ||
| return &t | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
默认时区回退成 UTC 会把配额周期算偏。
这里在设置缺失、读取失败或时区非法时一律回退到
UTC,但internal/domain/model.go对SettingKeyTimezone的注释已经声明默认值是Asia/Shanghai。这会让新实例或设置暂时不可读时的日/周/月窗口整体偏移,进而提前或延后判定 provider 超额,直接影响路由结果。建议这里复用系统默认时区,而不是硬编码成UTC。🤖 Prompt for AI Agents