log.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "net/http"
  5. "one-api/common"
  6. "one-api/model"
  7. "strconv"
  8. )
  9. func GetAllLogs(c *gin.Context) {
  10. p, _ := strconv.Atoi(c.Query("p"))
  11. if p < 0 {
  12. p = 0
  13. }
  14. logType, _ := strconv.Atoi(c.Query("type"))
  15. startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
  16. endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
  17. username := c.Query("username")
  18. tokenName := c.Query("token_name")
  19. modelName := c.Query("model_name")
  20. channel, _ := strconv.Atoi(c.Query("channel"))
  21. logs, err := model.GetAllLogs(logType, startTimestamp, endTimestamp, modelName, username, tokenName, p*common.ItemsPerPage, common.ItemsPerPage, channel)
  22. if err != nil {
  23. c.JSON(http.StatusOK, gin.H{
  24. "success": false,
  25. "message": err.Error(),
  26. })
  27. return
  28. }
  29. c.JSON(http.StatusOK, gin.H{
  30. "success": true,
  31. "message": "",
  32. "data": logs,
  33. })
  34. return
  35. }
  36. func GetUserLogs(c *gin.Context) {
  37. p, _ := strconv.Atoi(c.Query("p"))
  38. if p < 0 {
  39. p = 0
  40. }
  41. userId := c.GetInt("id")
  42. logType, _ := strconv.Atoi(c.Query("type"))
  43. startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
  44. endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
  45. tokenName := c.Query("token_name")
  46. modelName := c.Query("model_name")
  47. logs, err := model.GetUserLogs(userId, logType, startTimestamp, endTimestamp, modelName, tokenName, p*common.ItemsPerPage, common.ItemsPerPage)
  48. if err != nil {
  49. c.JSON(http.StatusOK, gin.H{
  50. "success": false,
  51. "message": err.Error(),
  52. })
  53. return
  54. }
  55. c.JSON(http.StatusOK, gin.H{
  56. "success": true,
  57. "message": "",
  58. "data": logs,
  59. })
  60. return
  61. }
  62. func SearchAllLogs(c *gin.Context) {
  63. keyword := c.Query("keyword")
  64. logs, err := model.SearchAllLogs(keyword)
  65. if err != nil {
  66. c.JSON(http.StatusOK, gin.H{
  67. "success": false,
  68. "message": err.Error(),
  69. })
  70. return
  71. }
  72. c.JSON(http.StatusOK, gin.H{
  73. "success": true,
  74. "message": "",
  75. "data": logs,
  76. })
  77. return
  78. }
  79. func SearchUserLogs(c *gin.Context) {
  80. keyword := c.Query("keyword")
  81. userId := c.GetInt("id")
  82. logs, err := model.SearchUserLogs(userId, keyword)
  83. if err != nil {
  84. c.JSON(http.StatusOK, gin.H{
  85. "success": false,
  86. "message": err.Error(),
  87. })
  88. return
  89. }
  90. c.JSON(http.StatusOK, gin.H{
  91. "success": true,
  92. "message": "",
  93. "data": logs,
  94. })
  95. return
  96. }
  97. func GetLogsStat(c *gin.Context) {
  98. logType, _ := strconv.Atoi(c.Query("type"))
  99. startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
  100. endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
  101. tokenName := c.Query("token_name")
  102. username := c.Query("username")
  103. modelName := c.Query("model_name")
  104. channel, _ := strconv.Atoi(c.Query("channel"))
  105. quotaNum := model.SumUsedQuota(logType, startTimestamp, endTimestamp, modelName, username, tokenName, channel)
  106. //tokenNum := model.SumUsedToken(logType, startTimestamp, endTimestamp, modelName, username, "")
  107. c.JSON(http.StatusOK, gin.H{
  108. "success": true,
  109. "message": "",
  110. "data": gin.H{
  111. "quota": quotaNum,
  112. //"token": tokenNum,
  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. quotaNum := model.SumUsedQuota(logType, startTimestamp, endTimestamp, modelName, username, tokenName, channel)
  126. //tokenNum := model.SumUsedToken(logType, startTimestamp, endTimestamp, modelName, username, tokenName)
  127. c.JSON(http.StatusOK, gin.H{
  128. "success": true,
  129. "message": "",
  130. "data": gin.H{
  131. "quota": quotaNum,
  132. //"token": tokenNum,
  133. },
  134. })
  135. return
  136. }
  137. func DeleteHistoryLogs(c *gin.Context) {
  138. targetTimestamp, _ := strconv.ParseInt(c.Query("target_timestamp"), 10, 64)
  139. if targetTimestamp == 0 {
  140. c.JSON(http.StatusOK, gin.H{
  141. "success": false,
  142. "message": "target timestamp is required",
  143. })
  144. return
  145. }
  146. count, err := model.DeleteOldLog(targetTimestamp)
  147. if err != nil {
  148. c.JSON(http.StatusOK, gin.H{
  149. "success": false,
  150. "message": err.Error(),
  151. })
  152. return
  153. }
  154. c.JSON(http.StatusOK, gin.H{
  155. "success": true,
  156. "message": "",
  157. "data": count,
  158. })
  159. return
  160. }