From b2db5ed499a2656c54f7642b0672bbdb56b7d02e Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 18 Jun 2026 17:26:00 +0800 Subject: [PATCH] Show runtime metrics and screenshot on dashboard --- gui/dashboard_app/page_home.py | 95 +++++++++++++++++++++++----------- 1 file changed, 64 insertions(+), 31 deletions(-) diff --git a/gui/dashboard_app/page_home.py b/gui/dashboard_app/page_home.py index 975d829..407931d 100644 --- a/gui/dashboard_app/page_home.py +++ b/gui/dashboard_app/page_home.py @@ -1,52 +1,85 @@ from __future__ import annotations +import re import tkinter as tk +import time +from datetime import datetime from . import theme from .data import DashboardData, fmt_ts from .page_context import PageContext from .page_helpers import reservation_rows -from .widgets import flow_grid, section, stat_tile, table_panel, text_panel +from .widgets import flow_grid, preview_panel, section, stat_tile, table_panel -def _current_action_status(data: DashboardData, fallback: str = "暂无动作") -> str: - for raw_line in reversed(data.action_log): - line = str(raw_line).strip() - if not line or set(line) == {"="}: - continue - if "识别" in line: - return "识别中" - if "点击" in line or "参与" in line: - return "点击参与" - if "等待" in line or "开奖" in line: - return "等待开奖" - if "上滑" in line or "切换直播间" in line: - return "切换直播间" - if "预约" in line: - return "记录预约" - if "程序运行结束" in line: - return "已结束" +_RUN_TS_RE = re.compile(r"(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(?:[.,]\d+)?)") + + +def _parse_run_ts(line: str) -> float | None: + match = _RUN_TS_RE.search(line) + if not match: + return None + text = match.group(1).replace(",", ".") + try: + return datetime.strptime(text, "%Y-%m-%d %H:%M:%S.%f").timestamp() + except ValueError: + try: + return datetime.strptime(text, "%Y-%m-%d %H:%M:%S").timestamp() + except ValueError: + return None + + +def _fmt_duration(seconds: float) -> str: + seconds = max(0, int(seconds)) + hours, rem = divmod(seconds, 3600) + minutes, secs = divmod(rem, 60) + if hours: + return f"{hours:02d}:{minutes:02d}:{secs:02d}" + return f"{minutes:02d}:{secs:02d}" + + +def _run_durations(data: DashboardData) -> tuple[str, str]: + starts: list[float] = [] + total = 0.0 + last_duration = 0.0 + running_start: float | None = None + + for raw_line in [*data.run_log, *data.action_log]: + line = str(raw_line) if "程序开始运行" in line: - return "运行中" - marker = " - [ACTION] " - if marker in line: - action = line.split(marker, 1)[1].split("耗时=", 1)[0].strip() - return action[:12] or fallback - return fallback + ts = _parse_run_ts(line) + if ts is not None: + starts.append(ts) + running_start = ts + elif "程序运行结束" in line: + ts = _parse_run_ts(line) + if ts is not None and starts: + start = starts.pop() + duration = max(0.0, ts - start) + total += duration + last_duration = duration + running_start = starts[-1] if starts else None + + if running_start is not None: + current = max(0.0, time.time() - running_start) + total += current + else: + current = last_duration + return _fmt_duration(current), _fmt_duration(total) def build_home(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> None: - section(parent, "数据看板", "运行状态 / 队列 / 动作") - metrics = flow_grid(parent, 2) - fallback_status = ctx.status_var.get() if ctx.status_var is not None else "暂无动作" - status_text = _current_action_status(data, fallback_status or "暂无动作") + section(parent, "数据看板", "运行时间 / 队列 / 截图") + metrics = flow_grid(parent, 3) + current_run, total_run = _run_durations(data) values = [ ("中奖截图", len(data.win_images), "pic/lottery", theme.RED, theme.RED_SOFT), - ("当前状态", status_text or "就绪", "运行状态", theme.TEAL, theme.TEAL_SOFT), + ("本次运行时间", current_run, "当前轮次", theme.TEAL, theme.TEAL_SOFT), + ("总运行时间", total_run, "日志累计", theme.BLUE, theme.BLUE_SOFT), ] for idx, item in enumerate(values): tile = stat_tile(metrics, *item[:3], color=item[3], soft=item[4], compact=True) - tile.grid(row=idx // 2, column=idx % 2, sticky="ew", padx=4, pady=4) + tile.grid(row=idx // 3, column=idx % 3, sticky="ew", padx=4, pady=4) body = tk.Frame(parent, bg=theme.BG) body.pack(fill="both", expand=True) @@ -66,7 +99,7 @@ def build_home(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> None: ("value", "价值", 54), ("status", "状态", 58), ("time", "时间", 92), ), rows, height=18).pack(fill="both", expand=True) - text_panel(right, "详细动作日志", data.action_log[-160:], height=24).pack(fill="both", expand=True) + preview_panel(right, data.screenshot_path, "实时截图").pack(fill="both", expand=True) def build_statistics(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> None: