| 389 | } |
| 390 | |
| 391 | func 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)") |
| 393 | if username != "" { |
| 394 | tx = tx.Where("username = ?", username) |
| 395 | } |
| 396 | if tokenName != "" { |
| 397 | tx = tx.Where("token_name = ?", tokenName) |
| 398 | } |
| 399 | if startTimestamp != 0 { |
| 400 | tx = tx.Where("created_at >= ?", startTimestamp) |
| 401 | } |
| 402 | if endTimestamp != 0 { |
| 403 | tx = tx.Where("created_at <= ?", endTimestamp) |
| 404 | } |
| 405 | if modelName != "" { |
| 406 | tx = tx.Where("model_name = ?", modelName) |
| 407 | } |
| 408 | tx.Where("type = ?", LogTypeConsume).Scan(&token) |
| 409 | return token |
| 410 | } |
| 411 | |
| 412 | func DeleteOldLog(ctx context.Context, targetTimestamp int64, limit int) (int64, error) { |
| 413 | var total int64 = 0 |