log.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "one-api/common"
  5. "one-api/model"
  6. "strconv"
  7. )
  8. func GetAllLogs(c *gin.Context) {
  9. p, _ := strconv.Atoi(c.Query("p"))
  10. if p < 0 {
  11. p = 0
  12. }
  13. logType, _ := strconv.Atoi(c.Query("type"))
  14. startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
  15. endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
  16. username := c.Query("username")
  17. tokenName := c.Query("token_name")
  18. modelName := c.Query("model_name")
  19. logs, err := model.GetAllLogs(logType, startTimestamp, endTimestamp, modelName, username, tokenName, p*common.ItemsPerPage, common.ItemsPerPage)
  20. if err != nil {
  21. c.JSON(200, gin.H{
  22. "success": false,
  23. "message": err.Error(),
  24. })
  25. return
  26. }
  27. c.JSON(200, gin.H{
  28. "success": true,
  29. "message": "",
  30. "data": logs,
  31. })
  32. }
  33. func GetUserLogs(c *gin.Context) {
  34. p, _ := strconv.Atoi(c.Query("p"))
  35. if p < 0 {
  36. p = 0
  37. }
  38. userId := c.GetInt("id")
  39. logType, _ := strconv.Atoi(c.Query("type"))
  40. startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
  41. endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
  42. tokenName := c.Query("token_name")
  43. modelName := c.Query("model_name")
  44. logs, err := model.GetUserLogs(userId, logType, startTimestamp, endTimestamp, modelName, tokenName, p*common.ItemsPerPage, common.ItemsPerPage)
  45. if err != nil {
  46. c.JSON(200, gin.H{
  47. "success": false,
  48. "message": err.Error(),
  49. })
  50. return
  51. }
  52. c.JSON(200, gin.H{
  53. "success": true,
  54. "message": "",
  55. "data": logs,
  56. })
  57. }
  58. func SearchAllLogs(c *gin.Context) {
  59. keyword := c.Query("keyword")
  60. logs, err := model.SearchAllLogs(keyword)
  61. if err != nil {
  62. c.JSON(200, gin.H{
  63. "success": false,
  64. "message": err.Error(),
  65. })
  66. return
  67. }
  68. c.JSON(200, gin.H{
  69. "success": true,
  70. "message": "",
  71. "data": logs,
  72. })
  73. }
  74. func SearchUserLogs(c *gin.Context) {
  75. keyword := c.Query("keyword")
  76. userId := c.GetInt("id")
  77. logs, err := model.SearchUserLogs(userId, keyword)
  78. if err != nil {
  79. c.JSON(200, gin.H{
  80. "success": false,
  81. "message": err.Error(),
  82. })
  83. return
  84. }
  85. c.JSON(200, gin.H{
  86. "success": true,
  87. "message": "",
  88. "data": logs,
  89. })
  90. }
  91. func GetLogByKey(c *gin.Context) {
  92. key := c.Query("key")
  93. logs, err := model.GetLogByKey(key)
  94. if err != nil {
  95. c.JSON(200, gin.H{
  96. "success": false,
  97. "message": err.Error(),
  98. })
  99. return
  100. }
  101. c.JSON(200, gin.H{
  102. "success": true,
  103. "message": "",
  104. "data": logs,
  105. })
  106. }
  107. func GetLogsStat(c *gin.Context) {
  108. logType, _ := strconv.Atoi(c.Query("type"))
  109. startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
  110. endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
  111. tokenName := c.Query("token_name")
  112. username := c.Query("username")
  113. modelName := c.Query("model_name")
  114. stat := model.SumUsedQuota(logType, startTimestamp, endTimestamp, modelName, username, tokenName)
  115. //tokenNum := model.SumUsedToken(logType, startTimestamp, endTimestamp, modelName, username, "")
  116. c.JSON(200, gin.H{
  117. "success": true,
  118. "message": "",
  119. "data": gin.H{
  120. "quota": stat.Quota,
  121. "rpm": stat.Rpm,
  122. "tpm": stat.Tpm,
  123. },
  124. })
  125. }
  126. func GetLogsSelfStat(c *gin.Context) {
  127. username := c.GetString("username")
  128. logType, _ := strconv.Atoi(c.Query("type"))
  129. startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
  130. endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
  131. tokenName := c.Query("token_name")
  132. modelName := c.Query("model_name")
  133. quotaNum := model.SumUsedQuota(logType, startTimestamp, endTimestamp, modelName, username, tokenName)
  134. //tokenNum := model.SumUsedToken(logType, startTimestamp, endTimestamp, modelName, username, tokenName)
  135. c.JSON(200, gin.H{
  136. "success": true,
  137. "message": "",
  138. "data": gin.H{
  139. "quota": quotaNum.Quota,
  140. "rpm": quotaNum.Rpm,
  141. "tpm": quotaNum.Tpm,
  142. //"token": tokenNum,
  143. },
  144. })
  145. }