GetUsageStatisticsSummary 获取用量统计摘要信息
(startDate, endDate string, tokenId int, modelName string)
| 265 | |
| 266 | // GetUsageStatisticsSummary 获取用量统计摘要信息 |
| 267 | func GetUsageStatisticsSummary(startDate, endDate string, tokenId int, modelName string) (map[string]interface{}, error) { |
| 268 | query := DB.Model(&UsageStatistics{}) |
| 269 | |
| 270 | // 添加查询条件 |
| 271 | if startDate != "" { |
| 272 | query = query.Where("date >= ?", startDate) |
| 273 | } |
| 274 | if endDate != "" { |
| 275 | query = query.Where("date <= ?", endDate) |
| 276 | } |
| 277 | if tokenId > 0 { |
| 278 | query = query.Where("token_id = ?", tokenId) |
| 279 | } |
| 280 | if modelName != "" { |
| 281 | query = query.Where("model_name LIKE ?", "%"+modelName+"%") |
| 282 | } |
| 283 | |
| 284 | var result struct { |
| 285 | TotalRequests int `json:"total_requests"` |
| 286 | SuccessfulRequests int `json:"successful_requests"` |
| 287 | FailedRequests int `json:"failed_requests"` |
| 288 | TotalTokens int `json:"total_tokens"` |
| 289 | TotalQuota int `json:"total_quota"` |
| 290 | } |
| 291 | |
| 292 | err := query.Select( |
| 293 | "SUM(total_requests) as total_requests", |
| 294 | "SUM(successful_requests) as successful_requests", |
| 295 | "SUM(failed_requests) as failed_requests", |
| 296 | "SUM(total_tokens) as total_tokens", |
| 297 | "SUM(total_quota) as total_quota", |
| 298 | ).Scan(&result).Error |
| 299 | |
| 300 | if err != nil { |
| 301 | return nil, err |
| 302 | } |
| 303 | |
| 304 | summary := map[string]interface{}{ |
| 305 | "total_requests": result.TotalRequests, |
| 306 | "successful_requests": result.SuccessfulRequests, |
| 307 | "failed_requests": result.FailedRequests, |
| 308 | "success_rate": 0.0, |
| 309 | "total_tokens": result.TotalTokens, |
| 310 | "total_quota": result.TotalQuota, |
| 311 | } |
| 312 | |
| 313 | if result.TotalRequests > 0 { |
| 314 | summary["success_rate"] = float64(result.SuccessfulRequests) / float64(result.TotalRequests) * 100 |
| 315 | } |
| 316 | |
| 317 | return summary, nil |
| 318 | } |
| 319 | |
| 320 | // GetMonthlyUsageStatisticsSummary 获取月度用量统计摘要信息 |
| 321 | func GetMonthlyUsageStatisticsSummary(startDate, endDate string, tokenId int, modelName string) (map[string]interface{}, error) { |
no test coverage detected