log.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package model
  2. import (
  3. "context"
  4. "fmt"
  5. "gorm.io/gorm"
  6. "one-api/common"
  7. )
  8. type Log struct {
  9. Id int `json:"id"`
  10. UserId int `json:"user_id"`
  11. CreatedAt int64 `json:"created_at" gorm:"bigint;index"`
  12. Type int `json:"type" gorm:"index"`
  13. Content string `json:"content"`
  14. Username string `json:"username" gorm:"index;default:''"`
  15. TokenName string `json:"token_name" gorm:"index;default:''"`
  16. ModelName string `json:"model_name" gorm:"index;default:''"`
  17. Quota int `json:"quota" gorm:"default:0"`
  18. PromptTokens int `json:"prompt_tokens" gorm:"default:0"`
  19. CompletionTokens int `json:"completion_tokens" gorm:"default:0"`
  20. }
  21. const (
  22. LogTypeUnknown = iota
  23. LogTypeTopup
  24. LogTypeConsume
  25. LogTypeManage
  26. LogTypeSystem
  27. )
  28. func RecordLog(userId int, logType int, content string) {
  29. if logType == LogTypeConsume && !common.LogConsumeEnabled {
  30. return
  31. }
  32. log := &Log{
  33. UserId: userId,
  34. Username: GetUsernameById(userId),
  35. CreatedAt: common.GetTimestamp(),
  36. Type: logType,
  37. Content: content,
  38. }
  39. err := DB.Create(log).Error
  40. if err != nil {
  41. common.SysError("failed to record log: " + err.Error())
  42. }
  43. }
  44. func RecordConsumeLog(ctx context.Context, userId int, promptTokens int, completionTokens int, modelName string, tokenName string, quota int, content string) {
  45. common.LogInfo(ctx, fmt.Sprintf("record consume log: userId=%d, promptTokens=%d, completionTokens=%d, modelName=%s, tokenName=%s, quota=%d, content=%s", userId, promptTokens, completionTokens, modelName, tokenName, quota, content))
  46. if !common.LogConsumeEnabled {
  47. return
  48. }
  49. log := &Log{
  50. UserId: userId,
  51. Username: GetUsernameById(userId),
  52. CreatedAt: common.GetTimestamp(),
  53. Type: LogTypeConsume,
  54. Content: content,
  55. PromptTokens: promptTokens,
  56. CompletionTokens: completionTokens,
  57. TokenName: tokenName,
  58. ModelName: modelName,
  59. Quota: quota,
  60. }
  61. err := DB.Create(log).Error
  62. if err != nil {
  63. common.LogError(ctx, "failed to record log: "+err.Error())
  64. }
  65. }
  66. func GetAllLogs(logType int, startTimestamp int64, endTimestamp int64, modelName string, username string, tokenName string, startIdx int, num int) (logs []*Log, err error) {
  67. var tx *gorm.DB
  68. if logType == LogTypeUnknown {
  69. tx = DB
  70. } else {
  71. tx = DB.Where("type = ?", logType)
  72. }
  73. if modelName != "" {
  74. tx = tx.Where("model_name = ?", modelName)
  75. }
  76. if username != "" {
  77. tx = tx.Where("username = ?", username)
  78. }
  79. if tokenName != "" {
  80. tx = tx.Where("token_name = ?", tokenName)
  81. }
  82. if startTimestamp != 0 {
  83. tx = tx.Where("created_at >= ?", startTimestamp)
  84. }
  85. if endTimestamp != 0 {
  86. tx = tx.Where("created_at <= ?", endTimestamp)
  87. }
  88. err = tx.Order("id desc").Limit(num).Offset(startIdx).Find(&logs).Error
  89. return logs, err
  90. }
  91. func GetUserLogs(userId int, logType int, startTimestamp int64, endTimestamp int64, modelName string, tokenName string, startIdx int, num int) (logs []*Log, err error) {
  92. var tx *gorm.DB
  93. if logType == LogTypeUnknown {
  94. tx = DB.Where("user_id = ?", userId)
  95. } else {
  96. tx = DB.Where("user_id = ? and type = ?", userId, logType)
  97. }
  98. if modelName != "" {
  99. tx = tx.Where("model_name = ?", modelName)
  100. }
  101. if tokenName != "" {
  102. tx = tx.Where("token_name = ?", tokenName)
  103. }
  104. if startTimestamp != 0 {
  105. tx = tx.Where("created_at >= ?", startTimestamp)
  106. }
  107. if endTimestamp != 0 {
  108. tx = tx.Where("created_at <= ?", endTimestamp)
  109. }
  110. err = tx.Order("id desc").Limit(num).Offset(startIdx).Omit("id").Find(&logs).Error
  111. return logs, err
  112. }
  113. func SearchAllLogs(keyword string) (logs []*Log, err error) {
  114. err = DB.Where("type = ? or content LIKE ?", keyword, keyword+"%").Order("id desc").Limit(common.MaxRecentItems).Find(&logs).Error
  115. return logs, err
  116. }
  117. func SearchUserLogs(userId int, keyword string) (logs []*Log, err error) {
  118. err = DB.Where("user_id = ? and type = ?", userId, keyword).Order("id desc").Limit(common.MaxRecentItems).Omit("id").Find(&logs).Error
  119. return logs, err
  120. }
  121. func SumUsedQuota(logType int, startTimestamp int64, endTimestamp int64, modelName string, username string, tokenName string) (quota int) {
  122. tx := DB.Table("logs").Select("sum(quota)")
  123. if username != "" {
  124. tx = tx.Where("username = ?", username)
  125. }
  126. if tokenName != "" {
  127. tx = tx.Where("token_name = ?", tokenName)
  128. }
  129. if startTimestamp != 0 {
  130. tx = tx.Where("created_at >= ?", startTimestamp)
  131. }
  132. if endTimestamp != 0 {
  133. tx = tx.Where("created_at <= ?", endTimestamp)
  134. }
  135. if modelName != "" {
  136. tx = tx.Where("model_name = ?", modelName)
  137. }
  138. tx.Where("type = ?", LogTypeConsume).Scan(&quota)
  139. return quota
  140. }
  141. func SumUsedToken(logType int, startTimestamp int64, endTimestamp int64, modelName string, username string, tokenName string) (token int) {
  142. tx := DB.Table("logs").Select("sum(prompt_tokens) + sum(completion_tokens)")
  143. if username != "" {
  144. tx = tx.Where("username = ?", username)
  145. }
  146. if tokenName != "" {
  147. tx = tx.Where("token_name = ?", tokenName)
  148. }
  149. if startTimestamp != 0 {
  150. tx = tx.Where("created_at >= ?", startTimestamp)
  151. }
  152. if endTimestamp != 0 {
  153. tx = tx.Where("created_at <= ?", endTimestamp)
  154. }
  155. if modelName != "" {
  156. tx = tx.Where("model_name = ?", modelName)
  157. }
  158. tx.Where("type = ?", LogTypeConsume).Scan(&token)
  159. return token
  160. }