log.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package controller
  2. import (
  3. "net/http"
  4. "strconv"
  5. "github.com/QuantumNous/new-api/common"
  6. "github.com/QuantumNous/new-api/model"
  7. "github.com/gin-gonic/gin"
  8. )
  9. func GetAllLogs(c *gin.Context) {
  10. pageInfo := common.GetPageQuery(c)
  11. logType, _ := strconv.Atoi(c.Query("type"))
  12. startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
  13. endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
  14. username := c.Query("username")
  15. tokenName := c.Query("token_name")
  16. modelName := c.Query("model_name")
  17. channel, _ := strconv.Atoi(c.Query("channel"))
  18. group := c.Query("group")
  19. requestId := c.Query("request_id")
  20. logs, total, err := model.GetAllLogs(logType, startTimestamp, endTimestamp, modelName, username, tokenName, pageInfo.GetStartIdx(), pageInfo.GetPageSize(), channel, group, requestId)
  21. if err != nil {
  22. common.ApiError(c, err)
  23. return
  24. }
  25. pageInfo.SetTotal(int(total))
  26. pageInfo.SetItems(logs)
  27. common.ApiSuccess(c, pageInfo)
  28. return
  29. }
  30. func GetUserLogs(c *gin.Context) {
  31. pageInfo := common.GetPageQuery(c)
  32. userId := c.GetInt("id")
  33. logType, _ := strconv.Atoi(c.Query("type"))
  34. startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
  35. endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
  36. tokenName := c.Query("token_name")
  37. modelName := c.Query("model_name")
  38. group := c.Query("group")
  39. requestId := c.Query("request_id")
  40. logs, total, err := model.GetUserLogs(userId, logType, startTimestamp, endTimestamp, modelName, tokenName, pageInfo.GetStartIdx(), pageInfo.GetPageSize(), group, requestId)
  41. if err != nil {
  42. common.ApiError(c, err)
  43. return
  44. }
  45. pageInfo.SetTotal(int(total))
  46. pageInfo.SetItems(logs)
  47. common.ApiSuccess(c, pageInfo)
  48. return
  49. }
  50. // Deprecated: SearchAllLogs 已废弃,前端未使用该接口。
  51. func SearchAllLogs(c *gin.Context) {
  52. c.JSON(http.StatusOK, gin.H{
  53. "success": false,
  54. "message": "该接口已废弃",
  55. })
  56. }
  57. // Deprecated: SearchUserLogs 已废弃,前端未使用该接口。
  58. func SearchUserLogs(c *gin.Context) {
  59. c.JSON(http.StatusOK, gin.H{
  60. "success": false,
  61. "message": "该接口已废弃",
  62. })
  63. }
  64. func GetLogByKey(c *gin.Context) {
  65. tokenId := c.GetInt("token_id")
  66. if tokenId == 0 {
  67. c.JSON(200, gin.H{
  68. "success": false,
  69. "message": "无效的令牌",
  70. })
  71. return
  72. }
  73. logs, err := model.GetLogByTokenId(tokenId)
  74. if err != nil {
  75. c.JSON(200, gin.H{
  76. "success": false,
  77. "message": err.Error(),
  78. })
  79. return
  80. }
  81. c.JSON(200, gin.H{
  82. "success": true,
  83. "message": "",
  84. "data": logs,
  85. })
  86. }
  87. func GetLogsStat(c *gin.Context) {
  88. logType, _ := strconv.Atoi(c.Query("type"))
  89. startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
  90. endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
  91. tokenName := c.Query("token_name")
  92. username := c.Query("username")
  93. modelName := c.Query("model_name")
  94. channel, _ := strconv.Atoi(c.Query("channel"))
  95. group := c.Query("group")
  96. stat, err := model.SumUsedQuota(logType, startTimestamp, endTimestamp, modelName, username, tokenName, channel, group)
  97. if err != nil {
  98. common.ApiError(c, err)
  99. return
  100. }
  101. //tokenNum := model.SumUsedToken(logType, startTimestamp, endTimestamp, modelName, username, "")
  102. c.JSON(http.StatusOK, gin.H{
  103. "success": true,
  104. "message": "",
  105. "data": gin.H{
  106. "quota": stat.Quota,
  107. "rpm": stat.Rpm,
  108. "tpm": stat.Tpm,
  109. },
  110. })
  111. return
  112. }
  113. func GetLogsSelfStat(c *gin.Context) {
  114. username := c.GetString("username")
  115. logType, _ := strconv.Atoi(c.Query("type"))
  116. startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
  117. endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
  118. tokenName := c.Query("token_name")
  119. modelName := c.Query("model_name")
  120. channel, _ := strconv.Atoi(c.Query("channel"))
  121. group := c.Query("group")
  122. quotaNum, err := model.SumUsedQuota(logType, startTimestamp, endTimestamp, modelName, username, tokenName, channel, group)
  123. if err != nil {
  124. common.ApiError(c, err)
  125. return
  126. }
  127. //tokenNum := model.SumUsedToken(logType, startTimestamp, endTimestamp, modelName, username, tokenName)
  128. c.JSON(200, gin.H{
  129. "success": true,
  130. "message": "",
  131. "data": gin.H{
  132. "quota": quotaNum.Quota,
  133. "rpm": quotaNum.Rpm,
  134. "tpm": quotaNum.Tpm,
  135. //"token": tokenNum,
  136. },
  137. })
  138. return
  139. }
  140. func DeleteHistoryLogs(c *gin.Context) {
  141. targetTimestamp, _ := strconv.ParseInt(c.Query("target_timestamp"), 10, 64)
  142. if targetTimestamp == 0 {
  143. c.JSON(http.StatusOK, gin.H{
  144. "success": false,
  145. "message": "target timestamp is required",
  146. })
  147. return
  148. }
  149. count, err := model.DeleteOldLog(c.Request.Context(), targetTimestamp, 100)
  150. if err != nil {
  151. common.ApiError(c, err)
  152. return
  153. }
  154. c.JSON(http.StatusOK, gin.H{
  155. "success": true,
  156. "message": "",
  157. "data": count,
  158. })
  159. return
  160. }