fix: 任务开始后显示已开启和学时刷新倒计时

This commit is contained in:
2026-11-17 12:07:45 +08:00
parent e30e2321e7
commit ad0b18ecfc
4 changed files with 43 additions and 4 deletions
+3
View File
@@ -131,6 +131,8 @@ def _set_user_status(employee_id: str, **kw: object) -> None:
users = _status.setdefault("users", {}) users = _status.setdefault("users", {})
info = users.setdefault(employee_id, {}) info = users.setdefault(employee_id, {})
info.update(kw) info.update(kw)
if kw.get("state") in ("已停止", "已学完", "学习失败", "会话失效", "已达标"):
info.pop("hours_refresh_at", None)
info["month"] = str(kw["month"]) if "month" in kw else month_key() info["month"] = str(kw["month"]) if "month" in kw else month_key()
_status["updated_at"] = datetime.now(timezone.utc).isoformat() _status["updated_at"] = datetime.now(timezone.utc).isoformat()
_notify(employee_id) _notify(employee_id)
@@ -762,6 +764,7 @@ class HoursWatcher:
async def ticker(self, global_stop: asyncio.Event) -> None: async def ticker(self, global_stop: asyncio.Event) -> None:
while not global_stop.is_set() and not self.user_stop.is_set(): while not global_stop.is_set() and not self.user_stop.is_set():
_set_user_status(self.employee_id, hours_refresh_at=time.time() + self.interval)
if await wait_stop(self.interval, global_stop, self.user_stop): if await wait_stop(self.interval, global_stop, self.user_stop):
return return
await self.refresh() await self.refresh()
+7
View File
@@ -2,8 +2,10 @@
from __future__ import annotations from __future__ import annotations
import math
import re import re
import threading import threading
import time
from datetime import datetime from datetime import datetime
from euniv.study_window import month_key, window_open from euniv.study_window import month_key, window_open
@@ -148,6 +150,11 @@ def assemble(account, live: dict | None = None, now: datetime | None = None) ->
if recorded_month and recorded_month != month_key(now): if recorded_month and recorded_month != month_key(now):
stored_hours = "" stored_hours = ""
payload["month_hours"] = cached_hours or stored_hours payload["month_hours"] = cached_hours or stored_hours
refresh_at = info.get("hours_refresh_at")
left = 0
if running and isinstance(refresh_at, (int, float)) and not isinstance(refresh_at, bool):
left = max(0, int(math.ceil(float(refresh_at) - time.time())))
payload["hours_refresh_in"] = left
try: try:
payload["monthly_target"] = int(getattr(account, "monthly_target", 0) or 0) payload["monthly_target"] = int(getattr(account, "monthly_target", 0) or 0)
except (TypeError, ValueError): except (TypeError, ValueError):
+13
View File
@@ -469,6 +469,19 @@ def test_manual_start_expires_when_the_month_changes() -> None:
assert study_eligible(account, datetime(2026, 12, 1, tzinfo=CN_TZ)) is False assert study_eligible(account, datetime(2026, 12, 1, tzinfo=CN_TZ)) is False
def test_assemble_reports_hours_refresh_countdown(monkeypatch) -> None:
import time
monkeypatch.setattr("euniv.autostudy.work_running", lambda _eid: True)
account = _Start(1, 0, 0, 0)
account.employee_id = "u-hours"
payload = assemble(account, {"hours_refresh_at": time.time() + 40})
assert 1 <= payload["hours_refresh_in"] <= 40
monkeypatch.setattr("euniv.autostudy.work_running", lambda _eid: False)
stopped = assemble(account, {"hours_refresh_at": time.time() + 40})
assert stopped["hours_refresh_in"] == 0
def test_hours_met_clears_started_without_pause() -> None: def test_hours_met_clears_started_without_pause() -> None:
account = _Start(1, 0, 0, 0) account = _Start(1, 0, 0, 0)
start_now(account) start_now(account)
+20 -4
View File
@@ -74,6 +74,8 @@ const studyControl = ref({
workers: [] as StudyWorker[], workers: [] as StudyWorker[],
}); });
const studyBusy = ref(false); const studyBusy = ref(false);
const hoursRefreshIn = ref(0);
const hoursRefreshSeen = ref(0);
const studyError = ref(""); const studyError = ref("");
const studyingWorkers = computed(() => ( const studyingWorkers = computed(() => (
@@ -104,8 +106,7 @@ const studyLabel = computed(() => {
if (studyFailed.value) return message || state; if (studyFailed.value) return message || state;
if (state === "排队中") return "排队中"; if (state === "排队中") return "排队中";
if (state === "准备") return "正在连接学习平台"; if (state === "准备") return "正在连接学习平台";
if (studyingWorkers.value.length) return studyControl.value.course || "学习中"; if (studyingWorkers.value.length || state === "学习中") return "任务已开启";
if (state === "学习中") return "正在领取课程";
if (studyControl.value.active || studyControl.value.started) return "正在启动学习任务"; if (studyControl.value.active || studyControl.value.started) return "正在启动学习任务";
if (state === "已停止" || studyControl.value.paused) return "任务已停止"; if (state === "已停止" || studyControl.value.paused) return "任务已停止";
if (state === "已学完") return "课程已学完"; if (state === "已学完") return "课程已学完";
@@ -223,6 +224,14 @@ function workerCount(row: StudyWorker) {
return "—"; return "—";
} }
const showHoursRefresh = computed(() => studyLabel.value === "任务已开启" && hoursRefreshIn.value > 0);
function hoursRefreshLeft() {
void nowTick.value;
const elapsed = (Date.now() - hoursRefreshSeen.value) / 1000;
return Math.max(0, Math.ceil(hoursRefreshIn.value - elapsed));
}
function heartbeatLeft(row: StudyWorker) { function heartbeatLeft(row: StudyWorker) {
void nowTick.value; void nowTick.value;
if (row.heartbeat_in > 0) { if (row.heartbeat_in > 0) {
@@ -564,6 +573,12 @@ function applyStudyControl(payload: Record<string, unknown>) {
workers: asWorkers(payload.workers), workers: asWorkers(payload.workers),
}; };
const studying = payload.active === true || payload.started === true; const studying = payload.active === true || payload.started === true;
if (studying && typeof payload.hours_refresh_in === "number" && payload.hours_refresh_in > 0) {
hoursRefreshIn.value = payload.hours_refresh_in;
hoursRefreshSeen.value = Date.now();
} else if (!studying) {
hoursRefreshIn.value = 0;
}
if (studying && typeof payload.month_hours === "string" && payload.month_hours) { if (studying && typeof payload.month_hours === "string" && payload.month_hours) {
progressHours.value = payload.month_hours; progressHours.value = payload.month_hours;
if (summary.value) summary.value = { ...summary.value, month_hours: payload.month_hours }; if (summary.value) summary.value = { ...summary.value, month_hours: payload.month_hours };
@@ -741,8 +756,8 @@ function onStudyVisible() {
if (document.visibilityState === "visible") studyWake(); if (document.visibilityState === "visible") studyWake();
} }
watch(countdownOn, (on) => { watch([countdownOn, showHoursRefresh], ([heart, hours]) => {
if (on) startStudyTick(); if (heart || hours) startStudyTick();
else stopStudyTick(); else stopStudyTick();
}); });
@@ -926,6 +941,7 @@ onUnmounted(() => {
}" }"
></span> ></span>
<span class="study-text" aria-live="polite" :class="{ muted: !studyControl.started && !studyControl.active }">{{ studyLabel }}</span> <span class="study-text" aria-live="polite" :class="{ muted: !studyControl.started && !studyControl.active }">{{ studyLabel }}</span>
<span v-if="showHoursRefresh" class="study-text muted">刷新学时 {{ hoursRefreshLeft() }}s</span>
</div> </div>
<div v-if="targetLoaded" class="study-progress"> <div v-if="targetLoaded" class="study-progress">
<div class="study-progress-meta"> <div class="study-progress-meta">