log.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 GetLogsStat(c *gin.Context) {
  92. logType, _ := strconv.Atoi(c.Query("type"))
  93. startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
  94. endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
  95. tokenName := c.Query("token_name")
  96. username := c.Query("username")
  97. modelName := c.Query("model_name")
  98. quotaNum := model.SumUsedQuota(logType, startTimestamp, endTimestamp, modelName, username, tokenName)
  99. //tokenNum := model.SumUsedToken(logType, startTimestamp, endTimestamp, modelName, username, "")
  100. c.JSON(200, gin.H{
  101. "success": true,
  102. "message": "",
  103. "data": gin.H{
  104. "quota": quotaNum,
  105. //"token": tokenNum,
  106. },
  107. })
  108. }
  109. func GetLogsSelfStat(c *gin.Context) {
  110. username := c.GetString("username")
  111. logType, _ := strconv.Atoi(c.Query("type"))
  112. startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
  113. endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
  114. tokenName := c.Query("token_name")
  115. modelName := c.Query("model_name")
  116. quotaNum := model.SumUsedQuota(logType, startTimestamp, endTimestamp, modelName, username, tokenName)
  117. //tokenNum := model.SumUsedToken(logType, startTimestamp, endTimestamp, modelName, username, tokenName)
  118. c.JSON(200, gin.H{
  119. "success": true,
  120. "message": "",
  121. "data": gin.H{
  122. "quota": quotaNum,
  123. //"token": tokenNum,
  124. },
  125. })
  126. }