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, preview_panel, section, stat_tile, table_panel _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: 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, 3) current_run, total_run = _run_durations(data) values = [ ("中奖截图", len(data.win_images), "pic/lottery", theme.RED, theme.RED_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 // 3, column=idx % 3, sticky="ew", padx=4, pady=4) body = tk.Frame(parent, bg=theme.BG) body.pack(fill="both", expand=True) body.grid_columnconfigure(0, weight=3, minsize=540) body.grid_columnconfigure(1, weight=2, minsize=380) left = tk.Frame(body, bg=theme.BG) right = tk.Frame(body, bg=theme.BG) left.grid(row=0, column=0, sticky="nsew", padx=(0, 10)) right.grid(row=0, column=1, sticky="nsew", padx=(10, 0)) rows = reservation_rows(data) for row in rows: row["time"] = fmt_ts(row["time"]) table_panel(left, "预约队列", ( ("idx", "#", 32), ("title", "直播间", 108), ("content", "内容", 168), ("value", "价值", 54), ("status", "状态", 58), ("time", "时间", 92), ), rows, height=18).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: section(parent, "统计信息", "本地运行统计看板") build_home(parent, data, ctx)