log.go 5.2 KB

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