log.go 4.1 KB

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