Keep task OCR out of fudai detail critical path

This commit is contained in:
Your Name committed 2026-06-19 14:12:23 +08:00
1 parent aa3690fecf
commit 6b987fd04f
4 files changed
+113 -5

No files matched your search

@@ -82,6 +82,7 @@ def apply_fudai_defaults(self: Any) -> None:
self.fudai_detail_ocr_button_enabled = True
self.fudai_detail_ocr_participants_enabled = False
self.fudai_detail_ocr_live_title_enabled = False
self.fudai_detail_task_ocr_in_critical_path_enabled = False
self.pixel_detect_fudai_icon_enabled = True
self.pixel_detect_coupon_popup_enabled = True
self.pixel_detect_shopping_redpacket_enabled = True
+65 -5
View File
@@ -3,6 +3,7 @@ from __future__ import annotations
import os
import time
import threading
import json
from typing import Tuple, Any, Optional, Dict, List
from douyin.utils.douyin_utils import extract_prize_value_yuan
@@ -112,6 +113,54 @@ def _format_enabled_ocr(ocr_switches: Any) -> str:
return ",".join(enabled) if enabled else "无"
def _coerce_bool_setting(value: Any, default: bool) -> bool:
if isinstance(value, bool):
return value
if isinstance(value, (int, float)):
return bool(value)
if value is None:
return default
text = str(value).strip().lower()
if text in ("1", "true", "yes", "on", "y"):
return True
if text in ("0", "false", "no", "off", "n", ""):
return False
return default
def _get_bool_setting(analyser: Any, key: str, default: bool = False) -> bool:
"""Read a boolean analyser/config setting without making config mandatory."""
try:
if hasattr(analyser, key):
return _coerce_bool_setting(getattr(analyser, key), default)
except Exception:
pass
try:
cfg_path = path_config.get_config_path()
if cfg_path and os.path.exists(cfg_path):
with open(cfg_path, "r", encoding="utf-8") as f:
cfg = json.load(f)
if isinstance(cfg, dict) and key in cfg:
return _coerce_bool_setting(cfg.get(key), default)
except Exception:
pass
return default
def _should_schedule_task_list_ocr(analyser: Any, ocr_switches: Any) -> bool:
"""Keep the expensive task-list OCR out of the detail critical path by default."""
try:
if not bool(getattr(ocr_switches, "task", False)):
return False
except Exception:
return False
return _get_bool_setting(
analyser,
"fudai_detail_task_ocr_in_critical_path_enabled",
False,
)
def _slowest_ocr_task(durations: Dict[str, Any]) -> str:
if not durations:
return ""
@@ -691,18 +740,29 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo
# 准备任务列表区域的截图(用于并行OCR)
try:
# 保存任务列表区域的调试截图(仅在配置启用时)
task_list_ocr_in_critical_path = _should_schedule_task_list_ocr(
analyser,
ocr_switches,
)
# 保存任务列表区域的调试截图(仅在配置启用且进入关键路径时)
task_list_box = tuple(profile.get("task_list") or (50, 1621, 1032, 1960))
detail_debug_regions.append(
{
"key": "task",
"name": "任务",
"coords": task_list_box,
"enabled": bool(ocr_switches.task),
"enabled": bool(task_list_ocr_in_critical_path),
}
)
if ocr_switches.task:
if task_list_ocr_in_critical_path:
analyser.cut_pic(*task_list_box, "", "福袋任务列表", analyser.y_pianyi)
elif ocr_switches.task:
analyser._log_kv(
"性能",
"跳过任务列表OCR关键路径",
reason="task_list_ocr_not_in_critical_path",
enable_key="fudai_detail_task_ocr_in_critical_path_enabled",
)
except Exception as e_inner: # pyright: ignore[reportTooBroadExceptionType]
# 任务列表截图失败不影响后续流程,记录日志即可
try:
@@ -818,7 +878,7 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo
executor = _get_fudai_ocr_executor(optimal_workers)
task_funcs = {}
if ocr_switches.task:
if _should_schedule_task_list_ocr(analyser, ocr_switches):
task_funcs["task_list"] = ocr_task_list
if ocr_switches.countdown:
task_funcs["countdown_mm"] = ocr_countdown_mm
@@ -1241,7 +1301,7 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo
executor = _get_fudai_ocr_executor(optimal_workers)
task_funcs = {}
if ocr_switches.task:
if _should_schedule_task_list_ocr(analyser, ocr_switches):
task_funcs["task_list"] = ocr_task_list_fallback
if ocr_switches.countdown:
task_funcs["countdown_mm"] = ocr_countdown_mm_fallback
+1
View File
@@ -412,6 +412,7 @@ class ConfigManager:
"fudai_detail_ocr_button_enabled": True,
"fudai_detail_ocr_participants_enabled": False,
"fudai_detail_ocr_live_title_enabled": False,
"fudai_detail_task_ocr_in_critical_path_enabled": False,
}.items():
if key in config:
setattr(analyser, key, bool(config.get(key, default)))
+46
View File
@@ -258,6 +258,52 @@ def test_code_defaults_include_v2_ocr_keys():
assert defaults.live_title is False
def test_task_list_ocr_is_not_in_detail_critical_path_by_default(monkeypatch):
from douyin.fudai.douyin_fudai_contain_impl import (
_should_schedule_task_list_ocr,
)
from douyin.fudai.douyin_fudai_core_pkg.ocr_switches import (
resolve_fudai_detail_ocr_switches,
)
monkeypatch.setattr(
"douyin.core.path_config.get_config_path",
lambda: "__missing__.json",
)
switches = resolve_fudai_detail_ocr_switches(
{"fudai_detail_ocr_task_enabled": True}
)
assert _should_schedule_task_list_ocr(SimpleNamespace(), switches) is False
def test_task_list_ocr_critical_path_requires_explicit_opt_in(monkeypatch):
from douyin.fudai.douyin_fudai_contain_impl import (
_should_schedule_task_list_ocr,
)
from douyin.fudai.douyin_fudai_core_pkg.ocr_switches import (
resolve_fudai_detail_ocr_switches,
)
monkeypatch.setattr(
"douyin.core.path_config.get_config_path",
lambda: "__missing__.json",
)
switches = resolve_fudai_detail_ocr_switches(
{"fudai_detail_ocr_task_enabled": True}
)
disabled_switches = resolve_fudai_detail_ocr_switches(
{"fudai_detail_ocr_task_enabled": False}
)
analyser = SimpleNamespace(
fudai_detail_task_ocr_in_critical_path_enabled=True,
)
assert _should_schedule_task_list_ocr(analyser, switches) is True
assert _should_schedule_task_list_ocr(analyser, disabled_switches) is False
def test_config_manager_loads_fudai_detail_ui_and_ocr_switches(tmp_path):
from douyin.utils.douyin_config import ConfigManager