log.go 5.0 KB

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