This commit is contained in:
薇薇安 2026-02-12 10:15:44 +08:00
parent a033d1ea6d
commit 8c91db3f60

View File

@ -6,7 +6,7 @@ import time
from pathlib import Path
from typing import Any, Dict, Optional, Tuple
from fastapi import APIRouter, HTTPException, Header, Depends
from fastapi import APIRouter, HTTPException, Header, Depends, BackgroundTasks
from pydantic import BaseModel
import logging
@ -708,6 +708,51 @@ def _action_with_fallback(action: str, program: str) -> Tuple[str, Optional[str]
return out, resolved, status_all
def _run_fix_script():
"""Run the fix_trade_records.py script in a subprocess"""
try:
script_path = Path(__file__).parent.parent.parent.parent / "scripts" / "fix_trade_records.py"
if not script_path.exists():
logger.error(f"Fix script not found at {script_path}")
return
logger.info(f"Starting trade record fix script: {script_path}")
# Ensure project root is in PYTHONPATH
env = os.environ.copy()
project_root = Path(__file__).parent.parent.parent.parent
env["PYTHONPATH"] = f"{env.get('PYTHONPATH', '')}:{project_root}"
process = subprocess.Popen(
["python3", str(script_path)],
env=env,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
stdout, stderr = process.communicate()
if process.returncode == 0:
logger.info(f"Trade record fix completed successfully:\n{stdout}")
else:
logger.error(f"Trade record fix failed (exit code {process.returncode}):\n{stderr}")
except Exception as e:
logger.error(f"Error running trade record fix script: {e}")
@router.post("/fix-trade-records")
async def fix_trade_records(
background_tasks: BackgroundTasks,
_admin: Dict[str, Any] = Depends(require_system_admin)
):
"""
Trigger the trade record fix script (time inversion & commission backfill).
Runs in background.
"""
background_tasks.add_task(_run_fix_script)
return {"message": "Trade fix task started in background"}
@router.post("/clear-cache")
async def clear_cache(
_admin: Dict[str, Any] = Depends(require_system_admin),