feat: add summary configuration and sync controls
This commit is contained in:
@ -57,6 +57,8 @@ def sync_all_chats_for_day(
|
||||
start_ts: int | None = None,
|
||||
end_ts: int | None = None,
|
||||
chat_ids: list[str] | None = None,
|
||||
exclude_chat_ids: list[str] | None = None,
|
||||
force: bool = False,
|
||||
) -> dict[str, int]:
|
||||
"""Save one natural day from every visible INBOX chat; never print content."""
|
||||
if not 1 <= batch_size <= 200:
|
||||
@ -72,6 +74,9 @@ def sync_all_chats_for_day(
|
||||
if chat_ids:
|
||||
requested = {str(chat_id) for chat_id in chat_ids}
|
||||
chats = [chat for chat in chats if str(chat.get("id") or "") in requested]
|
||||
if exclude_chat_ids:
|
||||
excluded = {str(chat_id) for chat_id in exclude_chat_ids}
|
||||
chats = [chat for chat in chats if str(chat.get("id") or "") not in excluded]
|
||||
stats = {
|
||||
"chats": len(chats),
|
||||
"scanned": 0,
|
||||
@ -80,6 +85,7 @@ def sync_all_chats_for_day(
|
||||
"saved": 0,
|
||||
"failed": 0,
|
||||
"requests": 0,
|
||||
"skipped": 0,
|
||||
}
|
||||
|
||||
for chat in chats:
|
||||
@ -89,9 +95,31 @@ def sync_all_chats_for_day(
|
||||
if not chat_id or upper < floor:
|
||||
stats["empty"] += 1
|
||||
continue
|
||||
|
||||
progress = None
|
||||
if not force:
|
||||
progress = storage.get_sync_progress(chat_id)
|
||||
if progress and progress.last_synced_date == target_day.isoformat():
|
||||
stats["skipped"] += 1
|
||||
continue
|
||||
|
||||
# 如果目标日期完全早于已扫描范围,从断点继续往前拉,避免从最新position重扫
|
||||
if (
|
||||
progress
|
||||
and progress.min_scanned_time
|
||||
and end_ts <= progress.min_scanned_time
|
||||
and progress.min_scanned_position > floor
|
||||
):
|
||||
upper = min(upper, progress.min_scanned_position - 1)
|
||||
if upper < floor:
|
||||
stats["skipped"] += 1
|
||||
continue
|
||||
|
||||
stats["scanned"] += 1
|
||||
try:
|
||||
batches = 0
|
||||
min_position_scanned = upper
|
||||
min_time_scanned = 0
|
||||
while upper >= floor:
|
||||
if batches >= 100:
|
||||
raise HistoryError("单会话历史分页超过安全批次上限。")
|
||||
@ -104,6 +132,7 @@ def sync_all_chats_for_day(
|
||||
)
|
||||
stats["requests"] += 1
|
||||
batches += 1
|
||||
min_position_scanned = min(min_position_scanned, lower)
|
||||
timestamps = []
|
||||
for message in messages:
|
||||
timestamp = int(message.get("create_time") or 0)
|
||||
@ -122,9 +151,19 @@ def sync_all_chats_for_day(
|
||||
direction=direction,
|
||||
)
|
||||
stats["saved"] += int(bool(saved))
|
||||
if timestamps and min(timestamps) < start_ts:
|
||||
break
|
||||
if timestamps:
|
||||
batch_min_time = min(timestamps)
|
||||
if not min_time_scanned or batch_min_time < min_time_scanned:
|
||||
min_time_scanned = batch_min_time
|
||||
if batch_min_time < start_ts:
|
||||
break
|
||||
upper = lower - 1
|
||||
storage.update_sync_progress(
|
||||
chat_id,
|
||||
target_day.isoformat(),
|
||||
min_position_scanned,
|
||||
min_time_scanned,
|
||||
)
|
||||
except Exception: # noqa: BLE001 - isolate chat failures without exposing upstream data
|
||||
stats["failed"] += 1
|
||||
return stats
|
||||
|
||||
Reference in New Issue
Block a user