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

@@ -112,3 +112,73 @@ func (h *MasterHandler) GetSelfRealtime(c *gin.Context) {
}
c.JSON(http.StatusOK, toMasterRealtimeView(stats))
}
// SystemRealtimeView represents system-level realtime statistics
type SystemRealtimeView struct {
QPS int64 `json:"qps"`
RPM int64 `json:"rpm"`
RateLimitedCount int64 `json:"rate_limited_count"`
ByMaster []MasterRealtimeSummaryView `json:"by_master"`
UpdatedAt *int64 `json:"updated_at,omitempty"`
}
// MasterRealtimeSummaryView is a brief summary of a master's realtime stats
type MasterRealtimeSummaryView struct {
MasterID uint `json:"master_id"`
QPS int64 `json:"qps"`
RateLimited bool `json:"rate_limited"`
}
// GetAdminRealtime godoc
// @Summary System-level realtime stats (admin)
// @Description Return aggregated realtime counters across all masters
// @Tags admin
// @Produce json
// @Security AdminAuth
// @Success 200 {object} SystemRealtimeView
// @Failure 500 {object} gin.H
// @Router /admin/realtime [get]
func (h *AdminHandler) GetAdminRealtime(c *gin.Context) {
if h.statsService == nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "stats service not configured"})
return
}
// Get all active master IDs
var masterIDs []uint
if err := h.db.Model(&model.Master{}).
Where("status = ?", "active").
Pluck("id", &masterIDs).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to load masters", "details": err.Error()})
return
}
stats, err := h.statsService.GetSystemRealtimeSnapshot(c.Request.Context(), masterIDs)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to load realtime stats", "details": err.Error()})
return
}
var updatedAt *int64
if stats.UpdatedAt != nil {
sec := stats.UpdatedAt.Unix()
updatedAt = &sec
}
byMaster := make([]MasterRealtimeSummaryView, 0, len(stats.ByMaster))
for _, m := range stats.ByMaster {
byMaster = append(byMaster, MasterRealtimeSummaryView{
MasterID: m.MasterID,
QPS: m.QPS,
RateLimited: m.RateLimited,
})
}
c.JSON(http.StatusOK, SystemRealtimeView{
QPS: stats.TotalQPS,
RPM: stats.TotalRPM,
RateLimitedCount: stats.RateLimitedCount,
ByMaster: byMaster,
UpdatedAt: updatedAt,
})
}