
- Removed key management configuration from .env.example and related code. - Updated Makefile to load environment variables for HOST and PORT. - Modified main.go to handle request logging with a wait group for graceful shutdown. - Simplified dashboard statistics handler to focus on key counts and request metrics. - Removed key manager implementation and related interfaces. - Updated proxy server to use atomic counters for round-robin key selection. - Cleaned up unused types and configurations in types.go. - Added package-lock.json for frontend dependencies.
47 lines
1.4 KiB
Go
47 lines
1.4 KiB
Go
package handler
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"gpt-load/internal/models"
|
|
"gpt-load/internal/response"
|
|
)
|
|
|
|
// GetDashboardStats godoc
|
|
// @Summary Get dashboard statistics
|
|
// @Description Get statistics for the dashboard, including key counts and request metrics.
|
|
// @Tags Dashboard
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {object} map[string]interface{}
|
|
// @Router /api/dashboard/stats [get]
|
|
func (s *Server) Stats(c *gin.Context) {
|
|
var totalRequests, successRequests int64
|
|
var groupStats []models.GroupRequestStat
|
|
|
|
// 1. Get total and successful requests from the api_keys table
|
|
s.DB.Model(&models.APIKey{}).Select("SUM(request_count)").Row().Scan(&totalRequests)
|
|
s.DB.Model(&models.APIKey{}).Select("SUM(request_count) - SUM(failure_count)").Row().Scan(&successRequests)
|
|
|
|
// 2. Get request counts per group
|
|
s.DB.Table("api_keys").
|
|
Select("groups.name as group_name, SUM(api_keys.request_count) as request_count").
|
|
Joins("join groups on groups.id = api_keys.group_id").
|
|
Group("groups.name").
|
|
Order("request_count DESC").
|
|
Scan(&groupStats)
|
|
|
|
// 3. Calculate success rate
|
|
var successRate float64
|
|
if totalRequests > 0 {
|
|
successRate = float64(successRequests) / float64(totalRequests) * 100
|
|
}
|
|
|
|
stats := models.DashboardStats{
|
|
TotalRequests: totalRequests,
|
|
SuccessRequests: successRequests,
|
|
SuccessRate: successRate,
|
|
GroupStats: groupStats,
|
|
}
|
|
|
|
response.Success(c, stats)
|
|
} |