2023-05-22 09:10:31 +00:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-06-20 12:09:17 +00:00
|
|
|
"one-api/common"
|
2023-05-22 09:10:31 +00:00
|
|
|
"one-api/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
func GetSubscription(c *gin.Context) {
|
2023-07-15 15:01:54 +00:00
|
|
|
var remainQuota int
|
|
|
|
var usedQuota int
|
2023-06-23 12:14:53 +00:00
|
|
|
var err error
|
|
|
|
var token *model.Token
|
|
|
|
if common.DisplayTokenStatEnabled {
|
|
|
|
tokenId := c.GetInt("token_id")
|
|
|
|
token, err = model.GetTokenById(tokenId)
|
2023-07-15 15:01:54 +00:00
|
|
|
remainQuota = token.RemainQuota
|
|
|
|
usedQuota = token.UsedQuota
|
2023-06-23 12:14:53 +00:00
|
|
|
} else {
|
|
|
|
userId := c.GetInt("id")
|
2023-07-15 15:01:54 +00:00
|
|
|
remainQuota, err = model.GetUserQuota(userId)
|
|
|
|
usedQuota, err = model.GetUserUsedQuota(userId)
|
2023-06-23 12:14:53 +00:00
|
|
|
}
|
2023-05-22 09:10:31 +00:00
|
|
|
if err != nil {
|
|
|
|
openAIError := OpenAIError{
|
|
|
|
Message: err.Error(),
|
|
|
|
Type: "one_api_error",
|
|
|
|
}
|
|
|
|
c.JSON(200, gin.H{
|
|
|
|
"error": openAIError,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2023-07-15 15:01:54 +00:00
|
|
|
quota := remainQuota + usedQuota
|
2023-06-20 12:09:17 +00:00
|
|
|
amount := float64(quota)
|
|
|
|
if common.DisplayInCurrencyEnabled {
|
|
|
|
amount /= common.QuotaPerUnit
|
|
|
|
}
|
2023-06-25 02:40:54 +00:00
|
|
|
if token != nil && token.UnlimitedQuota {
|
2023-06-25 02:52:46 +00:00
|
|
|
amount = 100000000
|
2023-06-25 02:39:22 +00:00
|
|
|
}
|
2023-05-22 09:10:31 +00:00
|
|
|
subscription := OpenAISubscriptionResponse{
|
|
|
|
Object: "billing_subscription",
|
|
|
|
HasPaymentMethod: true,
|
2023-06-20 12:09:17 +00:00
|
|
|
SoftLimitUSD: amount,
|
|
|
|
HardLimitUSD: amount,
|
|
|
|
SystemHardLimitUSD: amount,
|
2023-05-22 09:10:31 +00:00
|
|
|
}
|
|
|
|
c.JSON(200, subscription)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetUsage(c *gin.Context) {
|
2023-06-23 12:14:53 +00:00
|
|
|
var quota int
|
|
|
|
var err error
|
|
|
|
var token *model.Token
|
|
|
|
if common.DisplayTokenStatEnabled {
|
|
|
|
tokenId := c.GetInt("token_id")
|
|
|
|
token, err = model.GetTokenById(tokenId)
|
|
|
|
quota = token.UsedQuota
|
|
|
|
} else {
|
|
|
|
userId := c.GetInt("id")
|
|
|
|
quota, err = model.GetUserUsedQuota(userId)
|
|
|
|
}
|
2023-06-20 12:09:17 +00:00
|
|
|
if err != nil {
|
|
|
|
openAIError := OpenAIError{
|
|
|
|
Message: err.Error(),
|
|
|
|
Type: "one_api_error",
|
|
|
|
}
|
|
|
|
c.JSON(200, gin.H{
|
|
|
|
"error": openAIError,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
amount := float64(quota)
|
2023-06-20 13:05:07 +00:00
|
|
|
if common.DisplayInCurrencyEnabled {
|
|
|
|
amount /= common.QuotaPerUnit
|
|
|
|
}
|
2023-05-22 09:10:31 +00:00
|
|
|
usage := OpenAIUsageResponse{
|
|
|
|
Object: "list",
|
2023-06-25 01:36:26 +00:00
|
|
|
TotalUsage: amount * 100,
|
2023-05-22 09:10:31 +00:00
|
|
|
}
|
|
|
|
c.JSON(200, usage)
|
|
|
|
return
|
|
|
|
}
|