feat: 历史统计时对照显示官方本月学时
This commit is contained in:
+22
-4
@@ -62,8 +62,10 @@ def _iso_time(value: datetime | None) -> str | None:
|
||||
|
||||
|
||||
def list_users(db: Session) -> list[dict]:
|
||||
from admin.settings import hours_source_of
|
||||
from euniv.autostudy import autostudy_status, work_running
|
||||
|
||||
source = hours_source_of()
|
||||
accounts = db.scalars(select(Account).order_by(Account.employee_id)).all()
|
||||
live_users = autostudy_status().get("users") or {}
|
||||
rows = []
|
||||
@@ -82,6 +84,8 @@ def list_users(db: Session) -> list[dict]:
|
||||
"last_refresh_at": _iso_time(account.last_refresh_at),
|
||||
"credential_valid": _optional_bool(account.credential_valid),
|
||||
"month_hours": account.month_hours or "",
|
||||
"official_month_hours": account.official_month_hours or "",
|
||||
"hours_source": source,
|
||||
"monthly_target": account.monthly_target if account.monthly_target is not None else 600,
|
||||
"has_proxy": enabled(account),
|
||||
"proxy": summary_of(account),
|
||||
@@ -111,13 +115,23 @@ def _require(db: Session, employee_id: str) -> Account:
|
||||
return account
|
||||
|
||||
|
||||
def _checked_hours(client) -> str:
|
||||
def _checked_hours(client) -> tuple[str, str]:
|
||||
from admin.settings import hours_source_of
|
||||
from euniv.service import _month_hours
|
||||
|
||||
try:
|
||||
return _month_hours(client)
|
||||
hours = _month_hours(client)
|
||||
except Exception:
|
||||
return ""
|
||||
hours = ""
|
||||
official = ""
|
||||
if hours and hours_source_of() == "history":
|
||||
reader = getattr(client, "month_hours", None)
|
||||
if callable(reader):
|
||||
try:
|
||||
official = str(reader() or "").strip()
|
||||
except Exception:
|
||||
official = ""
|
||||
return hours, official
|
||||
|
||||
|
||||
def _checked_profile(client) -> dict:
|
||||
@@ -146,6 +160,7 @@ def check_employee(db: Session, employee_id: str) -> dict:
|
||||
valid: bool | None = None
|
||||
profile: dict = {}
|
||||
hours = ""
|
||||
official_hours = ""
|
||||
try:
|
||||
for _ in range(MAX_SWITCHES + 1):
|
||||
proxy = bind_url(account) if pooled else url_of(account)
|
||||
@@ -157,11 +172,12 @@ def check_employee(db: Session, employee_id: str) -> dict:
|
||||
valid = alive
|
||||
profile = _checked_profile(client) if alive else {}
|
||||
if alive:
|
||||
hours = _checked_hours(client)
|
||||
hours, official_hours = _checked_hours(client)
|
||||
break
|
||||
except Exception as exc:
|
||||
profile = {}
|
||||
hours = ""
|
||||
official_hours = ""
|
||||
if pooled and proxy and is_proxy_failure(exc):
|
||||
retire_if_dead(employee_id, proxy, str(exc)[:200])
|
||||
client.close()
|
||||
@@ -190,6 +206,8 @@ def check_employee(db: Session, employee_id: str) -> dict:
|
||||
from euniv.study_control import stamp_hours
|
||||
|
||||
stamp_hours(account, hours)
|
||||
if official_hours:
|
||||
account.official_month_hours = official_hours[:32]
|
||||
db.commit()
|
||||
payload = {"employee_id": employee_id, "result": result}
|
||||
if result == "检查失败":
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
"""记下页面上的本月学时,便于和系统统计对照。"""
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
revision = "0014_official_month_hours"
|
||||
down_revision = "0013_study_opened_at"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
with op.batch_alter_table("accounts") as batch:
|
||||
batch.add_column(sa.Column("official_month_hours", sa.String(length=32), server_default="", nullable=False))
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
with op.batch_alter_table("accounts") as batch:
|
||||
batch.drop_column("official_month_hours")
|
||||
@@ -892,16 +892,22 @@ class HoursWatcher:
|
||||
from admin.settings import hours_source_of
|
||||
|
||||
source = hours_source_of()
|
||||
official = ""
|
||||
if source == "history":
|
||||
current = int(await self.session.month_history_minutes())
|
||||
text = str(current)
|
||||
try:
|
||||
official = await self.session.month_hours_text()
|
||||
except Exception:
|
||||
official = ""
|
||||
else:
|
||||
text = await self.session.month_hours_text()
|
||||
current = _parse_minutes(text)
|
||||
official = text
|
||||
if not text:
|
||||
raise RuntimeError("学时没有解析到")
|
||||
self._misses = 0
|
||||
await asyncio.to_thread(_update_month_hours, self.employee_id, text)
|
||||
await asyncio.to_thread(_update_month_hours, self.employee_id, text, official)
|
||||
target = self.current_target()
|
||||
if target > 0 and current >= target:
|
||||
self.reached = True
|
||||
@@ -2389,12 +2395,14 @@ class AutoStudyWorker:
|
||||
|
||||
# ── 数据库更新 ──
|
||||
|
||||
def _update_month_hours(employee_id: str, hours: str) -> None:
|
||||
def _update_month_hours(employee_id: str, hours: str, official: str = "") -> None:
|
||||
from euniv.db import SessionLocal
|
||||
with SessionLocal() as db:
|
||||
account = db.get(Account, employee_id)
|
||||
if account:
|
||||
stamp_hours(account, hours)
|
||||
if official.strip():
|
||||
account.official_month_hours = official.strip()[:32]
|
||||
db.commit()
|
||||
_set_user_status(employee_id, month_hours=hours)
|
||||
|
||||
|
||||
@@ -56,6 +56,7 @@ class Account(Base):
|
||||
last_refresh_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
||||
credential_valid: Mapped[bool | None] = mapped_column(Boolean, nullable=True)
|
||||
month_hours: Mapped[str] = mapped_column(String(32), nullable=False, default="", server_default=text("''"))
|
||||
official_month_hours: Mapped[str] = mapped_column(String(32), nullable=False, default="", server_default=text("''"))
|
||||
user_name: Mapped[str] = mapped_column(String(64), nullable=False, default="", server_default=text("''"))
|
||||
company: Mapped[str] = mapped_column(String(300), nullable=False, default="", server_default=text("''"))
|
||||
hours_month: Mapped[str] = mapped_column(String(7), nullable=False, default="", server_default=text("''"))
|
||||
|
||||
@@ -416,6 +416,7 @@ def _apply_hours_source(client, summary: dict, items: list | None = None) -> dic
|
||||
data = dict(summary)
|
||||
source = hours_source_of()
|
||||
data["hours_source"] = source
|
||||
data["official_month_hours"] = data.get("month_hours")
|
||||
if source != "history":
|
||||
return data
|
||||
walker = getattr(client, "month_history_minutes", None)
|
||||
@@ -440,6 +441,7 @@ def _api_hour_fields(client) -> dict:
|
||||
return {
|
||||
"year_hours": summary.get("year_hours"),
|
||||
"month_hours": summary.get("month_hours"),
|
||||
"official_month_hours": summary.get("official_month_hours") or summary.get("month_hours"),
|
||||
"today_hours": summary.get("today_hours"),
|
||||
"total_hours": summary.get("total_hours"),
|
||||
}
|
||||
@@ -461,6 +463,11 @@ def _remember_month_hours(db: Session, employee_id: str, payload: dict) -> None:
|
||||
from euniv.study_control import stamp_hours
|
||||
|
||||
stamp_hours(account, text)
|
||||
official = payload.get("official_month_hours")
|
||||
if official is not None and not isinstance(official, bool):
|
||||
official_text = str(official).strip()
|
||||
if official_text:
|
||||
account.official_month_hours = official_text[:32]
|
||||
db.commit()
|
||||
|
||||
|
||||
@@ -497,6 +504,7 @@ def _hour_fields(data: dict) -> dict:
|
||||
"hours_source": data.get("hours_source") or "api",
|
||||
"year_hours": data.get("year_hours"),
|
||||
"month_hours": data.get("month_hours"),
|
||||
"official_month_hours": data.get("official_month_hours"),
|
||||
"today_hours": data.get("today_hours"),
|
||||
"total_hours": data.get("total_hours"),
|
||||
}
|
||||
|
||||
@@ -508,6 +508,8 @@ def test_check_skips_empty_credential_and_updates_saved_one(monkeypatch) -> None
|
||||
"last_refresh_at",
|
||||
"credential_valid",
|
||||
"month_hours",
|
||||
"official_month_hours",
|
||||
"hours_source",
|
||||
"monthly_target",
|
||||
"has_proxy",
|
||||
"proxy",
|
||||
|
||||
@@ -150,6 +150,7 @@ def test_login_stores_password_and_credential_only(monkeypatch) -> None:
|
||||
"last_refresh_at",
|
||||
"credential_valid",
|
||||
"month_hours",
|
||||
"official_month_hours",
|
||||
"user_name",
|
||||
"company",
|
||||
"hours_month",
|
||||
@@ -1155,6 +1156,7 @@ def test_home_and_history_hours_follow_source(monkeypatch) -> None:
|
||||
history = client.get("/api/home/history")
|
||||
assert home.json()["hours_source"] == history.json()["hours_source"] == "history"
|
||||
assert home.json()["month_hours"] == history.json()["month_hours"] == 2
|
||||
assert home.json()["official_month_hours"] == history.json()["official_month_hours"] == "60"
|
||||
assert home.json()["year_hours"] == history.json()["year_hours"] == "100"
|
||||
assert home.json()["total_hours"] == history.json()["total_hours"] == "10"
|
||||
assert home.json()["today_hours"] == history.json()["today_hours"] == 1
|
||||
|
||||
Reference in New Issue
Block a user