修复复盘列表先读缓存再后台同步,避免模拟盘握库卡死加载。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-10-07 13:50:04 +08:00
parent 2158a30179
commit d7b3f7a8e7
4 changed files with 78 additions and 12 deletions
+29 -3
View File
@@ -302,6 +302,11 @@ def sync_options_from_local_trades(
LIMIT 500
"""
).fetchall()
# 读完先提交,后续拉指数不与写事务叠在一起
try:
conn.commit()
except Exception:
pass
def _index_px(underly: str) -> float | None:
u = (underly or "ETH").strip().upper() or "ETH"
@@ -356,6 +361,18 @@ def sync_options_from_local_trades(
inserted = updated = skipped = 0
idx_cache: dict[str, float | None] = {}
need_idx: set[str] = set()
for r in rows:
inst0 = str(r["inst_id"] or "")
underly0 = str(r["underlying"] or (inst0.split("-")[0] if inst0 else "ETH") or "ETH")
ccy0 = str(r["premium_ccy"] or "").strip().upper()
if not ccy0:
ccy0 = premium_ccy_for_mode(margin_mode_from_inst_id(inst0), underly0)
if ccy0 in ("ETH", "BTC"):
need_idx.add(underly0)
for underly0 in need_idx:
idx_cache[underly0] = _index_px(underly0)
for r in rows:
trade_id = int(r["id"])
history_key = f"local_opt:{trade_id}"
@@ -364,8 +381,6 @@ def sync_options_from_local_trades(
ccy = str(r["premium_ccy"] or "").strip().upper()
if not ccy:
ccy = premium_ccy_for_mode(margin_mode_from_inst_id(inst), underly)
if underly not in idx_cache:
idx_cache[underly] = _index_px(underly)
idx = idx_cache.get(underly)
pnl = _safe_float(r["realized_pnl"])
if pnl is None:
@@ -644,6 +659,11 @@ def ensure_local_review_synced(
note_swap = repair_swapped_closed_at_signal_note(conn)
except Exception as e:
note_swap = {"ok": False, "msg": str(e)}
# 提交后再拉历史/行情,避免模拟盘 broker 二次开库与本连接互锁
try:
conn.commit()
except Exception:
pass
if backfill_exchange_pnl and ex is not None:
try:
from lib.exchange.okx_options_lib import fetch_all_option_positions_history
@@ -653,8 +673,12 @@ def ensure_local_review_synced(
hist = fetch_all_option_positions_history(ex, limit=200)
backfill_closed_options_realized_pnl_from_history(conn, hist)
conn.commit()
except Exception:
pass
try:
conn.rollback()
except Exception:
pass
try:
out = sync_all_review_sources(conn, ex=ex, from_exchange=False)
except sqlite3.OperationalError as e:
@@ -662,6 +686,8 @@ def ensure_local_review_synced(
if isinstance(out, dict):
out["ts_migrate"] = ts_migrate
out["note_swap"] = note_swap
if out.get("ok") is not False:
out["ok"] = True
return out