2025-12-13 10:30:31 +08:00
|
|
|
|
"""
|
|
|
|
|
|
测试 Database Service
|
|
|
|
|
|
|
|
|
|
|
|
演示如何使用数据库管理服务的API
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
|
|
import httpx
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BASE_URL = "http://localhost:8015"
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-12-13 11:27:08 +08:00
|
|
|
|
async def test_database_admin_service():
|
2025-12-13 10:30:31 +08:00
|
|
|
|
"""测试数据库管理服务"""
|
|
|
|
|
|
print("=" * 70)
|
2025-12-13 11:27:08 +08:00
|
|
|
|
print("测试 Database Admin Service".center(70))
|
2025-12-13 10:30:31 +08:00
|
|
|
|
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(
|
2025-12-13 11:27:08 +08:00
|
|
|
|
f" 启动命令: uvicorn src.database_admin_service.app:app --host 0.0.0.0 --port 8015"
|
2025-12-13 10:30:31 +08:00
|
|
|
|
)
|
|
|
|
|
|
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__":
|
2025-12-13 11:27:08 +08:00
|
|
|
|
asyncio.run(test_database_admin_service())
|