log.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 GetLogByKey(c *gin.Context) {
  98. key := c.Query("key")
  99. logs, err := model.GetLogByKey(key)
  100. if err != nil {
  101. c.JSON(200, gin.H{
  102. "success": false,
  103. "message": err.Error(),
  104. })
  105. return
  106. }
  107. c.JSON(200, gin.H{
  108. "success": true,
  109. "message": "",
  110. "data": logs,
  111. })
  112. }
  113. func GetLogsStat(c *gin.Context) {
  114. logType, _ := strconv.Atoi(c.Query("type"))
  115. startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
  116. endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
  117. tokenName := c.Query("token_name")
  118. username := c.Query("username")
  119. modelName := c.Query("model_name")
  120. channel, _ := strconv.Atoi(c.Query("channel"))
  121. stat := model.SumUsedQuota(logType, startTimestamp, endTimestamp, modelName, username, tokenName, channel)
  122. //tokenNum := model.SumUsedToken(logType, startTimestamp, endTimestamp, modelName, username, "")
  123. c.JSON(http.StatusOK, gin.H{
  124. "success": true,
  125. "message": "",
  126. "data": gin.H{
  127. "quota": stat.Quota,
  128. "rpm": stat.Rpm,
  129. "tpm": stat.Tpm,
  130. },
  131. })
  132. return
  133. }
  134. func GetLogsSelfStat(c *gin.Context) {
  135. username := c.GetString("username")
  136. logType, _ := strconv.Atoi(c.Query("type"))
  137. startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
  138. endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
  139. tokenName := c.Query("token_name")
  140. modelName := c.Query("model_name")
  141. channel, _ := strconv.Atoi(c.Query("channel"))
  142. quotaNum := model.SumUsedQuota(logType, startTimestamp, endTimestamp, modelName, username, tokenName, channel)
  143. //tokenNum := model.SumUsedToken(logType, startTimestamp, endTimestamp, modelName, username, tokenName)
  144. c.JSON(200, gin.H{
  145. "success": true,
  146. "message": "",
  147. "data": gin.H{
  148. "quota": quotaNum.Quota,
  149. "rpm": quotaNum.Rpm,
  150. "tpm": quotaNum.Tpm,
  151. //"token": tokenNum,
  152. },
  153. })
  154. return
  155. }
  156. func DeleteHistoryLogs(c *gin.Context) {
  157. targetTimestamp, _ := strconv.ParseInt(c.Query("target_timestamp"), 10, 64)
  158. if targetTimestamp == 0 {
  159. c.JSON(http.StatusOK, gin.H{
  160. "success": false,
  161. "message": "target timestamp is required",
  162. })
  163. return
  164. }
  165. count, err := model.DeleteOldLog(targetTimestamp)
  166. if err != nil {
  167. c.JSON(http.StatusOK, gin.H{
  168. "success": false,
  169. "message": err.Error(),
  170. })
  171. return
  172. }
  173. c.JSON(http.StatusOK, gin.H{
  174. "success": true,
  175. "message": "",
  176. "data": count,
  177. })
  178. return
  179. }