Prevent duplicate strategy trade snapshots on plan close.

Finalize plans before writing snapshots, dedupe on startup and page load, and add a cleanup script for existing repeated rows.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-08 09:00:51 +08:00
parent ea92160d54
commit e71bfe095c
6 changed files with 355 additions and 11 deletions
+24
View File
@@ -12,6 +12,29 @@ from strategy_db import init_strategy_tables
from strategy_roll_lib import preview_roll
def _dedupe_strategy_snapshots_on_startup(cfg: dict[str, Any]) -> None:
"""启动时清理历史重复快照(同计划同结果仅保留最新一条)。"""
get_db = cfg.get("get_db")
if not callable(get_db):
return
try:
from strategy_snapshot_lib import dedupe_strategy_snapshots
conn = get_db()
try:
removed = dedupe_strategy_snapshots(conn)
if removed:
conn.commit()
print(
f"[strategy] deduped {removed} duplicate strategy_trade_snapshots",
flush=True,
)
finally:
conn.close()
except Exception as e:
print(f"[strategy] snapshot dedupe skipped: {e}", flush=True)
def install_strategy_trading(app: Flask, repo_root: str, app_module: Any = None, **build_kw) -> None:
"""在 app.py 末尾调用(login_required 已定义后)。仅注册 POST API;页面由各 app 的 render_main_page 渲染。"""
from strategy_config import build_strategy_config
@@ -24,6 +47,7 @@ def install_strategy_trading(app: Flask, repo_root: str, app_module: Any = None,
register_strategy_records(app, cfg)
app.extensions["strategy_roll_cfg"] = cfg
_dedupe_strategy_snapshots_on_startup(cfg)
def attach_strategy_templates(app: Flask, repo_root: str) -> None: