Files
taiji-AI-PAD/services/mcp-server/check_quota_data.py
T
2026-01-11 13:00:46 +00:00

118 lines
4.5 KiB
Python

#!/usr/bin/env python3
"""
检查配额数据是否正确写入数据库
"""
import asyncio
import sys
from sqlalchemy import select, text
from sqlalchemy.ext.asyncio import AsyncSession
# 添加项目路径
sys.path.insert(0, '/app')
from database import engine
from models import TenantCustomAgentQuota, ChannelCustomAgentQuota, User
async def check_quota_data():
"""检查配额数据"""
async with AsyncSession(engine) as db:
print("=" * 80)
print("检查租户自定义 Agent 配额数据")
print("=" * 80)
# 查询所有租户配额
result = await db.execute(
select(TenantCustomAgentQuota)
)
tenant_quotas = result.scalars().all()
print(f"\n找到 {len(tenant_quotas)} 条租户配额记录:\n")
for quota in tenant_quotas:
# 获取租户信息
user_result = await db.execute(
select(User).where(User.id == quota.tenant_id)
)
user = user_result.scalar_one_or_none()
print(f"租户ID: {quota.tenant_id}")
print(f" - 租户名称: {user.name if user else 'N/A'}")
print(f" - 租户邮箱: {user.email if user else 'N/A'}")
print(f" - CPU 配额: {quota.cpu_quota} 核")
print(f" - 内存配额: {quota.memory_quota} GB")
print(f" - CPU 已使用: {quota.cpu_used} 核")
print(f" - 内存已使用: {quota.memory_used} GB")
print(f" - Agent 数量: {quota.agent_count}")
print(f" - CPU 剩余: {float(quota.cpu_quota) - float(quota.cpu_used)} 核")
print(f" - 内存剩余: {float(quota.memory_quota) - float(quota.memory_used)} GB")
print()
print("=" * 80)
print("检查渠道自定义 Agent 配额数据")
print("=" * 80)
# 查询所有渠道配额
result = await db.execute(
select(ChannelCustomAgentQuota)
)
channel_quotas = result.scalars().all()
print(f"\n找到 {len(channel_quotas)} 条渠道配额记录:\n")
for quota in channel_quotas:
print(f"渠道ID: {quota.channel_id}")
print(f" - CPU 配额: {quota.cpu_quota} 核")
print(f" - 内存配额: {quota.memory_quota} GB")
print(f" - CPU 已分配: {quota.cpu_allocated} 核")
print(f" - 内存已分配: {quota.memory_allocated} GB")
print(f" - CPU 剩余: {float(quota.cpu_quota) - float(quota.cpu_allocated)} 核")
print(f" - 内存剩余: {float(quota.memory_quota) - float(quota.memory_allocated)} GB")
print()
# 特别检查指定的租户ID
target_tenant_id = "b00a7b8e-9e8b-463d-9593-a3b4d0006778"
print("=" * 80)
print(f"检查目标租户: {target_tenant_id}")
print("=" * 80)
result = await db.execute(
select(TenantCustomAgentQuota).where(
TenantCustomAgentQuota.tenant_id == target_tenant_id
)
)
target_quota = result.scalar_one_or_none()
if target_quota:
print(f"\n找到配额记录:")
print(f" - CPU 配额: {target_quota.cpu_quota} 核")
print(f" - 内存配额: {target_quota.memory_quota} GB")
print(f" - CPU 已使用: {target_quota.cpu_used} 核")
print(f" - 内存已使用: {target_quota.memory_used} GB")
print(f" - Agent 数量: {target_quota.agent_count}")
print(f" - CPU 剩余: {float(target_quota.cpu_quota) - float(target_quota.cpu_used)} 核")
print(f" - 内存剩余: {float(target_quota.memory_quota) - float(target_quota.memory_used)} GB")
else:
print(f"\n❌ 未找到该租户的配额记录!")
print(f"这就是为什么会出现 '剩余: 0.00 核' 的原因")
# 检查该租户是否存在
user_result = await db.execute(
select(User).where(User.id == target_tenant_id)
)
user = user_result.scalar_one_or_none()
if user:
print(f"\n租户信息:")
print(f" - 名称: {user.name}")
print(f" - 邮箱: {user.email}")
print(f" - 角色: {user.role}")
print(f" - 渠道ID: {user.channel_id}")
else:
print(f"\n❌ 该租户不存在!")
await engine.dispose()
if __name__ == "__main__":
asyncio.run(check_quota_data())