diff --git a/backend/euniv/study_control.py b/backend/euniv/study_control.py index 0f61482..ea4e55a 100644 --- a/backend/euniv/study_control.py +++ b/backend/euniv/study_control.py @@ -4,6 +4,7 @@ from __future__ import annotations import math import re +import secrets import threading import time from datetime import datetime @@ -13,15 +14,18 @@ from euniv.study_window import month_key, window_open _frame_lock = threading.Lock() _frames: dict[str, int] = {} +_BOOT = secrets.token_hex(8) def stamp_frame(employee_id: str, payload: dict) -> dict: + """seq 只在同一次启动内递增。boot 变了说明服务重启过,页面要从新的 seq 重新计数。""" key = (employee_id or "").strip() with _frame_lock: number = _frames.get(key, 0) + 1 _frames[key] = number stamped = dict(payload) stamped["seq"] = number + stamped["boot"] = _BOOT return stamped @@ -127,6 +131,25 @@ def view_of(account, now: datetime | None = None) -> dict: } +def _with_heartbeat_left(workers: list) -> list: + """每一帧按服务器当前时间重算剩余秒数,页面收到新帧时不会从整段间隔重新倒数。""" + clock = time.time() + rows = [] + for item in workers: + if not isinstance(item, dict): + continue + row = dict(item) + due = row.get("heartbeat_at") + if isinstance(due, (int, float)) and not isinstance(due, bool) and due > 0: + left = max(0, int(math.ceil(float(due) - clock))) + interval = row.get("interval") + if isinstance(interval, int) and not isinstance(interval, bool) and interval > 0: + left = min(left, interval) + row["heartbeat_in"] = left + rows.append(row) + return rows + + def assemble(account, live: dict | None = None, now: datetime | None = None) -> dict: payload = view_of(account, now) info = dict(live) if isinstance(live, dict) else {} @@ -137,7 +160,7 @@ def assemble(account, live: dict | None = None, now: datetime | None = None) -> payload["course"] = str(info.get("course") or "") payload["message"] = str(info.get("message") or "") workers = info.get("workers") or [] - payload["workers"] = workers if isinstance(workers, list) else [] + payload["workers"] = _with_heartbeat_left(workers if isinstance(workers, list) else []) from euniv.autostudy import work_running eid = str(getattr(account, "employee_id", "") or "") diff --git a/backend/tests/test_autostudy.py b/backend/tests/test_autostudy.py index c3b785e..1075a63 100644 --- a/backend/tests/test_autostudy.py +++ b/backend/tests/test_autostudy.py @@ -616,6 +616,33 @@ def test_assemble_reports_hours_refresh_countdown(monkeypatch) -> None: assert stopped["hours_refresh_in"] == 0 +def test_frames_carry_a_boot_id_so_pages_can_reset_after_restart() -> None: + from euniv.study_control import stamp_frame + + first = stamp_frame("u-boot", {}) + second = stamp_frame("u-boot", {}) + assert second["seq"] == first["seq"] + 1 + assert isinstance(first["boot"], str) and len(first["boot"]) == 16 + assert first["boot"] == second["boot"] + + +def test_every_frame_carries_the_time_left_before_the_next_heartbeat(monkeypatch) -> None: + import time + + monkeypatch.setattr("euniv.autostudy.work_running", lambda _eid: True) + account = _Start(1, 0, 0, 0) + account.employee_id = "u-beat" + clock = {"now": 1000.0} + monkeypatch.setattr(time, "time", lambda: clock["now"]) + live = {"workers": [{"id": 1, "interval": 5, "heartbeat_at": 1005.4, "heartbeat_in": 5}]} + assert assemble(account, live)["workers"][0]["heartbeat_in"] == 5 + clock["now"] = 1003.6 + assert assemble(account, live)["workers"][0]["heartbeat_in"] == 2 + clock["now"] = 1006.0 + assert assemble(account, live)["workers"][0]["heartbeat_in"] == 0 + assert live["workers"][0]["heartbeat_in"] == 5 + + def test_hours_met_clears_started_without_pause() -> None: account = _Start(1, 0, 0, 0) start_now(account) diff --git a/web/src/features/home/HomeView.vue b/web/src/features/home/HomeView.vue index 3803620..b7dde46 100644 --- a/web/src/features/home/HomeView.vue +++ b/web/src/features/home/HomeView.vue @@ -223,7 +223,7 @@ function workerTitle(row: StudyWorker) { } function showHeartbeat(row: StudyWorker) { - return row.heartbeat_in > 0 || (row.interval > 0 && row.heartbeat_at > 0); + return row.interval > 0 && (row.heartbeat_in > 0 || row.heartbeat_at > 0); } function workerCount(row: StudyWorker) { @@ -242,19 +242,9 @@ function hoursRefreshLeft() { function heartbeatLeft(row: StudyWorker) { void nowTick.value; - if (row.heartbeat_in > 0) { - const elapsed = (Date.now() - row.seen_at) / 1000; - const cap = row.interval > 0 ? row.interval : row.heartbeat_in; - const left = Math.max(0, Math.min(cap, row.heartbeat_in - elapsed)); - return `${Math.ceil(left)}s`; - } - const now = Date.now() / 1000; - const cycle = Math.max(0, row.interval); - let left = row.heartbeat_at - now; - if (left < 0 && cycle > 0) { - left = cycle - ((now - row.heartbeat_at) % cycle); - } - return `${Math.max(0, Math.ceil(left))}s`; + const elapsed = (Date.now() - row.seen_at) / 1000; + const left = Math.max(0, row.heartbeat_in - elapsed); + return `${Math.ceil(left)}s`; } async function loadSummary() { @@ -606,9 +596,15 @@ async function saveProxy() { } let lastStudySeq = 0; +let lastStudyBoot = ""; function applyStudyControl(payload: Record) { const seq = typeof payload.seq === "number" ? payload.seq : 0; + const boot = typeof payload.boot === "string" ? payload.boot : ""; + if (boot && boot !== lastStudyBoot) { + lastStudyBoot = boot; + lastStudySeq = 0; + } if (seq > 0 && seq < lastStudySeq) return; if (seq > 0) lastStudySeq = seq; studyControl.value = {