migrate: remove final JSON cold backup lines, prune/branch→DB

This commit is contained in:
知微
2026-07-03 12:41:20 +08:00
parent b3bedc8024
commit 9124a7ad56
5 changed files with 285 additions and 264 deletions
+9 -2
View File
@@ -101,8 +101,15 @@ def main():
br["last_triggered"] = now.strftime("%Y-%m-%d")
break
with open(DECISIONS_PATH, "w") as f:
json.dump(data, f, indent=2, ensure_ascii=False)
# 写入 DB(替代 decisions.json
try:
from mofin_db import get_conn, write_holding_strategy
conn = get_conn()
for e in data.get('decisions', []):
write_holding_strategy(conn, e.get('code', ''), e.get('name', ''), e)
conn.close()
except Exception:
pass
# 更新状态快照
state = {"scenario": sid, "updated_at": now.isoformat(), "branches": {}}
+7 -4
View File
@@ -19,13 +19,16 @@ PRUNE_LOG = "/home/hmo/MoFin/data/prune_log.json"
def load_decisions():
with open(DECISIONS_PATH) as f:
return json.load(f)
from mo_data import read_decisions
return read_decisions()
def save_decisions(data):
with open(DECISIONS_PATH, "w") as f:
json.dump(data, f, indent=2, ensure_ascii=False)
from mofin_db import get_conn, write_holding_strategy
conn = get_conn()
for e in data.get('decisions', []):
write_holding_strategy(conn, e.get('code', ''), e.get('name', ''), e)
conn.close()
def main():
+37
View File
@@ -0,0 +1,37 @@
"""Final verification: full data cycle after JSON→DB migration"""
import sys
sys.path.insert(0, '/home/hmo/MoFin')
from mo_data import read_portfolio, read_decisions, read_watchlist
pf = read_portfolio()
dec = read_decisions()
wl = read_watchlist()
h = len(pf.get('holdings', []))
d = len(dec.get('decisions', []))
w = len(wl.get('stocks', []))
print(f"portfolio holdings: {h}")
print(f"decisions: {d}")
print(f"watchlist: {w}")
# Check one HK stock has correct CNY cost
for holding in pf.get('holdings', []):
if holding.get('code') == '01888':
print(f"\n01888 cost={holding.get('cost')} price={holding.get('price')} curr={holding.get('currency')}")
c = holding.get('cost', 0); p = holding.get('price', 0)
if c and p:
print(f"P&L: {(p-c)/c*100:.1f}%")
# Check decisions have currency=CNY
cnys = sum(1 for d in dec.get('decisions', []) if d.get('currency') == 'CNY')
print(f"\ndecisions with CNY: {cnys}/{d}")
# Check no JSON fallback in mo_data (pure DB)
with open('/home/hmo/MoFin/mo_data.py') as f:
content = f.read()
pure_db = 'json.load(open' not in content
print(f"mo_data pure DB: {'YES' if pure_db else 'NO — still has JSON'}")
print(f"\n{'ALL GOOD' if h and d and w else 'FAIL'}")
-13
View File
@@ -2029,19 +2029,6 @@ def regenerate_all(stdout=True):
conn.close()
except Exception as e:
print(f" [DB写入失败] {e}", flush=True)
# JSON 冷备
json.dump(existing_pf, open(PORTFOLIO_PATH, "w"), ensure_ascii=False, indent=2)
json.dump(wl, open(WATCHLIST_PATH, "w"), ensure_ascii=False, indent=2)
# 写 decisions.json
decisions_path = "/home/hmo/web-dashboard/data/decisions.json"
decisions_data = {
"decisions": decisions, # 全部保留
"total": len(decisions),
"regenerated_at": datetime.now().strftime('%Y-%m-%d %H:%M'),
}
json.dump(decisions_data, open(decisions_path, "w"), ensure_ascii=False, indent=2)
# DB 已在上方写入(和 portfolio/watchlist 一起)
# 记录策略→提示词版本关联
if HAS_PROMPT_TRACKING:
-13
View File
@@ -2540,19 +2540,6 @@ def regenerate_all(stdout=True):
conn.close()
except Exception as e:
print(f" [DB写入失败] {e}", flush=True)
# JSON 冷备
json.dump(existing_pf, open(PORTFOLIO_PATH, "w"), ensure_ascii=False, indent=2)
json.dump(wl, open(WATCHLIST_PATH, "w"), ensure_ascii=False, indent=2)
# 写 decisions.json
decisions_path = "/home/hmo/web-dashboard/data/decisions.json"
decisions_data = {
"decisions": decisions, # 全部保留
"total": len(decisions),
"regenerated_at": datetime.now().strftime('%Y-%m-%d %H:%M'),
}
json.dump(decisions_data, open(decisions_path, "w"), ensure_ascii=False, indent=2)
# DB 已在上方写入(和 portfolio/watchlist 一起)
# 记录策略→提示词版本关联
if HAS_PROMPT_TRACKING: