From 9b04fe19435f98cab6dcc6110592d628375392a7 Mon Sep 17 00:00:00 2001 From: xxm Date: Tue, 18 Aug 2026 20:24:59 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20sync=5Fstrategy=5Fdef=E7=BB=B4=E6=8A=A4?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E2=80=94=E2=80=94scanner=E8=87=AA=E6=B3=A8?= =?UTF-8?q?=E5=86=8C+key=5Fparams=20drift=E6=A3=80=E6=B5=8B(=E5=8F=98?= =?UTF-8?q?=E6=9B=B4=E2=86=92needs=5Freview+version=20bump)(=E8=80=81?= =?UTF-8?q?=E8=8E=AB:=E7=AD=96=E7=95=A5=E5=AE=9A=E4=B9=89=E4=B8=8D?= =?UTF-8?q?=E8=BF=87=E6=9C=9F)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deploy/profile-scripts/mofin_db.py | 53 ++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/deploy/profile-scripts/mofin_db.py b/deploy/profile-scripts/mofin_db.py index 97621dd6..9b6b8187 100644 --- a/deploy/profile-scripts/mofin_db.py +++ b/deploy/profile-scripts/mofin_db.py @@ -2388,3 +2388,56 @@ def read_capital_flow_cache(conn) -> dict: import json r = conn.execute("SELECT cache_json FROM capital_flow_cache WHERE id=1 ORDER BY updated_at DESC LIMIT 1").fetchone() return json.loads(r['cache_json']) if r else {} + + +def sync_strategy_def(conn, d: dict): + """upsert 策略定义。d 只含非空字段;prose 缺省时保留旧值;key_params 变化→needs_review。""" + import json, hashlib + name = d.get("strategy_name", "") + if not name: + print(" [SYNC_DEF] strategy_name 为空,跳过") + return + old = conn.execute( + "SELECT key_params, params_hash, version FROM strategy_defs WHERE strategy_name=?", (name,)).fetchone() + kp = d.get("key_params") + hp = hashlib.md5((kp or "").encode()).hexdigest()[:12] if kp else None + now = "2026-08-18 17:40:00" # 用 DB 时间更好,但保持简单 + changed = False + if old: + # 已有:只更新非空字段;key_params 变化 → bump + sets, params = [], [] + for col in ("display_name", "market", "regime", "family", "summary", "entry_logic", + "exit_logic", "review_focus", "holding_style", "version", "scanner_file", "doc_ref"): + if d.get(col): + sets.append(f"{col}=?") + params.append(d.get(col)) + if kp: + if not old[0] or old[1] != hp: + sets.append("key_params=?") + params.append(kp) + sets.append("params_hash=?") + params.append(hp) + sets.append("needs_review=1") # 参数变了要人审 + sets.append("version=?") + params.append(d.get("version") or (int((old[2] or "v0")[1:]) + 1 if str(old[2] or "").startswith("v") else "v1")) + changed = True + print(f" [SYNC_DEF] {name} 参数变更 → needs_review=1, version bump") + if sets: + sets.append("updated_at=datetime('now','localtime')") + params.append(name) + conn.execute(f"UPDATE strategy_defs SET {', '.join(sets)} WHERE strategy_name=?", params + [name]) + if not changed: + print(f" [SYNC_DEF] {name} 定义一致,无需更新") + else: + # 新增:全量插入 + if not d.get("display_name"): d["display_name"] = name + d["params_hash"] = hp or "" + d["needs_review"] = 1 # 新注册需人工确认 prose + d["updated_at"] = "datetime('now','localtime')" + cols = ",".join(d.keys()) + ph = ",".join("?" * len(d)) + conn.execute(f"INSERT OR IGNORE INTO strategy_defs ({cols}) VALUES ({ph})", + [d[k] for k in d]) + print(f" [SYNC_DEF] {name} 新注册(needs_review=1)") + conn.commit() + return changed