fix: 探测整批代理加上总时限,学时翻页先判重再累加
This commit is contained in:
@@ -1065,9 +1065,9 @@ class StudySession:
|
||||
async def month_history_minutes(self, now: datetime | None = None) -> int:
|
||||
from euniv.client import (
|
||||
HISTORY_PAGE_SIZE,
|
||||
fold_month_minutes,
|
||||
history_items_from_payload,
|
||||
history_search_body,
|
||||
take_history_page,
|
||||
)
|
||||
from euniv.study_window import CN_TZ
|
||||
|
||||
@@ -1075,7 +1075,7 @@ class StudySession:
|
||||
year, month = current.year, current.month
|
||||
offset = 0
|
||||
total = 0
|
||||
first_mark: tuple | None = None
|
||||
seen: set[tuple] = set()
|
||||
for _ in range(50):
|
||||
resp = await self._api(
|
||||
"POST",
|
||||
@@ -1087,22 +1087,9 @@ class StudySession:
|
||||
if offset == 0:
|
||||
raise RuntimeError("学时历史没有获取到")
|
||||
return total
|
||||
if not rows:
|
||||
return total
|
||||
add, done = fold_month_minutes(rows, year, month)
|
||||
add, stop = take_history_page(rows, seen, year, month, HISTORY_PAGE_SIZE)
|
||||
total += add
|
||||
if done or len(rows) != HISTORY_PAGE_SIZE:
|
||||
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:
|
||||
if stop:
|
||||
return total
|
||||
offset += HISTORY_PAGE_SIZE
|
||||
return total
|
||||
|
||||
+20
-14
@@ -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]:
|
||||
"""倒序分页累加本月分钟,遇到上个月即停。重复页直接返回,不再累加。"""
|
||||
offset = 0
|
||||
@@ -450,21 +468,9 @@ def accumulate_month_minutes(fetch_page, year: int, month: int, page_size: int =
|
||||
if offset == 0:
|
||||
raise RuntimeError("学时历史没有获取到")
|
||||
return total, False
|
||||
if not rows:
|
||||
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)
|
||||
add, stop = take_history_page(rows, seen, year, month, page_size)
|
||||
total += add
|
||||
if done or len(rows) != page_size:
|
||||
if stop:
|
||||
return total, False
|
||||
offset += page_size
|
||||
return total, True
|
||||
|
||||
@@ -31,6 +31,7 @@ LATENCY_LIMIT = 3.0
|
||||
LATENCY_LIMIT_MS = 3000
|
||||
PROBE_CONCURRENCY = 16
|
||||
MAX_PROBES = 80
|
||||
PROBE_BATCH_TIMEOUT = 20.0
|
||||
ALIVE_REPROBE_SLOTS = 20
|
||||
MAX_SWITCHES = 8
|
||||
PROBE_URL = EUNIV + "/"
|
||||
@@ -558,18 +559,24 @@ def _probe_many(nodes: list[tuple[int, str, str, int]]) -> list[tuple[int, bool,
|
||||
return []
|
||||
workers = min(PROBE_CONCURRENCY, len(nodes))
|
||||
results: list[tuple[int, bool, int, str]] = []
|
||||
with ThreadPoolExecutor(max_workers=workers) as pool:
|
||||
pool = ThreadPoolExecutor(max_workers=workers)
|
||||
try:
|
||||
futures = {
|
||||
pool.submit(probe, scheme, host, port): node_id
|
||||
for node_id, scheme, host, port in nodes
|
||||
}
|
||||
for future in as_completed(futures):
|
||||
node_id = futures[future]
|
||||
try:
|
||||
ok, latency, reason = future.result()
|
||||
except Exception as exc:
|
||||
ok, latency, reason = False, 0, _short_reason(exc)
|
||||
results.append((node_id, ok, latency, reason))
|
||||
try:
|
||||
for future in as_completed(futures, timeout=PROBE_BATCH_TIMEOUT):
|
||||
node_id = futures[future]
|
||||
try:
|
||||
ok, latency, reason = future.result()
|
||||
except Exception as exc:
|
||||
ok, latency, reason = False, 0, _short_reason(exc)
|
||||
results.append((node_id, ok, latency, reason))
|
||||
except TimeoutError:
|
||||
pass
|
||||
finally:
|
||||
pool.shutdown(wait=False, cancel_futures=True)
|
||||
return results
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user