Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

176 wiersze
4.8 KiB

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