GetUsageStatistics 获取用量统计数据
(startDate, endDate string, tokenId int, modelName string, page, pageSize int)
| 31 | |
| 32 | // GetUsageStatistics 获取用量统计数据 |
| 33 | func GetUsageStatistics(startDate, endDate string, tokenId int, modelName string, page, pageSize int) ([]*UsageStatistics, int64, error) { |
| 34 | var statistics []*UsageStatistics |
| 35 | var total int64 |
| 36 | |
| 37 | query := DB.Model(&UsageStatistics{}) |
| 38 | |
| 39 | // 添加查询条件 |
| 40 | if startDate != "" { |
| 41 | query = query.Where("date >= ?", startDate) |
| 42 | } |
| 43 | if endDate != "" { |
| 44 | query = query.Where("date <= ?", endDate) |
| 45 | } |
| 46 | if tokenId > 0 { |
| 47 | query = query.Where("token_id = ?", tokenId) |
| 48 | } |
| 49 | if modelName != "" { |
| 50 | query = query.Where("model_name LIKE ?", "%"+modelName+"%") |
| 51 | } |
| 52 | |
| 53 | // 获取总数 |
| 54 | err := query.Count(&total).Error |
| 55 | if err != nil { |
| 56 | return nil, 0, err |
| 57 | } |
| 58 | |
| 59 | // 分页查询 |
| 60 | offset := (page - 1) * pageSize |
| 61 | err = query.Order("date DESC, token_id ASC, model_name ASC"). |
| 62 | Offset(offset).Limit(pageSize).Find(&statistics).Error |
| 63 | |
| 64 | return statistics, total, err |
| 65 | } |
| 66 | |
| 67 | // GetMonthlyUsageStatistics 获取月度用量统计数据 |
| 68 | func GetMonthlyUsageStatistics(startDate, endDate string, tokenId int, modelName string, page, pageSize int) ([]*UsageStatistics, int64, error) { |
no outgoing calls
no test coverage detected