2023-05-22 09:10:31 +00:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gin-gonic/gin"
|
2024-01-28 11:38:58 +00:00
|
|
|
"github.com/songquanpeng/one-api/common/config"
|
2024-04-21 11:43:23 +00:00
|
|
|
"github.com/songquanpeng/one-api/common/ctxkey"
|
2024-01-28 11:38:58 +00:00
|
|
|
"github.com/songquanpeng/one-api/model"
|
2024-02-17 16:15:31 +00:00
|
|
|
relaymodel "github.com/songquanpeng/one-api/relay/model"
|
2023-05-22 09:10:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func GetSubscription(c *gin.Context) {
|
2024-03-13 12:00:51 +00:00
|
|
|
var remainQuota int64
|
|
|
|
var usedQuota int64
|
2023-06-23 12:14:53 +00:00
|
|
|
var err error
|
|
|
|
var token *model.Token
|
2023-07-23 05:49:09 +00:00
|
|
|
var expiredTime int64
|
2024-01-21 15:21:42 +00:00
|
|
|
if config.DisplayTokenStatEnabled {
|
2024-04-21 11:43:23 +00:00
|
|
|
tokenId := c.GetInt(ctxkey.TokenId)
|
2023-06-23 12:14:53 +00:00
|
|
|
token, err = model.GetTokenById(tokenId)
|
2023-07-23 05:49:09 +00:00
|
|
|
expiredTime = token.ExpiredTime
|
2023-07-15 15:01:54 +00:00
|
|
|
remainQuota = token.RemainQuota
|
|
|
|
usedQuota = token.UsedQuota
|
2023-06-23 12:14:53 +00:00
|
|
|
} else {
|
2024-04-21 11:43:23 +00:00
|
|
|
userId := c.GetInt(ctxkey.Id)
|
2023-07-15 15:01:54 +00:00
|
|
|
remainQuota, err = model.GetUserQuota(userId)
|
2024-02-12 13:35:40 +00:00
|
|
|
if err != nil {
|
|
|
|
usedQuota, err = model.GetUserUsedQuota(userId)
|
|
|
|
}
|
2023-06-23 12:14:53 +00:00
|
|
|
}
|
2023-07-23 05:49:09 +00:00
|
|
|
if expiredTime <= 0 {
|
|
|
|
expiredTime = 0
|
|
|
|
}
|
2023-05-22 09:10:31 +00:00
|
|
|
if err != nil {
|
2024-02-17 16:15:31 +00:00
|
|
|
Error := relaymodel.Error{
|
2023-05-22 09:10:31 +00:00
|
|
|
Message: err.Error(),
|
2023-09-17 03:30:20 +00:00
|
|
|
Type: "upstream_error",
|
2023-05-22 09:10:31 +00:00
|
|
|
}
|
|
|
|
c.JSON(200, gin.H{
|
2024-01-14 11:21:03 +00:00
|
|
|
"error": Error,
|
2023-05-22 09:10:31 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2023-07-15 15:01:54 +00:00
|
|
|
quota := remainQuota + usedQuota
|
2023-06-20 12:09:17 +00:00
|
|
|
amount := float64(quota)
|
2024-01-21 15:21:42 +00:00
|
|
|
if config.DisplayInCurrencyEnabled {
|
|
|
|
amount /= config.QuotaPerUnit
|
2023-06-20 12:09:17 +00:00
|
|
|
}
|
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-07-23 05:49:09 +00:00
|
|
|
AccessUntil: expiredTime,
|
2023-05-22 09:10:31 +00:00
|
|
|
}
|
|
|
|
c.JSON(200, subscription)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetUsage(c *gin.Context) {
|
2024-03-13 12:00:51 +00:00
|
|
|
var quota int64
|
2023-06-23 12:14:53 +00:00
|
|
|
var err error
|
|
|
|
var token *model.Token
|
2024-01-21 15:21:42 +00:00
|
|
|
if config.DisplayTokenStatEnabled {
|
2024-04-21 11:43:23 +00:00
|
|
|
tokenId := c.GetInt(ctxkey.TokenId)
|
2023-06-23 12:14:53 +00:00
|
|
|
token, err = model.GetTokenById(tokenId)
|
|
|
|
quota = token.UsedQuota
|
|
|
|
} else {
|
2024-04-21 11:43:23 +00:00
|
|
|
userId := c.GetInt(ctxkey.Id)
|
2023-06-23 12:14:53 +00:00
|
|
|
quota, err = model.GetUserUsedQuota(userId)
|
|
|
|
}
|
2023-06-20 12:09:17 +00:00
|
|
|
if err != nil {
|
2024-02-17 16:15:31 +00:00
|
|
|
Error := relaymodel.Error{
|
2023-06-20 12:09:17 +00:00
|
|
|
Message: err.Error(),
|
|
|
|
Type: "one_api_error",
|
|
|
|
}
|
|
|
|
c.JSON(200, gin.H{
|
2024-01-14 11:21:03 +00:00
|
|
|
"error": Error,
|
2023-06-20 12:09:17 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
amount := float64(quota)
|
2024-01-21 15:21:42 +00:00
|
|
|
if config.DisplayInCurrencyEnabled {
|
|
|
|
amount /= config.QuotaPerUnit
|
2023-06-20 13:05:07 +00:00
|
|
|
}
|
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
|
|
|
|
}
|