## 高优先级修复 - stats_service_impl: 使用 strings.SplitN 替代错误的索引分割 - provider_handler: 使用 errors.Is(err, gorm.ErrDuplicatedKey) 替代字符串匹配 - client: 重写 isNetworkError 使用 errors.As/Is 类型安全判断 - proxy_handler: 使用 encoding/json 标准库解析 JSON(extractModelName、isStreamRequest) ## 中优先级修复 - stats_handler: 添加 parseDateParam 辅助函数消除重复日期解析 - pkg/errors: 新增 ErrRequestCreate/Send/ResponseRead 错误类型和 WithCause 方法 - client: 使用结构化错误替代 fmt.Errorf - ConversionEngine: logger 依赖注入,替换所有 zap.L() 调用 ## 低优先级修复 - encoder: 删除 joinStrings,使用 strings.Join - adapter: 删除 modelInfoRegex 正则,使用 isModelInfoPath 字符串函数 ## 文档更新 - README.md: 添加公共库使用指南和编码规范章节 - specs: 同步 delta specs 到 main specs(error-handling、structured-logging、request-validation) ## 归档 - openspec/changes/archive/2026-04-20-refactor-backend-code-quality/
91 lines
2.1 KiB
Go
91 lines
2.1 KiB
Go
package handler
|
|
|
|
import (
|
|
"fmt"
|
|
"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")
|
|
|
|
startDate, err := parseDateParam(c, "start_date")
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
endDate, err := parseDateParam(c, "end_date")
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
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")
|
|
groupBy := c.Query("group_by")
|
|
|
|
startDate, err := parseDateParam(c, "start_date")
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
endDate, err := parseDateParam(c, "end_date")
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
// parseDateParam 解析日期查询参数
|
|
func parseDateParam(c *gin.Context, paramName string) (*time.Time, error) {
|
|
dateStr := c.Query(paramName)
|
|
if dateStr == "" {
|
|
return nil, nil
|
|
}
|
|
t, err := time.Parse("2006-01-02", dateStr)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("无效的 %s 格式,应为 YYYY-MM-DD", paramName)
|
|
}
|
|
return &t, nil
|
|
}
|