refactor: 消费者切 SQLite 优先读取
切换策略: SQLite 优先 → 失败回退 JSON price_events (100%覆盖): - strategy_feedback.py: run() 优先 query_price_events() - system_health_check.py: 优先 query_price_events() + query_price_events_by_date() stock_sector_map (100%覆盖): - strategy_lifecycle.py: load_stock_sector_map() 优先 stock_sectors 表 market.json (85%覆盖): - strategy_lifecycle.py: load_market_context() 优先 query_latest_market() - market_insight.py: generate() 优先 query_latest_market() portfolio.json + watchlist.json (70%覆盖): - strategy_lifecycle.py: regenerate_all() 优先 query_holdings() + query_watchlist() - server.py: /api/portfolio, /api/watchlist, /api/overview, /api/market 优先 SQLite 所有改动保留 JSON 回退路径,SQLite 不可用时自动降级
This commit is contained in:
+32
-11
@@ -55,17 +55,38 @@ def load_holding_industry_map():
|
||||
|
||||
|
||||
def generate():
|
||||
# 加载数据
|
||||
market_path = DATA_DIR / "market.json"
|
||||
with open(market_path, "r", encoding="utf-8") as f:
|
||||
market = json.load(f)
|
||||
|
||||
sectors = market.get("sectors", [])
|
||||
top_gainers = market.get("top_gainers", [])
|
||||
top_losers = market.get("top_losers", [])
|
||||
mood = market.get("mood", "unknown")
|
||||
up_ratio = market.get("up_ratio", 0)
|
||||
timestamp = market.get("timestamp", "")
|
||||
# 优先从 SQLite 读取市场数据
|
||||
try:
|
||||
from mofin_db import get_conn, query_latest_market
|
||||
conn = get_conn()
|
||||
market = query_latest_market(conn)
|
||||
conn.close()
|
||||
if market and market.get("sectors"):
|
||||
sectors = market["sectors"]
|
||||
top_gainers = market.get("top_gainers", [])
|
||||
top_losers = market.get("top_losers", [])
|
||||
mood = market.get("mood", "unknown")
|
||||
up_ratio = market.get("up_ratio", 0)
|
||||
timestamp = market.get("timestamp", "")
|
||||
# 字段名适配
|
||||
for s in sectors:
|
||||
s["change"] = s.get("change_pct", 0)
|
||||
for g in top_gainers:
|
||||
g["change"] = g.get("change_pct", 0)
|
||||
for l in top_losers:
|
||||
l["change"] = l.get("change_pct", 0)
|
||||
else:
|
||||
raise Exception("no data")
|
||||
except Exception:
|
||||
market_path = DATA_DIR / "market.json"
|
||||
with open(market_path, "r", encoding="utf-8") as f:
|
||||
market = json.load(f)
|
||||
sectors = market.get("sectors", [])
|
||||
top_gainers = market.get("top_gainers", [])
|
||||
top_losers = market.get("top_losers", [])
|
||||
mood = market.get("mood", "unknown")
|
||||
up_ratio = market.get("up_ratio", 0)
|
||||
timestamp = market.get("timestamp", "")
|
||||
|
||||
industry_holdings = load_holding_industry_map()
|
||||
insights = []
|
||||
|
||||
Reference in New Issue
Block a user