460 lines
17 KiB
Python
460 lines
17 KiB
Python
"""reservation_logic.add - add_reservation_core_logic 主入口。
|
|
|
|
往预约队列添加一条新预约:
|
|
- 去重 / 触发优先级排序 / 队列上限裁剪 / 状态写回 / 错误日志
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import time
|
|
from typing import Any, Callable, Dict, List, Optional
|
|
|
|
from ._common import (
|
|
_RESERVATION_ERROR_UTILS_AVAILABLE,
|
|
ReservationItem,
|
|
format_reservation_context,
|
|
log_reservation_error,
|
|
safe_write_state,
|
|
validate_reservation_item,
|
|
)
|
|
from .helpers import (
|
|
_calculate_string_similarity,
|
|
_extract_content_core,
|
|
_normalize_room_title_for_matching,
|
|
_now_ts,
|
|
_sort_reservations_inplace,
|
|
)
|
|
|
|
|
|
def add_reservation_core_logic(
|
|
reservations: List[ReservationItem],
|
|
item: ReservationItem,
|
|
max_reservations: Optional[int] = None,
|
|
analyser: Any = None,
|
|
write_state_callback: Optional[Callable[[], None]] = None,
|
|
) -> bool:
|
|
"""向预约队列追加/更新一条记录,并根据上限裁剪。
|
|
|
|
去重策略:
|
|
- key = (future_ts, content) 相同则视为同一条记录,保留价值更高的那条。
|
|
- 如果直播间名字相同(规范化后),且 (future_ts, content) 相同,也视为同一条记录。
|
|
|
|
返回:
|
|
bool: 如果队列已满且需要回到最早开奖的预约直播间等待,返回 True;否则返回 False。
|
|
"""
|
|
start_time = time.time()
|
|
step_start_time = start_time
|
|
|
|
ft = float(item.get("future_ts") or 0.0)
|
|
content = str(item.get("content") or "")
|
|
prize_value = float(item.get("prize_value_yuan") or 0.0)
|
|
room_title = str(item.get("room_title") or "")
|
|
room_title_normalized = _normalize_room_title_for_matching(room_title)
|
|
|
|
# 步骤1: 参数处理和规范化
|
|
try:
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] 步骤1: 参数处理和规范化",
|
|
log_type="OP",
|
|
elapsed_seconds=round(time.time() - step_start_time, 3),
|
|
)
|
|
except Exception:
|
|
pass
|
|
step_start_time = time.time()
|
|
|
|
# 添加函数入口日志
|
|
try:
|
|
from datetime import datetime
|
|
|
|
ft_str = (
|
|
datetime.fromtimestamp(ft).strftime("%Y-%m-%d %H:%M:%S")
|
|
if ft > 0
|
|
else "无效"
|
|
)
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] add_reservation_core_logic 函数入口",
|
|
log_type="OP",
|
|
开奖时间=ft_str if ft_str else "未知",
|
|
prize_value_yuan=prize_value,
|
|
room_title=room_title[:50] if room_title else "",
|
|
content=content[:50] if content else "",
|
|
queue_len_before=len(reservations),
|
|
max_reservations=max_reservations,
|
|
)
|
|
except Exception as e:
|
|
if _RESERVATION_ERROR_UTILS_AVAILABLE:
|
|
log_reservation_error(
|
|
analyser,
|
|
e,
|
|
format_reservation_context(item=item, reservations=reservations),
|
|
level="WARNING",
|
|
stage="记录函数入口日志",
|
|
)
|
|
else:
|
|
try:
|
|
analyser._log_kv(
|
|
"预约", "[预约] 记录函数入口日志失败", log_type="ERR", error=str(e)
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
# 步骤2: 查找是否已存在相同预约
|
|
try:
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] 步骤2: 开始查找是否已存在相同预约",
|
|
log_type="OP",
|
|
elapsed_seconds=round(time.time() - step_start_time, 3),
|
|
)
|
|
except Exception:
|
|
pass
|
|
step_start_time = time.time()
|
|
|
|
same_idx: Optional[int] = None
|
|
for idx, r in enumerate(reservations):
|
|
try:
|
|
r_ft = float(r.get("future_ts") or 0.0)
|
|
r_content = str(r.get("content") or "")
|
|
r_prize_value = float(r.get("prize_value_yuan") or 0.0)
|
|
r_room_title = str(r.get("room_title") or "")
|
|
r_room_title_normalized = _normalize_room_title_for_matching(r_room_title)
|
|
|
|
# 判断是否为同一个预约:
|
|
# 1. (future_ts, content) 相同,且直播间名字相同(规范化后)
|
|
# 2. 或者 (future_ts, content) 相同(兼容旧数据,没有直播间名字的情况)
|
|
# 3. 或者通过相似度匹配:直播间名称相似 + 开奖时间相近 + 奖品价值相同 + 内容核心部分相似
|
|
is_same_future_and_content = r_ft == ft and r_content == content
|
|
is_same_room_title = (
|
|
room_title_normalized
|
|
and r_room_title_normalized
|
|
and room_title_normalized == r_room_title_normalized
|
|
)
|
|
|
|
# 相似度匹配(处理OCR误差)
|
|
is_similar_room_title = False
|
|
is_similar_time = False
|
|
is_same_prize_value = False
|
|
is_similar_content = False
|
|
|
|
if room_title_normalized and r_room_title_normalized:
|
|
# 计算直播间名称相似度
|
|
room_title_similarity = _calculate_string_similarity(
|
|
room_title_normalized, r_room_title_normalized
|
|
)
|
|
# 如果一个是另一个的子串,或者相似度 >= 0.6,视为相似
|
|
is_similar_room_title = (
|
|
room_title_normalized in r_room_title_normalized
|
|
or r_room_title_normalized in room_title_normalized
|
|
or room_title_similarity >= 0.6
|
|
)
|
|
|
|
# 开奖时间相近(相差60秒内,放宽容忍度以处理OCR误差和实际时间差)
|
|
time_diff = abs(r_ft - ft)
|
|
is_similar_time = time_diff <= 60.0
|
|
|
|
# 奖品价值相同(允许0.01的误差)
|
|
prize_diff = abs(r_prize_value - prize_value)
|
|
is_same_prize_value = prize_diff < 0.01
|
|
|
|
# 内容核心部分相似
|
|
content_core = _extract_content_core(content)
|
|
r_content_core = _extract_content_core(r_content)
|
|
if content_core and r_content_core:
|
|
content_similarity = _calculate_string_similarity(
|
|
content_core, r_content_core
|
|
)
|
|
is_similar_content = content_similarity >= 0.7
|
|
|
|
# 判断是否为同一个预约
|
|
if is_same_future_and_content:
|
|
# 如果 (future_ts, content) 相同
|
|
if is_same_room_title:
|
|
# 直播间名字也相同(规范化后),确定是同一个预约
|
|
same_idx = idx
|
|
break
|
|
elif not room_title_normalized and not r_room_title_normalized:
|
|
# 都没有直播间名字,视为同一个预约(兼容旧数据)
|
|
same_idx = idx
|
|
break
|
|
# 如果只有一个有直播间名字,也视为同一个预约(兼容旧数据)
|
|
elif (room_title_normalized and not r_room_title_normalized) or (
|
|
not room_title_normalized and r_room_title_normalized
|
|
):
|
|
same_idx = idx
|
|
break
|
|
# 如果直播间名称相同(规范化后)且价值相同,即使时间差稍大也视为重复
|
|
elif is_same_room_title and is_same_prize_value and time_diff <= 300.0:
|
|
# 直播间名称相同且价值相同,时间差在5分钟内,视为重复
|
|
same_idx = idx
|
|
try:
|
|
if analyser:
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] 通过直播间名称和价值匹配识别为重复预约",
|
|
log_type="OP",
|
|
room_title_normalized=room_title_normalized,
|
|
time_diff=round(time_diff, 2),
|
|
prize_value_yuan=prize_value,
|
|
)
|
|
except Exception:
|
|
pass
|
|
break
|
|
elif (
|
|
is_similar_room_title
|
|
and is_similar_time
|
|
and is_same_prize_value
|
|
and (
|
|
is_similar_content
|
|
or (
|
|
# 如果内容核心均为空(OCR 未识别到内容),则允许仅凭直播间名称相似、时间接近和价值相同判定为重复
|
|
not content_core
|
|
and not r_content_core
|
|
)
|
|
)
|
|
):
|
|
# 通过相似度匹配判断为同一个预约(处理OCR误差)
|
|
same_idx = idx
|
|
try:
|
|
if analyser:
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] 通过相似度匹配识别为重复预约",
|
|
log_type="OP",
|
|
room_title_normalized=room_title_normalized,
|
|
r_room_title_normalized=r_room_title_normalized,
|
|
time_diff=round(time_diff, 2),
|
|
prize_diff=round(prize_diff, 2),
|
|
content_core=content_core[:50],
|
|
r_content_core=r_content_core[:50],
|
|
)
|
|
except Exception:
|
|
pass
|
|
break
|
|
except Exception:
|
|
continue
|
|
|
|
# 步骤3: 相似度匹配完成
|
|
try:
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] 步骤3: 相似度匹配完成",
|
|
log_type="OP",
|
|
same_idx=same_idx,
|
|
elapsed_seconds=round(time.time() - step_start_time, 3),
|
|
)
|
|
except Exception:
|
|
pass
|
|
step_start_time = time.time()
|
|
|
|
if same_idx is not None:
|
|
# 若新记录价值更高,则更新
|
|
try:
|
|
old_val = float(reservations[same_idx].get("prize_value_yuan") or 0.0)
|
|
except Exception:
|
|
old_val = 0.0
|
|
if prize_value > old_val:
|
|
try:
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] 检测到重复预约,更新为更高价值",
|
|
same_idx=same_idx,
|
|
old_value_yuan=old_val,
|
|
new_value_yuan=prize_value,
|
|
开奖时间=(
|
|
datetime.fromtimestamp(ft).strftime("%Y-%m-%d %H:%M:%S")
|
|
if ft > 0
|
|
else "未知"
|
|
),
|
|
)
|
|
except Exception:
|
|
pass
|
|
reservations[same_idx].update(dict(item))
|
|
else:
|
|
try:
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] 检测到重复预约,但新价值不高于旧值,保留旧记录",
|
|
same_idx=same_idx,
|
|
old_value_yuan=old_val,
|
|
new_value_yuan=prize_value,
|
|
开奖时间=(
|
|
datetime.fromtimestamp(ft).strftime("%Y-%m-%d %H:%M:%S")
|
|
if ft > 0
|
|
else "未知"
|
|
),
|
|
)
|
|
except Exception:
|
|
pass
|
|
else:
|
|
try:
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] 添加新预约到队列",
|
|
开奖时间=(
|
|
datetime.fromtimestamp(ft).strftime("%Y-%m-%d %H:%M:%S")
|
|
if ft > 0
|
|
else "未知"
|
|
),
|
|
prize_value_yuan=prize_value,
|
|
queue_len_before=len(reservations),
|
|
)
|
|
except Exception:
|
|
pass
|
|
reservations.append(dict(item))
|
|
|
|
# 步骤4: 添加/更新预约完成
|
|
try:
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] 步骤4: 添加/更新预约完成",
|
|
log_type="OP",
|
|
same_idx=same_idx,
|
|
queue_len_after=len(reservations),
|
|
elapsed_seconds=round(time.time() - step_start_time, 3),
|
|
)
|
|
except Exception:
|
|
pass
|
|
step_start_time = time.time()
|
|
|
|
# 排序 + 按上限裁剪
|
|
_sort_reservations_inplace(reservations)
|
|
|
|
# 步骤5: 排序完成
|
|
try:
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] 步骤5: 排序完成",
|
|
log_type="OP",
|
|
elapsed_seconds=round(time.time() - step_start_time, 3),
|
|
)
|
|
except Exception:
|
|
pass
|
|
step_start_time = time.time()
|
|
|
|
queue_full = False
|
|
if (
|
|
max_reservations is not None
|
|
and max_reservations > 0
|
|
and len(reservations) > max_reservations
|
|
):
|
|
# 记录被丢弃的记录信息
|
|
dropped_items = reservations[max_reservations:]
|
|
for dropped in dropped_items:
|
|
try:
|
|
dropped_value = float(dropped.get("prize_value_yuan") or 0.0)
|
|
dropped_future_ts = float(dropped.get("future_ts") or 0.0)
|
|
dropped_content = str(dropped.get("content") or "")[:50]
|
|
try:
|
|
from datetime import datetime
|
|
|
|
dropped_future_str = datetime.fromtimestamp(
|
|
dropped_future_ts
|
|
).strftime("%Y-%m-%d %H:%M:%S")
|
|
except Exception:
|
|
dropped_future_str = str(dropped_future_ts)
|
|
|
|
if analyser:
|
|
try:
|
|
analyser._log_kv(
|
|
"预约",
|
|
"预约队列已满,丢弃最低优先级记录",
|
|
cap=max_reservations,
|
|
dropped_value_yuan=dropped_value,
|
|
dropped_future_ts=int(dropped_future_ts),
|
|
dropped_future_str=dropped_future_str,
|
|
dropped_content=dropped_content,
|
|
queue_len_before=len(reservations),
|
|
)
|
|
except Exception:
|
|
pass
|
|
except Exception:
|
|
pass
|
|
|
|
# 按当前排序规则,队尾是"优先级最低"的记录
|
|
del reservations[max_reservations:]
|
|
queue_full = True
|
|
|
|
try:
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] 预约队列已满,已裁剪到上限",
|
|
max_reservations=max_reservations,
|
|
queue_len_after=len(reservations),
|
|
dropped_count=len(dropped_items),
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
# 如果队列已满,写入状态文件
|
|
if write_state_callback:
|
|
if _RESERVATION_ERROR_UTILS_AVAILABLE:
|
|
safe_write_state(
|
|
write_state_callback,
|
|
analyser,
|
|
format_reservation_context(
|
|
item=item,
|
|
reservations=reservations,
|
|
queue_full=True,
|
|
dropped_count=len(dropped_items) if dropped_items else 0,
|
|
),
|
|
)
|
|
else:
|
|
try:
|
|
write_state_callback()
|
|
except Exception as e:
|
|
try:
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] 写入状态文件失败(队列已满)",
|
|
error=str(e),
|
|
error_type=type(e).__name__,
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
# 步骤6: 队列裁剪和状态写入完成
|
|
try:
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] 步骤6: 队列裁剪和状态写入完成",
|
|
log_type="OP",
|
|
queue_full=queue_full,
|
|
queue_len_final=len(reservations),
|
|
elapsed_seconds=round(time.time() - step_start_time, 3),
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
# 添加函数完成日志
|
|
try:
|
|
elapsed = time.time() - start_time
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] add_reservation_core_logic 完成",
|
|
log_type="OP",
|
|
queue_len_after=len(reservations),
|
|
queue_full=queue_full,
|
|
elapsed_seconds=round(elapsed, 3),
|
|
)
|
|
except Exception as e:
|
|
if _RESERVATION_ERROR_UTILS_AVAILABLE:
|
|
log_reservation_error(
|
|
analyser,
|
|
e,
|
|
format_reservation_context(item=item, reservations=reservations),
|
|
level="WARNING",
|
|
stage="记录函数完成日志",
|
|
)
|
|
else:
|
|
try:
|
|
analyser._log_kv(
|
|
"预约", "[预约] 记录函数完成日志失败", log_type="ERR", error=str(e)
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
return queue_full
|
|
|
|
|