更新agent manager数据接口

This commit is contained in:
zhanggangyong
2026-01-12 13:52:38 +00:00
parent e3d2cee85a
commit cd4dea8a0d
25 changed files with 2087 additions and 1265 deletions
+24 -13
View File
@@ -5,7 +5,7 @@
from datetime import datetime, timedelta
from typing import List, Optional, Dict, Any
from fastapi import APIRouter, Depends, HTTPException, Query, status
from sqlalchemy import select, func, and_, desc, or_
from sqlalchemy import select, func, and_, desc, or_, update
from sqlalchemy.ext.asyncio import AsyncSession
import uuid
import structlog
@@ -349,15 +349,17 @@ async def allocate_tenant_resources(
# agentId 在这里是模板名称(如 echo_agent)
template_name = agent_alloc.agentId
# 获取渠道的平台 Agent 配额
# 获取渠道的平台 Agent 配额(使用行锁防止并发更新)
channel_quota_result = await db.execute(
select(PlatformAgentQuota).where(
select(PlatformAgentQuota)
.where(
and_(
PlatformAgentQuota.target_id == channel_id,
PlatformAgentQuota.target_type == "channel",
PlatformAgentQuota.template_name == template_name
)
)
.with_for_update() # 行锁
)
channel_quota = channel_quota_result.scalar_one_or_none()
@@ -378,15 +380,17 @@ async def allocate_tenant_resources(
)
other_quota = other_tenants_quota_result.scalar() or 0
# 获取当前租户已有配额
# 获取当前租户已有配额(使用行锁防止并发更新)
tenant_quota_result = await db.execute(
select(PlatformAgentQuota).where(
select(PlatformAgentQuota)
.where(
and_(
PlatformAgentQuota.target_id == tenant_id,
PlatformAgentQuota.target_type == "tenant",
PlatformAgentQuota.template_name == template_name
)
)
.with_for_update() # 行锁
)
tenant_quota = tenant_quota_result.scalar_one_or_none()
current_tenant_quota = tenant_quota.pod_quota if tenant_quota else 0
@@ -816,9 +820,11 @@ async def recharge_tenant(
detail="租户不存在或不属于该渠道"
)
# 从 Balance 表获取或创建余额记录
# 使用行锁保护余额更新,防止并发充值问题
balance_result = await db.execute(
select(Balance).where(Balance.user_id == tenant_id)
select(Balance)
.where(Balance.user_id == tenant_id)
.with_for_update() # 行锁
)
balance_obj = balance_result.scalar_one_or_none()
@@ -826,11 +832,12 @@ async def recharge_tenant(
# 如果余额记录不存在,创建一个新的
balance_obj = Balance(user_id=tenant_id, eu_balance=0.0)
db.add(balance_obj)
await db.flush() # 确保记录创建
# 更新余额(使用 Balance 表)
old_balance = float(balance_obj.eu_balance)
balance_obj.eu_balance = old_balance + req.amount
new_balance = balance_obj.eu_balance
new_balance = old_balance + req.amount
balance_obj.eu_balance = new_balance
# 创建充值记录
recharge = RechargeRecord(
@@ -3152,15 +3159,17 @@ async def allocate_platform_agent_to_tenant(
detail=f"平台 Agent 模板 '{req.templateName}' 不存在"
)
# 获取渠道的配额
# 获取渠道的配额(使用行锁防止并发更新)
channel_quota_result = await db.execute(
select(PlatformAgentQuota).where(
select(PlatformAgentQuota)
.where(
and_(
PlatformAgentQuota.target_id == channel_id,
PlatformAgentQuota.target_type == "channel",
PlatformAgentQuota.template_name == req.templateName
)
)
.with_for_update() # 行锁
)
channel_quota = channel_quota_result.scalar_one_or_none()
@@ -3194,15 +3203,17 @@ async def allocate_platform_agent_to_tenant(
detail=f"配额超出渠道剩余配额。渠道剩余: {remaining},请求: {req.podQuota}"
)
# 查找或创建租户配额记录
# 查找或创建租户配额记录(使用行锁防止并发更新)
tenant_quota_result = await db.execute(
select(PlatformAgentQuota).where(
select(PlatformAgentQuota)
.where(
and_(
PlatformAgentQuota.target_id == tenant_id,
PlatformAgentQuota.target_type == "tenant",
PlatformAgentQuota.template_name == req.templateName
)
)
.with_for_update() # 行锁
)
tenant_quota = tenant_quota_result.scalar_one_or_none()