问题1和2修复: - 移除 data_preparer.py(数据生成应该在 data_service 中) - 移除 data 路由(不再提供数据准备接口) - 明确 database_admin_service 只负责 DDL 操作 命名优化: - database_service database_admin_service - 更明确的服务定位:DBA级别的数据库管理 职责边界: - database_admin_service: 数据库/表的创建、删除(DDL) - data_service: 数据的下载、查询、生成(DML) 变更文件: - 重命名目录和所有引用 - 更新配置文件 (global.yaml) - 更新所有 logger 名称 - 更新测试文件 - 更新 backtest/main.py 中的服务注册
104 lines
3.7 KiB
Python
104 lines
3.7 KiB
Python
"""
|
||
测试 Database Service
|
||
|
||
演示如何使用数据库管理服务的API
|
||
"""
|
||
|
||
import asyncio
|
||
import httpx
|
||
|
||
|
||
BASE_URL = "http://localhost:8015"
|
||
|
||
|
||
async def test_database_admin_service():
|
||
"""测试数据库管理服务"""
|
||
print("=" * 70)
|
||
print("测试 Database Admin Service".center(70))
|
||
print("=" * 70)
|
||
|
||
async with httpx.AsyncClient(timeout=30.0) as client:
|
||
# 1. 查看服务状态
|
||
print("\n1️⃣ 检查服务状态...")
|
||
try:
|
||
response = await client.get(f"{BASE_URL}/health")
|
||
print(f" ✓ 服务状态: {response.json()}")
|
||
except httpx.ConnectError:
|
||
print(" ✗ 无法连接到服务,请确保服务已启动")
|
||
print(
|
||
f" 启动命令: uvicorn src.database_admin_service.app:app --host 0.0.0.0 --port 8015"
|
||
)
|
||
return
|
||
|
||
# 2. 列出所有数据库
|
||
print("\n2️⃣ 列出所有数据库...")
|
||
response = await client.get(f"{BASE_URL}/api/databases")
|
||
result = response.json()
|
||
print(f" 找到 {result['total']} 个数据库:")
|
||
for db in result["databases"]:
|
||
print(
|
||
f" • {db['name']} ({db['environment']}) - "
|
||
f"{db['table_count']} 表, {db['size_mb']:.2f} MB"
|
||
)
|
||
|
||
# 3. 查看回测数据库的表
|
||
print("\n3️⃣ 查看回测数据库的表...")
|
||
response = await client.get(f"{BASE_URL}/api/tables/backtest")
|
||
result = response.json()
|
||
print(f" 数据库 {result['database']} 有 {result['total']} 个表:")
|
||
for table in result["tables"][:5]: # 只显示前5个
|
||
print(
|
||
f" • {table['name']} - {table['rows']} 行, {table['size_mb']:.2f} MB"
|
||
)
|
||
if result["total"] > 5:
|
||
print(f" ... 还有 {result['total'] - 5} 个表")
|
||
|
||
# 4. 清空kline_1d表(如果存在)
|
||
print("\n4️⃣ 清空 kline_1d 表...")
|
||
try:
|
||
response = await client.post(
|
||
f"{BASE_URL}/api/tables/backtest/kline_1d/truncate"
|
||
)
|
||
result = response.json()
|
||
print(f" ✓ {result['message']}")
|
||
except httpx.HTTPStatusError as e:
|
||
print(f" ⚠️ {e.response.json()['detail']}")
|
||
|
||
# 5. 准备测试数据
|
||
print("\n5️⃣ 准备测试数据...")
|
||
data_request = {
|
||
"environment": "backtest",
|
||
"symbols": ["000001.SZ", "600000.SH"],
|
||
"period": "1d",
|
||
"days": 30,
|
||
}
|
||
response = await client.post(f"{BASE_URL}/api/data/prepare", json=data_request)
|
||
result = response.json()
|
||
print(f" ✓ {result['message']}")
|
||
print(f" 环境: {result['environment']}")
|
||
print(f" 股票: {', '.join(result['symbols'])}")
|
||
print(f" 生成数量: {result['generated_count']} 条")
|
||
|
||
# 6. 再次查看表信息
|
||
print("\n6️⃣ 查看数据准备后的表...")
|
||
response = await client.get(f"{BASE_URL}/api/tables/backtest")
|
||
result = response.json()
|
||
for table in result["tables"]:
|
||
if table["name"].startswith("kline"):
|
||
print(
|
||
f" • {table['name']} - {table['rows']} 行, {table['size_mb']:.2f} MB"
|
||
)
|
||
|
||
print("\n" + "=" * 70)
|
||
print("✅ 测试完成".center(70))
|
||
print("=" * 70)
|
||
print("\n💡 更多操作:")
|
||
print(" • API文档: http://localhost:8015/docs")
|
||
print(" • 删除数据库: DELETE /api/databases/{environment}")
|
||
print(" • 创建表: POST /api/tables")
|
||
print(" • 删除所有表: DELETE /api/tables/{environment}")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
asyncio.run(test_database_admin_service())
|