feat(api): add API key stats flush and summary endpoints

Introduce internal endpoint for flushing accumulated APIKey statistics
from data plane to control plane database, updating both individual
API keys and their parent provider groups with request counts and
success/failure rates.

Add admin endpoint to retrieve aggregated API key statistics summary
across all provider groups, including total requests, success/failure
counts, and calculated rates.
This commit is contained in:
zenfun
2025-12-30 00:11:52 +08:00
parent 5156ca9cec
commit 1a2cc5b798
6 changed files with 390 additions and 29 deletions

View File

@@ -9,18 +9,18 @@ import (
// ProviderGroup represents a shared upstream definition.
type ProviderGroup struct {
gorm.Model
Name string `gorm:"size:255;uniqueIndex;not null" json:"name"`
Type string `gorm:"size:50;not null" json:"type"` // openai, anthropic, gemini
BaseURL string `gorm:"size:512;not null" json:"base_url"`
GoogleProject string `gorm:"size:128" json:"google_project,omitempty"`
GoogleLocation string `gorm:"size:64" json:"google_location,omitempty"`
StaticHeaders string `gorm:"type:text" json:"static_headers,omitempty"`
HeadersProfile string `gorm:"size:64" json:"headers_profile,omitempty"`
Models string `json:"models"` // comma-separated list of supported models
Status string `gorm:"size:50;default:'active'" json:"status"`
TotalRequests int64 `gorm:"default:0" json:"total_requests"`
SuccessRequests int64 `gorm:"default:0" json:"success_requests"`
FailureRequests int64 `gorm:"default:0" json:"failure_requests"`
Name string `gorm:"size:255;uniqueIndex;not null" json:"name"`
Type string `gorm:"size:50;not null" json:"type"` // openai, anthropic, gemini
BaseURL string `gorm:"size:512;not null" json:"base_url"`
GoogleProject string `gorm:"size:128" json:"google_project,omitempty"`
GoogleLocation string `gorm:"size:64" json:"google_location,omitempty"`
StaticHeaders string `gorm:"type:text" json:"static_headers,omitempty"`
HeadersProfile string `gorm:"size:64" json:"headers_profile,omitempty"`
Models string `json:"models"` // comma-separated list of supported models
Status string `gorm:"size:50;default:'active'" json:"status"`
TotalRequests int64 `gorm:"default:0" json:"total_requests"`
SuccessRequests int64 `gorm:"default:0" json:"success_requests"`
FailureRequests int64 `gorm:"default:0" json:"failure_requests"`
SuccessRate float64 `gorm:"default:0" json:"success_rate"`
FailureRate float64 `gorm:"default:0" json:"failure_rate"`
}
@@ -28,21 +28,21 @@ type ProviderGroup struct {
// APIKey represents a credential within a provider group.
type APIKey struct {
gorm.Model
GroupID uint `gorm:"not null;index" json:"group_id"`
APIKey string `gorm:"not null" json:"api_key"`
AccessToken string `gorm:"type:text" json:"access_token,omitempty"`
RefreshToken string `gorm:"type:text" json:"-"`
ExpiresAt *time.Time `json:"expires_at,omitempty"`
AccountID string `gorm:"size:255" json:"account_id,omitempty"`
ProjectID string `gorm:"size:255" json:"project_id,omitempty"`
Weight int `gorm:"default:1" json:"weight"`
Status string `gorm:"size:50;default:'active'" json:"status"`
AutoBan bool `gorm:"default:true" json:"auto_ban"`
BanReason string `gorm:"size:255" json:"ban_reason"`
BanUntil *time.Time `json:"ban_until"`
TotalRequests int64 `gorm:"default:0" json:"total_requests"`
SuccessRequests int64 `gorm:"default:0" json:"success_requests"`
FailureRequests int64 `gorm:"default:0" json:"failure_requests"`
SuccessRate float64 `gorm:"default:0" json:"success_rate"`
FailureRate float64 `gorm:"default:0" json:"failure_rate"`
GroupID uint `gorm:"not null;index" json:"group_id"`
APIKey string `gorm:"not null" json:"api_key"`
AccessToken string `gorm:"type:text" json:"access_token,omitempty"`
RefreshToken string `gorm:"type:text" json:"-"`
ExpiresAt *time.Time `json:"expires_at,omitempty"`
AccountID string `gorm:"size:255" json:"account_id,omitempty"`
ProjectID string `gorm:"size:255" json:"project_id,omitempty"`
Weight int `gorm:"default:1" json:"weight"`
Status string `gorm:"size:50;default:'active'" json:"status"`
AutoBan bool `gorm:"default:true" json:"auto_ban"`
BanReason string `gorm:"size:255" json:"ban_reason"`
BanUntil *time.Time `json:"ban_until"`
TotalRequests int64 `gorm:"default:0" json:"total_requests"`
SuccessRequests int64 `gorm:"default:0" json:"success_requests"`
FailureRequests int64 `gorm:"default:0" json:"failure_requests"`
SuccessRate float64 `gorm:"default:0" json:"success_rate"`
FailureRate float64 `gorm:"default:0" json:"failure_rate"`
}