feat: 用户列表显示任务开启时间

This commit is contained in:
Nixevol
2026-11-19 02:19:01 +08:00
parent 28f424f2d3
commit 85e437b74c
8 changed files with 62 additions and 3 deletions
+1
View File
@@ -95,6 +95,7 @@ def list_users(db: Session) -> list[dict]:
"study_paused": control["paused"], "study_paused": control["paused"],
"study_ignore_window": control["ignore_window"], "study_ignore_window": control["ignore_window"],
"study_started": control["started"], "study_started": control["started"],
"study_opened_at": _iso_time(account.study_opened_at),
"hours_month": account.hours_month or "", "hours_month": account.hours_month or "",
"study_active": work_running(account.employee_id), "study_active": work_running(account.employee_id),
"state": str(live.get("state") or ""), "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")
+3
View File
@@ -2072,6 +2072,9 @@ class AutoStudyWorker:
_release_pool_later(eid, bool(job[6])) _release_pool_later(eid, bool(job[6]))
return False return False
_set_user_status(eid, state="排队中", course="") _set_user_status(eid, state="排队中", course="")
from euniv.study_control import remember_study_opened
remember_study_opened(eid)
want_work(eid) want_work(eid)
with _halt_lock: with _halt_lock:
generation = _wanted.get(eid, 0) generation = _wanted.get(eid, 0)
+1
View File
@@ -74,6 +74,7 @@ class Account(Base):
study_paused: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default=sql_false()) 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_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_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()) 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_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") heartbeat_video_seconds: Mapped[int] = mapped_column(nullable=False, default=30, server_default="30")
+28 -1
View File
@@ -8,7 +8,7 @@ import re
import secrets import secrets
import threading import threading
import time import time
from datetime import datetime from datetime import datetime, timezone
from types import SimpleNamespace from types import SimpleNamespace
from euniv.study_window import month_key, window_open from euniv.study_window import month_key, window_open
@@ -89,16 +89,41 @@ def stamp_frame(employee_id: str, payload: dict) -> dict:
return stamped 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: def start_now(account, now: datetime | None = None) -> None:
account.study_paused = False account.study_paused = False
account.study_stop_month = month_key(now) account.study_stop_month = month_key(now)
account.study_ignore_window = True account.study_ignore_window = True
account.study_opened_at = _aware(now)
def stop_now(account, now: datetime | None = None) -> None: def stop_now(account, now: datetime | None = None) -> None:
account.study_paused = True account.study_paused = True
account.study_ignore_window = False account.study_ignore_window = False
account.study_stop_month = month_key(now) 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: def schedule_enabled(account) -> bool:
@@ -146,6 +171,7 @@ def expire_pause(account, now: datetime | None = None) -> bool:
changed = False changed = False
if ignores_window(account) and recorded and recorded != key: if ignores_window(account) and recorded and recorded != key:
account.study_ignore_window = False account.study_ignore_window = False
account.study_opened_at = None
changed = True changed = True
if not bool(getattr(account, "study_paused", False)): if not bool(getattr(account, "study_paused", False)):
if changed: if changed:
@@ -158,6 +184,7 @@ def expire_pause(account, now: datetime | None = None) -> bool:
account.study_paused = False account.study_paused = False
account.study_stop_month = "" account.study_stop_month = ""
account.study_ignore_window = False account.study_ignore_window = False
account.study_opened_at = None
return True return True
return changed return changed
+5
View File
@@ -521,6 +521,7 @@ def test_check_skips_empty_credential_and_updates_saved_one(monkeypatch) -> None
"study_paused", "study_paused",
"study_ignore_window", "study_ignore_window",
"study_started", "study_started",
"study_opened_at",
"hours_month", "hours_month",
"study_active", "study_active",
"state", "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") 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") two = next(item for item in listed if item["employee_id"] == "wait-user")
assert one["study_started"] is True assert one["study_started"] is True
assert one["study_opened_at"]
assert two["study_paused"] is False 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"}) stopped = client.post("/api/admin/users/study", json={"employee_ids": ["run-user"], "action": "stop"})
assert stopped.status_code == 200 assert stopped.status_code == 200
listed = client.get("/api/admin/users").json() 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") two = next(item for item in listed if item["employee_id"] == "wait-user")
assert one["study_started"] is False assert one["study_started"] is False
assert one["study_paused"] is True assert one["study_paused"] is True
assert one["study_opened_at"] is None
assert two["study_started"] is True 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"}) bad = client.post("/api/admin/users/study", json={"employee_ids": ["run-user"], "action": "pause"})
assert bad.status_code == 400 assert bad.status_code == 400
+1
View File
@@ -168,6 +168,7 @@ def test_login_stores_password_and_credential_only(monkeypatch) -> None:
"study_paused", "study_paused",
"study_stop_month", "study_stop_month",
"study_ignore_window", "study_ignore_window",
"study_opened_at",
"study_schedule_enabled", "study_schedule_enabled",
"heartbeat_mode", "heartbeat_mode",
"heartbeat_video_seconds", "heartbeat_video_seconds",
+4 -2
View File
@@ -29,6 +29,7 @@ interface UserRow {
study_paused: boolean; study_paused: boolean;
study_ignore_window: boolean; study_ignore_window: boolean;
study_started: boolean; study_started: boolean;
study_opened_at: string | null;
hours_month: string; hours_month: string;
study_active: boolean | null; study_active: boolean | null;
study_state: string; study_state: string;
@@ -186,6 +187,7 @@ function asUser(value: unknown): UserRow | null {
study_paused: value.study_paused === true, study_paused: value.study_paused === true,
study_ignore_window: value.study_ignore_window === true, study_ignore_window: value.study_ignore_window === true,
study_started: value.study_started === 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), hours_month: textOf(value.hours_month),
study_active: typeof value.study_active === "boolean" ? value.study_active : null, study_active: typeof value.study_active === "boolean" ? value.study_active : null,
study_state: textOf(value.state) || textOf(value.study_state), study_state: textOf(value.state) || textOf(value.study_state),
@@ -899,9 +901,9 @@ onMounted(() => {
<th>用户名称</th> <th>用户名称</th>
<th>公司</th> <th>公司</th>
<th>凭据</th> <th>凭据</th>
<th>有效</th>
<th>本月学时</th> <th>本月学时</th>
<th>任务</th> <th>任务</th>
<th>开启任务时间</th>
<th>代理</th> <th>代理</th>
<th>上次刷新时间</th> <th>上次刷新时间</th>
<th>自动刷新</th> <th>自动刷新</th>
@@ -916,10 +918,10 @@ onMounted(() => {
<td class="employee">{{ row.employee_id }}</td> <td class="employee">{{ row.employee_id }}</td>
<td class="name">{{ row.user_name || "—" }}</td> <td class="name">{{ row.user_name || "—" }}</td>
<td class="company">{{ row.company || "—" }}</td> <td class="company">{{ row.company || "—" }}</td>
<td>{{ row.has_credential ? "已保存凭据" : "未保存凭据" }}</td>
<td :class="validityClass(validityText(row))">{{ validityText(row) }}</td> <td :class="validityClass(validityText(row))">{{ validityText(row) }}</td>
<td class="when">{{ monthHoursText(row) }}</td> <td class="when">{{ monthHoursText(row) }}</td>
<td :class="taskText(row) === '运行中' ? 'ok' : 'quiet'">{{ taskText(row) }}</td> <td :class="taskText(row) === '运行中' ? 'ok' : 'quiet'">{{ taskText(row) }}</td>
<td class="when">{{ formatRefreshTime(row.study_opened_at) || "—" }}</td>
<td class="when">{{ row.proxy || "—" }}</td> <td class="when">{{ row.proxy || "—" }}</td>
<td class="when">{{ formatRefreshTime(row.last_refresh_at) }}</td> <td class="when">{{ formatRefreshTime(row.last_refresh_at) }}</td>
<td>{{ row.refresh_paused ? "已停止" : "开启" }}</td> <td>{{ row.refresh_paused ? "已停止" : "开启" }}</td>