diff --git a/backend/euniv/autostudy.py b/backend/euniv/autostudy.py index fc91a9e..9e45f26 100644 --- a/backend/euniv/autostudy.py +++ b/backend/euniv/autostudy.py @@ -213,14 +213,11 @@ class WorkerView: def after_beat(self, progress: float, **kw: object) -> None: self.sent += 1 - planned = _count_total(self.sent, progress, self._origin) - if self.total <= 0: - self.total = planned - elif progress >= 100: + if progress >= 100: self.total = self.sent - elif planned > self.total: - self.total = planned - if self.sent > self.total: + elif self.total <= 0: + self.total = _count_total(self.sent, progress, self._origin) + if self.total > 0 and self.sent > self.total: self.total = self.sent self.update(progress=progress, sent=self.sent, total=self.total, **kw) @@ -608,8 +605,18 @@ async def _open_play(session: "StudySession", path: str) -> dict: return {} +def _planned_beats(cap: int, position: int, step: int) -> int: + """用课件时长和每拍申报的秒数算出固定总次数,不随进度回写变大。""" + if cap <= 0 or step <= 0: + return 0 + remaining = max(0, int(cap) - max(0, int(position))) + if remaining <= 0: + return 1 + return max(1, math.ceil(remaining / int(step))) + + def _count_total(sent: int, progress: float, origin: float = 0.0) -> int: - """按开课进度估算剩余心跳数。总数在 WorkerView 里冻结,避免每拍都变。""" + """没有课件时长时,用第一次看得到的进度估算总次数,之后不再上调。""" if sent <= 0: return 0 if progress >= 100: @@ -1173,6 +1180,11 @@ async def _study_video(session: StudySession, kid: str, pkg_id: str, if step <= 0: step = 30 view = 0 + cap = int(getattr(session, "_video_cap", 0) or 0) + if worker is not None and worker.total <= 0 and cap > 0: + start_at = _whole((snapshot or {}).get("viewSchedule"), 0) + worker.total = _planned_beats(cap, start_at, step) + worker.update(total=worker.total, sent=worker.sent) started = False stall = _Stall() pending = snapshot diff --git a/backend/tests/test_autostudy.py b/backend/tests/test_autostudy.py index 59094a9..2c6b164 100644 --- a/backend/tests/test_autostudy.py +++ b/backend/tests/test_autostudy.py @@ -13,6 +13,7 @@ from euniv.autostudy import ( _clear_user_status, _completed, _count_total, + _planned_beats, _finish_user, _interval_plan, _jitter, @@ -174,6 +175,29 @@ def test_worker_view_freezes_total_after_first_beat() -> None: _clear_user_status("u-total") +def test_worker_view_does_not_raise_total_when_progress_lags() -> None: + from euniv.autostudy import WorkerView + + _clear_user_status("u-lag") + view = WorkerView("u-lag", 1) + view.update(course="甲", knowledge_id="k-lag", progress=2) + view.after_beat(2.1, ok=True) + frozen = view.total + assert frozen == 980 + view.after_beat(2.1, ok=True) + view.after_beat(2.12, ok=True) + assert view.sent == 3 + assert view.total == frozen + _clear_user_status("u-lag") + + +def test_planned_beats_follows_course_length() -> None: + assert _planned_beats(0, 0, 1) == 0 + assert _planned_beats(4190, 0, 1) == 4190 + assert _planned_beats(3600, 0, 30) == 120 + assert _planned_beats(100, 90, 30) == 1 + + def test_hours_watcher_default_interval_is_one_minute() -> None: watcher = HoursWatcher(_HourSession("0"), "u1", 600, asyncio.Event()) assert _HOURS_REFRESH_SECONDS == 60