From 9ffbe47bdb37ab6411543a9928b7e1b069532af6 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 18 Jun 2026 04:48:16 +0800 Subject: [PATCH] Improve fudai live flow diagnostics --- .../fudai_modules/handle_detail_filter.py | 55 ++++++++++++++++++- .../fudai_modules/handle_detail_helpers.py | 4 +- .../douyin_fudai_core_pkg/button_state.py | 6 +- .../douyin_logging_wrapper_pkg/simple.py | 25 +++++++-- .../dump_state.py | 6 ++ .../record_title.py | 8 +++ tests/test_fudai_button_state.py | 8 ++- tests/test_logging_simple_mode.py | 51 +++++++++++++++++ tests/test_record_title_mapping.py | 14 ++++- 9 files changed, 163 insertions(+), 14 deletions(-) diff --git a/douyin/core/fudai_modules/handle_detail_filter.py b/douyin/core/fudai_modules/handle_detail_filter.py index c14f723..7010a0c 100644 --- a/douyin/core/fudai_modules/handle_detail_filter.py +++ b/douyin/core/fudai_modules/handle_detail_filter.py @@ -29,7 +29,7 @@ def check_skip_and_handle( return "return" if high_value_result == "continue": return "continue" - return handle_skip_filter(self, skip_reason, start_time) + return handle_skip_filter(self, skip_reason, start_time, detail=detail) def try_high_value_reservation( @@ -126,7 +126,42 @@ def try_high_value_reservation( return "still_skip" -def handle_skip_filter(self: "fudai_analyse", skip_reason: str, start_time: float) -> str: +def _summarize_detail_for_skip(detail) -> dict: + info = { + "fudai_content": "", + "prize_value_yuan": None, + "remain_seconds": None, + "lottery_result_expected_ts": None, + } + try: + if isinstance(detail, tuple): + if len(detail) >= 1: + info["fudai_content"] = str(detail[0] or "")[:60] + if len(detail) >= 5: + try: + info["prize_value_yuan"] = float(detail[4] or 0.0) + except Exception: + info["prize_value_yuan"] = None + if len(detail) >= 6: + countdown_tuple = detail[5] + if isinstance(countdown_tuple, (tuple, list)) and len(countdown_tuple) >= 2: + import time + + future_ts = float(countdown_tuple[1] or 0.0) + info["lottery_result_expected_ts"] = future_ts if future_ts > 0 else None + if future_ts > 0: + info["remain_seconds"] = max(0, int(future_ts - time.time())) + except Exception: + pass + return info + + +def handle_skip_filter( + self: "fudai_analyse", + skip_reason: str, + start_time: float, + detail=None, +) -> str: """普通筛选不通过:关详情 + 按需切直播间。返回 return。""" import time @@ -134,6 +169,22 @@ def handle_skip_filter(self: "fudai_analyse", skip_reason: str, start_time: floa is_countdown_zero = str(skip_reason or "").strip() == "countdown_zero" + try: + detail_info = _summarize_detail_for_skip(detail) + self._log_kv( + "筛选", + "_handle_in_fudai_detail:筛选不通过,确认未发送参与点击", + decision="skip_without_attend", + skip_reason=skip_reason, + attend_click_sent=False, + waiting_for_lottery=bool(getattr(self, "_waiting_for_lottery_result", False)), + button_status=str(getattr(self, "_fudai_button_status", "") or ""), + button_text=str(getattr(self, "_fudai_button_text", "") or ""), + **detail_info, + ) + except Exception: + pass + try: self._log_kv("流程", "_handle_in_fudai_detail:第一次打开时筛选不通过,直接跳过", skip_reason=skip_reason) except Exception: diff --git a/douyin/core/fudai_modules/handle_detail_helpers.py b/douyin/core/fudai_modules/handle_detail_helpers.py index 65200df..65f434c 100644 --- a/douyin/core/fudai_modules/handle_detail_helpers.py +++ b/douyin/core/fudai_modules/handle_detail_helpers.py @@ -636,10 +636,10 @@ def _try_high_value_reservation( def _handle_skip_filter( - self: "fudai_analyse", skip_reason: str, start_time: float + self: "fudai_analyse", skip_reason: str, start_time: float, detail=None ) -> str: from .handle_detail_filter import handle_skip_filter as _impl - return _impl(self, skip_reason, start_time) + return _impl(self, skip_reason, start_time, detail=detail) def _fallback_swipe_up(self: "fudai_analyse") -> None: diff --git a/douyin/fudai/douyin_fudai_core_pkg/button_state.py b/douyin/fudai/douyin_fudai_core_pkg/button_state.py index d1fe948..4ce14c1 100644 --- a/douyin/fudai/douyin_fudai_core_pkg/button_state.py +++ b/douyin/fudai/douyin_fudai_core_pkg/button_state.py @@ -58,8 +58,10 @@ def apply_button_ocr_result( except Exception: pass analyser._log_kv( - "参与", - f"{log_prefix}按钮状态判断", + "按钮识别", + f"{log_prefix}参与按钮状态判断", + decision="button_ocr_state_only", + attend_click_sent=False, status=button_status, button_text=button_text if button_text else "", ) diff --git a/douyin/logging/douyin_logging_wrapper_pkg/simple.py b/douyin/logging/douyin_logging_wrapper_pkg/simple.py index 3e64889..674b263 100644 --- a/douyin/logging/douyin_logging_wrapper_pkg/simple.py +++ b/douyin/logging/douyin_logging_wrapper_pkg/simple.py @@ -57,6 +57,24 @@ _SIMPLE_NOISE_PATTERNS = ( _SIMPLE_MAX_LINE_LEN = 150 +def _should_write_simple_action(tag: str, msg: str, fields: Dict[str, Any]) -> bool: + tag_text = str(tag or "") + msg_text = str(msg or "") + decision = str(fields.get("decision", "") or "") + action = str(fields.get("action", "") or "") + + if "福袋查找" in tag_text and ("检测完成" in tag_text or "检测完成" in msg_text): + return False + if decision == "button_ocr_state_only": + return False + if decision.endswith("_click_sent") or action: + return True + return bool( + tag_text + and any(keyword in tag_text for keyword in ["参与", "中奖", "完成", "失败", "运行"]) + ) + + def _clean_simple_tag(tag: str) -> str: return str(tag or "").strip().strip("[]").strip() @@ -234,12 +252,7 @@ def _enhanced_log_kv_simple( # 简化行为日志记录,避免重复输出 try: # 只在重要事件时写入行为日志 - should_log_action = False - if tag and any( - keyword in str(tag) - for keyword in ["参与", "中奖", "完成", "失败", "运行"] - ): - should_log_action = True + should_log_action = _should_write_simple_action(tag, msg, fields) if should_log_action: # 获取时间戳 diff --git a/douyin/reservation/douyin_reservation_handler_pkg/dump_state.py b/douyin/reservation/douyin_reservation_handler_pkg/dump_state.py index b49a2ca..ce56564 100644 --- a/douyin/reservation/douyin_reservation_handler_pkg/dump_state.py +++ b/douyin/reservation/douyin_reservation_handler_pkg/dump_state.py @@ -148,6 +148,12 @@ def _dump_room_pos_map_to_state(analyser: Any) -> None: if isinstance(v, dict): title = (v.get("title", "") or "").strip() title_raw = (v.get("title_raw", "") or "").strip() + if "has_fudai" not in v: + v["has_fudai"] = False + if "fudai_content" not in v: + v["fudai_content"] = "" + if "fudai_detail" not in v: + v["fudai_detail"] = None has_fudai = bool(v.get("has_fudai", False)) fudai_detail = v.get("fudai_detail") # 如果既没有标题也没有福袋信息,则跳过 diff --git a/douyin/reservation/douyin_reservation_handler_pkg/record_title.py b/douyin/reservation/douyin_reservation_handler_pkg/record_title.py index 5c08a7b..1ac2fe8 100644 --- a/douyin/reservation/douyin_reservation_handler_pkg/record_title.py +++ b/douyin/reservation/douyin_reservation_handler_pkg/record_title.py @@ -65,6 +65,14 @@ def _merge_room_info(base: dict[str, Any], extra: dict[str, Any]) -> dict[str, A if tr: base["title_raw"] = tr + # 福袋信息:True 不被 False 覆盖;但 False 也要显式保留,便于诊断映射表是否记录过。 + if "has_fudai" in extra and not bool(base.get("has_fudai", False)): + base["has_fudai"] = bool(extra.get("has_fudai", False)) + if "fudai_content" in extra and "fudai_content" not in base: + base["fudai_content"] = str(extra.get("fudai_content", "") or "") + if "fudai_detail" in extra and "fudai_detail" not in base: + base["fudai_detail"] = extra.get("fudai_detail") + # 福袋信息:只要任一来源有,尽量保留完整 if bool(extra.get("has_fudai", False)): base["has_fudai"] = True diff --git a/tests/test_fudai_button_state.py b/tests/test_fudai_button_state.py index 481889f..bb3ef6d 100644 --- a/tests/test_fudai_button_state.py +++ b/tests/test_fudai_button_state.py @@ -13,8 +13,9 @@ def test_decide_button_status_attended_for_notice_text() -> None: def test_apply_button_ocr_result_records_room_pos_when_enabled() -> None: + log_calls = [] analyser = SimpleNamespace(room_pos=12) - analyser._log_kv = lambda *args, **kwargs: None + analyser._log_kv = lambda *args, **kwargs: log_calls.append((args, kwargs)) apply_button_ocr_result( analyser, @@ -31,3 +32,8 @@ def test_apply_button_ocr_result_records_room_pos_when_enabled() -> None: ) assert analyser._fudai_button_room_pos == 12 + assert log_calls + args, kwargs = log_calls[-1] + assert args[0] == "按钮识别" + assert kwargs["decision"] == "button_ocr_state_only" + assert kwargs["attend_click_sent"] is False diff --git a/tests/test_logging_simple_mode.py b/tests/test_logging_simple_mode.py index d4befa0..6a9a9e2 100644 --- a/tests/test_logging_simple_mode.py +++ b/tests/test_logging_simple_mode.py @@ -171,6 +171,57 @@ def test_simple_suppresses_ocr_summary_and_validation_noise(capsys): assert capsys.readouterr().out == "" +def test_simple_actions_log_skips_fudai_probe_noise(monkeypatch, tmp_path, capsys): + import douyin.core.path_config as path_config + + monkeypatch.setattr(path_config, "get_logs_dir", lambda: str(tmp_path)) + monkeypatch.setattr( + path_config, "get_actions_log_path", lambda: str(tmp_path / "actions.log") + ) + + analyser = _analyser() + _enhanced_log_kv_simple( + analyser, + "[福袋查找] 检测完成", + "[福袋查找] 检测完成", + "INFO", + ["[2026-06-18 04:35:40.000]", "[INFO]", "[福袋查找]", "[福袋查找] 检测完成"], + "2026-06-18 04:35:40.000", + {"elapsed_ms": 10.0}, + False, + "", + ) + + capsys.readouterr() + assert not (tmp_path / "actions.log").exists() + + +def test_simple_actions_log_keeps_real_attend_result(monkeypatch, tmp_path, capsys): + import douyin.core.path_config as path_config + + monkeypatch.setattr(path_config, "get_logs_dir", lambda: str(tmp_path)) + monkeypatch.setattr( + path_config, "get_actions_log_path", lambda: str(tmp_path / "actions.log") + ) + + analyser = _analyser() + _enhanced_log_kv_simple( + analyser, + "参与", + "参与成功,设置等待开奖标志", + "INFO", + ["[2026-06-18 04:35:40.000]", "[INFO]", "[参与]", "参与成功,设置等待开奖标志"], + "2026-06-18 04:35:40.000", + {}, + False, + "", + ) + + capsys.readouterr() + actions_text = (tmp_path / "actions.log").read_text(encoding="utf-8-sig") + assert "参与成功,设置等待开奖标志" in actions_text + + def test_enhanced_log_preserves_simple_and_detail_files(monkeypatch, tmp_path): import douyin.core.path_config as path_config diff --git a/tests/test_record_title_mapping.py b/tests/test_record_title_mapping.py index 076a551..4fbeb0c 100644 --- a/tests/test_record_title_mapping.py +++ b/tests/test_record_title_mapping.py @@ -61,6 +61,19 @@ def test_duplicate_titles_are_merged_to_smaller_pos(): assert merged.get("merged_positions") == [1, 2] +def test_merge_room_info_keeps_explicit_no_fudai_state(): + from douyin.reservation.douyin_reservation_handler_pkg import record_title as mod + + merged = mod._merge_room_info( + {"title": "直播间A"}, + {"has_fudai": False, "fudai_content": "", "fudai_detail": None}, + ) + + assert merged["has_fudai"] is False + assert merged["fudai_content"] == "" + assert merged["fudai_detail"] is None + + def test_same_room_streak_triggers_reset_and_consumes_swipe(): from douyin.reservation.douyin_reservation_handler_pkg import record_title as mod @@ -97,4 +110,3 @@ def test_same_room_streak_triggers_reset_and_consumes_swipe(): # 回顶逻辑不应清空映射表 assert isinstance(a._room_pos_to_title_map, dict) assert len(a._room_pos_to_title_map) >= 1 -