Slim embed tab rendering to cut memory use and restore calculator.

Load only per-tab data for embed fragments, skip exchange capital fetches on tab switches, and harden calculator market imports/timeouts.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-24 01:33:42 +08:00
parent bced61b9d7
commit 384d404bb3
8 changed files with 380 additions and 109 deletions
+63 -27
View File
@@ -6215,20 +6215,41 @@ def render_main_page(page="trade", embed_mode=None):
conn = get_db()
session_row = ensure_session(conn, trading_day)
local_current_capital = float(session_row["current_capital"])
funding_capital, trading_capital = get_exchange_capitals()
from instance_embed_context_lib import (
embed_render_plan,
minimal_stats_bundle,
trade_records_summary,
)
plan = embed_render_plan(page, embed_mode)
if plan.exchange_capitals:
funding_capital, trading_capital = get_exchange_capitals()
else:
funding_capital, trading_capital = None, None
funding_usdt = round(funding_capital, FUNDS_DECIMALS) if funding_capital is not None else None
current_capital = round(trading_capital, FUNDS_DECIMALS) if trading_capital is not None else round(local_current_capital, FUNDS_DECIMALS)
recommended_capital = get_recommended_capital(current_capital)
key_list = conn.execute("SELECT * FROM key_monitors").fetchall()
key_history = conn.execute(
"SELECT * FROM key_monitor_history WHERE closed_at >= ? AND closed_at <= ? ORDER BY id DESC LIMIT 500",
(start_bj, end_bj),
).fetchall()
stats_bundle = compute_stats_bundle(conn, trading_day, now)
raw_order_list = conn.execute("SELECT * FROM order_monitors WHERE status='active'").fetchall()
key_list = (
conn.execute("SELECT * FROM key_monitors").fetchall() if plan.key_list else []
)
key_history = (
conn.execute(
"SELECT * FROM key_monitor_history WHERE closed_at >= ? AND closed_at <= ? ORDER BY id DESC LIMIT 500",
(start_bj, end_bj),
).fetchall()
if plan.key_history
else []
)
stats_bundle = (
compute_stats_bundle(conn, trading_day, now)
if plan.stats_bundle
else minimal_stats_bundle(TRADING_DAY_RESET_HOUR)
)
order_list = []
for o in raw_order_list:
order_list.append(enrich_order_item(row_to_dict(o), current_capital))
if plan.orders:
raw_order_list = conn.execute("SELECT * FROM order_monitors WHERE status='active'").fetchall()
for o in raw_order_list:
order_list.append(enrich_order_item(row_to_dict(o), current_capital))
exchange_pnl_sync = {}
if exchange_private_api_configured() and not request_is_hub_soft_nav() and embed_mode not in (
"fragment",
@@ -6238,22 +6259,37 @@ def render_main_page(page="trade", embed_mode=None):
exchange_pnl_sync = sync_trade_records_from_exchange(conn) or {}
except Exception as e:
exchange_pnl_sync = {"ok": False, "reason": str(e)}
raw_records = conn.execute(
f"SELECT * FROM trade_records WHERE {sql_list_time_field('closed_at', 'created_at', 'opened_at')} >= ? "
f"AND {sql_list_time_field('closed_at', 'created_at', 'opened_at')} <= ? ORDER BY id DESC LIMIT 1000",
(start_bj, end_bj),
).fetchall()
records = [to_effective_trade_dict(r) for r in raw_records]
total = len(records)
miss_count = sum(1 for r in records if (r.get("effective_result") or "") == "错过")
win = sum(1 for r in records if (r.get("effective_result") or "") in ("止盈", "保本止盈", "移动止盈"))
occupied_miss_total = sum(
1
for r in records
if (r.get("effective_result") or "") == "错过"
and ("持仓占用" in str(r.get("effective_miss_reason") or ""))
)
rate = round(win/total*100,2) if total else 0
tr_ts = sql_list_time_field("closed_at", "created_at", "opened_at")
if plan.records_rows:
raw_records = conn.execute(
f"SELECT * FROM trade_records WHERE {tr_ts} >= ? AND {tr_ts} <= ? ORDER BY id DESC LIMIT 1000",
(start_bj, end_bj),
).fetchall()
records = [to_effective_trade_dict(r) for r in raw_records]
total = len(records)
miss_count = sum(1 for r in records if (r.get("effective_result") or "") == "错过")
win = sum(
1
for r in records
if (r.get("effective_result") or "") in ("止盈", "保本止盈", "移动止盈")
)
occupied_miss_total = sum(
1
for r in records
if (r.get("effective_result") or "") == "错过"
and ("持仓占用" in str(r.get("effective_miss_reason") or ""))
)
rate = round(win / total * 100, 2) if total else 0
elif plan.records_summary:
summary = trade_records_summary(conn, start_bj, end_bj, tr_ts)
records = summary["records"]
total = summary["total"]
miss_count = summary["miss_count"]
rate = summary["rate"]
occupied_miss_total = summary["occupied_miss_total"]
else:
records = []
total = miss_count = rate = occupied_miss_total = 0
active_count = len(order_list)
open_guard_enabled = get_trading_day_reset_open_guard_enabled(conn)
open_guard_blocks_now = open_guard_enabled and now.hour < TRADING_DAY_RESET_HOUR
@@ -6284,7 +6320,7 @@ def render_main_page(page="trade", embed_mode=None):
trigger_entry_validity_hours=TRIGGER_ENTRY_VALIDITY_HOURS,
)
strategy_extra = {}
if page in ("strategy", "strategy_trend", "strategy_roll", "strategy_records"):
if plan.strategy:
from strategy_ui import strategy_render_extras
strategy_extra = strategy_render_extras(