Files
MoFin/scripts/test_dual_write.py
T
hmo efdfaf956a fix(price_events): unify event storage to DB (dual-write + backfill)
User caught the inconsistency: system claims DB-first but price events
only went to price_events.json, leaving DB table stale since Jul 6.

Root cause chain found:
- record_event() only wrote JSON, never called mofin_db.write_price_event
- price_events.code has FK -> stocks(code); events for unregistered stocks
  (new candidates, HK) silently failed INSERT and were lost to DB
- mofin_db.write_price_event swallows errors (returns False silently)

Fixes:
- record_event now dual-writes: DB (authoritative) + JSON (compat for
  legacy readers mo_config/strategy_feedback/system_health_check)
- auto-registers unknown codes into stocks table before event insert
- one-time backfill: 4064 JSON events -> DB (total 6353 rows, last=today)
- verified: record_event TEST99 lands in both DB and JSON
2026-07-20 17:46:57 +08:00

23 lines
914 B
Python

import sys
sys.path.insert(0, '/home/hmo/.hermes/profiles/position-analyst/scripts')
import price_monitor as pm
pm.record_event('TEST99', '测试股', 'entry_zone', 12.34, '12.0~12.5', '加仓区间')
print('record_event OK')
import sqlite3
c = sqlite3.connect('/home/hmo/MoFin/data/mofin.db')
r = c.execute("SELECT code,name,event_type,price,created_at FROM price_events WHERE code='TEST99' ORDER BY id DESC LIMIT 1").fetchone()
print('DB row:', r)
c.execute("DELETE FROM price_events WHERE code='TEST99'")
c.commit()
print('cleaned')
# also check JSON got it
import json
d = json.load(open('/home/hmo/web-dashboard/data/price_events.json'))
last = d['events'][-1]
print('JSON last:', last['code'], last['price'])
if last['code'] == 'TEST99':
d['events'] = d['events'][:-1]
json.dump(d, open('/home/hmo/web-dashboard/data/price_events.json', 'w'), ensure_ascii=False, indent=2)
print('JSON cleaned')