MCPcopy Create free account
hub / github.com/aiprodcoder/MIXAPI / GetMonthlyUsageStatistics

Function GetMonthlyUsageStatistics

model/usage_statistics.go:68–152  ·  view source on GitHub ↗

GetMonthlyUsageStatistics 获取月度用量统计数据

(startDate, endDate string, tokenId int, modelName string, page, pageSize int)

Source from the content-addressed store, hash-verified

66
67// GetMonthlyUsageStatistics 获取月度用量统计数据
68func GetMonthlyUsageStatistics(startDate, endDate string, tokenId int, modelName string, page, pageSize int) ([]*UsageStatistics, int64, error) {
69 var statistics []*UsageStatistics
70 var total int64
71
72 // 使用原生SQL查询实现按月分组统计
73 db := DB.Model(&UsageStatistics{})
74
75 // 构建查询条件
76 conditions := ""
77 params := []interface{}{}
78
79 // 添加日期范围条件(按月查询)
80 if startDate != "" {
81 conditions += " AND date >= ?"
82 params = append(params, startDate+"-01")
83 }
84 if endDate != "" {
85 // 获取 endDate 所在月份的最后一天
86 if len(endDate) >= 7 {
87 year := endDate[0:4]
88 month := endDate[5:7]
89 conditions += " AND date <= ?"
90 params = append(params, year+"-"+month+"-31")
91 }
92 }
93
94 if tokenId > 0 {
95 conditions += " AND token_id = ?"
96 params = append(params, tokenId)
97 }
98 if modelName != "" {
99 conditions += " AND model_name LIKE ?"
100 params = append(params, "%"+modelName+"%")
101 }
102
103 // 构建完整的SQL查询
104 sql := `
105 SELECT
106 MAX(id) as id,
107 SUBSTR(date, 1, 7) as date,
108 token_id,
109 token_name,
110 model_name,
111 SUM(total_requests) as total_requests,
112 SUM(successful_requests) as successful_requests,
113 SUM(failed_requests) as failed_requests,
114 SUM(total_tokens) as total_tokens,
115 SUM(prompt_tokens) as prompt_tokens,
116 SUM(completion_tokens) as completion_tokens,
117 SUM(total_quota) as total_quota,
118 MAX(created_time) as created_time,
119 MAX(updated_time) as updated_time
120 FROM usage_statistics
121 WHERE 1=1` + conditions + `
122 GROUP BY SUBSTR(date, 1, 7), token_id, token_name, model_name
123 ORDER BY date DESC, token_id ASC, model_name ASC
124 `
125

Callers 1

Calls 1

ScanMethod · 0.45

Tested by

no test coverage detected