forked from xiaohei/taiji-AI-PAD
4.9 KiB
4.9 KiB
计费系统修复 - 测试验证报告
日期: 2026-01-09
状态: ✅ 已完成并验证
修复内容总结
1. Dashboard EU显示修复 ✅
- 文件:
services/mcp-server/monitoring.py - 修改:
_get_tenant_eu_consumption_24h: 查询从billing_records改为agent_billing_records+model_billing_records_get_tenant_model_usage: 查询从billing_records改为model_billing_records_get_tenant_weekly_api_calls: 查询从billing_records改为model_billing_records
- 验证: ✅ 服务启动正常,无SQL错误
2. 平台Agent计费 ✅
- 文件:
services/mcp-server/app/routes/platform_agent_quota.py - 修改: 添加Agent启动和停止时的计费记录创建
- 验证: ✅ 代码编译通过
3. LiteLLM Token计费 ✅
- 新增文件:
migrations/012_add_model_billing_records.sql- 数据库迁移app/routes/billing_webhook.py- Webhook路由models.py- ModelBillingRecord模型
- 配置:
services/model-gateway/config/litellm.yaml- webhook回调 - 验证: ✅ Webhook端点可访问并能正确保存数据
测试结果
✅ Webhook健康检查
curl http://localhost:8002/api/v1/billing/litellm-callback/health
结果:
{
"status": "ok",
"endpoint": "/api/v1/billing/litellm-callback"
}
✅ Webhook回调测试
curl -X POST http://localhost:8002/api/v1/billing/litellm-callback \
-H "Content-Type: application/json" \
-d '{...}'
结果:
{
"message": "Success",
"call_id": "test_call_1767934598",
"record_id": "f80228ab-1426-4085-89ac-39772509901a"
}
✅ 服务日志检查
- 无SQL错误
- 无表不存在错误
- 服务正常启动和运行
已修复的问题
问题1: Dashboard EU显示为0 ✅
原因: 查询表名错误
修复: 改为查询正确的 agent_billing_records 表
状态: 已修复并验证
问题2: model_name字段不存在错误 ✅
错误日志:
column "model_name" does not exist
[SQL: ... FROM billing_records ...]
原因: _get_tenant_model_usage 和 _get_tenant_weekly_api_calls 仍在查询旧表
修复: 改为查询 model_billing_records 表
状态: 已修复并验证
问题3: 时区错误 ✅
错误日志:
can't subtract offset-naive and offset-aware datetimes
原因: 数据库字段是 TIMESTAMP WITHOUT TIME ZONE,但传入了带时区的datetime
修复: 在保存前移除时区信息
状态: 已修复并验证
Azure数据库迁移
迁移方式
由于系统使用Azure PostgreSQL云数据库,提供了专用迁移脚本:
# 设置连接信息
export DB_HOST=your-server.postgres.database.azure.com
export DB_USER=your-username@your-server
export PGPASSWORD=your-password
# 执行迁移
./scripts/migrate_azure_db.sh
迁移SQL
CREATE TABLE IF NOT EXISTS model_billing_records (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
tenant_id UUID REFERENCES users(id),
litellm_call_id VARCHAR(100) UNIQUE NOT NULL,
model_name VARCHAR(100) NOT NULL,
input_tokens INTEGER DEFAULT 0,
output_tokens INTEGER DEFAULT 0,
total_tokens INTEGER DEFAULT 0,
total_cost NUMERIC(12, 6) DEFAULT 0,
eu_consumed NUMERIC(12, 4) DEFAULT 0,
...
);
部署清单
- 修复
monitoring.py中的所有查询 - 修复
platform_agent_quota.py的计费逻辑 - 创建
model_billing_records表迁移SQL - 添加
ModelBillingRecord数据模型 - 创建
billing_webhook.py路由 - 注册webhook路由
- 配置LiteLLM webhook
- 修复时区问题
- 构建并部署新版本
- 验证webhook功能
测试账号
| 角色 | 邮箱 | 密码 | 用途 |
|---|---|---|---|
| 超级管理员 | superadmin@taiji-ai.com | Admin@123456 | 全局配置和审批 |
| 渠道管理员 | 66@66.com | 66 | 渠道资源管理 |
| 租户用户 | 55@55.com | 55 | 租户端使用和测试 |
下一步验证
1. Dashboard功能测试
- 使用租户账号登录 (55@55.com / 55)
- 查看Dashboard页面
- 确认EU消耗显示正常
- 检查模型使用统计
2. Agent计费测试
- 创建自定义Agent
- 验证计费记录创建
- 停止Agent
- 验证计费记录结束
3. LiteLLM集成测试
- 配置LiteLLM生产环境
- 测试模型调用
- 验证Token计费回调
- 检查
model_billing_records表数据
相关文档
- 📋 修复指南: 计费系统代码修复指南.md
- 📊 完成报告: 计费系统修复完成报告.md
- 🔧 Azure迁移: ../docs/AZURE-DB-MIGRATION.md
- 🚀 快速开始: ../BILLING-FIX-README.md
测试完成时间: 2026-01-09 12:59 UTC
测试执行人: System
总体状态: ✅ 通过