a
This commit is contained in:
parent
e5cea19b6a
commit
a3129f57b6
|
|
@ -4,7 +4,6 @@
|
||||||
from fastapi import APIRouter, HTTPException
|
from fastapi import APIRouter, HTTPException
|
||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import asyncio
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
project_root = Path(__file__).parent.parent.parent.parent
|
project_root = Path(__file__).parent.parent.parent.parent
|
||||||
|
|
@ -13,7 +12,6 @@ sys.path.insert(0, str(project_root / 'backend'))
|
||||||
sys.path.insert(0, str(project_root / 'trading_system'))
|
sys.path.insert(0, str(project_root / 'trading_system'))
|
||||||
|
|
||||||
from database.models import TradingConfig
|
from database.models import TradingConfig
|
||||||
from fastapi import HTTPException
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
@ -111,10 +109,14 @@ async def get_realtime_positions():
|
||||||
api_secret = TradingConfig.get_value('BINANCE_API_SECRET')
|
api_secret = TradingConfig.get_value('BINANCE_API_SECRET')
|
||||||
use_testnet = TradingConfig.get_value('USE_TESTNET', False)
|
use_testnet = TradingConfig.get_value('USE_TESTNET', False)
|
||||||
|
|
||||||
|
logger.info(f"尝试获取实时持仓数据 (testnet={use_testnet})")
|
||||||
|
|
||||||
if not api_key or not api_secret:
|
if not api_key or not api_secret:
|
||||||
|
error_msg = "API密钥未配置"
|
||||||
|
logger.warning(error_msg)
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=400,
|
status_code=400,
|
||||||
detail="API密钥未配置"
|
detail=error_msg
|
||||||
)
|
)
|
||||||
|
|
||||||
# 导入BinanceClient
|
# 导入BinanceClient
|
||||||
|
|
@ -131,10 +133,13 @@ async def get_realtime_positions():
|
||||||
testnet=use_testnet
|
testnet=use_testnet
|
||||||
)
|
)
|
||||||
|
|
||||||
|
logger.info("连接币安API获取持仓...")
|
||||||
await client.connect()
|
await client.connect()
|
||||||
positions = await client.get_open_positions()
|
positions = await client.get_open_positions()
|
||||||
await client.disconnect()
|
await client.disconnect()
|
||||||
|
|
||||||
|
logger.info(f"获取到 {len(positions)} 个持仓")
|
||||||
|
|
||||||
# 格式化持仓数据
|
# 格式化持仓数据
|
||||||
formatted_positions = []
|
formatted_positions = []
|
||||||
for pos in positions:
|
for pos in positions:
|
||||||
|
|
@ -165,9 +170,11 @@ async def get_realtime_positions():
|
||||||
"leverage": int(pos.get('leverage', 1))
|
"leverage": int(pos.get('leverage', 1))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
logger.info(f"格式化后 {len(formatted_positions)} 个有效持仓")
|
||||||
return formatted_positions
|
return formatted_positions
|
||||||
except HTTPException:
|
except HTTPException:
|
||||||
raise
|
raise
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"获取持仓数据失败: {e}", exc_info=True)
|
error_msg = f"获取持仓数据失败: {str(e)}"
|
||||||
raise HTTPException(status_code=500, detail=f"获取持仓数据失败: {str(e)}")
|
logger.error(error_msg, exc_info=True)
|
||||||
|
raise HTTPException(status_code=500, detail=error_msg)
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ from fastapi import APIRouter, Query
|
||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
import logging
|
||||||
|
|
||||||
project_root = Path(__file__).parent.parent.parent.parent
|
project_root = Path(__file__).parent.parent.parent.parent
|
||||||
sys.path.insert(0, str(project_root))
|
sys.path.insert(0, str(project_root))
|
||||||
|
|
@ -13,6 +14,7 @@ sys.path.insert(0, str(project_root / 'backend'))
|
||||||
from database.models import AccountSnapshot, Trade, MarketScan, TradingSignal
|
from database.models import AccountSnapshot, Trade, MarketScan, TradingSignal
|
||||||
from fastapi import HTTPException
|
from fastapi import HTTPException
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -41,41 +43,109 @@ async def get_dashboard_data():
|
||||||
"""获取仪表板数据"""
|
"""获取仪表板数据"""
|
||||||
try:
|
try:
|
||||||
account_data = None
|
account_data = None
|
||||||
|
account_error = None
|
||||||
|
|
||||||
# 优先尝试获取实时账户数据
|
# 优先尝试获取实时账户数据
|
||||||
try:
|
try:
|
||||||
from api.routes.account import get_realtime_account_data
|
from api.routes.account import get_realtime_account_data
|
||||||
account_data = await get_realtime_account_data()
|
account_data = await get_realtime_account_data()
|
||||||
except Exception as e:
|
logger.info("成功获取实时账户数据")
|
||||||
logger.warning(f"获取实时账户数据失败,使用数据库快照: {e}")
|
except HTTPException as e:
|
||||||
|
# HTTPException 需要特殊处理,提取错误信息
|
||||||
|
account_error = e.detail
|
||||||
|
logger.warning(f"获取实时账户数据失败 (HTTP {e.status_code}): {account_error}")
|
||||||
# 回退到数据库快照
|
# 回退到数据库快照
|
||||||
snapshots = AccountSnapshot.get_recent(1)
|
try:
|
||||||
account_data = snapshots[0] if snapshots else None
|
snapshots = AccountSnapshot.get_recent(1)
|
||||||
|
if snapshots:
|
||||||
|
account_data = {
|
||||||
|
"total_balance": snapshots[0].get('total_balance', 0),
|
||||||
|
"available_balance": snapshots[0].get('available_balance', 0),
|
||||||
|
"total_position_value": snapshots[0].get('total_position_value', 0),
|
||||||
|
"total_pnl": snapshots[0].get('total_pnl', 0),
|
||||||
|
"open_positions": snapshots[0].get('open_positions', 0)
|
||||||
|
}
|
||||||
|
logger.info("使用数据库快照作为账户数据")
|
||||||
|
else:
|
||||||
|
logger.warning("数据库中没有账户快照数据")
|
||||||
|
except Exception as db_error:
|
||||||
|
logger.error(f"从数据库获取账户快照失败: {db_error}")
|
||||||
|
except Exception as e:
|
||||||
|
account_error = str(e)
|
||||||
|
logger.warning(f"获取实时账户数据失败: {account_error}", exc_info=True)
|
||||||
|
# 回退到数据库快照
|
||||||
|
try:
|
||||||
|
snapshots = AccountSnapshot.get_recent(1)
|
||||||
|
if snapshots:
|
||||||
|
account_data = {
|
||||||
|
"total_balance": snapshots[0].get('total_balance', 0),
|
||||||
|
"available_balance": snapshots[0].get('available_balance', 0),
|
||||||
|
"total_position_value": snapshots[0].get('total_position_value', 0),
|
||||||
|
"total_pnl": snapshots[0].get('total_pnl', 0),
|
||||||
|
"open_positions": snapshots[0].get('open_positions', 0)
|
||||||
|
}
|
||||||
|
logger.info("使用数据库快照作为账户数据")
|
||||||
|
except Exception as db_error:
|
||||||
|
logger.error(f"从数据库获取账户快照失败: {db_error}")
|
||||||
|
|
||||||
# 获取持仓数据(优先实时,回退到数据库)
|
# 获取持仓数据(优先实时,回退到数据库)
|
||||||
open_trades = []
|
open_trades = []
|
||||||
|
positions_error = None
|
||||||
try:
|
try:
|
||||||
from api.routes.account import get_realtime_positions
|
from api.routes.account import get_realtime_positions
|
||||||
positions = await get_realtime_positions()
|
positions = await get_realtime_positions()
|
||||||
# 转换为前端需要的格式
|
# 转换为前端需要的格式
|
||||||
open_trades = positions
|
open_trades = positions
|
||||||
except Exception as e:
|
logger.info(f"成功获取实时持仓数据: {len(open_trades)} 个持仓")
|
||||||
logger.warning(f"获取实时持仓失败,使用数据库记录: {e}")
|
except HTTPException as e:
|
||||||
|
positions_error = e.detail
|
||||||
|
logger.warning(f"获取实时持仓失败 (HTTP {e.status_code}): {positions_error}")
|
||||||
# 回退到数据库记录
|
# 回退到数据库记录
|
||||||
open_trades = Trade.get_all(status='open')[:10]
|
try:
|
||||||
|
open_trades = Trade.get_all(status='open')[:10]
|
||||||
|
logger.info(f"使用数据库记录作为持仓数据: {len(open_trades)} 个持仓")
|
||||||
|
except Exception as db_error:
|
||||||
|
logger.error(f"从数据库获取持仓记录失败: {db_error}")
|
||||||
|
except Exception as e:
|
||||||
|
positions_error = str(e)
|
||||||
|
logger.warning(f"获取实时持仓失败: {positions_error}", exc_info=True)
|
||||||
|
# 回退到数据库记录
|
||||||
|
try:
|
||||||
|
open_trades = Trade.get_all(status='open')[:10]
|
||||||
|
logger.info(f"使用数据库记录作为持仓数据: {len(open_trades)} 个持仓")
|
||||||
|
except Exception as db_error:
|
||||||
|
logger.error(f"从数据库获取持仓记录失败: {db_error}")
|
||||||
|
|
||||||
# 最近的扫描记录
|
# 最近的扫描记录
|
||||||
recent_scans = MarketScan.get_recent(10)
|
recent_scans = []
|
||||||
|
try:
|
||||||
|
recent_scans = MarketScan.get_recent(10)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"获取扫描记录失败: {e}")
|
||||||
|
|
||||||
# 最近的信号
|
# 最近的信号
|
||||||
recent_signals = TradingSignal.get_recent(20)
|
recent_signals = []
|
||||||
|
try:
|
||||||
|
recent_signals = TradingSignal.get_recent(20)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"获取交易信号失败: {e}")
|
||||||
|
|
||||||
return {
|
result = {
|
||||||
"account": account_data,
|
"account": account_data,
|
||||||
"open_trades": open_trades,
|
"open_trades": open_trades,
|
||||||
"recent_scans": recent_scans,
|
"recent_scans": recent_scans,
|
||||||
"recent_signals": recent_signals
|
"recent_signals": recent_signals
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# 如果有错误,在响应中包含错误信息(但不影响返回)
|
||||||
|
if account_error or positions_error:
|
||||||
|
result["warnings"] = {}
|
||||||
|
if account_error:
|
||||||
|
result["warnings"]["account"] = account_error
|
||||||
|
if positions_error:
|
||||||
|
result["warnings"]["positions"] = positions_error
|
||||||
|
|
||||||
|
return result
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"获取仪表板数据失败: {e}", exc_info=True)
|
logger.error(f"获取仪表板数据失败: {e}", exc_info=True)
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=f"获取仪表板数据失败: {str(e)}")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user