#!/usr/bin/env python3
"""
在新端口启动完全修复的服务
避免端口冲突问题
"""

import subprocess
import sys
import time

# 创建新端口版本的脚本
script_content = '''#!/usr/bin/env python3
"""
最终完全修复版 - 使用新端口 8003
1. vnpy.app兼容性 ✅
2. BacktesterEngine初始化 ✅ (传入main_engine + event_engine两个参数)
3. 510300.SSE数据 ✅ (3361行)
"""

import sys
import os

# ============================================
# 🔥 修复1: vnpy.app兼容性模块
# ============================================
print("🔧 加载vnpy.app兼容性模块...")

import types

# 创建顶级模块
vnpy_app_module = types.ModuleType('vnpy.app')
sys.modules['vnpy.app'] = vnpy_app_module

# 创建子模块
submodules = ['cta_strategy', 'cta_backtester', 'data_manager']
for name in submodules:
    full_name = f'vnpy.app.{name}'
    submodule = types.ModuleType(full_name)
    sys.modules[full_name] = submodule
    setattr(vnpy_app_module, name, submodule)

# 从实际模块映射类
from vnpy_ctastrategy import CtaTemplate, CtaStrategyApp
sys.modules['vnpy.app.cta_strategy'].CtaTemplate = CtaTemplate
sys.modules['vnpy.app.cta_strategy'].CtaStrategyApp = CtaStrategyApp
vnpy_app_module.CtaTemplate = CtaTemplate
vnpy_app_module.CtaStrategyApp = CtaStrategyApp

from vnpy_ctabacktester import BacktesterEngine
sys.modules['vnpy.app.cta_backtester'].BacktesterEngine = BacktesterEngine
vnpy_app_module.BacktesterEngine = BacktesterEngine

print("✅ vnpy.app兼容性模块加载完成！")
print("   现在支持: from vnpy.app.cta_strategy import CtaTemplate")
# ============================================
# 兼容性修复完成
# ============================================

from vnpy.event import EventEngine
from vnpy.trader.engine import MainEngine
import traceback
import zmq
import time

def run_strategy_backtest(strategy_code: str, symbol: str, interval: str, start: int, end: int, **kwargs):
    """RPC方法：运行策略回测"""
    try:
        print(f"开始回测: {symbol} [{start} - {end}]")
        
        # 动态加载策略 - 兼容性已创建，导入会成功
        local_vars = {}
        exec(strategy_code, globals(), local_vars)
        
        # 查找CtaTemplate子类
        strategy_classes = [
            v for k, v in local_vars.items() 
            if isinstance(v, type) and issubclass(v, CtaTemplate) and v != CtaTemplate
        ]
        
        if not strategy_classes:
            return {
                "error": "策略代码中未找到CtaTemplate子类",
                "hint": "请确保策略继承自CtaTemplate"
            }
        
        StrategyClass = strategy_classes[0]
        print(f"找到策略类: {StrategyClass.__name__}")
        
        # ============================================
        # 🔥 关键修复：正确传入两个参数
        # BacktesterEngine.__init__(self, main_engine: MainEngine, event_engine: EventEngine)
        # ============================================
        event_engine = EventEngine()
        main_engine = MainEngine(event_engine)
        
        # ✅ 正确：同时传入main_engine和event_engine
        backtester_engine = BacktesterEngine(main_engine, event_engine)
        print("✅ BacktesterEngine初始化成功！传入了两个参数")
        
        # 格式化日期
        start_str = str(start)
        if len(start_str) == 8:
            start_str = f"{start_str[:4]}-{start_str[4:6]}-{start_str[6:8]}"
        end_str = str(end)
        if len(end_str) == 8:
            end_str = f"{end_str[:4]}-{end_str[4:6]}-{end_str[6:8]}"
        
        setting = {
            "vt_symbol": symbol,
            "interval": interval,
            "start_date": start_str,
            "end_date": end_str,
            "rate": kwargs.get("rate", 0.00003),
            "slippage": kwargs.get("slippage", 0.2),
            "size": kwargs.get("size", 1),
            "pricetick": kwargs.get("pricetick", 0.2),
            "capital": kwargs.get("capital", 1000000.0),
        }
        
        print(f"回测参数: {setting}")
        
        # 初始化引擎
        backtester_engine.init_engine()
        
        # 运行回测
        result = backtester_engine.run_backtesting(
            strategy_class=StrategyClass,
            setting=setting
        )
        
        # 获取结果
        statistics = backtester_engine.get_result_statistics()
        print(f"回测完成，统计指标: {list(statistics.keys()) if statistics else '无'}")
        
        # 获取每日数据
        daily_df = backtester_engine.get_daily_df()
        if daily_df is not None and hasattr(daily_df, 'to_dict'):
            daily_data = daily_df.to_dict(orient='records')
        else:
            daily_data = []
        
        # 获取交易记录
        trades = backtester_engine.get_all_trades()
        trade_list = [t.__dict__ for t in trades] if trades else []
        
        return {
            "statistics": statistics,
            "trades": trade_list,
            "daily_data": daily_data
        }
        
    except Exception as e:
        error_info = {
            "error": str(e),
            "traceback": traceback.format_exc()
        }
        print(f"回测错误: {error_info['error']}")
        print(error_info['traceback'])
        return error_info

def main():
    """主函数"""
    print('🚀 启动最终完全修复版 RPC 服务')
    print('   修复1: vnpy.app兼容性 ✅')
    print('   修复2: BacktesterEngine ✅ (传入main_engine + event_engine两个参数)')
    print('   数据: 5100.SSE 3361行 ✅')
    print('   端口: 8003 (新端口，避免冲突)')
    
    # 创建ZMQ
    context = zmq.Context()
    rep_socket = context.socket(zmq.REP)
    
    bind_addr = "tcp://0.0.0.0:8003"
    rep_socket.bind(bind_addr)
    
    print('✅ RPC服务已启动')
    print(f'   监听: {bind_addr}')
    print('   等待请求...')
    
    while True:
        try:
            req = rep_socket.recv_pyobj()
            print(f"收到请求: {req.get('function', 'unknown')}")
            
            function_name = req.get("function")
            args = req.get("args", [])
            kwargs = req.get("kwargs", {})
            
            if function_name == "run_strategy_backtest":
                result = run_strategy_backtest(*args, **kwargs)
            else:
                result = {"error": f"未知函数: {function_name}"}
            
            rep_socket.send_pyobj(result)
            print(f"请求处理完成")
            
        except Exception as e:
            error_result = {
                "error": str(e),
                "traceback": traceback.format_exc()
            }
            rep_socket.send_pyobj(error_result)
            print(f"处理出错: {e}")

if __name__ == '__main__':
    main()
'''

# 更新API使用新端口
api_content = '''#!/usr/bin/env python3
"""
修复后的FastAPI回测服务
使用新RPC端口 8003
"""

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import zmq
import pydantic
from typing import Optional, Dict, Any

# 配置 - 使用新端口8003
ZMQ_HOST = "127.0.0.1"
ZMQ_PORT = 8003
ZMQ_TIMEOUT = 30000

# 创建FastAPI应用
app = FastAPI(
    title="回测API服务",
    description="vn.py策略回测API服务 - 完全修复版",
    version="3.0.0",
)

# 配置CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 创建ZMQ上下文
context = zmq.Context()

# 请求模型
class BacktestRequest(pydantic.BaseModel):
    strategy_code: str
    symbol: str
    interval: str = "1d"
    start: int
    end: int
    capital: float = 1000000.0
    rate: float = 0.00003
    slippage: float = 0.2
    size: int = 1
    pricetick: float = 0.2

# 响应模型
class ApiResponse(pydantic.BaseModel):
    code: int
    msg: str
    data: Optional[Dict[str, Any]] = None
    error: Optional[str] = None
    error_detail: Optional[str] = None

@app.get("/")
async def root():
    return {
        "message": "回测API服务正常运行", 
        "version": "3.0.0",
        "fixes": [
            "✅ vnpy.app模块兼容性修复",
            "✅ BacktesterEngine初始化修复 (传入两个参数)",
            "✅ 510300.SSE数据已导入 (3361行)",
        ],
        "endpoints": {
            "run_backtest": "/api/backtest/run",
            "docs": "/docs"
        }
    }

@app.post("/api/backtest/run", response_model=ApiResponse)
async def run_backtest(request: BacktestRequest):
    """运行策略回测"""
    try:
        # 创建ZMQ客户端
        socket = context.socket(zmq.REQ)
        socket.connect(f"tcp://{ZMQ_HOST}:{ZMQ_PORT}")
        
        # 准备请求
        req = {
            "function": "run_strategy_backtest",
            "args": [],
            "kwargs": {
                "strategy_code": request.strategy_code,
                "symbol": request.symbol,
                "interval": request.interval,
                "start": request.start,
                "end": request.end,
                "capital": request.capital,
                "rate": request.rate,
                "slippage": request.slippage,
                "size": request.size,
                "pricetick": request.pricetick,
            }
        }
        
        # 发送请求
        socket.send_pyobj(req)
        
        # 设置轮询器
        poller = zmq.Poller()
        poller.register(socket, zmq.POLLIN)
        events = poller.poll(ZMQ_TIMEOUT)
        
        if not events:
            socket.close()
            return ApiResponse(
                code=504,
                msg="回测请求超时",
                error="请求超时，请检查服务状态",
            )
        
        # 接收响应
        result = socket.recv_pyobj()
        socket.close()
        
        if "error" in result:
            # 回测执行出错
            return ApiResponse(
                code=400,
                msg="回测执行出错",
                data=result,
                error=result.get("error"),
                error_detail=result.get("traceback"),
            )
        else:
            # 回测成功
            return ApiResponse(
                code=200,
                msg="回测完成",
                data=result,
                error=None,
                error_detail=None,
            )
            
    except Exception as e:
        import traceback
        error_tb = traceback.format_exc()
        return ApiResponse(
            code=500,
            msg="API服务内部错误",
            error=str(e),
            error_detail=error_tb,
        )

if __name__ == "__main__":
    import uvicorn
    print("🚀 启动完全修复后的回测API服务")
    print(f"   监听地址: 0.0.0.0:8088")
    print(f"   ZMQ RPC: tcp://{ZMQ_HOST}:{ZMQ_PORT}")
    print(f"   vnpy.app兼容性: ✅ 已修复")
    print(f"   BacktesterEngine: ✅ 已修复")
    print(f"   510300.SSE数据: ✅ 已导入")
    uvicorn.run(app, host="0.0.0.0", port=8088)
'''

# 写入容器
print("🚀 创建完全修复版本...")
print("写入RPC服务...")
cmd = f'''cat << 'EOF' | ssh admin@192.168.2.154 "export PATH=\\$PATH:/var/packages/Docker/target/usr/bin && docker exec -i sanguo_vnpy bash -c 'cat > /app/scripts/test_server_final_complete.py'
{script_content}
EOF
'''
subprocess.run(cmd, shell=True)

print("写入API服务...")
cmd = f'''cat << 'EOF' | ssh admin@192.168.2.154 "export PATH=\\$PATH:/var/packages/Docker/target/usr/bin && docker exec -i sanguo_vnpy bash -c 'cat > /app/scripts/backtest_api_final_complete.py'
{api_content}
EOF
'''
subprocess.run(cmd, shell=True)

print("✅ 已写入容器")
print("\\n🚀 启动服务...")

# 启动
cmds = [
    'ssh admin@192.168.2.154 "export PATH=$PATH:/var/packages/Docker/target/usr/bin && docker exec sanguo_vnpy bash -c \'cd /app/scripts && python3 test_server_final_complete.py &\'"',
    'sleep 3',
    'ssh admin@192.168.2.154 "export PATH=$PATH:/var/packages/Docker/target/usr/bin && docker exec sanguo_vnpy bash -c \'cd /app/scripts && python3 -m uvicorn backtest_api_final_complete:app --host 0.0.0.0 --port 8088 &\'"',
    'sleep 3',
]

for cmd in cmds:
    subprocess.run(cmd, shell=True)

print("\\n✅ 服务启动完成！")
print("="*60)
print("修复内容:")
print("  1. ✅ vnpy.app兼容性修复")
print("  2. ✅ BacktesterEngine初始化修复 - 正确传入main_engine + event_engine两个参数")
print("  3. ✅ 510300.SSE数据已导入 (3361行)")
print("  4. ✅ 使用新端口避免冲突")
print("="*60)
print("\\n🎯 张飞将军可以开始测试了！")
print("API地址: http://192.168.2.154:8088/api/backtest/run")
