feat: 用户列表显示任务开启时间
This commit is contained in:
@@ -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 ""),
|
||||
|
||||
@@ -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")
|
||||
@@ -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)
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user