1
0

log.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package controller
  2. import (
  3. "net/http"
  4. "one-api/common"
  5. "one-api/model"
  6. "strconv"
  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. logs, total, err := model.GetAllLogs(logType, startTimestamp, endTimestamp, modelName, username, tokenName, pageInfo.GetStartIdx(), pageInfo.GetPageSize(), channel, group)
  20. if err != nil {
  21. common.ApiError(c, err)
  22. return
  23. }
  24. pageInfo.SetTotal(int(total))
  25. pageInfo.SetItems(logs)
  26. common.ApiSuccess(c, pageInfo)
  27. return
  28. }
  29. func GetUserLogs(c *gin.Context) {
  30. pageInfo := common.GetPageQuery(c)
  31. userId := c.GetInt("id")
  32. logType, _ := strconv.Atoi(c.Query("type"))
  33. startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
  34. endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
  35. tokenName := c.Query("token_name")
  36. modelName := c.Query("model_name")
  37. group := c.Query("group")
  38. logs, total, err := model.GetUserLogs(userId, logType, startTimestamp, endTimestamp, modelName, tokenName, pageInfo.GetStartIdx(), pageInfo.GetPageSize(), group)
  39. if err != nil {
  40. common.ApiError(c, err)
  41. return
  42. }
  43. pageInfo.SetTotal(int(total))
  44. pageInfo.SetItems(logs)
  45. common.ApiSuccess(c, pageInfo)
  46. return
  47. }
  48. func SearchAllLogs(c *gin.Context) {
  49. keyword := c.Query("keyword")
  50. logs, err := model.SearchAllLogs(keyword)
  51. if err != nil {
  52. common.ApiError(c, err)
  53. return
  54. }
  55. c.JSON(http.StatusOK, gin.H{
  56. "success": true,
  57. "message": "",
  58. "data": logs,
  59. })
  60. return
  61. }
  62. func SearchUserLogs(c *gin.Context) {
  63. keyword := c.Query("keyword")
  64. userId := c.GetInt("id")
  65. logs, err := model.SearchUserLogs(userId, keyword)
  66. if err != nil {
  67. common.ApiError(c, err)
  68. return
  69. }
  70. c.JSON(http.StatusOK, gin.H{
  71. "success": true,
  72. "message": "",
  73. "data": logs,
  74. })
  75. return
  76. }
  77. func GetLogByKey(c *gin.Context) {
  78. key := c.Query("key")
  79. logs, err := model.GetLogByKey(key)
  80. if err != nil {
  81. c.JSON(200, gin.H{
  82. "success": false,
  83. "message": err.Error(),
  84. })
  85. return
  86. }
  87. c.JSON(200, gin.H{
  88. "success": true,
  89. "message": "",
  90. "data": logs,
  91. })
  92. }
  93. func GetLogsStat(c *gin.Context) {
  94. logType, _ := strconv.Atoi(c.Query("type"))
  95. startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
  96. endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
  97. tokenName := c.Query("token_name")
  98. username := c.Query("username")
  99. modelName := c.Query("model_name")
  100. channel, _ := strconv.Atoi(c.Query("channel"))
  101. group := c.Query("group")
  102. stat := model.SumUsedQuota(logType, startTimestamp, endTimestamp, modelName, username, tokenName, channel, group)
  103. //tokenNum := model.SumUsedToken(logType, startTimestamp, endTimestamp, modelName, username, "")
  104. c.JSON(http.StatusOK, gin.H{
  105. "success": true,
  106. "message": "",
  107. "data": gin.H{
  108. "quota": stat.Quota,
  109. "rpm": stat.Rpm,
  110. "tpm": stat.Tpm,
  111. },
  112. })
  113. return
  114. }
  115. func GetLogsSelfStat(c *gin.Context) {
  116. username := c.GetString("username")
  117. logType, _ := strconv.Atoi(c.Query("type"))
  118. startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
  119. endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
  120. tokenName := c.Query("token_name")
  121. modelName := c.Query("model_name")
  122. channel, _ := strconv.Atoi(c.Query("channel"))
  123. group := c.Query("group")
  124. quotaNum := model.SumUsedQuota(logType, startTimestamp, endTimestamp, modelName, username, tokenName, channel, group)
  125. //tokenNum := model.SumUsedToken(logType, startTimestamp, endTimestamp, modelName, username, tokenName)
  126. c.JSON(200, gin.H{
  127. "success": true,
  128. "message": "",
  129. "data": gin.H{
  130. "quota": quotaNum.Quota,
  131. "rpm": quotaNum.Rpm,
  132. "tpm": quotaNum.Tpm,
  133. //"token": tokenNum,
  134. },
  135. })
  136. return
  137. }
  138. func DeleteHistoryLogs(c *gin.Context) {
  139. // 获取开始和结束时间戳参数
  140. startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
  141. endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
  142. // 兼容旧的target_timestamp参数
  143. if startTimestamp == 0 && endTimestamp == 0 {
  144. targetTimestamp, _ := strconv.ParseInt(c.Query("target_timestamp"), 10, 64)
  145. if targetTimestamp != 0 {
  146. // 如果只提供了target_timestamp,则将其作为结束时间,开始时间为0(删除该时间之前的所有日志)
  147. endTimestamp = targetTimestamp
  148. }
  149. }
  150. // 验证参数
  151. if startTimestamp == 0 && endTimestamp == 0 {
  152. c.JSON(http.StatusOK, gin.H{
  153. "success": false,
  154. "message": "start timestamp or end timestamp is required",
  155. })
  156. return
  157. }
  158. // 调用模型层函数删除日志
  159. count, err := model.DeleteLogsByTimeRange(c.Request.Context(), startTimestamp, endTimestamp, 100)
  160. if err != nil {
  161. common.ApiError(c, err)
  162. return
  163. }
  164. c.JSON(http.StatusOK, gin.H{
  165. "success": true,
  166. "message": "",
  167. "data": count,
  168. })
  169. return
  170. }