log.go 4.5 KB

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