fix: 修复统计日历 bootstrap 导致整站 500

日历数据改为安全 JSON 内嵌,仅统计页构建;构建失败时降级为空,避免拖垮其他页面。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-30 09:24:54 +08:00
parent 3b687d17eb
commit 32079bb4c2
12 changed files with 91 additions and 35 deletions
+10 -6
View File
@@ -1857,7 +1857,7 @@ def _compute_period_metrics(trades):
}
def compute_stats_bundle(conn, trading_day, now_dt=None):
def compute_stats_bundle(conn, trading_day, now_dt=None, *, with_calendar=False):
"""日 / 周 / 月 统计:平仓按北京时间交易日(默认 8:00 切日)计入。"""
now_dt = now_dt or app_now()
pnls = _load_completed_trade_pnls(conn)
@@ -1888,11 +1888,14 @@ def compute_stats_bundle(conn, trading_day, now_dt=None):
dm, wm, mm = slice_metrics("all")
from trade_stats_calendar_lib import build_initial_stats_calendar
initial_calendar = None
calendar_bootstrap_json = None
if with_calendar:
from trade_stats_calendar_lib import build_stats_calendar_bootstrap
initial_calendar = build_initial_stats_calendar(
pnls, now_dt, _pnl_row_matches_segment, reset_hour=TRADING_DAY_RESET_HOUR
)
initial_calendar, calendar_bootstrap_json = build_stats_calendar_bootstrap(
pnls, now_dt, _pnl_row_matches_segment, reset_hour=TRADING_DAY_RESET_HOUR
)
return {
"trading_day": trading_day,
@@ -1903,6 +1906,7 @@ def compute_stats_bundle(conn, trading_day, now_dt=None):
"segments": segments,
"stats_reset_hour": TRADING_DAY_RESET_HOUR,
"initial_calendar": initial_calendar,
"calendar_bootstrap_json": calendar_bootstrap_json,
}
@@ -7040,7 +7044,7 @@ def render_main_page(page="trade", embed_mode=None):
else []
)
stats_bundle = (
compute_stats_bundle(conn, trading_day, now)
compute_stats_bundle(conn, trading_day, now, with_calendar=(page == "stats"))
if plan.stats_bundle
else minimal_stats_bundle(TRADING_DAY_RESET_HOUR)
)
+2 -2
View File
@@ -807,8 +807,8 @@
</select>
</label>
</div>
{% if stats_bundle.initial_calendar %}
<script type="application/json" id="stats-calendar-bootstrap">{{ stats_bundle.initial_calendar | tojson }}</script>
{% if stats_bundle.calendar_bootstrap_json %}
<script type="application/json" id="stats-calendar-bootstrap">{{ stats_bundle.calendar_bootstrap_json | safe }}</script>
{% endif %}
<div id="stats-calendar-wrap" class="trade-cal-wrap stats-calendar-wrap">
<div class="trade-cal-head">