mirror of
https://github.com/labring/sealos.git
synced 2026-08-30 17:58:09 +08:00
perf(account): skip traffic and AI quota queries for non-subscription users (#6444)
Check namespace annotation 'subscription.sealos.io/status' to identify subscription users. Only query and return traffic/ai_quota data for subscription users, reducing unnecessary database queries for non-subscription workspaces. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -103,29 +103,52 @@ func GetWorkspaceResourceQuota(c *gin.Context) {
|
||||
)
|
||||
return
|
||||
}
|
||||
total, used, err := dao.DBClient.GetWorkspaceSubscriptionTraffic(
|
||||
req.Workspace,
|
||||
dao.DBClient.GetLocalRegion().Domain,
|
||||
)
|
||||
|
||||
// Check if workspace is a subscription user by checking namespace annotation
|
||||
var namespace v1.Namespace
|
||||
err = dao.K8sManager.GetClient().
|
||||
Get(context.Background(), client.ObjectKey{Name: req.Workspace}, &namespace)
|
||||
if err != nil {
|
||||
c.JSON(
|
||||
http.StatusInternalServerError,
|
||||
helper.ErrorMessage{
|
||||
Error: fmt.Sprintf("failed to get workspace subscription traffic: %v", err),
|
||||
},
|
||||
helper.ErrorMessage{Error: fmt.Sprintf("failed to get namespace: %v", err)},
|
||||
)
|
||||
return
|
||||
}
|
||||
aiQuotaTotal, aiQuotaUsed, err := dao.DBClient.GetAIQuota(
|
||||
req.Workspace,
|
||||
dao.DBClient.GetLocalRegion().Domain,
|
||||
)
|
||||
if err != nil {
|
||||
c.JSON(
|
||||
http.StatusInternalServerError,
|
||||
helper.ErrorMessage{Error: fmt.Sprintf("failed to get AI quota: %v", err)},
|
||||
|
||||
isSubscriptionUser := false
|
||||
if namespace.Annotations != nil {
|
||||
_, isSubscriptionUser = namespace.Annotations["subscription.sealos.io/status"]
|
||||
}
|
||||
|
||||
var total, used int64
|
||||
var aiQuotaTotal, aiQuotaUsed int64
|
||||
|
||||
if isSubscriptionUser {
|
||||
total, used, err = dao.DBClient.GetWorkspaceSubscriptionTraffic(
|
||||
req.Workspace,
|
||||
dao.DBClient.GetLocalRegion().Domain,
|
||||
)
|
||||
return
|
||||
if err != nil {
|
||||
c.JSON(
|
||||
http.StatusInternalServerError,
|
||||
helper.ErrorMessage{
|
||||
Error: fmt.Sprintf("failed to get workspace subscription traffic: %v", err),
|
||||
},
|
||||
)
|
||||
return
|
||||
}
|
||||
aiQuotaTotal, aiQuotaUsed, err = dao.DBClient.GetAIQuota(
|
||||
req.Workspace,
|
||||
dao.DBClient.GetLocalRegion().Domain,
|
||||
)
|
||||
if err != nil {
|
||||
c.JSON(
|
||||
http.StatusInternalServerError,
|
||||
helper.ErrorMessage{Error: fmt.Sprintf("failed to get AI quota: %v", err)},
|
||||
)
|
||||
return
|
||||
}
|
||||
}
|
||||
// 创建自定义配额状态
|
||||
customQuota := CustomResourceQuotaStatus{
|
||||
@@ -146,10 +169,14 @@ func GetWorkspaceResourceQuota(c *gin.Context) {
|
||||
}
|
||||
customQuota.Used[key] = value
|
||||
}
|
||||
customQuota.Hard["traffic"] = total
|
||||
customQuota.Used["traffic"] = used
|
||||
customQuota.Hard["ai_quota"] = aiQuotaTotal
|
||||
customQuota.Used["ai_quota"] = aiQuotaUsed
|
||||
|
||||
// Only add traffic and ai_quota for subscription users
|
||||
if isSubscriptionUser {
|
||||
customQuota.Hard["traffic"] = total
|
||||
customQuota.Used["traffic"] = used
|
||||
customQuota.Hard["ai_quota"] = aiQuotaTotal
|
||||
customQuota.Used["ai_quota"] = aiQuotaUsed
|
||||
}
|
||||
c.JSON(http.StatusOK, struct {
|
||||
Quota CustomResourceQuotaStatus `json:"quota"`
|
||||
}{
|
||||
|
||||
Reference in New Issue
Block a user