fix: 心跳倒计时按服务器剩余时间显示,服务重启后页面继续更新
This commit is contained in:
@@ -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 "")
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user