relay-dashboard.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package controller
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. "github.com/gin-gonic/gin"
  7. "github.com/labring/aiproxy/core/common/balance"
  8. "github.com/labring/aiproxy/core/middleware"
  9. "github.com/labring/aiproxy/core/relay/adaptor/openai"
  10. "github.com/shopspring/decimal"
  11. log "github.com/sirupsen/logrus"
  12. )
  13. // GetQuota godoc
  14. //
  15. // @Summary Get quota
  16. // @Description Get quota
  17. // @Tags relay
  18. // @Produce json
  19. // @Security ApiKeyAuth
  20. // @Success 200 {object} balance.GroupQuota
  21. // @Router /v1/dashboard/billing/quota [get]
  22. func GetQuota(c *gin.Context) {
  23. group := middleware.GetGroup(c)
  24. groupQuota, err := balance.GetGroupQuota(c.Request.Context(), group)
  25. if err != nil {
  26. log.Errorf("get group (%s) balance failed: %s", group.ID, err)
  27. middleware.ErrorResponse(
  28. c,
  29. http.StatusInternalServerError,
  30. fmt.Sprintf("get group (%s) balance failed", group.ID),
  31. )
  32. return
  33. }
  34. token := middleware.GetToken(c)
  35. if token.Quota > 0 {
  36. groupQuota.Total = min(groupQuota.Total, token.Quota)
  37. groupQuota.Remain = min(groupQuota.Remain, decimal.NewFromFloat(token.Quota).
  38. Sub(decimal.NewFromFloat(token.UsedAmount)).
  39. InexactFloat64())
  40. }
  41. c.JSON(http.StatusOK, groupQuota)
  42. }
  43. // GetSubscription godoc
  44. //
  45. // @Summary Get subscription
  46. // @Description Get subscription
  47. // @Tags relay
  48. // @Produce json
  49. // @Security ApiKeyAuth
  50. // @Success 200 {object} openai.SubscriptionResponse
  51. // @Router /v1/dashboard/billing/subscription [get]
  52. func GetSubscription(c *gin.Context) {
  53. group := middleware.GetGroup(c)
  54. groupQuota, err := balance.GetGroupQuota(c.Request.Context(), group)
  55. if err != nil {
  56. if errors.Is(err, balance.ErrNoRealNameUsedAmountLimit) {
  57. middleware.ErrorResponse(c, http.StatusForbidden, err.Error())
  58. return
  59. }
  60. log.Errorf("get group (%s) balance failed: %s", group.ID, err)
  61. middleware.ErrorResponse(
  62. c,
  63. http.StatusInternalServerError,
  64. fmt.Sprintf("get group (%s) balance failed", group.ID),
  65. )
  66. return
  67. }
  68. token := middleware.GetToken(c)
  69. quota := token.Quota
  70. if quota <= 0 {
  71. quota = groupQuota.Total
  72. } else {
  73. quota = min(quota, groupQuota.Total)
  74. }
  75. hlimit := decimal.NewFromFloat(quota).
  76. Add(decimal.NewFromFloat(token.UsedAmount)).
  77. InexactFloat64()
  78. c.JSON(http.StatusOK, openai.SubscriptionResponse{
  79. HardLimitUSD: hlimit,
  80. SoftLimitUSD: groupQuota.Remain,
  81. SystemHardLimitUSD: hlimit,
  82. })
  83. }
  84. // GetUsage godoc
  85. //
  86. // @Summary Get usage
  87. // @Description Get usage
  88. // @Tags relay
  89. // @Produce json
  90. // @Security ApiKeyAuth
  91. // @Success 200 {object} openai.UsageResponse
  92. // @Router /v1/dashboard/billing/usage [get]
  93. func GetUsage(c *gin.Context) {
  94. token := middleware.GetToken(c)
  95. c.JSON(
  96. http.StatusOK,
  97. openai.UsageResponse{
  98. TotalUsage: decimal.NewFromFloat(token.UsedAmount).
  99. Mul(decimal.NewFromFloat(100)).
  100. InexactFloat64(),
  101. },
  102. )
  103. }