feat: 实现分层架构,包含 domain、service、repository 和 pkg 层
- 新增 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(已迁移至分层架构)
This commit is contained in:
@@ -6,20 +6,21 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"nex/backend/internal/config"
|
||||
"nex/backend/internal/service"
|
||||
)
|
||||
|
||||
// StatsHandler 统计处理器
|
||||
type StatsHandler struct{}
|
||||
type StatsHandler struct {
|
||||
statsService service.StatsService
|
||||
}
|
||||
|
||||
// NewStatsHandler 创建统计处理器
|
||||
func NewStatsHandler() *StatsHandler {
|
||||
return &StatsHandler{}
|
||||
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")
|
||||
@@ -27,7 +28,6 @@ func (h *StatsHandler) GetStats(c *gin.Context) {
|
||||
|
||||
var startDate, endDate *time.Time
|
||||
|
||||
// 解析日期
|
||||
if startDateStr != "" {
|
||||
t, err := time.Parse("2006-01-02", startDateStr)
|
||||
if err != nil {
|
||||
@@ -50,8 +50,7 @@ func (h *StatsHandler) GetStats(c *gin.Context) {
|
||||
endDate = &t
|
||||
}
|
||||
|
||||
// 查询统计
|
||||
stats, err := config.GetStats(providerID, modelName, startDate, endDate)
|
||||
stats, err := h.statsService.Get(providerID, modelName, startDate, endDate)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"error": "查询统计失败: " + err.Error(),
|
||||
@@ -64,16 +63,14 @@ func (h *StatsHandler) GetStats(c *gin.Context) {
|
||||
|
||||
// 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"
|
||||
groupBy := c.Query("group_by")
|
||||
|
||||
var startDate, endDate *time.Time
|
||||
|
||||
// 解析日期
|
||||
if startDateStr != "" {
|
||||
t, err := time.Parse("2006-01-02", startDateStr)
|
||||
if err != nil {
|
||||
@@ -96,8 +93,7 @@ func (h *StatsHandler) AggregateStats(c *gin.Context) {
|
||||
endDate = &t
|
||||
}
|
||||
|
||||
// 查询统计
|
||||
stats, err := config.GetStats(providerID, modelName, startDate, endDate)
|
||||
stats, err := h.statsService.Get(providerID, modelName, startDate, endDate)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"error": "查询统计失败: " + err.Error(),
|
||||
@@ -105,80 +101,6 @@ func (h *StatsHandler) AggregateStats(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 聚合
|
||||
result := h.aggregate(stats, groupBy)
|
||||
|
||||
result := h.statsService.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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user