diff --git a/gui/dashboard_app/page_data.py b/gui/dashboard_app/page_data.py index 7552699..2d10bc8 100644 --- a/gui/dashboard_app/page_data.py +++ b/gui/dashboard_app/page_data.py @@ -1,6 +1,8 @@ from __future__ import annotations +import re import tkinter as tk +from tkinter import ttk from . import theme from .data import DashboardData, fmt_ts @@ -8,17 +10,97 @@ from .page_context import PageContext from .page_helpers import reservation_rows from .widgets import flow_grid, log_panel, preview_panel, section, stat_tile, table_panel, text_panel, toolbar +_ACTION_EMPTY_RE = re.compile(r"^\d{4}-\d{2}-\d{2} .* - \[ACTION\] (行为|参与|运行)\s*$") +_ACTION_TIME_PREFIX_RE = re.compile(r"^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(?:[,.]\d+)?) - ") +_BRACKET_TIME_RE = re.compile(r"^\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(?:[,.]\d+)?)\]\s*") +_ACTION_KEYWORDS = ( + "[手动操作]", + "[操作]", + "[参与]", + "[中奖]", + "[运行]", + "[预约]", + "[重访]", + "[养号]", + "[返回]", + "[截图]", + "[ACTION]", + "上滑切换直播间", + "下滑回退直播间", + "发送返回键", + "点击参与", + "识别福袋详情完成", + "程序开始运行", + "程序运行结束", +) -def _set_log_text(text: tk.Text, lines: list[str]) -> None: + +def _clean_action_line(line: str) -> str | None: + text = str(line or "").strip().lstrip("\ufeff") + if not text or set(text) == {"="}: + return None + if _ACTION_EMPTY_RE.match(text): + return None + text = _ACTION_TIME_PREFIX_RE.sub("", text, count=1) + text = _BRACKET_TIME_RE.sub("", text, count=1) + text = text.replace("[ACTION] ", "").strip() + if text in {"行为", "参与", "运行"}: + return None + return text[:180] + + +def _is_action_line(line: str) -> bool: + text = str(line or "") + return any(keyword in text for keyword in _ACTION_KEYWORDS) + + +def _runner_action_lines(ctx: PageContext) -> list[str]: + runner = ctx.runner + if runner is None: + return [] try: - at_bottom = text.yview()[1] >= 0.98 + with runner._lock: + lines = list(runner._log[-800:]) except Exception: + return [] + return [line for line in lines if _is_action_line(line)] + + +def _compact_action_lines(file_lines: list[str], runner_lines: list[str]) -> list[str]: + merged: list[str] = [] + seen_recent: list[str] = [] + for raw in [*file_lines, *runner_lines]: + line = _clean_action_line(raw) + if not line: + continue + if seen_recent and line == seen_recent[-1]: + continue + if line in seen_recent[-12:]: + continue + merged.append(line) + seen_recent.append(line) + if len(seen_recent) > 24: + seen_recent = seen_recent[-24:] + return merged[-700:] + + +def _set_log_text(text: tk.Text, lines: list[str], *, scroll_mode: str = "follow") -> None: + try: + before = text.yview() + top_pos = before[0] + at_bottom = before[1] >= 0.98 + except Exception: + top_pos = 0.0 at_bottom = True text.configure(state="normal") text.delete("1.0", "end") text.insert("1.0", "\n".join(lines[-1000:]) or "暂无数据") text.configure(state="disabled") - if at_bottom: + if scroll_mode == "top": + text.yview_moveto(0.0) + elif scroll_mode == "keep": + text.yview_moveto(top_pos) + elif at_bottom: text.yview_moveto(1.0) @@ -95,9 +177,37 @@ def build_high_value(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> def build_actions_log(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> None: section(parent, "行为日志", "actions.log / 独立滚动 / 刷新") toolbar(parent, [("刷新", ctx.action("refresh"), "soft"), ("运行日志", ctx.action("show_run_log"), "soft"), "上一个", "下一个", "清除"]) - box, text = log_panel(parent, "行为日志", data.action_log, height=24) + controls = tk.Frame(parent, bg=theme.BG) + controls.pack(fill="x", pady=(0, 8)) + tk.Label(controls, text="滚动:", bg=theme.BG, fg=theme.MUTED, font=theme.FONT_SMALL).pack(side="left") + scroll_var = tk.StringVar(value="跟随底部") + scroll_combo = ttk.Combobox( + controls, + textvariable=scroll_var, + values=("跟随底部", "保持位置", "强制顶部"), + state="readonly", + width=10, + ) + scroll_combo.pack(side="left") + tk.Label( + controls, + text="合并重复,只看关键操作", + bg=theme.BG, + fg=theme.SUBTLE, + font=theme.FONT_SMALL, + ).pack(side="left", padx=(10, 0)) + lines = _compact_action_lines(data.action_log, _runner_action_lines(ctx)) + box, text = log_panel(parent, "行为日志", lines, height=24) box.pack(fill="both", expand=True) + def scroll_mode() -> str: + value = scroll_var.get() + if value == "强制顶部": + return "top" + if value == "保持位置": + return "keep" + return "follow" + def refresh_file() -> None: try: if not text.winfo_exists(): @@ -109,7 +219,7 @@ def build_actions_log(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> latest = load_dashboard_data().action_log except Exception: latest = data.action_log - _set_log_text(text, latest) + _set_log_text(text, _compact_action_lines(latest, _runner_action_lines(ctx)), scroll_mode=scroll_mode()) try: text.after(2000, refresh_file) except Exception: