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

Function GetUserMonthlyUsageStatistics

controller/usage_statistics_monthly.go:103–186  ·  view source on GitHub ↗

GetUserMonthlyUsageStatistics 获取用户自己的月度用量统计数据

(c *gin.Context)

Source from the content-addressed store, hash-verified

101
102// GetUserMonthlyUsageStatistics 获取用户自己的月度用量统计数据
103func GetUserMonthlyUsageStatistics(c *gin.Context) {
104 userId := c.GetInt("id")
105 if userId == 0 {
106 c.JSON(http.StatusOK, gin.H{
107 "success": false,
108 "message": "用户ID不能为空",
109 })
110 return
111 }
112
113 // 获取查询参数
114 page, _ := strconv.Atoi(c.DefaultQuery("p", "1"))
115 pageSize, _ := strconv.Atoi(c.DefaultQuery("size", "20"))
116 startDate := c.Query("start_date")
117 endDate := c.Query("end_date")
118 tokenId, _ := strconv.Atoi(c.Query("token_id"))
119 modelName := c.Query("model_name")
120
121 // 设置默认查询最近6个月
122 if startDate == "" && endDate == "" {
123 now := time.Now()
124 startDate = now.AddDate(0, -6, 0).Format("2006-01")
125 endDate = now.Format("2006-01")
126 }
127
128 // 验证参数
129 if page < 1 {
130 page = 1
131 }
132 if pageSize < 1 || pageSize > 100 {
133 pageSize = 20
134 }
135
136 // 如果指定了tokenId,需要验证token是否属于当前用户
137 if tokenId > 0 {
138 token, err := model.GetTokenByIds(tokenId, userId)
139 if err != nil {
140 c.JSON(http.StatusOK, gin.H{
141 "success": false,
142 "message": "Token不存在或无权访问",
143 })
144 return
145 }
146 if token.UserId != userId {
147 c.JSON(http.StatusOK, gin.H{
148 "success": false,
149 "message": "无权访问该Token的统计数据",
150 })
151 return
152 }
153 }
154
155 // 查询数据
156 statistics, total, err := model.GetUserMonthlyUsageStatistics(userId, startDate, endDate, tokenId, modelName, page, pageSize)
157 if err != nil {
158 c.JSON(http.StatusOK, gin.H{
159 "success": false,
160 "message": err.Error(),

Callers

nothing calls this directly

Calls 4

GetTokenByIdsFunction · 0.92
ErrorMethod · 0.80

Tested by

no test coverage detected