log.go 4.9 KB

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