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 "")
|
||||
|
||||
Reference in New Issue
Block a user