将 3 个独立的日志记录块抽到独立模块: - log_fudai_found_and_metrics(约 52 行):命中后写"进入直播间→找到福袋"耗时统计 + 命中调试 - log_loop_progress(约 26 行):每 3 次循环记录一次轻量调试信息 - log_no_fudai_found(约 13 行):未找到时记录总耗时与循环次数 finder.py: 435 → 372 行 (-63) 新增 finder_loop_logging.py: 131 行 finder.py 跌破 400 行(1438 → 372,本会话累计削减 1066 行 = -74%) 验证: - pytest 1013 passed - compile OK Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
132 lines
3.9 KiB
Python
132 lines
3.9 KiB
Python
"""find_fudai 辅助:循环中的调试日志
|
|
|
|
从 douyin/detection/detection_pkg/finder.py:check_have_fudai_with_ready_screenshot 中提取。
|
|
- log_fudai_found_and_metrics:命中福袋后记录"进入直播间→找到福袋"耗时统计 + 命中调试信息
|
|
- log_loop_progress:每 3 次循环记录一次轻量调试信息
|
|
- log_no_fudai_found:未找到时记录总耗时与循环次数
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import time
|
|
from typing import Any
|
|
|
|
|
|
def log_fudai_found_and_metrics(
|
|
analyser: Any,
|
|
detected_x,
|
|
func_start: float,
|
|
loop: int,
|
|
ry: int,
|
|
y_pianyi_val: int,
|
|
) -> None:
|
|
"""命中福袋后:记录"进入直播间→找到福袋"耗时统计 + 命中调试信息。"""
|
|
# 统计:每次"进入直播间"后首次找到福袋图标耗时
|
|
try:
|
|
enter_ts = float(getattr(analyser, "_enter_live_ts", 0.0) or 0.0)
|
|
except Exception:
|
|
enter_ts = 0.0
|
|
try:
|
|
already = bool(getattr(analyser, "_enter_live_fudai_logged", False))
|
|
except Exception:
|
|
already = False
|
|
if (enter_ts > 0) and (not already):
|
|
try:
|
|
dt = float(time.time() - enter_ts)
|
|
except Exception:
|
|
dt = -1.0
|
|
if dt >= 0:
|
|
try:
|
|
reason = str(getattr(analyser, "_enter_live_reason", "") or "")
|
|
except Exception:
|
|
reason = ""
|
|
try:
|
|
analyser._enter_live_fudai_logged = True
|
|
except Exception:
|
|
pass
|
|
try:
|
|
analyser._log_kv(
|
|
"性能",
|
|
"进入直播间后找到福袋耗时",
|
|
seconds=round(dt, 2),
|
|
enter_reason=reason,
|
|
fudai_x=int(detected_x or 0),
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
# 命中时记录调试信息
|
|
try:
|
|
elapsed_ms = int((time.time() - func_start) * 1000)
|
|
scan_y_for_log = 403 * ry // 2400 + y_pianyi_val
|
|
analyser._log_kv(
|
|
"[找福袋图标] 命中",
|
|
loop=loop,
|
|
elapsed_ms=elapsed_ms,
|
|
scan_y=scan_y_for_log,
|
|
x=detected_x,
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
def log_loop_progress(
|
|
analyser: Any,
|
|
loop: int,
|
|
func_start: float,
|
|
pic_new,
|
|
width: int,
|
|
height: int,
|
|
scan_x_start: int,
|
|
scan_x_end: int,
|
|
scan_y: int,
|
|
consecutive_failures: int,
|
|
) -> None:
|
|
"""每 3 次循环记录一次轻量调试信息(帮助诊断为何未命中)。"""
|
|
if loop % 3 != 0:
|
|
return
|
|
try:
|
|
elapsed_ms = int((time.time() - func_start) * 1000)
|
|
sample_color = None
|
|
try:
|
|
mid_x = (scan_x_start + scan_x_end) // 2
|
|
if 0 <= scan_y < height and 0 <= mid_x < width:
|
|
pix_sample = pic_new.load()
|
|
sample_color = pix_sample[mid_x, scan_y]
|
|
except Exception:
|
|
sample_color = None
|
|
analyser._log_kv(
|
|
"[找福袋图标] 循环中",
|
|
loop=loop,
|
|
elapsed_ms=elapsed_ms,
|
|
scan_y=scan_y,
|
|
scan_x_start=scan_x_start,
|
|
scan_x_end=scan_x_end,
|
|
sample_color=(
|
|
str(sample_color) if sample_color is not None else "None"
|
|
),
|
|
consecutive_failures=consecutive_failures,
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
def log_no_fudai_found(
|
|
analyser: Any,
|
|
func_start: float,
|
|
loop: int,
|
|
max_wait_s: float,
|
|
retry_delay_s: float,
|
|
) -> None:
|
|
"""像素检测未找到时记录总耗时与循环次数。"""
|
|
try:
|
|
total_elapsed_ms = int((time.time() - func_start) * 1000)
|
|
analyser._log_kv(
|
|
"[找福袋图标] 未找到福袋图标",
|
|
elapsed_ms=total_elapsed_ms,
|
|
total_loops=loop,
|
|
max_wait_s=max_wait_s,
|
|
retry_delay_s=retry_delay_s,
|
|
)
|
|
except Exception:
|
|
pass
|