diff --git a/backend/admin/users.py b/backend/admin/users.py index 356fc30..5eee7c8 100644 --- a/backend/admin/users.py +++ b/backend/admin/users.py @@ -95,6 +95,7 @@ def list_users(db: Session) -> list[dict]: "study_paused": control["paused"], "study_ignore_window": control["ignore_window"], "study_started": control["started"], + "study_opened_at": _iso_time(account.study_opened_at), "hours_month": account.hours_month or "", "study_active": work_running(account.employee_id), "state": str(live.get("state") or ""), diff --git a/backend/alembic/versions/0013_study_opened_at.py b/backend/alembic/versions/0013_study_opened_at.py new file mode 100644 index 0000000..075d2f9 --- /dev/null +++ b/backend/alembic/versions/0013_study_opened_at.py @@ -0,0 +1,19 @@ +"""记下任务开启时间,供管理端用户列表显示。""" + +from alembic import op +import sqlalchemy as sa + +revision = "0013_study_opened_at" +down_revision = "0012_account_profile" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + with op.batch_alter_table("accounts") as batch: + batch.add_column(sa.Column("study_opened_at", sa.DateTime(timezone=True), nullable=True)) + + +def downgrade() -> None: + with op.batch_alter_table("accounts") as batch: + batch.drop_column("study_opened_at") diff --git a/backend/euniv/autostudy.py b/backend/euniv/autostudy.py index 6aed4bc..e0c24e4 100644 --- a/backend/euniv/autostudy.py +++ b/backend/euniv/autostudy.py @@ -2072,6 +2072,9 @@ class AutoStudyWorker: _release_pool_later(eid, bool(job[6])) return False _set_user_status(eid, state="排队中", course="") + from euniv.study_control import remember_study_opened + + remember_study_opened(eid) want_work(eid) with _halt_lock: generation = _wanted.get(eid, 0) diff --git a/backend/euniv/models.py b/backend/euniv/models.py index 860423d..de28a7e 100644 --- a/backend/euniv/models.py +++ b/backend/euniv/models.py @@ -74,6 +74,7 @@ class Account(Base): study_paused: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default=sql_false()) study_stop_month: Mapped[str] = mapped_column(String(7), nullable=False, default="", server_default=text("''")) study_ignore_window: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default=sql_false()) + study_opened_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True) study_schedule_enabled: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default=sql_false()) heartbeat_mode: Mapped[str] = mapped_column(String(16), nullable=False, default="auto", server_default="auto") heartbeat_video_seconds: Mapped[int] = mapped_column(nullable=False, default=30, server_default="30") diff --git a/backend/euniv/study_control.py b/backend/euniv/study_control.py index 2dbb3cb..75bd463 100644 --- a/backend/euniv/study_control.py +++ b/backend/euniv/study_control.py @@ -8,7 +8,7 @@ import re import secrets import threading import time -from datetime import datetime +from datetime import datetime, timezone from types import SimpleNamespace from euniv.study_window import month_key, window_open @@ -89,16 +89,41 @@ def stamp_frame(employee_id: str, payload: dict) -> dict: return stamped +def _aware(now: datetime | None) -> datetime: + moment = now or datetime.now(timezone.utc) + if moment.tzinfo is None: + return moment.replace(tzinfo=timezone.utc) + return moment + + def start_now(account, now: datetime | None = None) -> None: account.study_paused = False account.study_stop_month = month_key(now) account.study_ignore_window = True + account.study_opened_at = _aware(now) def stop_now(account, now: datetime | None = None) -> None: account.study_paused = True account.study_ignore_window = False account.study_stop_month = month_key(now) + account.study_opened_at = None + + +def remember_study_opened(employee_id: str, now: datetime | None = None) -> None: + """定时任务第一次真正启动时补记开启时间。已经记过的不改,避免重启把时间刷掉。""" + from euniv.db import SessionLocal + from euniv.models import Account + + key = (employee_id or "").strip() + if not key: + return + with SessionLocal() as db: + account = db.get(Account, key) + if account is None or account.study_opened_at is not None: + return + account.study_opened_at = _aware(now) + db.commit() def schedule_enabled(account) -> bool: @@ -146,6 +171,7 @@ def expire_pause(account, now: datetime | None = None) -> bool: changed = False if ignores_window(account) and recorded and recorded != key: account.study_ignore_window = False + account.study_opened_at = None changed = True if not bool(getattr(account, "study_paused", False)): if changed: @@ -158,6 +184,7 @@ def expire_pause(account, now: datetime | None = None) -> bool: account.study_paused = False account.study_stop_month = "" account.study_ignore_window = False + account.study_opened_at = None return True return changed diff --git a/backend/tests/test_admin.py b/backend/tests/test_admin.py index 74b4491..6a5958e 100644 --- a/backend/tests/test_admin.py +++ b/backend/tests/test_admin.py @@ -521,6 +521,7 @@ def test_check_skips_empty_credential_and_updates_saved_one(monkeypatch) -> None "study_paused", "study_ignore_window", "study_started", + "study_opened_at", "hours_month", "study_active", "state", @@ -934,7 +935,9 @@ def test_admin_can_start_and_stop_study_for_selected_users() -> None: one = next(item for item in listed if item["employee_id"] == "run-user") two = next(item for item in listed if item["employee_id"] == "wait-user") assert one["study_started"] is True + assert one["study_opened_at"] assert two["study_paused"] is False + assert two["study_opened_at"] stopped = client.post("/api/admin/users/study", json={"employee_ids": ["run-user"], "action": "stop"}) assert stopped.status_code == 200 listed = client.get("/api/admin/users").json() @@ -942,7 +945,9 @@ def test_admin_can_start_and_stop_study_for_selected_users() -> None: two = next(item for item in listed if item["employee_id"] == "wait-user") assert one["study_started"] is False assert one["study_paused"] is True + assert one["study_opened_at"] is None assert two["study_started"] is True + assert two["study_opened_at"] bad = client.post("/api/admin/users/study", json={"employee_ids": ["run-user"], "action": "pause"}) assert bad.status_code == 400 diff --git a/backend/tests/test_api.py b/backend/tests/test_api.py index c427592..868383d 100644 --- a/backend/tests/test_api.py +++ b/backend/tests/test_api.py @@ -168,6 +168,7 @@ def test_login_stores_password_and_credential_only(monkeypatch) -> None: "study_paused", "study_stop_month", "study_ignore_window", + "study_opened_at", "study_schedule_enabled", "heartbeat_mode", "heartbeat_video_seconds", diff --git a/web/src/features/admin/UsersView.vue b/web/src/features/admin/UsersView.vue index 1dd022c..0b11f60 100644 --- a/web/src/features/admin/UsersView.vue +++ b/web/src/features/admin/UsersView.vue @@ -29,6 +29,7 @@ interface UserRow { study_paused: boolean; study_ignore_window: boolean; study_started: boolean; + study_opened_at: string | null; hours_month: string; study_active: boolean | null; study_state: string; @@ -186,6 +187,7 @@ function asUser(value: unknown): UserRow | null { study_paused: value.study_paused === true, study_ignore_window: value.study_ignore_window === true, study_started: value.study_started === true, + study_opened_at: typeof value.study_opened_at === "string" && value.study_opened_at ? value.study_opened_at : null, hours_month: textOf(value.hours_month), study_active: typeof value.study_active === "boolean" ? value.study_active : null, study_state: textOf(value.state) || textOf(value.study_state), @@ -899,9 +901,9 @@ onMounted(() => {