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

Function GetChannelStatistics

model/statistics_charts.go:35–160  ·  view source on GitHub ↗

GetChannelStatistics 获取按渠道统计的数据

(startTimestamp, endTimestamp int, username, tokenName, modelName string, channel int, group, defaultTime string)

Source from the content-addressed store, hash-verified

33
34// GetChannelStatistics 获取按渠道统计的数据
35func GetChannelStatistics(startTimestamp, endTimestamp int, username, tokenName, modelName string, channel int, group, defaultTime string) ([]ChannelStatistics, error) {
36 var statistics []ChannelStatistics
37
38 // 构建查询条件
39 tx := LOG_DB.Table("logs").Select(`
40 created_at,
41 channel_id,
42 COUNT(*) as count,
43 SUM(quota) as quota
44 `).Where("type = ?", 2) // LogTypeConsume = 2
45
46 if username != "" {
47 tx = tx.Where("username = ?", username)
48 }
49 if tokenName != "" {
50 tx = tx.Where("token_name = ?", tokenName)
51 }
52 if modelName != "" {
53 tx = tx.Where("model_name LIKE ?", "%"+modelName+"%")
54 }
55 if channel != 0 {
56 tx = tx.Where("channel_id = ?", channel)
57 }
58 if group != "" {
59 tx = tx.Where("`group` = ?", group)
60 }
61 if startTimestamp != 0 {
62 tx = tx.Where("created_at >= ?", startTimestamp)
63 }
64 if endTimestamp != 0 {
65 tx = tx.Where("created_at <= ?", endTimestamp)
66 }
67
68 // 按时间粒度分组 - 使用数据库无关的函数
69 var groupBy string
70 switch defaultTime {
71 case "hour":
72 if common.UsingMySQL {
73 groupBy = "channel_id, DATE_FORMAT(FROM_UNIXTIME(created_at), '%Y-%m-%d %H:00:00')"
74 } else if common.UsingPostgreSQL {
75 groupBy = "channel_id, TO_CHAR(TO_TIMESTAMP(created_at), 'YYYY-MM-DD HH24:00:00')"
76 } else {
77 // SQLite
78 groupBy = "channel_id, strftime('%Y-%m-%d %H:00:00', datetime(created_at, 'unixepoch'))"
79 }
80 case "day":
81 if common.UsingMySQL {
82 groupBy = "channel_id, DATE_FORMAT(FROM_UNIXTIME(created_at), '%Y-%m-%d')"
83 } else if common.UsingPostgreSQL {
84 groupBy = "channel_id, TO_CHAR(TO_TIMESTAMP(created_at), 'YYYY-MM-DD')"
85 } else {
86 // SQLite
87 groupBy = "channel_id, strftime('%Y-%m-%d', datetime(created_at, 'unixepoch'))"
88 }
89 case "week":
90 if common.UsingMySQL {
91 groupBy = "channel_id, DATE_FORMAT(FROM_UNIXTIME(created_at), '%Y-%U')"
92 } else if common.UsingPostgreSQL {

Callers 1

GetChannelStatisticsFunction · 0.92

Calls 2

formatTimeFunction · 0.85
ScanMethod · 0.45

Tested by

no test coverage detected