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

Function SumUsedQuota

model/log.go:345–389  ·  view source on GitHub ↗
(logType int, startTimestamp int64, endTimestamp int64, modelName string, username string, tokenName string, channel int, group string)

Source from the content-addressed store, hash-verified

343}
344
345func SumUsedQuota(logType int, startTimestamp int64, endTimestamp int64, modelName string, username string, tokenName string, channel int, group string) (stat Stat) {
346 tx := LOG_DB.Table("logs").Select("sum(quota) quota")
347
348 // 为rpm和tpm创建单独的查询
349 rpmTpmQuery := LOG_DB.Table("logs").Select("count(*) rpm, sum(prompt_tokens) + sum(completion_tokens) tpm")
350
351 if username != "" {
352 tx = tx.Where("username = ?", username)
353 rpmTpmQuery = rpmTpmQuery.Where("username = ?", username)
354 }
355 if tokenName != "" {
356 tx = tx.Where("token_name = ?", tokenName)
357 rpmTpmQuery = rpmTpmQuery.Where("token_name = ?", tokenName)
358 }
359 if startTimestamp != 0 {
360 tx = tx.Where("created_at >= ?", startTimestamp)
361 }
362 if endTimestamp != 0 {
363 tx = tx.Where("created_at <= ?", endTimestamp)
364 }
365 if modelName != "" {
366 tx = tx.Where("model_name like ?", modelName)
367 rpmTpmQuery = rpmTpmQuery.Where("model_name like ?", modelName)
368 }
369 if channel != 0 {
370 tx = tx.Where("channel_id = ?", channel)
371 rpmTpmQuery = rpmTpmQuery.Where("channel_id = ?", channel)
372 }
373 if group != "" {
374 tx = tx.Where(logGroupCol+" = ?", group)
375 rpmTpmQuery = rpmTpmQuery.Where(logGroupCol+" = ?", group)
376 }
377
378 tx = tx.Where("type = ?", LogTypeConsume)
379 rpmTpmQuery = rpmTpmQuery.Where("type = ?", LogTypeConsume)
380
381 // 只统计最近60秒的rpm和tpm
382 rpmTpmQuery = rpmTpmQuery.Where("created_at >= ?", time.Now().Add(-60*time.Second).Unix())
383
384 // 执行查询
385 tx.Scan(&stat)
386 rpmTpmQuery.Scan(&stat)
387
388 return stat
389}
390
391func SumUsedToken(logType int, startTimestamp int64, endTimestamp int64, modelName string, username string, tokenName string) (token int) {
392 tx := LOG_DB.Table("logs").Select("ifnull(sum(prompt_tokens),0) + ifnull(sum(completion_tokens),0)")

Callers 2

GetLogsStatFunction · 0.92
GetLogsSelfStatFunction · 0.92

Calls 2

AddMethod · 0.80
ScanMethod · 0.45

Tested by

no test coverage detected