feat(api): add dashboard summary and system realtime endpoints

Add new admin API endpoints for dashboard metrics and system-wide
realtime statistics:

- Add /admin/dashboard/summary endpoint with aggregated metrics
  including requests, tokens, latency, masters, keys, and provider
  keys statistics with time period filtering
- Add /admin/realtime endpoint for system-level realtime stats
  aggregated across all masters
- Add status filter parameter to ListAPIKeys endpoint
- Add hour grouping option to log stats aggregation
- Update OpenAPI documentation with new endpoints and schemas
This commit is contained in:
zenfun
2025-12-31 13:17:23 +08:00
parent 1a2cc5b798
commit 53c18c3867
9 changed files with 1644 additions and 21 deletions

View File

@@ -153,3 +153,69 @@ func readCmdTime(cmd *redis.StringCmd) *time.Time {
tm := time.Unix(v, 0).UTC()
return &tm
}
// SystemRealtimeSnapshot contains aggregated realtime stats across all masters
type SystemRealtimeSnapshot struct {
TotalQPS int64 `json:"qps"`
TotalRPM int64 `json:"rpm"`
RateLimitedCount int64 `json:"rate_limited_count"`
ByMaster []MasterRealtimeSummary `json:"by_master"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
}
// MasterRealtimeSummary is a brief summary of a master's realtime stats
type MasterRealtimeSummary struct {
MasterID uint `json:"master_id"`
QPS int64 `json:"qps"`
RateLimited bool `json:"rate_limited"`
}
// GetSystemRealtimeSnapshot aggregates realtime stats across all masters
func (s *StatsService) GetSystemRealtimeSnapshot(ctx context.Context, masterIDs []uint) (SystemRealtimeSnapshot, error) {
if s == nil || s.rdb == nil {
return SystemRealtimeSnapshot{}, fmt.Errorf("redis client is required")
}
if ctx == nil {
ctx = context.Background()
}
var totalQPS int64
var rateLimitedCount int64
byMaster := make([]MasterRealtimeSummary, 0, len(masterIDs))
var latestUpdatedAt *time.Time
for _, masterID := range masterIDs {
snapshot, err := s.GetMasterRealtimeSnapshot(ctx, masterID)
if err != nil {
continue // Skip masters with errors
}
totalQPS += snapshot.QPS
if snapshot.RateLimited {
rateLimitedCount++
}
byMaster = append(byMaster, MasterRealtimeSummary{
MasterID: masterID,
QPS: snapshot.QPS,
RateLimited: snapshot.RateLimited,
})
if snapshot.UpdatedAt != nil {
if latestUpdatedAt == nil || snapshot.UpdatedAt.After(*latestUpdatedAt) {
latestUpdatedAt = snapshot.UpdatedAt
}
}
}
// Estimate RPM as QPS * 60 (since we're measuring requests per second)
totalRPM := totalQPS * 60
return SystemRealtimeSnapshot{
TotalQPS: totalQPS,
TotalRPM: totalRPM,
RateLimitedCount: rateLimitedCount,
ByMaster: byMaster,
UpdatedAt: latestUpdatedAt,
}, nil
}