fix: persist regime to DB in shared _build_state, deduplicate save logic

- _build_market_state (CLI) now saves regime_history automatically
- _build_state (web) now saves regime_history automatically
- Remove duplicate regime save from cmd_score
- Remove unused imports (timedelta, get_connection)
- Fix: web dashboard never updated regime_history table
This commit is contained in:
jackyu66git
2026-06-24 18:31:00 +08:00
parent 7813e319b4
commit efb721b39f
2 changed files with 40 additions and 24 deletions
+20 -2
View File
@@ -6,7 +6,8 @@ import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from datetime import date as Date, timedelta
import json
from datetime import date as Date
from flask import Flask, render_template, jsonify, request
from database import get_connection
@@ -23,7 +24,7 @@ app = Flask(__name__)
def _build_state(target: Date):
"""Shared: build MarketStateVector for a date."""
"""Build MarketStateVector and persist regime to DB."""
ps = PriceStructureScorer().compute(target)
br = BreadthScorer().compute(target)
oi = OIMatrixScorer().compute(target)
@@ -44,6 +45,23 @@ def _build_state(target: Date):
oi_matrix_score=oi, volatility_regime_score=vol,
)
state.market_state_hash = state.compute_hash()
# Persist regime to DB so load_state() works across requests
conn = get_connection()
conn.execute("""
INSERT OR REPLACE INTO regime_history
(date, regime, confidence, regime_version, maturity_score, all_scores_json,
prior_regime, confirmation_days)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
""", (
str(target), r.regime.value, r.confidence, r.regime_version,
r.maturity_score, json.dumps(r.all_scores),
r.prior_regime.value if r.prior_regime else None,
r.confirmation_days,
))
conn.commit()
conn.close()
return state