feat: 心跳间隔改由每个用户自己设置

This commit is contained in:
2026-11-17 12:42:23 +08:00
parent b83fd637ab
commit cc9b2d56a4
10 changed files with 320 additions and 46 deletions
+33 -1
View File
@@ -80,6 +80,10 @@ class StudyBundleBody(BaseModel):
second: int = Field(strict=True, ge=0, le=59)
enabled: bool = True
workers: int = Field(strict=True, ge=1, le=8)
heartbeat_mode: str | None = None
heartbeat_video_seconds: int | None = Field(default=None, strict=True, ge=0, le=300)
heartbeat_document_seconds: int | None = Field(default=None, strict=True, ge=0, le=600)
heartbeat_scorm_seconds: int | None = Field(default=None, strict=True, ge=0, le=600)
class StudyControlBody(BaseModel):
@@ -140,6 +144,9 @@ _FIELD_ERRORS = {
"minute": "每月开始分须为 0 到 59 的整数",
"second": "每月开始秒须为 0 到 59 的整数",
"workers": "同时学习课程数须为 1 到 8",
"heartbeat_video_seconds": "视频心跳间隔须为 0 到 300 的整数",
"heartbeat_document_seconds": "文档心跳间隔须为 0 到 600 的整数",
"heartbeat_scorm_seconds": "Scorm 心跳间隔须为 0 到 600 的整数",
}
@@ -409,9 +416,20 @@ def put_study_start(body: StudyStartBody, request: Request, db: Session = Depend
return view_of(account)
@app.get("/api/settings/heartbeat")
def get_heartbeat(request: Request, db: Session = Depends(get_db)) -> dict:
from euniv.heartbeat import view_of
account = db.get(Account, _signed_in(request, db))
if account is None:
raise HTTPException(status_code=401, detail="未登录")
return view_of(account)
@app.put("/api/settings/study")
def put_study_bundle(body: StudyBundleBody, request: Request, db: Session = Depends(get_db)) -> dict:
from euniv.course_workers import CourseWorkersError, apply as apply_workers
from euniv.heartbeat import HeartbeatError, apply as apply_heartbeat, view_of as heartbeat_view
from euniv.study_window import StudyWindowError, apply as apply_start, view_of
if not 0 <= body.monthly_target <= 9999:
@@ -424,7 +442,20 @@ def put_study_bundle(body: StudyBundleBody, request: Request, db: Session = Depe
apply_start(account, body.day, body.hour, body.minute, body.second)
account.study_schedule_enabled = bool(body.enabled)
apply_workers(account, body.workers)
except (StudyWindowError, CourseWorkersError) as exc:
if (
body.heartbeat_mode is not None
or body.heartbeat_video_seconds is not None
or body.heartbeat_document_seconds is not None
or body.heartbeat_scorm_seconds is not None
):
apply_heartbeat(
account,
body.heartbeat_mode,
body.heartbeat_video_seconds,
body.heartbeat_document_seconds,
body.heartbeat_scorm_seconds,
)
except (StudyWindowError, CourseWorkersError, HeartbeatError) as exc:
db.rollback()
raise HTTPException(status_code=400, detail=str(exc)) from exc
db.commit()
@@ -434,6 +465,7 @@ def put_study_bundle(body: StudyBundleBody, request: Request, db: Session = Depe
payload = view_of(account)
payload["monthly_target"] = account.monthly_target
payload["workers"] = account.course_workers
payload.update(heartbeat_view(account))
return payload