log.go 4.9 KB

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