- 新增 domain 层:model、provider、route、stats 实体 - 新增 service 层:models、providers、routing、stats 业务逻辑 - 新增 repository 层:models、providers、stats 数据访问 - 新增 pkg 工具包:errors、logger、validator - 新增中间件:CORS、logging、recovery、request ID - 新增数据库迁移:初始 schema 和索引 - 新增单元测试和集成测试 - 新增规范文档:config-management、database-migration、error-handling、layered-architecture、middleware-system、request-validation、structured-logging、test-coverage - 移除 config 子包和 model_router(已迁移至分层架构)
107 lines
2.4 KiB
Go
107 lines
2.4 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"nex/backend/internal/service"
|
|
)
|
|
|
|
// StatsHandler 统计处理器
|
|
type StatsHandler struct {
|
|
statsService service.StatsService
|
|
}
|
|
|
|
// NewStatsHandler 创建统计处理器
|
|
func NewStatsHandler(statsService service.StatsService) *StatsHandler {
|
|
return &StatsHandler{statsService: statsService}
|
|
}
|
|
|
|
// 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 := h.statsService.Get(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")
|
|
|
|
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 := h.statsService.Get(providerID, modelName, startDate, endDate)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"error": "查询统计失败: " + err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
result := h.statsService.Aggregate(stats, groupBy)
|
|
c.JSON(http.StatusOK, result)
|
|
}
|