diff --git a/gui/dashboard_app/page_home.py b/gui/dashboard_app/page_home.py index c70b6ba..7103eff 100644 --- a/gui/dashboard_app/page_home.py +++ b/gui/dashboard_app/page_home.py @@ -11,7 +11,7 @@ from . import theme from .data import DashboardData, fmt_ts, load_dashboard_data from .page_context import PageContext from .page_helpers import reservation_rows -from .stats_view import build_stats_dashboard +from .stats_view import StatsDashboardView from .widgets import flow_grid, preview_panel, section, stat_tile, table_panel @@ -213,38 +213,37 @@ def build_home(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> None: preview_panel(right, data.current_screenshot_path, "实时截图").pack(fill="both", expand=True) -def _render_statistics_dashboard(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> None: - for child in list(parent.winfo_children()): - child.destroy() +def _statistics_values(data: DashboardData, ctx: PageContext) -> tuple[dict[str, Any], str, str]: stats = _runtime_stats(data, ctx) current_run = str(stats.get("dashboard_current_run") or "-") total_run = str(stats.get("dashboard_total_run") or "-") if stats.get("uptime_seconds"): current_run = _fmt_duration(float(stats.get("uptime_seconds") or 0)) - build_stats_dashboard(parent, stats, current_run, total_run) + return stats, current_run, total_run -def _schedule_statistics_refresh(widget: tk.Misc, ctx: PageContext, interval_ms: int = 2000) -> None: +def _schedule_statistics_refresh(view: StatsDashboardView, ctx: PageContext, interval_ms: int = 2000) -> None: def tick() -> None: try: - if not widget.winfo_exists(): + if not view.root.winfo_exists(): return except Exception: return try: - _render_statistics_dashboard(widget, load_dashboard_data(), ctx) + stats, current_run, total_run = _statistics_values(load_dashboard_data(), ctx) + view.update(stats, current_run, total_run) finally: - _schedule_statistics_refresh(widget, ctx, interval_ms) + _schedule_statistics_refresh(view, ctx, interval_ms) try: - widget.after(interval_ms, tick) + view.root.after(interval_ms, tick) except Exception: pass def build_statistics(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> None: section(parent, "统计信息", "实时运行指标 / 旧GUI同源统计") - dashboard = tk.Frame(parent, bg=theme.BG) - dashboard.pack(fill="both", expand=True) - _render_statistics_dashboard(dashboard, data, ctx) - _schedule_statistics_refresh(dashboard, ctx) + view = StatsDashboardView(parent) + stats, current_run, total_run = _statistics_values(data, ctx) + view.update(stats, current_run, total_run) + _schedule_statistics_refresh(view, ctx) diff --git a/gui/dashboard_app/stats_view.py b/gui/dashboard_app/stats_view.py index 477408b..c3c1fd5 100644 --- a/gui/dashboard_app/stats_view.py +++ b/gui/dashboard_app/stats_view.py @@ -97,6 +97,220 @@ def _bar_row(parent: tk.Misc, label: str, value: int, total: int, color: str = t fill.place(relx=0, rely=0, relwidth=pct, relheight=1) +class StatsDashboardView: + def __init__(self, parent: tk.Misc) -> None: + self.root = tk.Frame(parent, bg=theme.BG) + self.root.pack(fill="both", expand=True) + self.vars: dict[str, tk.StringVar] = {} + self.bars: dict[str, tuple[tk.StringVar, tk.Frame]] = {} + self._build() + + def _var(self, key: str, value: str = "-") -> tk.StringVar: + var = tk.StringVar(value=value) + self.vars[key] = var + return var + + def _kpi_tile(self, parent: tk.Misc, key: str, title: str, color: str, soft: str) -> tk.Frame: + box = rounded_surface(parent, fill=theme.SURFACE, outline=theme.BORDER, radius=theme.RADIUS_MD) + box.configure(height=104) + box.pack_propagate(False) + tk.Frame(box, bg=color, width=4).pack(side="left", fill="y") + inner = tk.Frame(box, bg=theme.SURFACE) + inner.pack(side="left", fill="both", expand=True, padx=14, pady=10) + head = tk.Frame(inner, bg=theme.SURFACE) + head.pack(fill="x") + tk.Label(head, text=title, bg=theme.SURFACE, fg=theme.MUTED, font=theme.FONT_SMALL).pack(side="left") + tk.Label(head, text="●", bg=theme.SURFACE, fg=color, font=("Segoe UI", 9, "bold")).pack(side="right") + tk.Label(inner, textvariable=self._var(f"{key}.value"), bg=theme.SURFACE, fg=theme.TEXT, font=("Microsoft YaHei UI", 18, "bold")).pack(anchor="w", pady=(4, 0)) + tk.Label(inner, textvariable=self._var(f"{key}.hint"), bg=soft, fg=color, font=("Microsoft YaHei UI", 10, "bold"), padx=9, pady=2).pack(anchor="w", pady=(5, 0)) + return box + + def _metric_row(self, parent: tk.Misc, key: str, label: str, color: str = theme.TEAL) -> None: + row = tk.Frame(parent, bg=theme.SURFACE) + row.pack(fill="x", pady=4) + tk.Label(row, text=label, bg=theme.SURFACE, fg=theme.MUTED, font=theme.FONT_UI).pack(side="left") + tk.Label(row, textvariable=self._var(f"{key}.value"), bg=theme.SURFACE, fg=color, font=("Microsoft YaHei UI", 15, "bold")).pack(side="right") + tk.Label(parent, textvariable=self._var(f"{key}.hint"), bg=theme.SURFACE, fg=theme.SUBTLE, font=theme.FONT_SMALL).pack(anchor="w", pady=(0, 4)) + + def _bar_row(self, parent: tk.Misc, key: str, label: str, color: str = theme.TEAL) -> None: + row = tk.Frame(parent, bg=theme.SURFACE) + row.pack(fill="x", pady=5) + tk.Label(row, text=label, bg=theme.SURFACE, fg=theme.TEXT, font=theme.FONT_UI).pack(side="left") + value_var = self._var(f"{key}.value") + tk.Label(row, textvariable=value_var, bg=theme.SURFACE, fg=theme.MUTED, font=theme.FONT_SMALL).pack(side="right") + track = tk.Frame(parent, bg=theme.BG_ALT, height=8) + track.pack(fill="x", pady=(0, 4)) + fill = tk.Frame(track, bg=color, height=8) + fill.place(relx=0, rely=0, relwidth=0, relheight=1) + self.bars[key] = (value_var, fill) + + def _group(self, parent: tk.Misc, title: str) -> tk.Frame: + box = rounded_surface(parent, fill=theme.SURFACE, outline=theme.BORDER, radius=theme.RADIUS_MD) + box.pack(fill="both", expand=True) + head = tk.Frame(box, bg=theme.SURFACE) + head.pack(fill="x", padx=16, pady=(14, 8)) + tk.Frame(head, bg=theme.TEAL, width=4, height=18).pack(side="left", padx=(0, 10)) + tk.Label(head, text=title, bg=theme.SURFACE, fg=theme.TEXT, font=theme.FONT_H2).pack(side="left") + body = tk.Frame(box, bg=theme.SURFACE) + body.pack(fill="both", expand=True, padx=16, pady=(0, 14)) + return body + + def _build(self) -> None: + grid = flow_grid(self.root, 4) + kpis = [ + ("status", "运行状态", theme.TEAL, theme.TEAL_SOFT), + ("current_run", "本次运行", theme.BLUE, theme.BLUE_SOFT), + ("attend", "参与成功", theme.GREEN, theme.GREEN_SOFT), + ("wins", "中奖次数", theme.RED, theme.RED_SOFT), + ("seen", "识别福袋", theme.TEAL, theme.TEAL_SOFT), + ("open", "打开详情", theme.BLUE, theme.BLUE_SOFT), + ("switch", "切换直播间", theme.AMBER, theme.AMBER_SOFT), + ("total_run", "总运行", theme.BLUE, theme.BLUE_SOFT), + ] + for idx, (key, title, color, soft) in enumerate(kpis): + self._kpi_tile(grid, key, title, color, soft).grid(row=idx // 4, column=idx % 4, sticky="ew", padx=4, pady=4) + + panels = tk.Frame(self.root, bg=theme.BG) + panels.pack(fill="both", expand=True) + for col in range(2): + panels.grid_columnconfigure(col, weight=1, uniform="stats") + for row in range(3): + panels.grid_rowconfigure(row, weight=1) + + slots = [] + for idx in range(6): + slot = tk.Frame(panels, bg=theme.BG) + slot.grid(row=idx // 2, column=idx % 2, sticky="nsew", padx=(0 if idx % 2 == 0 else 8, 8 if idx % 2 == 0 else 0), pady=6) + slots.append(slot) + + core = self._group(slots[0], "核心流程") + for key, label, color in [ + ("bar.seen", "识别福袋", theme.TEAL), + ("bar.open", "打开详情", theme.BLUE), + ("bar.click", "点击参与", theme.AMBER), + ("bar.attend", "参与成功", theme.GREEN), + ]: + self._bar_row(core, key, label, color) + self._metric_row(core, "win_rate", "中奖率", theme.RED) + + efficiency = self._group(slots[1], "效率与动作") + self._metric_row(efficiency, "seen_rate", "识别效率", theme.TEAL) + self._metric_row(efficiency, "attend_rate", "参与效率", theme.GREEN) + self._metric_row(efficiency, "switch_rate", "切换频率", theme.AMBER) + self._metric_row(efficiency, "ocr_rate", "OCR成功率", theme.BLUE) + + skip = self._group(slots[2], "过滤与跳过") + for key, label, color in [ + ("skip.value_low", "价值过低", theme.AMBER), + ("skip.participants_low", "人数过少", theme.BLUE), + ("skip.participants_high", "人数过多", theme.BLUE), + ("skip.content", "内容过滤", theme.TEAL), + ("skip.countdown_short", "倒计时太短", theme.RED), + ("skip.countdown_long", "倒计时太长", theme.AMBER), + ]: + self._bar_row(skip, key, label, color) + + reservation = self._group(slots[3], "预约与回访") + self._metric_row(reservation, "reservation_recorded", "记录预约", theme.TEAL) + self._metric_row(reservation, "reservation_back", "回退成功", theme.GREEN) + self._metric_row(reservation, "reservation_verify", "回退校验", theme.BLUE) + self._metric_row(reservation, "reservation_high", "高价值预约", theme.AMBER) + + health = self._group(slots[4], "运行健康") + self._metric_row(health, "error_total", "总错误数", theme.RED) + self._metric_row(health, "vision_errors", "识别/截图异常", theme.RED) + self._metric_row(health, "detail_errors", "详情异常", theme.RED) + self._metric_row(health, "slow_operations", "慢操作", theme.AMBER) + + latest = self._group(slots[5], "最近福袋") + self._metric_row(latest, "last_content", "内容", theme.TEXT) + self._metric_row(latest, "last_countdown", "倒计时", theme.BLUE) + self._metric_row(latest, "last_value", "价值", theme.AMBER) + self._metric_row(latest, "last_people", "人数/份数", theme.TEAL) + + def _set(self, key: str, value: object, hint: object | None = None) -> None: + if f"{key}.value" in self.vars: + self.vars[f"{key}.value"].set(str(value)) + if hint is not None and f"{key}.hint" in self.vars: + self.vars[f"{key}.hint"].set(str(hint)) + + def _set_bar(self, key: str, value: int, total: int) -> None: + if key not in self.bars: + return + value_var, fill = self.bars[key] + value_var.set(f"{value} {_fmt_rate(value, total)}") + pct = 0.0 if total <= 0 else min(1.0, max(0.0, value / total)) + fill.place_configure(relwidth=pct) + + def update(self, stats: dict[str, Any], current_run: str, total_run: str) -> None: + uptime = int(stats.get("uptime_seconds") or 0) + if uptime > 0: + current_run = _fmt_duration(uptime) + seen = _counter(stats, "fudai_seen_unique") + open_detail = _counter(stats, "fudai_open_detail_unique") + click = _counter(stats, "fudai_click_attend_unique") + attend = _counter(stats, "fudai_attend_ok_unique") + win = _counter(stats, "fudai_result_win") + no_win = _counter(stats, "fudai_result_no_win") + switch = _counter(stats, "switch_room") + + self._set("status", "运行中" if stats.get("running") else "未运行", f"日志 {_last_log_time(stats)}") + self._set("current_run", current_run, "当前轮次") + self._set("attend", attend, "去重口径") + self._set("wins", win, "开奖结果") + self._set("seen", seen, "唯一口径") + self._set("open", open_detail, "详情页") + self._set("switch", switch, "运行动作") + self._set("total_run", total_run, "日志累计") + + self._set_bar("bar.seen", seen, max(seen, 1)) + self._set_bar("bar.open", open_detail, max(seen, 1)) + self._set_bar("bar.click", click, max(open_detail, 1)) + self._set_bar("bar.attend", attend, max(click, attend, 1)) + self._set("win_rate", _fmt_rate(win, win + no_win), f"中奖 {win} / 未中 {no_win}") + + self._set("seen_rate", _per_hour(seen, uptime), "每小时识别福袋") + self._set("attend_rate", _per_hour(attend, uptime), "每小时成功参与") + self._set("switch_rate", _per_hour(switch, uptime), "每小时切换直播间") + ocr_attempt = _counter(stats, "ocr_attempt") + self._set("ocr_rate", _fmt_rate(_counter(stats, "ocr_success"), ocr_attempt), f"尝试 {ocr_attempt}") + + skips = [ + ("skip.value_low", "fudai_skip_value_low"), + ("skip.participants_low", "fudai_skip_participants_low"), + ("skip.participants_high", "fudai_skip_participants_high"), + ("skip.content", "fudai_skip_content"), + ("skip.countdown_short", "fudai_skip_countdown_short"), + ("skip.countdown_long", "fudai_skip_countdown_long"), + ] + skip_total = sum(_counter(stats, key) for _, key in skips) + for bar_key, counter_key in skips: + self._set_bar(bar_key, _counter(stats, counter_key), max(skip_total, 1)) + + recorded = _counter(stats, "reservation_recorded") + back_success = _counter(stats, "reservation_back_success") + verify_ok = _counter(stats, "reservation_back_verify_ok") + verify_fail = _counter(stats, "reservation_back_verify_fail") + self._set("reservation_recorded", recorded, "累计写入预约队列") + self._set("reservation_back", back_success, f"成功率 {_fmt_rate(back_success, recorded)}") + self._set("reservation_verify", _fmt_rate(verify_ok, verify_ok + verify_fail), f"成功 {verify_ok} / 失败 {verify_fail}") + self._set("reservation_high", _counter(stats, "fudai_reserved_high_value"), "倒计时长但价值高") + + total_errors = _counter(stats, "error_total") + detail_errors = _counter(stats, "error_read_detail") + _counter(stats, "fudai_open_error") + vision_errors = _counter(stats, "error_ocr") + _counter(stats, "error_screenshot") + _counter(stats, "error_detection") + self._set("error_total", total_errors, "运行日志异常计数") + self._set("vision_errors", vision_errors, "OCR、截图、识别") + self._set("detail_errors", detail_errors, "读详情或打开失败") + self._set("slow_operations", _counter(stats, "slow_operation"), ">5秒操作") + + last = stats.get("fudai_last", {}) or {} + self._set("last_content", last.get("content") or "-", "") + self._set("last_countdown", last.get("countdown") or "-", "") + self._set("last_value", last.get("prize_value_yuan") or "-", "元") + self._set("last_people", f"{last.get('participants') or '-'} / {last.get('copies') or '-'}", "") + + def _build_top_cards(parent: tk.Misc, stats: dict[str, Any], current_run: str, total_run: str) -> None: uptime = int(stats.get("uptime_seconds") or 0) if uptime > 0: