log.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. modelName := c.Query("model_name")
  18. logs, err := model.GetAllLogs(logType, startTimestamp, endTimestamp, modelName, username, p*common.ItemsPerPage, common.ItemsPerPage)
  19. if err != nil {
  20. c.JSON(200, gin.H{
  21. "success": false,
  22. "message": err.Error(),
  23. })
  24. return
  25. }
  26. c.JSON(200, gin.H{
  27. "success": true,
  28. "message": "",
  29. "data": logs,
  30. })
  31. }
  32. func GetUserLogs(c *gin.Context) {
  33. p, _ := strconv.Atoi(c.Query("p"))
  34. if p < 0 {
  35. p = 0
  36. }
  37. userId := c.GetInt("id")
  38. logType, _ := strconv.Atoi(c.Query("type"))
  39. startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
  40. endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
  41. tokenName := c.Query("token_name")
  42. modelName := c.Query("model_name")
  43. logs, err := model.GetUserLogs(userId, logType, startTimestamp, endTimestamp, modelName, tokenName, p*common.ItemsPerPage, common.ItemsPerPage)
  44. if err != nil {
  45. c.JSON(200, gin.H{
  46. "success": false,
  47. "message": err.Error(),
  48. })
  49. return
  50. }
  51. c.JSON(200, gin.H{
  52. "success": true,
  53. "message": "",
  54. "data": logs,
  55. })
  56. }
  57. func SearchAllLogs(c *gin.Context) {
  58. keyword := c.Query("keyword")
  59. logs, err := model.SearchAllLogs(keyword)
  60. if err != nil {
  61. c.JSON(200, gin.H{
  62. "success": false,
  63. "message": err.Error(),
  64. })
  65. return
  66. }
  67. c.JSON(200, gin.H{
  68. "success": true,
  69. "message": "",
  70. "data": logs,
  71. })
  72. }
  73. func SearchUserLogs(c *gin.Context) {
  74. keyword := c.Query("keyword")
  75. userId := c.GetInt("id")
  76. logs, err := model.SearchUserLogs(userId, keyword)
  77. if err != nil {
  78. c.JSON(200, gin.H{
  79. "success": false,
  80. "message": err.Error(),
  81. })
  82. return
  83. }
  84. c.JSON(200, gin.H{
  85. "success": true,
  86. "message": "",
  87. "data": logs,
  88. })
  89. }
  90. func GetLogsStat(c *gin.Context) {
  91. logType, _ := strconv.Atoi(c.Query("type"))
  92. startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
  93. endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
  94. username := c.Query("username")
  95. modelName := c.Query("model_name")
  96. quotaNum := model.SumUsedQuota(logType, startTimestamp, endTimestamp, modelName, username, "")
  97. //tokenNum := model.SumUsedToken(logType, startTimestamp, endTimestamp, modelName, username, "")
  98. c.JSON(200, gin.H{
  99. "success": true,
  100. "message": "",
  101. "data": gin.H{
  102. "quota": quotaNum,
  103. //"token": tokenNum,
  104. },
  105. })
  106. }
  107. func GetLogsSelfStat(c *gin.Context) {
  108. username := c.GetString("username")
  109. logType, _ := strconv.Atoi(c.Query("type"))
  110. startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
  111. endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
  112. tokenName := c.Query("token_name")
  113. modelName := c.Query("model_name")
  114. quotaNum := model.SumUsedQuota(logType, startTimestamp, endTimestamp, modelName, username, tokenName)
  115. //tokenNum := model.SumUsedToken(logType, startTimestamp, endTimestamp, modelName, username, tokenName)
  116. c.JSON(200, gin.H{
  117. "success": true,
  118. "message": "",
  119. "data": gin.H{
  120. "quota": quotaNum,
  121. //"token": tokenNum,
  122. },
  123. })
  124. }