log.go 4.8 KB

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