forked from coaidev/coai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusage.go
60 lines (51 loc) · 1.36 KB
/
usage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package manager
import (
"chat/auth"
"chat/utils"
"github.com/gin-gonic/gin"
"net/http"
)
type BillingResponse struct {
Object string `json:"object"`
TotalUsage float32 `json:"total_usage"`
}
type SubscriptionResponse struct {
Object string `json:"object"`
SoftLimit int64 `json:"soft_limit"`
HardLimit int64 `json:"hard_limit"`
SystemHardLimit int64 `json:"system_hard_limit"`
SoftLimitUSD float32 `json:"soft_limit_usd"`
HardLimitUSD float32 `json:"hard_limit_usd"`
SystemHardLimitUSD float32 `json:"system_hard_limit_usd"`
}
func GetBillingUsage(c *gin.Context) {
user := auth.RequireAuth(c)
if user == nil {
return
}
db := utils.GetDBFromContext(c)
usage := user.GetUsedQuota(db)
c.JSON(http.StatusOK, BillingResponse{
Object: "list",
TotalUsage: usage,
})
}
func GetSubscription(c *gin.Context) {
user := auth.RequireAuth(c)
if user == nil {
return
}
db := utils.GetDBFromContext(c)
quota := user.GetQuota(db)
used := user.GetUsedQuota(db)
total := quota + used
c.JSON(http.StatusOK, SubscriptionResponse{
Object: "billing_subscription",
SoftLimit: int64(quota * 100),
HardLimit: int64(total * 100),
SystemHardLimit: 100000000,
SoftLimitUSD: quota / 7.3 / 10,
HardLimitUSD: total / 7.3 / 10,
SystemHardLimitUSD: 1000000,
})
}