expand dashboard statistics page

This commit is contained in:
Your Name committed 2026-06-19 20:38:28 +08:00
1 parent 4891f3b019
commit 680c02d362
1 file changed
+100 -3
+100 -3
View File
@@ -9,7 +9,7 @@ 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
from .widgets import flow_grid, mini_table, 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+)?)")
@@ -68,6 +68,48 @@ def _run_durations(data: DashboardData) -> tuple[str, str]:
return _fmt_duration(current), _fmt_duration(total)
def _count_lines(lines: list[str], keyword: str) -> int:
return sum(1 for line in lines if keyword in str(line))
def _latest_ts_from_paths(paths) -> str:
items = list(paths or [])
if not items:
return "-"
try:
return fmt_ts(max(p.stat().st_mtime for p in items if p.exists()))
except Exception:
return "-"
def _latest_log_time(data: DashboardData) -> str:
for line in reversed([*data.run_log, *data.action_log]):
ts = _parse_run_ts(str(line))
if ts is not None:
return fmt_ts(ts)
return "-"
def _high_value_rows(data: DashboardData) -> list[tuple[str, str, str]]:
rows: list[tuple[str, str, str]] = []
for item in data.high_value_rooms[:8]:
title = str(item.get("room_title") or item.get("title") or "-")
value = str(item.get("prize_value_yuan") or item.get("value") or "-")
content = str(item.get("content") or item.get("prize_content") or "-")
rows.append((title[:36], value, content[:18]))
return rows
def _mapping_rows(data: DashboardData) -> list[tuple[str, str, str]]:
rows: list[tuple[str, str, str]] = []
for item in data.mapping_rows[:8]:
pos = str(item.get("pos") or "-")
title = str(item.get("title") or "-")
state = "有福袋" if item.get("has_fudai") == "是" else str(item.get("updated") or "-")
rows.append((f"位置 {pos} {title[:28]}", str(item.get("value") or "-"), state[:16]))
return rows
def build_home(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> None:
section(parent, "数据看板", "运行时间 / 队列 / 截图")
metrics = flow_grid(parent, 3)
@@ -103,5 +145,60 @@ def build_home(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> None:
def build_statistics(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> None:
section(parent, "统计信息", "本地运行统计看板")
build_home(parent, data, ctx)
section(parent, "统计信息", "运行概览 / 数据资产 / 最近记录")
current_run, total_run = _run_durations(data)
metrics = flow_grid(parent, 4)
values = [
("本次运行", current_run, "当前轮次", theme.TEAL, theme.TEAL_SOFT),
("总运行", total_run, "日志累计", theme.BLUE, theme.BLUE_SOFT),
("运行日志", len(data.run_log), "最近读取", theme.GREEN, theme.GREEN_SOFT),
("行为日志", len(data.action_log), "关键动作", theme.AMBER, theme.AMBER_SOFT),
("中奖截图", len(data.win_images), "pic/lottery", theme.RED, theme.RED_SOFT),
("映射条目", len(data.mapping_rows), "room_pos", theme.TEAL, theme.TEAL_SOFT),
("高价值记录", len(data.high_value_rooms), "持久化", theme.BLUE, theme.BLUE_SOFT),
("OCR文件", len(data.ocr_files), "识别结果", theme.GREEN, theme.GREEN_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 // 4, column=idx % 4, sticky="ew", padx=4, pady=4)
body = tk.Frame(parent, bg=theme.BG)
body.pack(fill="both", expand=True)
body.grid_columnconfigure(0, weight=1)
body.grid_columnconfigure(1, weight=1)
body.grid_rowconfigure(1, weight=1)
run_rows = [
("开始次数", str(_count_lines(data.run_log + data.action_log, "程序开始运行")), "日志"),
("结束次数", str(_count_lines(data.run_log + data.action_log, "程序运行结束")), "日志"),
("最近日志", _latest_log_time(data), "时间"),
("实时截图", "存在" if data.current_screenshot_path else "缺失", "pic"),
("运行日志行", str(len(data.run_log)), "log"),
("行为日志行", str(len(data.action_log)), "log"),
]
asset_rows = [
("最新中奖截图", _latest_ts_from_paths(data.win_images), "时间"),
("最新OCR文件", _latest_ts_from_paths(data.ocr_files), "时间"),
("当前截图", fmt_ts(data.current_screenshot_path.stat().st_mtime) if data.current_screenshot_path else "-", "时间"),
("配置项数量", str(len(data.cfg)), "config"),
("动作关键行", str(_count_lines(data.action_log, "[ACTION]") + _count_lines(data.action_log, "[操作]")), "actions"),
("运行关键行", str(_count_lines(data.run_log, "运行") + _count_lines(data.action_log, "[运行]")), "runtime"),
]
left = tk.Frame(body, bg=theme.BG)
right = tk.Frame(body, bg=theme.BG)
left.grid(row=0, column=0, sticky="nsew", padx=(0, 8), pady=(0, 12))
right.grid(row=0, column=1, sticky="nsew", padx=(8, 0), pady=(0, 12))
mini_table(left, "运行概览", run_rows, color=theme.TEAL).pack(fill="both", expand=True)
mini_table(right, "数据资产", asset_rows, color=theme.BLUE).pack(fill="both", expand=True)
lower = tk.Frame(body, bg=theme.BG)
lower.grid(row=1, column=0, columnspan=2, sticky="nsew")
lower.grid_columnconfigure(0, weight=1)
lower.grid_columnconfigure(1, weight=1)
high = tk.Frame(lower, bg=theme.BG)
mapping = tk.Frame(lower, bg=theme.BG)
high.grid(row=0, column=0, sticky="nsew", padx=(0, 8))
mapping.grid(row=0, column=1, sticky="nsew", padx=(8, 0))
mini_table(high, "最近高价值记录", _high_value_rows(data), color=theme.RED).pack(fill="both", expand=True)
mini_table(mapping, "最近映射记录", _mapping_rows(data), color=theme.GREEN).pack(fill="both", expand=True)