feat: 学习状态改为内存快照,页面连接每秒自取并自动重连

This commit is contained in:
2026-11-17 17:36:22 +08:00
parent 42b29fb4d6
commit 4d1a90c321
11 changed files with 675 additions and 256 deletions
+106 -15
View File
@@ -2,12 +2,14 @@
from __future__ import annotations
import copy
import math
import re
import secrets
import threading
import time
from datetime import datetime
from types import SimpleNamespace
from euniv.study_window import month_key, window_open
@@ -15,6 +17,64 @@ from euniv.study_window import month_key, window_open
_frame_lock = threading.Lock()
_frames: dict[str, int] = {}
_BOOT = secrets.token_hex(8)
_SECRET_COLUMNS = frozenset({"password_cipher", "credential_cipher", "proxy_password_cipher"})
_SNAP_SECONDS = 30.0
_snap_lock = threading.Lock()
_snaps: dict[str, tuple[float, SimpleNamespace]] = {}
_snap_marks: dict[str, int] = {}
_EMPTY = {
"started": False,
"paused": True,
"hours_met": False,
"schedule_enabled": True,
"workers": [],
"active": False,
"message": "",
}
def remember(account, mark: int | None = None) -> SimpleNamespace:
"""把首页学习状态要用的账号字段抄到内存,不含任何密文。账号一有写入就会失效。
mark 是读库之前取的写入记号。读库期间这个账号又被写过,就不把这份旧值留在内存里。
"""
from euniv.models import Account
fields = {
column.key: getattr(account, column.key, None)
for column in Account.__table__.columns
if column.key not in _SECRET_COLUMNS
}
snap = SimpleNamespace(**fields)
key = str(fields.get("employee_id") or "").strip()
if key:
with _snap_lock:
if mark is None or _snap_marks.get(key, 0) == mark:
_snaps[key] = (time.monotonic() + _SNAP_SECONDS, snap)
return snap
def forget(employee_id: str) -> None:
key = (employee_id or "").strip()
with _snap_lock:
_snaps.pop(key, None)
_snap_marks[key] = _snap_marks.get(key, 0) + 1
def _write_mark(employee_id: str) -> int:
with _snap_lock:
return _snap_marks.get(employee_id, 0)
def _remembered(employee_id: str) -> SimpleNamespace | None:
with _snap_lock:
held = _snaps.get(employee_id)
if held is None:
return None
if held[0] < time.monotonic():
_snaps.pop(employee_id, None)
return None
return held[1]
def stamp_frame(employee_id: str, payload: dict) -> dict:
@@ -185,38 +245,69 @@ def assemble(account, live: dict | None = None, now: datetime | None = None) ->
return payload
def _release_due(account, payload: dict) -> bool:
return bool(payload["hours_met"]) and ignores_window(account) and not payload["active"]
def settle(account, live: dict | None = None, now: datetime | None = None) -> bool:
"""跨月恢复定时;达标且当前没在学时,收回立即开始标记。"""
changed = expire_pause(account, now)
payload = assemble(account, live, now)
if payload["hours_met"] and ignores_window(account) and not payload["active"]:
if _release_due(account, assemble(account, live, now)):
account.study_ignore_window = False
changed = True
return changed
def payload_for(employee_id: str) -> dict:
def quick_state(employee_id: str) -> dict | None:
"""只用内存拼出首页学习状态,不读库。
内存里没有这个账号,或者有状态要写回库(跨月、达标后收回立即开始),返回 None,
调用方改用 load_state。
"""
from euniv.autostudy import account_status
key = (employee_id or "").strip()
held = _remembered(key) if key else None
if held is None:
return None
live = account_status(key)
probe = copy.copy(held)
changed = expire_pause(probe)
payload = assemble(probe, live)
if changed or _release_due(probe, payload):
return None
return payload
def load_state(employee_id: str) -> dict:
"""读库拼出首页学习状态,把该写回的写回,并把账号字段记到内存供之后直接用。"""
from euniv.autostudy import account_status
from euniv.db import SessionLocal
from euniv.models import Account
key = (employee_id or "").strip()
empty = {
"started": False,
"paused": True,
"hours_met": False,
"schedule_enabled": True,
"workers": [],
"active": False,
"message": "",
}
if not key:
return empty
return dict(_EMPTY)
with SessionLocal() as db:
mark = _write_mark(key)
account = db.get(Account, key)
if account is None:
return empty
forget(key)
return dict(_EMPTY)
live = account_status(key)
if settle(account, live):
db.commit()
return stamp_frame(key, assemble(account, live))
mark = _write_mark(key)
return assemble(remember(account, mark), live)
def state_for(employee_id: str) -> dict:
payload = quick_state(employee_id)
return payload if payload is not None else load_state(employee_id)
def payload_for(employee_id: str) -> dict:
key = (employee_id or "").strip()
if not key:
return dict(_EMPTY)
return stamp_frame(key, state_for(key))