log.go 4.6 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. func SearchAllLogs(c *gin.Context) {
  51. keyword := c.Query("keyword")
  52. logs, err := model.SearchAllLogs(keyword)
  53. if err != nil {
  54. common.ApiError(c, err)
  55. return
  56. }
  57. c.JSON(http.StatusOK, gin.H{
  58. "success": true,
  59. "message": "",
  60. "data": logs,
  61. })
  62. return
  63. }
  64. func SearchUserLogs(c *gin.Context) {
  65. keyword := c.Query("keyword")
  66. userId := c.GetInt("id")
  67. logs, err := model.SearchUserLogs(userId, keyword)
  68. if err != nil {
  69. common.ApiError(c, err)
  70. return
  71. }
  72. c.JSON(http.StatusOK, gin.H{
  73. "success": true,
  74. "message": "",
  75. "data": logs,
  76. })
  77. return
  78. }
  79. func GetLogByKey(c *gin.Context) {
  80. key := c.Query("key")
  81. logs, err := model.GetLogByKey(key)
  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 := model.SumUsedQuota(logType, startTimestamp, endTimestamp, modelName, username, tokenName, channel, group)
  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 := model.SumUsedQuota(logType, startTimestamp, endTimestamp, modelName, username, tokenName, channel, group)
  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. }