From be1eb6eda701f1c2a7bdf85b047176445f7d0df9 Mon Sep 17 00:00:00 2001 From: Codex Date: Sat, 23 May 2026 03:41:52 +0800 Subject: [PATCH] =?UTF-8?q?refactor(web=5Fgui):=20=E6=8A=BD=20ProcRunner.?= =?UTF-8?q?=5Fupdate=5Fstats=5Ffrom=5Fline=20=E8=A7=A3=E6=9E=90=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E5=88=B0=20stats=5Flog=5Fparser=20(2429=20=E2=86=92?= =?UTF-8?q?=201806)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ProcRunner._update_stats_from_line 是 624 行的"日志行 → 计数器更新"方法,与 gui_core.Runner 同名方法行为接近但有差异: - web_gui 解析 traceId / MDC 信息(gui_core 没有) - web_gui 没有 _lock / _last_log_ts 字段(不需要锁保护) - web_gui 没有 unique_keys_* / *_room_pos 这些去重数据结构 - web_gui 有"进入直播间到识别福袋耗时"事件解析 抽到独立的 gui/web_gui_pkg/stats_log_parser.py(不与 gui_core_pkg 版本共享, 避免在两边各加缺失字段,行为差异显式保留在两个文件中): - update_stats_from_line(runner, line) 主入口 - 16 个 _parse_* 子函数(含 traceId/MDC、enter_to_fudai_seen 等 web_gui 专属段) ProcRunner._update_stats_from_line 从 624 行变成 4 行委托。 1408 passed. Co-Authored-By: Claude Opus 4.7 (1M context) --- gui/web_gui.py | 625 +----------------------- gui/web_gui_pkg/stats_log_parser.py | 708 ++++++++++++++++++++++++++++ 2 files changed, 711 insertions(+), 622 deletions(-) create mode 100644 gui/web_gui_pkg/stats_log_parser.py diff --git a/gui/web_gui.py b/gui/web_gui.py index 2066a06..76506bd 100644 --- a/gui/web_gui.py +++ b/gui/web_gui.py @@ -193,628 +193,9 @@ class ProcRunner: } def _update_stats_from_line(self, line: str) -> None: - s = (line or "").strip() - if not s: - return - - # --- 统一日志格式解析:提取traceId和MDC信息 --- - # 格式:时间戳(毫秒) - [级别] - [线程名] - [模块名:函数名:行号] - [traceId:xxx] - 消息 | MDC: 上下文信息 - trace_id_match = re.search(r"\[traceId:([^\]]+)\]", s) - if trace_id_match: - trace_id = trace_id_match.group(1) - # 存储当前traceId(用于统计和显示) - if not self._current_trace_id: - self._current_trace_id = trace_id - elif trace_id != "N/A": - self._current_trace_id = trace_id - - # 提取MDC信息 - mdc_match = re.search(r"MDC:\s*([^|]+)", s) - if mdc_match: - mdc_str = mdc_match.group(1).strip() - # 解析MDC键值对 - mdc_pairs = re.findall(r"(\w+)=([^,\s]+)", mdc_str) - for key, value in mdc_pairs: - # 存储MDC信息(可用于统计) - self._mdc_context[key] = value - - # --- 福袋:抽取"最近一次"识别到的信息 --- - # 支持多种格式:旧格式"福袋内容:"和新格式"字段_内容="或"内容=" - content_extracted = False - room_title = "" - if "福袋内容:" in s: - self._fudai_last["content"] = ( - s.split("福袋内容:", 1)[1].strip().split()[0] - ) # 只取第一个词,避免包含后续字段 - self._fudai_last["updated_ts"] = time.time() - content_extracted = True - else: - # 新格式:字段_内容= 或 内容= - m = re.search(r"(?:字段_)?内容[=:]\s*([^\s]+)", s) - if m: - self._fudai_last["content"] = m.group(1).strip() - self._fudai_last["updated_ts"] = time.time() - content_extracted = True - - # 提取直播间标题 - m = re.search(r"room_title[=:]\s*([^\s,]+)", s) - if m: - room_title = m.group(1).strip() - - if content_extracted: - fudai_content = self._fudai_last.get("content", "") - key = (room_title, fudai_content) - if key not in self._recorded_fudai_seen: - self._recorded_fudai_seen.add(key) - self._inc("fudai_seen") - self._record_event("fudai_seen", self._snapshot_fudai()) - - # 参与人数:支持多种格式 - m = re.search(r"(?:字段_)?参与人数[=:]?\s*(\d+)", s) - if not m: - m = re.search(r"参与人数:\s*(\d+)", s) - if m: - self._fudai_last["participants"] = int(m.group(1)) - self._fudai_last["updated_ts"] = time.time() - - # 福袋份数:支持多种格式 - m = re.search(r"(?:字段_)?份数[=:]?\s*(\d+)", s) - if not m: - m = re.search(r"福袋份数:\s*(\d+)", s) - if m: - self._fudai_last["copies"] = int(m.group(1)) - self._fudai_last["updated_ts"] = time.time() - - # 倒计时:支持多种格式 - if "倒计时时间:" in s: - self._fudai_last["countdown"] = ( - s.split("倒计时时间:", 1)[1].strip().split()[0] - ) - self._fudai_last["updated_ts"] = time.time() - else: - m = re.search(r"(?:字段_)?倒计时[=:]?\s*([^\s]+)", s) - if m: - self._fudai_last["countdown"] = m.group(1).strip() - self._fudai_last["updated_ts"] = time.time() - - # 奖品价值:支持多种格式 - m = re.search( - r"(?:字段_)?(?:价值|价值元|奖品价值)[=:]?\s*([0-9]+(?:[.\u00B7\uFF0E\uFF61·.。][0-9]+)?)", - s, - ) - if not m: - m = re.search( - r"奖品价值:\s*([0-9]+(?:[.\u00B7\uFF0E\uFF61·.。][0-9]+)?)\s*元", s - ) - if m: - try: - self._fudai_last["prize_value_yuan"] = float(m.group(1)) - self._fudai_last["updated_ts"] = time.time() - except Exception: - pass - - # --- 福袋:关键流程计数 --- - if "点击打开福袋详情" in s or ("[流程]" in s and "检测到福袋图标并点击" in s): - self._inc("fudai_open_detail") - self._record_event("fudai_open_detail", self._snapshot_fudai()) - # 兼容新结构化中文日志: [参与] 动作=点击参与 - if "点击参与抽奖" in s or ( - "[参与]" in s and ("动作=点击参与" in s or "动作=参与" in s) - ): - self._inc("fudai_click_attend") - self._record_event("fudai_click_attend", self._snapshot_fudai()) - - # 参与成功/已参与:兼容旧文本 & 新结构化字段 - if ( - "已经参与,等待开奖" in s - or "已经参与,等待看播时间凑齐开奖" in s - or "已参与成功(或看播中)" in s - or ( - "[参与]" in s - and ("结果=已参与" in s or "结果=看播中" in s or "结果=参与成功" in s) - ) - or ("[参与]" in s and "已参与" in s and "等待开奖" in s) - or ("预约回退后直接参与" in s and "参与成功" in s) - ): - self._inc("fudai_attend_ok") - self._record_event("fudai_attend_ok", self._snapshot_fudai()) - if "参与抽奖失败" in s or ("[参与]" in s and "结果=失败" in s): - self._inc("fudai_attend_fail") - self._record_event("fudai_attend_fail", self._snapshot_fudai()) - - # --- 福袋:跳过/过滤原因 --- - if "参与人数" in s and "< 阈值" in s and "不参与" in s: - self._inc("fudai_skip_participants_low") - # 提取实际参与人数和阈值 - meta = self._snapshot_fudai() - m = re.search(r"参与人数[=:]?\s*(\d+)", s) - if m: - try: - meta["participants"] = int(m.group(1)) - except Exception: - pass - m = re.search(r"阈值[=:]?\s*(\d+)", s) - if m: - try: - meta["threshold"] = int(m.group(1)) - except Exception: - pass - self._record_event("fudai_skip", {"reason": "participants_low", **meta}) - if "参与人数" in s and "> 阈值" in s and "不参与" in s: - self._inc("fudai_skip_participants_high") - # 提取实际参与人数和阈值 - meta = self._snapshot_fudai() - m = re.search(r"参与人数[=:]?\s*(\d+)", s) - if m: - try: - meta["participants"] = int(m.group(1)) - except Exception: - pass - m = re.search(r"阈值[=:]?\s*(\d+)", s) - if m: - try: - meta["threshold"] = int(m.group(1)) - except Exception: - pass - self._record_event("fudai_skip", {"reason": "participants_high", **meta}) - if "奖品价值" in s and "< 阈值" in s and "不参与" in s: - self._inc("fudai_skip_value_low") - # 提取实际价值和阈值 - meta = self._snapshot_fudai() - m = re.search( - r"(?:奖品)?价值[=:]?\s*([0-9]+(?:[.\u00B7\uFF0E\uFF61·.。][0-9]+)?)", s - ) - if m: - try: - meta["prize_value_yuan"] = float(m.group(1)) - except Exception: - pass - m = re.search( - r"阈值[=:]?\s*([0-9]+(?:[.\u00B7\uFF0E\uFF61·.。][0-9]+)?)", s - ) - if m: - try: - meta["threshold"] = float(m.group(1)) - except Exception: - pass - self._record_event("fudai_skip", {"reason": "value_low", **meta}) - if "福袋内容不理想" in s or ("[筛选]" in s and "内容" in s and "黑名单" in s): - self._inc("fudai_skip_content") - self._record_event( - "fudai_skip", {"reason": "content", **self._snapshot_fudai()} - ) - if "抽奖倒计时时间小于" in s and "不参与" in s: - self._inc("fudai_skip_countdown_short") - # 提取倒计时秒数 - meta = self._snapshot_fudai() - m = re.search(r"(\d+)\s*秒", s) - if m: - try: - meta["countdown_seconds"] = int(m.group(1)) - except Exception: - pass - self._record_event("fudai_skip", {"reason": "countdown_short", **meta}) - if "抽奖倒计时时间大于" in s and "暂不参与" in s: - self._inc("fudai_skip_countdown_long") - # 提取倒计时秒数 - meta = self._snapshot_fudai() - m = re.search(r"(\d+)\s*秒", s) - if m: - try: - meta["countdown_seconds"] = int(m.group(1)) - except Exception: - pass - self._record_event("fudai_skip", {"reason": "countdown_long", **meta}) - # 看播门槛:默认不参与(新结构化日志) - if ( - "需要看播时长:默认不参与" in s - or "需要开始观看/看播门槛:默认不参与" in s - or ("[参与]" in s and "结果=看播门槛" in s) - ): - self._inc("fudai_skip_watch_required") - self._record_event( - "fudai_skip", {"reason": "watch_required", **self._snapshot_fudai()} - ) - if "打开福袋异常" in s or ("[流程]" in s and "打开福袋异常" in s): - self._inc("fudai_open_error") - self._record_event("fudai_open_error", self._snapshot_fudai()) - if "超过10次点击福袋无法打开详情" in s: - self._inc("fudai_open_error_too_many") - self._record_event("fudai_open_error_too_many", self._snapshot_fudai()) - # 倒计时大于阈值但价值达到高价值阈值,记录预约 - if ( - "[流程]" in s and "倒计时大于阈值,但价值达到高价值阈值,记录预约" in s - ) or ("[参与]" in s and "倒计时大于阈值,但价值达到高价值阈值,记录预约" in s): - self._inc("fudai_reserved_high_value") - # 提取价值和倒计时信息 - meta = self._snapshot_fudai() - m = re.search(r"(?:价值|价值元)[=:]?\s*([0-9]+(?:\.[0-9]+)?)", s) - if m: - try: - meta["prize_value_yuan"] = float(m.group(1)) - except Exception: - pass - m = re.search(r"(?:剩余|倒计时)[=:]?\s*(\d+)\s*秒", s) - if m: - try: - meta["remain_seconds"] = int(m.group(1)) - except Exception: - pass - self._record_event("fudai_reserved_high_value", meta) - - # --- 错误和异常统计(与 gui_core.py 保持一致) --- - if "[错误]" in s or "[异常]" in s or "错误=" in s or "异常=" in s: - self._inc("error_total") - error_type = "unknown" - if "UnboundLocalError" in s: - error_type = "UnboundLocalError" - self._inc("error_unbound_local") - elif "AttributeError" in s: - error_type = "AttributeError" - self._inc("error_attribute") - elif "KeyError" in s: - error_type = "KeyError" - self._inc("error_key") - elif "ValueError" in s: - error_type = "ValueError" - self._inc("error_value") - elif "TypeError" in s: - error_type = "TypeError" - self._inc("error_type") - elif "TimeoutError" in s or "超时" in s: - error_type = "TimeoutError" - self._inc("error_timeout") - elif "ConnectionError" in s or ("连接" in s and "失败" in s): - error_type = "ConnectionError" - self._inc("error_connection") - elif "OCR" in s and ("失败" in s or "错误" in s): - error_type = "OCR_Error" - self._inc("error_ocr") - elif "截图" in s and ("失败" in s or "错误" in s): - error_type = "Screenshot_Error" - self._inc("error_screenshot") - elif "识别" in s and ("失败" in s or "错误" in s): - error_type = "Detection_Error" - self._inc("error_detection") - self._record_event("error", {"error_type": error_type}) - - if "阶段诊断" in s and ("失败" in s or "错误" in s or "异常" in s): - self._inc("error_stage_diagnosis") - self._record_event("error_stage_diagnosis", {}) - if "读取福袋详情" in s and ("失败" in s or "错误" in s or "异常" in s): - self._inc("error_read_detail") - self._record_event("error_read_detail", {}) - if "load_config" in s and ("失败" in s or "错误" in s or "异常" in s): - self._inc("error_config_load") - self._record_event("error_config_load", {}) - if ("预约队列状态文件" in s or "reservations.json" in s) and ( - "失败" in s or "错误" in s - ): - self._inc("error_state_file") - self._record_event("error_state_file", {}) - if "直播间检测" in s and ("失败" in s or "错误" in s): - self._inc("error_live_room_detect") - self._record_event("error_live_room_detect", {}) - if "查找福袋" in s and ("失败" in s or "错误" in s or "未找到" in s): - self._inc("error_fudai_find") - self._record_event("error_fudai_find", {}) - - # --- 粉丝团门槛 --- - if "需要加入粉丝团才能参与" in s or ("[参与]" in s and "结果=需要粉丝团" in s): - self._inc("fansclub_required") - self._record_event("fansclub_required", self._snapshot_fudai()) - if "未启用自动加入粉丝团" in s: - self._inc("fansclub_manual_needed") - self._record_event("fansclub_manual_needed", self._snapshot_fudai()) - if "粉丝团门槛仍未解决" in s or ("[参与]" in s and "粉丝团门槛仍未解决" in s): - self._inc("fansclub_unresolved") - self._record_event("fansclub_unresolved", self._snapshot_fudai()) - - # --- 预约队列/回退校验 --- - if ( - "[预约]" in s and "回退校验通过" in s - ) or "回退校验通过:已回到目标直播间" in s: - self._inc("reservation_back_verify_ok") - self._record_event("reservation_back_verify_ok", {}) - if ("[预约]" in s and "回退校验失败" in s) or "回退校验失败" in s: - self._inc("reservation_back_verify_fail") - self._record_event("reservation_back_verify_fail", {}) - if ( - "[预约]" in s and "预约队列已满,丢弃最低价值" in s - ) or "预约队列已满,丢弃最低价值" in s: - self._inc("reservation_drop_low_value") - self._record_event("reservation_drop_low_value", {}) - if ("[预约]" in s and "清空预约队列" in s) or "清空预约队列" in s: - self._inc("reservation_clear") - self._record_event("reservation_clear", {}) - - # --- 预约记录相关(与 gui_core.py 保持一致) --- - if ( - "[预约]" in s and "已记录高价值福袋" in s - ) or "record_high_value_reservation:已记录高价值福袋" in s: - self._inc("reservation_recorded") - # 提取预约价值:支持多种格式 - value = None - m = re.search(r"(?:字段_)?(?:价值|价值元|value)[=:]?\s*(\d+(?:\.\d+)?)", s) - if not m: - m = re.search(r"value[=:]?\s*(\d+(?:\.\d+)?)", s) - if m: - try: - value = float(m.group(1)) - except Exception: - pass - # 提取开奖时间 - future_ts = None - m = re.search(r"(?:字段_)?(?:开奖时间|future|future_ts)[=:]?\s*(\d+)", s) - if m: - try: - future_ts = int(m.group(1)) - except Exception: - pass - # 提取直播间名称 - room_title = None - m = re.search( - r"(?:字段_)?(?:直播间|直播间名|room_title)[=:]?\s*([^\s]+)", s - ) - if m: - room_title = m.group(1).strip() - meta = self._snapshot_fudai() - if value is not None: - meta["prize_value_yuan"] = value - if future_ts is not None: - meta["future_ts"] = future_ts - if room_title: - meta["room_title"] = room_title - self._record_event("reservation_recorded", meta) - if ( - "[预约]" in s and "进入预约参与窗口" in s - ) or "进入预约参与窗口,准备回退到目标直播间" in s: - self._inc("reservation_window_entered") - self._record_event("reservation_window_entered", {}) - if ( - ("[预约]" in s and "已回退到目标直播间" in s) - or "已回退到目标直播间" in s - or ("[预约]" in s and "已回退到最早开奖的预约直播间" in s) - ): - self._inc("reservation_back_success") - # 提取回退信息 - meta = {} - m = re.search(r"(?:字段_)?(?:需回退次数|back_times)[=:]?\s*(\d+)", s) - if m: - try: - meta["back_times"] = int(m.group(1)) - except Exception: - pass - self._record_event("reservation_back_success", meta) - if ("[预约]" in s and "时间窗口未到" in s) or "时间窗口未到,跳过回退" in s: - self._inc("reservation_window_not_reached") - self._record_event("reservation_window_not_reached", {}) - if ( - "[预约]" in s and "已明显错过开奖时间" in s - ) or "已明显错过开奖时间,清理过期预约" in s: - self._inc("reservation_expired") - self._record_event("reservation_expired", {}) - if ( - "[预约]" in s and "预约队列为空" in s - ) or "process_reservation_attend_window:预约队列为空" in s: - self._inc("reservation_queue_empty") - if ("[预约]" in s and "锁定期未结束" in s) or "锁定期未结束" in s: - self._inc("reservation_locked") - self._record_event("reservation_locked", {}) - if "[预约]" in s and "已回退到第一个直播间" in s: - self._inc("reservation_back_to_first") - self._record_event("reservation_back_to_first", {}) - if "[预约]" in s and "run_once_basic:检查预约回退触发条件" in s: - self._inc("reservation_check_triggered") - if ( - ("[预约]" in s and "队列已满" in s) - or "handle_queue_full_wait_at_earliest" in s - or ("[预约]" in s and "队列已满,回退到最早开奖的预约直播间" in s) - ): - self._inc("reservation_queue_full_wait") - self._record_event("reservation_queue_full_wait", {}) - # 回退后直接参与 - if ("[预约]" in s and "预约回退后直接参与" in s) or "预约回退后直接参与" in s: - self._inc("reservation_back_direct_attend") - self._record_event("reservation_back_direct_attend", {}) - # 队列满回退后设置参与标志 - if "[预约]" in s and "队列满回退后,直接设置参与标志" in s: - self._inc("reservation_queue_full_back_attend") - self._record_event("reservation_queue_full_back_attend", {}) - # 通过相似度匹配识别重复预约 - if "[预约]" in s and "通过相似度匹配识别为重复预约" in s: - self._inc("reservation_duplicate_detected") - self._record_event("reservation_duplicate_detected", {}) - - # --- 开奖结果 --- - if "没有抽中" in s and "关闭弹窗" in s: - self._inc("fudai_result_no_win") - self._record_event("fudai_result_no_win", self._snapshot_fudai()) - if ( - "恭喜中奖了" in s - or "检测到中奖结果" in s - or ("[开奖结果]" in s and "中奖" in s) - ): - self._inc("fudai_result_win") - # 提取中奖信息 - meta = self._snapshot_fudai() - m = re.search(r"(?:奖品|商品)[=:]?\s*([^\s]+)", s) - if m: - meta["prize"] = m.group(1).strip() - self._record_event("fudai_result_win", meta) - self._record_win() - if "福袋商品下单成功" in s or ("[开奖结果]" in s and "下单成功" in s): - self._inc("fudai_order_success") - self._record_event("fudai_order_success", self._snapshot_fudai()) - # 等待开奖(只在设置等待开奖标志时计数,避免重复计数) - # 注意:不要匹配"已经参与,等待开奖",因为那已经在参与成功时计数了 - if ("[参与]" in s and "参与成功,设置等待开奖标志" in s) or ( - "[参与]" in s and "设置等待开奖标志" in s - ): - self._inc("fudai_waiting_lottery") - # 提取剩余秒数 - meta = self._snapshot_fudai() - m = re.search(r"(?:剩余|倒计时|remain_seconds)[=:]?\s*(\d+)", s) - if m: - try: - meta["remain_seconds"] = int(m.group(1)) - except Exception: - pass - self._record_event("fudai_waiting_lottery", meta) - - # --- 直播间切换(按原因粗分) --- - if "上划切换直播间" in s or ("[操作]" in s and "上滑切换直播间" in s): - self._inc("switch_room") - meta_reason = "unknown" - if "无福袋" in s: - self._inc("switch_room_no_fudai") - meta_reason = "no_fudai" - elif "参与人数" in s: - self._inc("switch_room_participants") - meta_reason = "participants" - elif "奖品价值" in s: - self._inc("switch_room_value") - meta_reason = "value" - elif "内容不理想" in s: - self._inc("switch_room_content") - meta_reason = "content" - elif "倒计时" in s: - self._inc("switch_room_countdown") - meta_reason = "countdown" - elif "参与抽奖失败" in s: - self._inc("switch_room_attend_fail") - meta_reason = "attend_fail" - elif "记录预约后" in s: - self._inc("switch_room_after_reservation") - meta_reason = "after_reservation" - elif "锁定期内" in s: - self._inc("switch_room_locked") - meta_reason = "locked" - self._record_event( - "switch_room", {"reason": meta_reason, **self._snapshot_fudai()} - ) - - # --- 阶段诊断统计 --- - if "[阶段诊断]" in s or "阶段诊断" in s: - if "阶段诊断完成" in s: - self._inc("stage_diagnosis_complete") - # 提取阶段和耗时 - meta = {} - m = re.search(r"阶段[=:]?\s*(\w+)", s) - if m: - meta["stage"] = m.group(1) - m = re.search( - r"耗时[=:]?\s*([0-9]+(?:[.\u00B7\uFF0E\uFF61·.。][0-9]+)?)\s*秒", s - ) - if m: - try: - meta["elapsed_seconds"] = float(m.group(1)) - except Exception: - pass - m = re.search( - r"分数[=:]?\s*([0-9]+(?:[.\u00B7\uFF0E\uFF61·.。][0-9]+)?)", s - ) - if m: - try: - meta["score"] = float(m.group(1)) - except Exception: - pass - self._record_event("stage_diagnosis_complete", meta) - elif "阶段诊断失败" in s or "阶段诊断错误" in s: - self._inc("stage_diagnosis_failed") - self._record_event("stage_diagnosis_failed", {}) - - # --- OCR识别统计 --- - if "[福袋]" in s and "OCR识别" in s: - self._inc("ocr_attempt") - if "识别成功" in s or "识别完成" in s: - self._inc("ocr_success") - elif "识别失败" in s or "识别错误" in s: - self._inc("ocr_failed") - - # --- 性能统计 --- - m = re.search( - r"耗时[=:]?\s*([0-9]+(?:[.\u00B7\uFF0E\uFF61·.。][0-9]+)?)\s*秒", s - ) - if m: - try: - elapsed = float(m.group(1)) - # 记录耗时较长的操作 - if elapsed > 5.0: - self._inc("slow_operation") - self._record_event( - "slow_operation", - {"elapsed_seconds": elapsed, "operation": s[:100]}, - ) - except Exception: - pass - - # --- 详情识别统计 --- - if ("[福袋详情]" in s and "识别" in s) or "识别福袋详情" in s: - self._inc("fudai_detail_read_attempt") - if ( - "识别成功" in s - or "识别完成" in s - or "read_fudai_detail_once完成(成功)" in s - ): - self._inc("fudai_detail_read_success") - # 提取任务数量 - meta = {} - m = re.search(r"(?:任务数量|task_count)[=:]?\s*(\d+)", s) - if m: - try: - meta["task_count"] = int(m.group(1)) - except Exception: - pass - self._record_event("fudai_detail_read_success", meta) - elif "识别失败" in s or "识别错误" in s: - self._inc("fudai_detail_read_failed") - self._record_event("fudai_detail_read_failed", {}) - - # --- 福袋查找统计 --- - if "[福袋查找]" in s or "查找福袋" in s: - if "检测完成" in s and "命中" in s: - self._inc("fudai_find_success") - elif "检测完成" in s and "未找到" in s: - self._inc("fudai_find_not_found") - elif "等待超时" in s: - self._inc("fudai_find_timeout") - - # --- 回退操作统计 --- - if ("[预约]" in s and "回退" in s) or "回退到" in s: - if "回退完成" in s or "回退成功" in s: - self._inc("reservation_back_operation") - # 提取回退次数 - meta = {} - m = re.search(r"(?:回退|back_times)[=:]?\s*(\d+)", s) - if m: - try: - meta["back_times"] = int(m.group(1)) - except Exception: - pass - self._record_event("reservation_back_operation", meta) - - # --- 性能:进入直播间到识别福袋耗时 --- - m = re.search( - r"进入直播间到识别福袋耗时:\s*([0-9]+(?:[.\u00B7\uFF0E\uFF61·.。][0-9]+)?)\s*秒(?:\s*进入原因=(.*))?", - s, - ) - if m: - try: - secs = float(m.group(1)) - except Exception: - secs = None - reason = str(m.group(2) or "") - # 记录为事件,便于后续做分布/趋势分析 - self._record_event( - "enter_to_fudai_seen", - {"secs": secs, "enter_reason": reason, **self._snapshot_fudai()}, - ) - # 同时做一个计数器 - try: - self._inc("enter_to_fudai_seen") - except Exception: - pass + """委托到 gui/web_gui_pkg/stats_log_parser.update_stats_from_line。""" + from gui.web_gui_pkg.stats_log_parser import update_stats_from_line + update_stats_from_line(self, line) def is_running(self) -> bool: with self._lock: diff --git a/gui/web_gui_pkg/stats_log_parser.py b/gui/web_gui_pkg/stats_log_parser.py new file mode 100644 index 0000000..31c1311 --- /dev/null +++ b/gui/web_gui_pkg/stats_log_parser.py @@ -0,0 +1,708 @@ +"""gui/web_gui_pkg/stats_log_parser - 把日志行解析成 ProcRunner 统计计数器更新 + +从 ProcRunner._update_stats_from_line 抽出的 624 行纯字符串解析逻辑。 +与 gui_core_pkg/stats_log_parser 行为相似但不能共用,因为: +- web_gui 解析 traceId / MDC 信息(gui_core 没有) +- web_gui 没有 _lock / _last_log_ts 字段(不需要锁保护) +- web_gui 没有 unique_keys_* / *_room_pos 这些去重数据结构(不做去重) +- web_gui 有额外的"进入直播间到识别福袋耗时"事件解析 + +入口:update_stats_from_line(runner, line) -> None + +runner 必须提供: +- _inc(counter) +- _record_event(typ, meta) +- _record_win() +- _snapshot_fudai() +- _fudai_last (dict) +- _recorded_fudai_seen (set) +- _current_trace_id (str) +- _mdc_context (dict) +""" +from __future__ import annotations + +import re +import time +from typing import Any + + +def update_stats_from_line(runner: Any, line: str) -> None: + """把日志行解析成各 counter 的累加 + event 记录(web_gui 版本)。""" + s = (line or "").strip() + if not s: + return + + _parse_trace_id_and_mdc(runner, s) + _parse_fudai_fields(runner, s) + _parse_open_attend_counters(runner, s) + _parse_skip_reasons(runner, s) + _parse_errors(runner, s) + _parse_fansclub(runner, s) + _parse_reservation(runner, s) + _parse_lottery_results(runner, s) + _parse_switch_room(runner, s) + _parse_stage_diag(runner, s) + _parse_ocr_stats(runner, s) + _parse_perf(runner, s) + _parse_detail_read(runner, s) + _parse_fudai_find(runner, s) + _parse_back_operation(runner, s) + _parse_enter_to_fudai_seen(runner, s) + + +# ============================================================ +# traceId / MDC(web_gui 专属) +# ============================================================ + +def _parse_trace_id_and_mdc(runner: Any, s: str) -> None: + """统一日志格式解析:提取 traceId 和 MDC 信息。 + + 格式:时间戳(毫秒) - [级别] - [线程名] - [模块:函数:行号] - [traceId:xxx] - 消息 | MDC: 上下文 + """ + trace_id_match = re.search(r"\[traceId:([^\]]+)\]", s) + if trace_id_match: + trace_id = trace_id_match.group(1) + if not runner._current_trace_id: + runner._current_trace_id = trace_id + elif trace_id != "N/A": + runner._current_trace_id = trace_id + + mdc_match = re.search(r"MDC:\s*([^|]+)", s) + if mdc_match: + mdc_str = mdc_match.group(1).strip() + mdc_pairs = re.findall(r"(\w+)=([^,\s]+)", mdc_str) + for key, value in mdc_pairs: + runner._mdc_context[key] = value + + +# ============================================================ +# 福袋字段提取 +# ============================================================ + +def _parse_fudai_fields(runner: Any, s: str) -> None: + content_extracted = False + room_title = "" + if "福袋内容:" in s: + runner._fudai_last["content"] = ( + s.split("福袋内容:", 1)[1].strip().split()[0] + ) + runner._fudai_last["updated_ts"] = time.time() + content_extracted = True + else: + m = re.search(r"(?:字段_)?内容[=:]\s*([^\s]+)", s) + if m: + runner._fudai_last["content"] = m.group(1).strip() + runner._fudai_last["updated_ts"] = time.time() + content_extracted = True + + m = re.search(r"room_title[=:]\s*([^\s,]+)", s) + if m: + room_title = m.group(1).strip() + + if content_extracted: + fudai_content = runner._fudai_last.get("content", "") + key = (room_title, fudai_content) + if key not in runner._recorded_fudai_seen: + runner._recorded_fudai_seen.add(key) + runner._inc("fudai_seen") + runner._record_event("fudai_seen", runner._snapshot_fudai()) + + m = re.search(r"(?:字段_)?参与人数[=:]?\s*(\d+)", s) or re.search( + r"参与人数:\s*(\d+)", s + ) + if m: + runner._fudai_last["participants"] = int(m.group(1)) + runner._fudai_last["updated_ts"] = time.time() + + m = re.search(r"(?:字段_)?份数[=:]?\s*(\d+)", s) or re.search( + r"福袋份数:\s*(\d+)", s + ) + if m: + runner._fudai_last["copies"] = int(m.group(1)) + runner._fudai_last["updated_ts"] = time.time() + + if "倒计时时间:" in s: + runner._fudai_last["countdown"] = ( + s.split("倒计时时间:", 1)[1].strip().split()[0] + ) + runner._fudai_last["updated_ts"] = time.time() + else: + m = re.search(r"(?:字段_)?倒计时[=:]?\s*([^\s]+)", s) + if m: + runner._fudai_last["countdown"] = m.group(1).strip() + runner._fudai_last["updated_ts"] = time.time() + + m = re.search( + r"(?:字段_)?(?:价值|价值元|奖品价值)[=:]?\s*([0-9]+(?:[.·.。·.。][0-9]+)?)", + s, + ) or re.search( + r"奖品价值:\s*([0-9]+(?:[.·.。·.。][0-9]+)?)\s*元", + s, + ) + if m: + try: + runner._fudai_last["prize_value_yuan"] = float(m.group(1)) + runner._fudai_last["updated_ts"] = time.time() + except Exception: + pass + + +# ============================================================ +# 打开详情 / 点击参与 / 参与成功 / 参与失败 +# ============================================================ + +def _parse_open_attend_counters(runner: Any, s: str) -> None: + if "点击打开福袋详情" in s or ("[流程]" in s and "检测到福袋图标并点击" in s): + runner._inc("fudai_open_detail") + runner._record_event("fudai_open_detail", runner._snapshot_fudai()) + + if "点击参与抽奖" in s or ( + "[参与]" in s and ("动作=点击参与" in s or "动作=参与" in s) + ): + runner._inc("fudai_click_attend") + runner._record_event("fudai_click_attend", runner._snapshot_fudai()) + + if ( + "已经参与,等待开奖" in s + or "已经参与,等待看播时间凑齐开奖" in s + or "已参与成功(或看播中)" in s + or ( + "[参与]" in s + and ("结果=已参与" in s or "结果=看播中" in s or "结果=参与成功" in s) + ) + or ("[参与]" in s and "已参与" in s and "等待开奖" in s) + or ("预约回退后直接参与" in s and "参与成功" in s) + ): + runner._inc("fudai_attend_ok") + runner._record_event("fudai_attend_ok", runner._snapshot_fudai()) + + if "参与抽奖失败" in s or ("[参与]" in s and "结果=失败" in s): + runner._inc("fudai_attend_fail") + runner._record_event("fudai_attend_fail", runner._snapshot_fudai()) + + +# ============================================================ +# 跳过原因 +# ============================================================ + +def _parse_skip_reasons(runner: Any, s: str) -> None: + if "参与人数" in s and "< 阈值" in s and "不参与" in s: + runner._inc("fudai_skip_participants_low") + meta = runner._snapshot_fudai() + m = re.search(r"参与人数[=:]?\s*(\d+)", s) + if m: + try: + meta["participants"] = int(m.group(1)) + except Exception: + pass + m = re.search(r"阈值[=:]?\s*(\d+)", s) + if m: + try: + meta["threshold"] = int(m.group(1)) + except Exception: + pass + runner._record_event("fudai_skip", {"reason": "participants_low", **meta}) + + if "参与人数" in s and "> 阈值" in s and "不参与" in s: + runner._inc("fudai_skip_participants_high") + meta = runner._snapshot_fudai() + m = re.search(r"参与人数[=:]?\s*(\d+)", s) + if m: + try: + meta["participants"] = int(m.group(1)) + except Exception: + pass + m = re.search(r"阈值[=:]?\s*(\d+)", s) + if m: + try: + meta["threshold"] = int(m.group(1)) + except Exception: + pass + runner._record_event("fudai_skip", {"reason": "participants_high", **meta}) + + if "奖品价值" in s and "< 阈值" in s and "不参与" in s: + runner._inc("fudai_skip_value_low") + meta = runner._snapshot_fudai() + m = re.search( + r"(?:奖品)?价值[=:]?\s*([0-9]+(?:[.·.。·.。][0-9]+)?)", s + ) + if m: + try: + meta["prize_value_yuan"] = float(m.group(1)) + except Exception: + pass + m = re.search( + r"阈值[=:]?\s*([0-9]+(?:[.·.。·.。][0-9]+)?)", s + ) + if m: + try: + meta["threshold"] = float(m.group(1)) + except Exception: + pass + runner._record_event("fudai_skip", {"reason": "value_low", **meta}) + + if "福袋内容不理想" in s or ("[筛选]" in s and "内容" in s and "黑名单" in s): + runner._inc("fudai_skip_content") + runner._record_event( + "fudai_skip", {"reason": "content", **runner._snapshot_fudai()} + ) + + if "抽奖倒计时时间小于" in s and "不参与" in s: + runner._inc("fudai_skip_countdown_short") + meta = runner._snapshot_fudai() + m = re.search(r"(\d+)\s*秒", s) + if m: + try: + meta["countdown_seconds"] = int(m.group(1)) + except Exception: + pass + runner._record_event("fudai_skip", {"reason": "countdown_short", **meta}) + + if "抽奖倒计时时间大于" in s and "暂不参与" in s: + runner._inc("fudai_skip_countdown_long") + meta = runner._snapshot_fudai() + m = re.search(r"(\d+)\s*秒", s) + if m: + try: + meta["countdown_seconds"] = int(m.group(1)) + except Exception: + pass + runner._record_event("fudai_skip", {"reason": "countdown_long", **meta}) + + if ( + "需要看播时长:默认不参与" in s + or "需要开始观看/看播门槛:默认不参与" in s + or ("[参与]" in s and "结果=看播门槛" in s) + ): + runner._inc("fudai_skip_watch_required") + runner._record_event( + "fudai_skip", {"reason": "watch_required", **runner._snapshot_fudai()} + ) + + if "打开福袋异常" in s or ("[流程]" in s and "打开福袋异常" in s): + runner._inc("fudai_open_error") + runner._record_event("fudai_open_error", runner._snapshot_fudai()) + + if "超过10次点击福袋无法打开详情" in s: + runner._inc("fudai_open_error_too_many") + runner._record_event("fudai_open_error_too_many", runner._snapshot_fudai()) + + if ( + "[流程]" in s and "倒计时大于阈值,但价值达到高价值阈值,记录预约" in s + ) or ("[参与]" in s and "倒计时大于阈值,但价值达到高价值阈值,记录预约" in s): + runner._inc("fudai_reserved_high_value") + meta = runner._snapshot_fudai() + m = re.search(r"(?:价值|价值元)[=:]?\s*([0-9]+(?:\.[0-9]+)?)", s) + if m: + try: + meta["prize_value_yuan"] = float(m.group(1)) + except Exception: + pass + m = re.search(r"(?:剩余|倒计时)[=:]?\s*(\d+)\s*秒", s) + if m: + try: + meta["remain_seconds"] = int(m.group(1)) + except Exception: + pass + runner._record_event("fudai_reserved_high_value", meta) + + +# ============================================================ +# 错误统计 +# ============================================================ + +def _parse_errors(runner: Any, s: str) -> None: + if "[错误]" in s or "[异常]" in s or "错误=" in s or "异常=" in s: + runner._inc("error_total") + error_type = "unknown" + if "UnboundLocalError" in s: + error_type = "UnboundLocalError" + runner._inc("error_unbound_local") + elif "AttributeError" in s: + error_type = "AttributeError" + runner._inc("error_attribute") + elif "KeyError" in s: + error_type = "KeyError" + runner._inc("error_key") + elif "ValueError" in s: + error_type = "ValueError" + runner._inc("error_value") + elif "TypeError" in s: + error_type = "TypeError" + runner._inc("error_type") + elif "TimeoutError" in s or "超时" in s: + error_type = "TimeoutError" + runner._inc("error_timeout") + elif "ConnectionError" in s or ("连接" in s and "失败" in s): + error_type = "ConnectionError" + runner._inc("error_connection") + elif "OCR" in s and ("失败" in s or "错误" in s): + error_type = "OCR_Error" + runner._inc("error_ocr") + elif "截图" in s and ("失败" in s or "错误" in s): + error_type = "Screenshot_Error" + runner._inc("error_screenshot") + elif "识别" in s and ("失败" in s or "错误" in s): + error_type = "Detection_Error" + runner._inc("error_detection") + runner._record_event("error", {"error_type": error_type}) + + if "阶段诊断" in s and ("失败" in s or "错误" in s or "异常" in s): + runner._inc("error_stage_diagnosis") + runner._record_event("error_stage_diagnosis", {}) + if "读取福袋详情" in s and ("失败" in s or "错误" in s or "异常" in s): + runner._inc("error_read_detail") + runner._record_event("error_read_detail", {}) + if "load_config" in s and ("失败" in s or "错误" in s or "异常" in s): + runner._inc("error_config_load") + runner._record_event("error_config_load", {}) + if ("预约队列状态文件" in s or "reservations.json" in s) and ( + "失败" in s or "错误" in s + ): + runner._inc("error_state_file") + runner._record_event("error_state_file", {}) + if "直播间检测" in s and ("失败" in s or "错误" in s): + runner._inc("error_live_room_detect") + runner._record_event("error_live_room_detect", {}) + if "查找福袋" in s and ("失败" in s or "错误" in s or "未找到" in s): + runner._inc("error_fudai_find") + runner._record_event("error_fudai_find", {}) + + +# ============================================================ +# 粉丝团门槛 +# ============================================================ + +def _parse_fansclub(runner: Any, s: str) -> None: + if "需要加入粉丝团才能参与" in s or ("[参与]" in s and "结果=需要粉丝团" in s): + runner._inc("fansclub_required") + runner._record_event("fansclub_required", runner._snapshot_fudai()) + if "未启用自动加入粉丝团" in s: + runner._inc("fansclub_manual_needed") + runner._record_event("fansclub_manual_needed", runner._snapshot_fudai()) + if "粉丝团门槛仍未解决" in s or ("[参与]" in s and "粉丝团门槛仍未解决" in s): + runner._inc("fansclub_unresolved") + runner._record_event("fansclub_unresolved", runner._snapshot_fudai()) + + +# ============================================================ +# 预约队列 / 回退校验 / 预约记录 +# ============================================================ + +def _parse_reservation(runner: Any, s: str) -> None: + if ( + "[预约]" in s and "回退校验通过" in s + ) or "回退校验通过:已回到目标直播间" in s: + runner._inc("reservation_back_verify_ok") + runner._record_event("reservation_back_verify_ok", {}) + if ("[预约]" in s and "回退校验失败" in s) or "回退校验失败" in s: + runner._inc("reservation_back_verify_fail") + runner._record_event("reservation_back_verify_fail", {}) + if ( + "[预约]" in s and "预约队列已满,丢弃最低价值" in s + ) or "预约队列已满,丢弃最低价值" in s: + runner._inc("reservation_drop_low_value") + runner._record_event("reservation_drop_low_value", {}) + if ("[预约]" in s and "清空预约队列" in s) or "清空预约队列" in s: + runner._inc("reservation_clear") + runner._record_event("reservation_clear", {}) + + if ( + "[预约]" in s and "已记录高价值福袋" in s + ) or "record_high_value_reservation:已记录高价值福袋" in s: + runner._inc("reservation_recorded") + value = None + m = re.search(r"(?:字段_)?(?:价值|价值元|value)[=:]?\s*(\d+(?:\.\d+)?)", s) + if not m: + m = re.search(r"value[=:]?\s*(\d+(?:\.\d+)?)", s) + if m: + try: + value = float(m.group(1)) + except Exception: + pass + future_ts = None + m = re.search(r"(?:字段_)?(?:开奖时间|future|future_ts)[=:]?\s*(\d+)", s) + if m: + try: + future_ts = int(m.group(1)) + except Exception: + pass + room_title = None + m = re.search( + r"(?:字段_)?(?:直播间|直播间名|room_title)[=:]?\s*([^\s]+)", s + ) + if m: + room_title = m.group(1).strip() + meta = runner._snapshot_fudai() + if value is not None: + meta["prize_value_yuan"] = value + if future_ts is not None: + meta["future_ts"] = future_ts + if room_title: + meta["room_title"] = room_title + runner._record_event("reservation_recorded", meta) + + if ( + "[预约]" in s and "进入预约参与窗口" in s + ) or "进入预约参与窗口,准备回退到目标直播间" in s: + runner._inc("reservation_window_entered") + runner._record_event("reservation_window_entered", {}) + + if ( + ("[预约]" in s and "已回退到目标直播间" in s) + or "已回退到目标直播间" in s + or ("[预约]" in s and "已回退到最早开奖的预约直播间" in s) + ): + runner._inc("reservation_back_success") + meta = {} + m = re.search(r"(?:字段_)?(?:需回退次数|back_times)[=:]?\s*(\d+)", s) + if m: + try: + meta["back_times"] = int(m.group(1)) + except Exception: + pass + runner._record_event("reservation_back_success", meta) + + if ("[预约]" in s and "时间窗口未到" in s) or "时间窗口未到,跳过回退" in s: + runner._inc("reservation_window_not_reached") + runner._record_event("reservation_window_not_reached", {}) + if ( + "[预约]" in s and "已明显错过开奖时间" in s + ) or "已明显错过开奖时间,清理过期预约" in s: + runner._inc("reservation_expired") + runner._record_event("reservation_expired", {}) + if ( + "[预约]" in s and "预约队列为空" in s + ) or "process_reservation_attend_window:预约队列为空" in s: + runner._inc("reservation_queue_empty") + if ("[预约]" in s and "锁定期未结束" in s) or "锁定期未结束" in s: + runner._inc("reservation_locked") + runner._record_event("reservation_locked", {}) + if "[预约]" in s and "已回退到第一个直播间" in s: + runner._inc("reservation_back_to_first") + runner._record_event("reservation_back_to_first", {}) + if "[预约]" in s and "run_once_basic:检查预约回退触发条件" in s: + runner._inc("reservation_check_triggered") + + if ( + ("[预约]" in s and "队列已满" in s) + or "handle_queue_full_wait_at_earliest" in s + or ("[预约]" in s and "队列已满,回退到最早开奖的预约直播间" in s) + ): + runner._inc("reservation_queue_full_wait") + runner._record_event("reservation_queue_full_wait", {}) + + if ("[预约]" in s and "预约回退后直接参与" in s) or "预约回退后直接参与" in s: + runner._inc("reservation_back_direct_attend") + runner._record_event("reservation_back_direct_attend", {}) + + if "[预约]" in s and "队列满回退后,直接设置参与标志" in s: + runner._inc("reservation_queue_full_back_attend") + runner._record_event("reservation_queue_full_back_attend", {}) + + if "[预约]" in s and "通过相似度匹配识别为重复预约" in s: + runner._inc("reservation_duplicate_detected") + runner._record_event("reservation_duplicate_detected", {}) + + +# ============================================================ +# 开奖结果 + 等待开奖 +# ============================================================ + +def _parse_lottery_results(runner: Any, s: str) -> None: + if "没有抽中" in s and "关闭弹窗" in s: + runner._inc("fudai_result_no_win") + runner._record_event("fudai_result_no_win", runner._snapshot_fudai()) + if ( + "恭喜中奖了" in s + or "检测到中奖结果" in s + or ("[开奖结果]" in s and "中奖" in s) + ): + runner._inc("fudai_result_win") + meta = runner._snapshot_fudai() + m = re.search(r"(?:奖品|商品)[=:]?\s*([^\s]+)", s) + if m: + meta["prize"] = m.group(1).strip() + runner._record_event("fudai_result_win", meta) + runner._record_win() + if "福袋商品下单成功" in s or ("[开奖结果]" in s and "下单成功" in s): + runner._inc("fudai_order_success") + runner._record_event("fudai_order_success", runner._snapshot_fudai()) + if ("[参与]" in s and "参与成功,设置等待开奖标志" in s) or ( + "[参与]" in s and "设置等待开奖标志" in s + ): + runner._inc("fudai_waiting_lottery") + meta = runner._snapshot_fudai() + m = re.search(r"(?:剩余|倒计时|remain_seconds)[=:]?\s*(\d+)", s) + if m: + try: + meta["remain_seconds"] = int(m.group(1)) + except Exception: + pass + runner._record_event("fudai_waiting_lottery", meta) + + +# ============================================================ +# 直播间切换 +# ============================================================ + +def _parse_switch_room(runner: Any, s: str) -> None: + if "上划切换直播间" in s or ("[操作]" in s and "上滑切换直播间" in s): + runner._inc("switch_room") + meta_reason = "unknown" + if "无福袋" in s: + runner._inc("switch_room_no_fudai") + meta_reason = "no_fudai" + elif "参与人数" in s: + runner._inc("switch_room_participants") + meta_reason = "participants" + elif "奖品价值" in s: + runner._inc("switch_room_value") + meta_reason = "value" + elif "内容不理想" in s: + runner._inc("switch_room_content") + meta_reason = "content" + elif "倒计时" in s: + runner._inc("switch_room_countdown") + meta_reason = "countdown" + elif "参与抽奖失败" in s: + runner._inc("switch_room_attend_fail") + meta_reason = "attend_fail" + elif "记录预约后" in s: + runner._inc("switch_room_after_reservation") + meta_reason = "after_reservation" + elif "锁定期内" in s: + runner._inc("switch_room_locked") + meta_reason = "locked" + runner._record_event( + "switch_room", {"reason": meta_reason, **runner._snapshot_fudai()} + ) + + +# ============================================================ +# 阶段诊断 / OCR / 性能 / 详情识别 / 福袋查找 / 回退操作 / 进入直播间到识别耗时 +# ============================================================ + +def _parse_stage_diag(runner: Any, s: str) -> None: + if "[阶段诊断]" in s or "阶段诊断" in s: + if "阶段诊断完成" in s: + runner._inc("stage_diagnosis_complete") + meta = {} + m = re.search(r"阶段[=:]?\s*(\w+)", s) + if m: + meta["stage"] = m.group(1) + m = re.search( + r"耗时[=:]?\s*([0-9]+(?:[.·.。·.。][0-9]+)?)\s*秒", s + ) + if m: + try: + meta["elapsed_seconds"] = float(m.group(1)) + except Exception: + pass + m = re.search( + r"分数[=:]?\s*([0-9]+(?:[.·.。·.。][0-9]+)?)", s + ) + if m: + try: + meta["score"] = float(m.group(1)) + except Exception: + pass + runner._record_event("stage_diagnosis_complete", meta) + elif "阶段诊断失败" in s or "阶段诊断错误" in s: + runner._inc("stage_diagnosis_failed") + runner._record_event("stage_diagnosis_failed", {}) + + +def _parse_ocr_stats(runner: Any, s: str) -> None: + if "[福袋]" in s and "OCR识别" in s: + runner._inc("ocr_attempt") + if "识别成功" in s or "识别完成" in s: + runner._inc("ocr_success") + elif "识别失败" in s or "识别错误" in s: + runner._inc("ocr_failed") + + +def _parse_perf(runner: Any, s: str) -> None: + m = re.search( + r"耗时[=:]?\s*([0-9]+(?:[.·.。·.。][0-9]+)?)\s*秒", s + ) + if m: + try: + elapsed = float(m.group(1)) + if elapsed > 5.0: + runner._inc("slow_operation") + runner._record_event( + "slow_operation", + {"elapsed_seconds": elapsed, "operation": s[:100]}, + ) + except Exception: + pass + + +def _parse_detail_read(runner: Any, s: str) -> None: + if ("[福袋详情]" in s and "识别" in s) or "识别福袋详情" in s: + runner._inc("fudai_detail_read_attempt") + if ( + "识别成功" in s + or "识别完成" in s + or "read_fudai_detail_once完成(成功)" in s + ): + runner._inc("fudai_detail_read_success") + meta = {} + m = re.search(r"(?:任务数量|task_count)[=:]?\s*(\d+)", s) + if m: + try: + meta["task_count"] = int(m.group(1)) + except Exception: + pass + runner._record_event("fudai_detail_read_success", meta) + elif "识别失败" in s or "识别错误" in s: + runner._inc("fudai_detail_read_failed") + runner._record_event("fudai_detail_read_failed", {}) + + +def _parse_fudai_find(runner: Any, s: str) -> None: + if "[福袋查找]" in s or "查找福袋" in s: + if "检测完成" in s and "命中" in s: + runner._inc("fudai_find_success") + elif "检测完成" in s and "未找到" in s: + runner._inc("fudai_find_not_found") + elif "等待超时" in s: + runner._inc("fudai_find_timeout") + + +def _parse_back_operation(runner: Any, s: str) -> None: + if ("[预约]" in s and "回退" in s) or "回退到" in s: + if "回退完成" in s or "回退成功" in s: + runner._inc("reservation_back_operation") + meta = {} + m = re.search(r"(?:回退|back_times)[=:]?\s*(\d+)", s) + if m: + try: + meta["back_times"] = int(m.group(1)) + except Exception: + pass + runner._record_event("reservation_back_operation", meta) + + +def _parse_enter_to_fudai_seen(runner: Any, s: str) -> None: + """web_gui 专属:进入直播间到识别福袋耗时事件。""" + m = re.search( + r"进入直播间到识别福袋耗时:\s*([0-9]+(?:[.·.。·.。][0-9]+)?)\s*秒(?:\s*进入原因=(.*))?", + s, + ) + if not m: + return + try: + secs = float(m.group(1)) + except Exception: + secs = None + reason = str(m.group(2) or "") + runner._record_event( + "enter_to_fudai_seen", + {"secs": secs, "enter_reason": reason, **runner._snapshot_fudai()}, + ) + try: + runner._inc("enter_to_fudai_seen") + except Exception: + pass