log.go 5.2 KB

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