1
0
Files
nex/backend/internal/handler/stats_handler.go
lanyuanxiaoyao 915b004924 feat: 初始化 AI Gateway 项目
实现支持 OpenAI 和 Anthropic 双协议的统一大模型 API 网关 MVP 版本,包含:
- OpenAI 和 Anthropic 协议代理
- 供应商和模型管理
- 用量统计
- 前端配置界面
2026-04-15 16:53:28 +08:00

185 lines
4.3 KiB
Go

package handler
import (
"net/http"
"time"
"github.com/gin-gonic/gin"
"nex/backend/internal/config"
)
// StatsHandler 统计处理器
type StatsHandler struct{}
// NewStatsHandler 创建统计处理器
func NewStatsHandler() *StatsHandler {
return &StatsHandler{}
}
// GetStats 查询统计
func (h *StatsHandler) GetStats(c *gin.Context) {
// 解析查询参数
providerID := c.Query("provider_id")
modelName := c.Query("model_name")
startDateStr := c.Query("start_date")
endDateStr := c.Query("end_date")
var startDate, endDate *time.Time
// 解析日期
if startDateStr != "" {
t, err := time.Parse("2006-01-02", startDateStr)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": "无效的 start_date 格式,应为 YYYY-MM-DD",
})
return
}
startDate = &t
}
if endDateStr != "" {
t, err := time.Parse("2006-01-02", endDateStr)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": "无效的 end_date 格式,应为 YYYY-MM-DD",
})
return
}
endDate = &t
}
// 查询统计
stats, err := config.GetStats(providerID, modelName, startDate, endDate)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": "查询统计失败: " + err.Error(),
})
return
}
c.JSON(http.StatusOK, stats)
}
// AggregateStats 聚合统计
func (h *StatsHandler) AggregateStats(c *gin.Context) {
// 解析查询参数
providerID := c.Query("provider_id")
modelName := c.Query("model_name")
startDateStr := c.Query("start_date")
endDateStr := c.Query("end_date")
groupBy := c.Query("group_by") // "provider", "model", "date"
var startDate, endDate *time.Time
// 解析日期
if startDateStr != "" {
t, err := time.Parse("2006-01-02", startDateStr)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": "无效的 start_date 格式,应为 YYYY-MM-DD",
})
return
}
startDate = &t
}
if endDateStr != "" {
t, err := time.Parse("2006-01-02", endDateStr)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": "无效的 end_date 格式,应为 YYYY-MM-DD",
})
return
}
endDate = &t
}
// 查询统计
stats, err := config.GetStats(providerID, modelName, startDate, endDate)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": "查询统计失败: " + err.Error(),
})
return
}
// 聚合
result := h.aggregate(stats, groupBy)
c.JSON(http.StatusOK, result)
}
// aggregate 执行聚合
func (h *StatsHandler) aggregate(stats []config.UsageStats, groupBy string) []map[string]interface{} {
switch groupBy {
case "provider":
return h.aggregateByProvider(stats)
case "model":
return h.aggregateByModel(stats)
case "date":
return h.aggregateByDate(stats)
default:
// 默认按供应商聚合
return h.aggregateByProvider(stats)
}
}
// aggregateByProvider 按供应商聚合
func (h *StatsHandler) aggregateByProvider(stats []config.UsageStats) []map[string]interface{} {
aggregated := make(map[string]int)
for _, stat := range stats {
aggregated[stat.ProviderID] += stat.RequestCount
}
result := make([]map[string]interface{}, 0, len(aggregated))
for providerID, count := range aggregated {
result = append(result, map[string]interface{}{
"provider_id": providerID,
"request_count": count,
})
}
return result
}
// aggregateByModel 按模型聚合
func (h *StatsHandler) aggregateByModel(stats []config.UsageStats) []map[string]interface{} {
aggregated := make(map[string]int)
for _, stat := range stats {
key := stat.ProviderID + "/" + stat.ModelName
aggregated[key] += stat.RequestCount
}
result := make([]map[string]interface{}, 0, len(aggregated))
for key, count := range aggregated {
result = append(result, map[string]interface{}{
"provider_id": key[:len(key)/2],
"model_name": key[len(key)/2+1:],
"request_count": count,
})
}
return result
}
// aggregateByDate 按日期聚合
func (h *StatsHandler) aggregateByDate(stats []config.UsageStats) []map[string]interface{} {
aggregated := make(map[string]int)
for _, stat := range stats {
key := stat.Date.Format("2006-01-02")
aggregated[key] += stat.RequestCount
}
result := make([]map[string]interface{}, 0, len(aggregated))
for date, count := range aggregated {
result = append(result, map[string]interface{}{
"date": date,
"request_count": count,
})
}
return result
}