log.go 5.6 KB

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