持仓同步改为后台执行,添加进度条实时反馈
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
from fastapi import APIRouter, Depends, Form, Request
|
||||
from fastapi.responses import HTMLResponse, RedirectResponse
|
||||
from fastapi.responses import HTMLResponse, RedirectResponse, JSONResponse
|
||||
from sqlalchemy.orm import Session
|
||||
from app.database import get_db
|
||||
from app.models import Product, Contract, DailyBar, PositionRanking
|
||||
from app.collector import sync_active_contracts, sync_one_contract, sync_position_rankings
|
||||
from app.collector import (
|
||||
sync_active_contracts, sync_one_contract, sync_position_rankings,
|
||||
start_sync_positions_background, get_sync_progress,
|
||||
)
|
||||
|
||||
router = APIRouter(prefix="/admin", tags=["admin"])
|
||||
|
||||
@@ -147,10 +150,15 @@ def sync_single(contract_code: str):
|
||||
|
||||
@router.post("/sync-positions")
|
||||
def sync_positions(request: Request):
|
||||
results = sync_position_rankings()
|
||||
total = sum(results.values())
|
||||
print(f"[sync] Position rankings: {total} rows across {len(results)} contracts")
|
||||
return RedirectResponse(f"/admin/?tab=sync&pos_synced={total}", status_code=303)
|
||||
started = start_sync_positions_background()
|
||||
if not started:
|
||||
return JSONResponse({"error": "同步正在进行中,请等待完成"})
|
||||
return JSONResponse({"started": True})
|
||||
|
||||
|
||||
@router.get("/sync-status")
|
||||
def sync_status():
|
||||
return JSONResponse(get_sync_progress())
|
||||
|
||||
|
||||
@router.post("/sync/product/{product_id}")
|
||||
|
||||
Reference in New Issue
Block a user