"""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