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 -22
View File
@@ -49,6 +49,24 @@ def _build_market_state(target: Date) -> tuple:
oi_matrix_score=oi, volatility_regime_score=vol,
)
state.market_state_hash = state.compute_hash()
# Persist regime to DB so subsequent calls have correct state
from database import get_connection
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, r
@@ -93,13 +111,13 @@ def cmd_fetch(args):
def cmd_score(args):
"""Compute all factor scores and regime for a date."""
from database import init_db, get_connection
from database import init_db
target = parse_date(args.date) if args.date else Date.today()
init_db()
logger.info(f"Computing scores for {target}...")
state, regime_result = _build_market_state(target)
state, _ = _build_market_state(target)
# Output
ps = state.price_structure_score
@@ -128,26 +146,6 @@ def cmd_score(args):
print(f" Market State Hash: {state.market_state_hash}")
print()
# Store regime to DB
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),
state.regime.value,
state.regime_confidence,
state.regime_version,
state.regime_maturity_score,
json.dumps(regime_result.all_scores),
regime_result.prior_regime.value if regime_result.prior_regime else None,
regime_result.confirmation_days,
))
conn.commit()
conn.close()
return state
+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