refactor: statistics.go.
This commit is contained in:
+76
-16
@@ -111,19 +111,21 @@ func (sc *StatisticsController) DeleteAIAnalysisRecord(c *gin.Context) {
|
||||
// --- 统计接口 ---
|
||||
|
||||
type regionStatisticsItem struct {
|
||||
RegionID uint32 `json:"regionId"`
|
||||
KindergartenName string `json:"kindergartenName"`
|
||||
Count int64 `json:"count"`
|
||||
TotalInputTokens int64 `json:"totalInputTokens"`
|
||||
TotalOutputTokens int64 `json:"totalOutputTokens"`
|
||||
TotalInputSizeBytes int64 `json:"totalInputSizeBytes"`
|
||||
TotalOutputSizeBytes int64 `json:"totalOutputSizeBytes"`
|
||||
TotalDurationMs int64 `json:"totalDurationMs"`
|
||||
AvgDurationMs float64 `json:"avgDurationMs"`
|
||||
TotalOriginalFileSize int64 `json:"totalOriginalFileSize"`
|
||||
TotalCompressedSize int64 `json:"totalCompressedSize"`
|
||||
FirstUsedAt *time.Time `json:"firstUsedAt"`
|
||||
LastUsedAt *time.Time `json:"lastUsedAt"`
|
||||
RegionID uint32 `json:"regionId"`
|
||||
KindergartenName string `json:"kindergartenName"`
|
||||
Count int64 `json:"count"`
|
||||
TotalInputTokens int64 `json:"totalInputTokens"`
|
||||
TotalOutputTokens int64 `json:"totalOutputTokens"`
|
||||
TotalInputSizeBytes int64 `json:"totalInputSizeBytes"`
|
||||
TotalOutputSizeBytes int64 `json:"totalOutputSizeBytes"`
|
||||
TotalDurationMs int64 `json:"totalDurationMs"`
|
||||
AvgDurationMs float64 `json:"avgDurationMs"`
|
||||
TotalOriginalFileSize int64 `json:"totalOriginalFileSize"`
|
||||
TotalCompressedSize int64 `json:"totalCompressedSize"`
|
||||
AnalysisTypeCounts map[string]int64 `json:"analysisTypeCounts"`
|
||||
SourceTypeCounts map[string]int64 `json:"sourceTypeCounts"`
|
||||
FirstUsedAt *time.Time `json:"firstUsedAt"`
|
||||
LastUsedAt *time.Time `json:"lastUsedAt"`
|
||||
}
|
||||
|
||||
func (sc *StatisticsController) StatisticsByRegion(c *gin.Context) {
|
||||
@@ -173,8 +175,8 @@ func (sc *StatisticsController) StatisticsByRegion(c *gin.Context) {
|
||||
COALESCE(SUM(duration_ms), 0) as total_duration_ms,
|
||||
COALESCE(SUM(original_file_size), 0) as total_original_file_size,
|
||||
COALESCE(SUM(compressed_content_size), 0) as total_compressed_size,
|
||||
MIN(created_at) as first_used_at,
|
||||
MAX(created_at) as last_used_at
|
||||
MIN(upload_time) as first_used_at,
|
||||
MAX(upload_time) as last_used_at
|
||||
`).Group("region_id").Scan(&rawResults).Error
|
||||
|
||||
if err != nil {
|
||||
@@ -182,6 +184,52 @@ func (sc *StatisticsController) StatisticsByRegion(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
type analysisTypeCount struct {
|
||||
RegionID *uint32
|
||||
AnalysisType string
|
||||
Count int64
|
||||
}
|
||||
var analysisTypeResults []analysisTypeCount
|
||||
if err := query.Select("region_id, analysis_type, COUNT(*) as count").Group("region_id, analysis_type").Scan(&analysisTypeResults).Error; err != nil {
|
||||
writeError(c, http.StatusInternalServerError, "failed to query analysis type statistics")
|
||||
return
|
||||
}
|
||||
|
||||
type sourceTypeCount struct {
|
||||
RegionID *uint32
|
||||
SourceType string
|
||||
Count int64
|
||||
}
|
||||
var sourceTypeResults []sourceTypeCount
|
||||
if err := query.Select("region_id, source_type, COUNT(*) as count").Group("region_id, source_type").Scan(&sourceTypeResults).Error; err != nil {
|
||||
writeError(c, http.StatusInternalServerError, "failed to query source type statistics")
|
||||
return
|
||||
}
|
||||
|
||||
analysisTypeMap := make(map[uint32]map[string]int64)
|
||||
for _, r := range analysisTypeResults {
|
||||
regionID := uint32(0)
|
||||
if r.RegionID != nil {
|
||||
regionID = *r.RegionID
|
||||
}
|
||||
if analysisTypeMap[regionID] == nil {
|
||||
analysisTypeMap[regionID] = make(map[string]int64)
|
||||
}
|
||||
analysisTypeMap[regionID][r.AnalysisType] = r.Count
|
||||
}
|
||||
|
||||
sourceTypeMap := make(map[uint32]map[string]int64)
|
||||
for _, r := range sourceTypeResults {
|
||||
regionID := uint32(0)
|
||||
if r.RegionID != nil {
|
||||
regionID = *r.RegionID
|
||||
}
|
||||
if sourceTypeMap[regionID] == nil {
|
||||
sourceTypeMap[regionID] = make(map[string]int64)
|
||||
}
|
||||
sourceTypeMap[regionID][r.SourceType] = r.Count
|
||||
}
|
||||
|
||||
// 收集所有 regionId 查询幼儿园名称
|
||||
regionIDs := make([]uint32, 0, len(rawResults))
|
||||
for _, r := range rawResults {
|
||||
@@ -199,7 +247,10 @@ func (sc *StatisticsController) StatisticsByRegion(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
overall := regionStatisticsItem{}
|
||||
overall := regionStatisticsItem{
|
||||
AnalysisTypeCounts: make(map[string]int64),
|
||||
SourceTypeCounts: make(map[string]int64),
|
||||
}
|
||||
regions := make(map[string]regionStatisticsItem, len(rawResults))
|
||||
|
||||
for _, r := range rawResults {
|
||||
@@ -227,6 +278,8 @@ func (sc *StatisticsController) StatisticsByRegion(c *gin.Context) {
|
||||
AvgDurationMs: avgDuration,
|
||||
TotalOriginalFileSize: r.TotalOriginalFileSize,
|
||||
TotalCompressedSize: r.TotalCompressedSize,
|
||||
AnalysisTypeCounts: analysisTypeMap[regionID],
|
||||
SourceTypeCounts: sourceTypeMap[regionID],
|
||||
FirstUsedAt: r.FirstUsedAt,
|
||||
LastUsedAt: r.LastUsedAt,
|
||||
}
|
||||
@@ -254,6 +307,13 @@ func (sc *StatisticsController) StatisticsByRegion(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
for _, r := range analysisTypeResults {
|
||||
overall.AnalysisTypeCounts[r.AnalysisType] += r.Count
|
||||
}
|
||||
for _, r := range sourceTypeResults {
|
||||
overall.SourceTypeCounts[r.SourceType] += r.Count
|
||||
}
|
||||
|
||||
if overall.Count > 0 {
|
||||
overall.AvgDurationMs = float64(overall.TotalDurationMs) / float64(overall.Count)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user