数据统计改为后台热缓存与 SSE,曲线放大置底并支持选机,对比增加模式与收益率。
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+21
-15
@@ -36,14 +36,20 @@ def normalize_window(raw: Any) -> str:
|
||||
return w if w in ("today", "7d", "30d", "all") else "7d"
|
||||
|
||||
|
||||
def build_stats_summary(window: str = "7d") -> dict[str, Any]:
|
||||
def build_stats_summary(
|
||||
window: str = "7d",
|
||||
*,
|
||||
snaps: list[dict[str, Any]] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
w = normalize_window(window)
|
||||
# 实时状态(余额/权利金/浮盈)来自快照;窗口 KPI 来自各机 stats
|
||||
snaps = pull_all_enabled()
|
||||
try:
|
||||
record_cash_samples_from_overview(snaps)
|
||||
except Exception:
|
||||
pass
|
||||
# snaps 可由热缓存传入,避免统计页再现场重复拉全机
|
||||
if snaps is None:
|
||||
snaps = pull_all_enabled()
|
||||
try:
|
||||
record_cash_samples_from_overview(snaps)
|
||||
except Exception:
|
||||
pass
|
||||
stats_rows = pull_all_stats(w)
|
||||
stats_by_id = {r.get("id"): r for r in stats_rows}
|
||||
|
||||
@@ -83,6 +89,12 @@ def build_stats_summary(window: str = "7d") -> dict[str, Any]:
|
||||
win_rate = _safe_float(stats.get("win_rate"))
|
||||
avg_hold = _safe_float(stats.get("avg_hold_sec"))
|
||||
online = bool(snap.get("online"))
|
||||
mode = ((body.get("instance") or {}) if isinstance(body.get("instance"), dict) else {}).get(
|
||||
"mode"
|
||||
)
|
||||
return_pct = None
|
||||
if online and stats.get("ok") and net is not None and cash is not None and cash > 0:
|
||||
return_pct = round(float(net) / float(cash) * 100.0, 2)
|
||||
if online:
|
||||
online_n += 1
|
||||
if cash is not None:
|
||||
@@ -110,9 +122,7 @@ def build_stats_summary(window: str = "7d") -> dict[str, Any]:
|
||||
"name": snap.get("name"),
|
||||
"online": online,
|
||||
"error": snap.get("error") or st.get("error") or "",
|
||||
"mode": ((body.get("instance") or {}) if isinstance(body.get("instance"), dict) else {}).get(
|
||||
"mode"
|
||||
),
|
||||
"mode": mode,
|
||||
"cash_usdt": cash,
|
||||
"premium_usdt": paid,
|
||||
"ratio_pct": ratio,
|
||||
@@ -123,6 +133,7 @@ def build_stats_summary(window: str = "7d") -> dict[str, Any]:
|
||||
"loss_count": losses if online and stats.get("ok") else None,
|
||||
"win_rate": win_rate if online and stats.get("ok") else None,
|
||||
"net_realized_pnl": net if online and stats.get("ok") else None,
|
||||
"return_rate_pct": return_pct,
|
||||
"total_profit": profit if online and stats.get("ok") else None,
|
||||
"total_loss": loss if online and stats.get("ok") else None,
|
||||
"avg_hold_sec": avg_hold if online and stats.get("ok") else None,
|
||||
@@ -153,12 +164,7 @@ def build_stats_summary(window: str = "7d") -> dict[str, Any]:
|
||||
|
||||
|
||||
def build_stats_curve(range_key: str = "7d") -> dict[str, Any]:
|
||||
# 顺手采一笔,让打开页面时也有点
|
||||
try:
|
||||
snaps = pull_all_enabled()
|
||||
record_cash_samples_from_overview(snaps)
|
||||
except Exception:
|
||||
pass
|
||||
# 曲线只读本地采样库;余额采样由总览常驻 pull 负责
|
||||
data = query_cash_series(range_key=range_key)
|
||||
data["ok"] = True
|
||||
return data
|
||||
|
||||
Reference in New Issue
Block a user