Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

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