feat: 按账号调度自动学习并让首页实时跟上状态

This commit is contained in:
2026-11-16 01:14:36 +08:00
parent 158578d17c
commit 7f0466b0f6
31 changed files with 2699 additions and 631 deletions
+2
View File
@@ -94,6 +94,7 @@ class AdminStudySettingsBody(BaseModel):
hour: int | None = None
minute: int | None = None
second: int | None = None
schedule_enabled: bool | None = None
workers: int | None = None
scheme: str | None = None
host: str = ""
@@ -289,6 +290,7 @@ def put_users_study_settings(
hour=body.hour,
minute=body.minute,
second=body.second,
schedule_enabled=body.schedule_enabled,
workers=body.workers,
scheme=body.scheme,
host=body.host,
+6 -6
View File
@@ -156,12 +156,12 @@ def save_settings(db: Session, payload: dict) -> SystemConfig:
hb_video = _as_int(payload.get("heartbeat_video_seconds"))
hb_doc = _as_int(payload.get("heartbeat_document_seconds"))
hb_scorm = _as_int(payload.get("heartbeat_scorm_seconds"))
if hb_video is None or not 10 <= hb_video <= 300:
raise SettingsError("视频心跳间隔须为 10 到 300 的整数")
if hb_doc is None or not 30 <= hb_doc <= 600:
raise SettingsError("文档心跳间隔须为 30 到 600 的整数")
if hb_scorm is None or not 30 <= hb_scorm <= 600:
raise SettingsError("Scorm 心跳间隔须为 30 到 600 的整数")
if hb_video is None or not 0 <= hb_video <= 300:
raise SettingsError("视频心跳间隔须为 0 到 300 的整数")
if hb_doc is None or not 0 <= hb_doc <= 600:
raise SettingsError("文档心跳间隔须为 0 到 600 的整数")
if hb_scorm is None or not 0 <= hb_scorm <= 600:
raise SettingsError("Scorm 心跳间隔须为 0 到 600 的整数")
source = str(payload.get("hours_source") or "api").strip().lower()
if source not in ("api", "history"):
raise SettingsError("学时达标依据须为 api 或 history")
+12 -2
View File
@@ -86,6 +86,7 @@ def list_users(db: Session) -> list[dict]:
"study_start_hour": int(account.study_start_hour or 0),
"study_start_minute": int(account.study_start_minute or 0),
"study_start_second": int(account.study_start_second or 0),
"study_schedule_enabled": control["schedule_enabled"],
"course_workers": workers_of(account),
"study_paused": control["paused"],
"study_ignore_window": control["ignore_window"],
@@ -284,7 +285,7 @@ def set_study_action(db: Session, employee_ids: list[str], action: str) -> dict:
if kind not in {"start", "stop"}:
raise HTTPException(status_code=400, detail="请选择开始或停止")
accounts = _accounts_by_ids(db, employee_ids)
from euniv.autostudy import request_stop, request_wake
from euniv.autostudy import request_stop, sync_work
for account in accounts:
if kind == "start":
@@ -292,8 +293,10 @@ def set_study_action(db: Session, employee_ids: list[str], action: str) -> dict:
else:
stop_now(account)
db.commit()
if kind == "start":
request_wake()
for account in accounts:
sync_work(account.employee_id)
else:
for account in accounts:
request_stop(account.employee_id)
@@ -308,6 +311,7 @@ def set_study_settings(
hour: int | None = None,
minute: int | None = None,
second: int | None = None,
schedule_enabled: bool | None = None,
workers: int | None = None,
scheme: str | None = None,
host: str = "",
@@ -325,6 +329,8 @@ def set_study_settings(
account.monthly_target = monthly_target
if day is not None:
apply_start(account, day, hour, minute, second)
if schedule_enabled is not None:
account.study_schedule_enabled = bool(schedule_enabled)
if workers is not None:
apply_workers(account, workers)
if scheme is not None:
@@ -333,6 +339,10 @@ def set_study_settings(
db.rollback()
raise HTTPException(status_code=400, detail=str(exc)) from exc
db.commit()
from euniv.autostudy import sync_work
for account in accounts:
sync_work(account.employee_id)
return {"ok": True, "count": len(accounts)}