fix: 探测整批代理加上总时限,学时翻页先判重再累加

This commit is contained in:
2026-11-16 17:51:07 +08:00
parent 36b15cce7b
commit 6bc1cd027c
3 changed files with 39 additions and 39 deletions
+4 -17
View File
@@ -1065,9 +1065,9 @@ class StudySession:
async def month_history_minutes(self, now: datetime | None = None) -> int: async def month_history_minutes(self, now: datetime | None = None) -> int:
from euniv.client import ( from euniv.client import (
HISTORY_PAGE_SIZE, HISTORY_PAGE_SIZE,
fold_month_minutes,
history_items_from_payload, history_items_from_payload,
history_search_body, history_search_body,
take_history_page,
) )
from euniv.study_window import CN_TZ from euniv.study_window import CN_TZ
@@ -1075,7 +1075,7 @@ class StudySession:
year, month = current.year, current.month year, month = current.year, current.month
offset = 0 offset = 0
total = 0 total = 0
first_mark: tuple | None = None seen: set[tuple] = set()
for _ in range(50): for _ in range(50):
resp = await self._api( resp = await self._api(
"POST", "POST",
@@ -1087,22 +1087,9 @@ class StudySession:
if offset == 0: if offset == 0:
raise RuntimeError("学时历史没有获取到") raise RuntimeError("学时历史没有获取到")
return total return total
if not rows: add, stop = take_history_page(rows, seen, year, month, HISTORY_PAGE_SIZE)
return total
add, done = fold_month_minutes(rows, year, month)
total += add total += add
if done or len(rows) != HISTORY_PAGE_SIZE: if stop:
return total
mark = (
len(rows),
rows[0].get("studied_at"),
rows[0].get("title"),
rows[-1].get("studied_at"),
rows[-1].get("title"),
)
if offset == 0:
first_mark = mark
elif mark == first_mark:
return total return total
offset += HISTORY_PAGE_SIZE offset += HISTORY_PAGE_SIZE
return total return total
+20 -14
View File
@@ -439,6 +439,24 @@ def history_hour_totals(items: list[dict], now: datetime | None = None) -> dict[
} }
def take_history_page(rows: list, seen: set[tuple], year: int, month: int, page_size: int) -> tuple[int, bool]:
"""先判断这一页是否重复。返回 (计入的分钟, 是否停止翻页)。"""
if not rows:
return 0, True
mark = (
len(rows),
rows[0].get("studied_at"),
rows[0].get("title"),
rows[-1].get("studied_at"),
rows[-1].get("title"),
)
if mark in seen:
return 0, True
seen.add(mark)
add, done = fold_month_minutes(rows, year, month)
return add, bool(done or len(rows) != page_size)
def accumulate_month_minutes(fetch_page, year: int, month: int, page_size: int = HISTORY_PAGE_SIZE) -> tuple[int, bool]: def accumulate_month_minutes(fetch_page, year: int, month: int, page_size: int = HISTORY_PAGE_SIZE) -> tuple[int, bool]:
"""倒序分页累加本月分钟,遇到上个月即停。重复页直接返回,不再累加。""" """倒序分页累加本月分钟,遇到上个月即停。重复页直接返回,不再累加。"""
offset = 0 offset = 0
@@ -450,21 +468,9 @@ def accumulate_month_minutes(fetch_page, year: int, month: int, page_size: int =
if offset == 0: if offset == 0:
raise RuntimeError("学时历史没有获取到") raise RuntimeError("学时历史没有获取到")
return total, False return total, False
if not rows: add, stop = take_history_page(rows, seen, year, month, page_size)
return total, False
mark = (
len(rows),
rows[0].get("studied_at"),
rows[0].get("title"),
rows[-1].get("studied_at"),
rows[-1].get("title"),
)
if mark in seen:
return total, False
seen.add(mark)
add, done = fold_month_minutes(rows, year, month)
total += add total += add
if done or len(rows) != page_size: if stop:
return total, False return total, False
offset += page_size offset += page_size
return total, True return total, True
+9 -2
View File
@@ -31,6 +31,7 @@ LATENCY_LIMIT = 3.0
LATENCY_LIMIT_MS = 3000 LATENCY_LIMIT_MS = 3000
PROBE_CONCURRENCY = 16 PROBE_CONCURRENCY = 16
MAX_PROBES = 80 MAX_PROBES = 80
PROBE_BATCH_TIMEOUT = 20.0
ALIVE_REPROBE_SLOTS = 20 ALIVE_REPROBE_SLOTS = 20
MAX_SWITCHES = 8 MAX_SWITCHES = 8
PROBE_URL = EUNIV + "/" PROBE_URL = EUNIV + "/"
@@ -558,18 +559,24 @@ def _probe_many(nodes: list[tuple[int, str, str, int]]) -> list[tuple[int, bool,
return [] return []
workers = min(PROBE_CONCURRENCY, len(nodes)) workers = min(PROBE_CONCURRENCY, len(nodes))
results: list[tuple[int, bool, int, str]] = [] results: list[tuple[int, bool, int, str]] = []
with ThreadPoolExecutor(max_workers=workers) as pool: pool = ThreadPoolExecutor(max_workers=workers)
try:
futures = { futures = {
pool.submit(probe, scheme, host, port): node_id pool.submit(probe, scheme, host, port): node_id
for node_id, scheme, host, port in nodes for node_id, scheme, host, port in nodes
} }
for future in as_completed(futures): try:
for future in as_completed(futures, timeout=PROBE_BATCH_TIMEOUT):
node_id = futures[future] node_id = futures[future]
try: try:
ok, latency, reason = future.result() ok, latency, reason = future.result()
except Exception as exc: except Exception as exc:
ok, latency, reason = False, 0, _short_reason(exc) ok, latency, reason = False, 0, _short_reason(exc)
results.append((node_id, ok, latency, reason)) results.append((node_id, ok, latency, reason))
except TimeoutError:
pass
finally:
pool.shutdown(wait=False, cancel_futures=True)
return results return results