process_reservation_attend_window 中两个互不依赖的子块抽到 attend_window_helpers: - compute_dynamic_ahead:根据回退步数 + 单步延迟动态计算提前进入参与窗口秒数(纯函数) - log_attend_window_check_details:输出锁定期 / 时间窗口诊断结构化日志 新增 13 个单元测试覆盖各种边界(零步 / 截断 / 大延迟 / 异常兜底 / 锁定期内外)。 1050 → 1063 用例全过。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
105 lines
3.5 KiB
Python
105 lines
3.5 KiB
Python
"""attend_window 内部辅助函数。
|
||
|
||
把 process_reservation_attend_window 中纯计算 + 纯日志的子块抽出来:
|
||
- compute_dynamic_ahead:根据回退步数和延迟动态计算"提前进入参与窗口"的秒数
|
||
- log_attend_window_check_details:记录锁定期 / 时间窗口诊断的结构化日志
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
from datetime import datetime
|
||
from typing import Any, Dict, Tuple
|
||
|
||
|
||
def compute_dynamic_ahead(
|
||
analyser: Any,
|
||
target: Dict[str, Any],
|
||
room_pos: int,
|
||
reservation_back_swipe_delay_s: float,
|
||
) -> Tuple[int, int]:
|
||
"""根据回退步数动态算出"参与提前量"。
|
||
|
||
返回 (dynamic_ahead, est_back_times):
|
||
- dynamic_ahead:建议提前进入参与窗口的秒数
|
||
- est_back_times:估算的回退步数(用于日志)
|
||
"""
|
||
try:
|
||
join_ahead = float(
|
||
getattr(analyser, "high_value_fudai_join_ahead_seconds", 100) or 100
|
||
)
|
||
except Exception:
|
||
join_ahead = 100.0
|
||
|
||
dynamic_ahead = int(join_ahead)
|
||
est_back_times = 0
|
||
try:
|
||
target_pos = int(target.get("pos_seen") or 0)
|
||
est_back_times = max(0, int(room_pos) - target_pos)
|
||
try:
|
||
max_back = int(
|
||
getattr(analyser, "high_value_fudai_max_swipe_back", 60) or 60
|
||
)
|
||
except Exception:
|
||
max_back = 60
|
||
if max_back > 0:
|
||
est_back_times = min(est_back_times, max_back)
|
||
delay_s = float(reservation_back_swipe_delay_s or 0.6)
|
||
dynamic_ahead = int(max(join_ahead, est_back_times * delay_s + 18))
|
||
except Exception:
|
||
dynamic_ahead = int(join_ahead)
|
||
return dynamic_ahead, est_back_times
|
||
|
||
|
||
def _ts_to_str(ts: float) -> str:
|
||
if ts <= 0:
|
||
return "无效"
|
||
try:
|
||
return datetime.fromtimestamp(ts).strftime("%Y-%m-%d %H:%M:%S")
|
||
except Exception:
|
||
return "无效"
|
||
|
||
|
||
def log_attend_window_check_details(
|
||
analyser: Any,
|
||
*,
|
||
now_ts: float,
|
||
focus_until: float,
|
||
ft: float,
|
||
dynamic_ahead: int,
|
||
join_ahead: float,
|
||
poll_before: float,
|
||
est_back_times: int,
|
||
) -> None:
|
||
"""记录锁定期 + 时间窗口诊断的详细日志。"""
|
||
try:
|
||
time_window_start_ts = ft - dynamic_ahead
|
||
time_window_end_ts = ft + max(0.0, poll_before)
|
||
|
||
analyser._log_kv(
|
||
"预约",
|
||
"[预约] process_reservation_attend_window 详细检查",
|
||
now_ts=int(now_ts),
|
||
now_str=_ts_to_str(now_ts),
|
||
focus_until=int(focus_until) if focus_until > 0 else 0,
|
||
target_future_ts=int(ft),
|
||
target_future_str=_ts_to_str(ft),
|
||
dynamic_ahead=dynamic_ahead,
|
||
join_ahead=join_ahead,
|
||
poll_before=poll_before,
|
||
est_back_times=est_back_times,
|
||
time_window_start=int(time_window_start_ts),
|
||
time_window_start_str=_ts_to_str(time_window_start_ts),
|
||
time_window_end=int(time_window_end_ts),
|
||
time_window_end_str=_ts_to_str(time_window_end_ts),
|
||
remain_seconds_to_open=int(ft - now_ts) if ft > now_ts else 0,
|
||
remain_seconds_to_window=(
|
||
int(time_window_start_ts - now_ts)
|
||
if time_window_start_ts > now_ts
|
||
else 0
|
||
),
|
||
in_lock_period=bool(focus_until > 0 and now_ts < focus_until),
|
||
in_time_window=bool(now_ts >= time_window_start_ts),
|
||
passed_time_window=bool(now_ts > time_window_end_ts),
|
||
)
|
||
except Exception:
|
||
pass
|