feat(douyu): 查询工作台区分导入/查询数据、导入后自动查CK并修复登录刷新闪烁 (v0.4.29)
1) 闪烁修复:轮询(loadAccountsPage/loadManualBudget)改为静默模式, 不再每 2.2s 点亮表格 loading 遮罩与预算 Tag spinner;活动结束后保留 12s 宽限期继续静默刷新,避免登录刚完成就停刷导致行上残留「登录队列中」。 2) 导入后自动检测 CK:工作台 PUT 返回 imported_account_ids,前端分片 并发检测并显示进度,失效/无 CK 账号直接标红;/accounts/ck-check 对 无 CK、登录中的账号改为跳过(返回 checked/skipped)而不是整批报错。 行内新增「重登/人工登录该账号」入口,走既有人工登录预算。 3) 数据来源区分:douyu_workbench_accounts 新增 data_state/imported_at/ queried_at/relogin_at(迁移 20261130_0142)。导入标 imported,查询任务 成功由 _mark_task 标 queried,人工重登入队标 stale;PUT 工作台改为 upsert,不再整删整建以免重置状态。账号列表新增 workbench_scope/ workbench_state 入参与 workbench_* 回填字段。 4) 重登后只查重登账号:新增 /query-workbench/pending-ids(导入未查 + 重登待重查,仅含有可用 CK 者)与「只查待查询(N)」下拉;一键查询改为 尊重当前筛选(跨页),修正原来无视筛选直接查整台的问题。 5) CSV 末尾追加「数据来源 / 数据更新说明」两列,保持原有列序不变。
This commit is contained in:
@@ -0,0 +1,179 @@
|
||||
"""斗鱼「查询工作台」的数据状态维护。
|
||||
|
||||
查询工作台里的一个账号,数据来源必须能区分开:
|
||||
|
||||
- ``imported``:刚导入,只做过 CK 检测;鱼翅/积分/宝典/换绑还是导入前的旧值;
|
||||
- ``queried``:已在本工作台跑过一键查询,界面展示的是本次查询结果;
|
||||
- ``stale``:重登成功或 CK 重新生效,账号数据已过期,需要按「重登的账号」单独再查一次。
|
||||
|
||||
写入口只有三处:导入(`update_workbench_accounts`)、查询成功(任务结束)、
|
||||
人工重登(`relogin-invalid`)。读取入口是账号列表与 `pending-ids`。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from collections.abc import Iterable
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from sqlalchemy import or_
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..models import Account, DouyuWorkbenchAccount
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
DATA_STATE_IMPORTED = "imported"
|
||||
DATA_STATE_QUERIED = "queried"
|
||||
DATA_STATE_STALE = "stale"
|
||||
DATA_STATES = (DATA_STATE_IMPORTED, DATA_STATE_QUERIED, DATA_STATE_STALE)
|
||||
|
||||
QUERY_WORKBENCH_SCOPE = "query"
|
||||
|
||||
|
||||
def _normalized_ids(account_ids: Iterable[int]) -> list[int]:
|
||||
values: list[int] = []
|
||||
for raw in account_ids:
|
||||
try:
|
||||
account_id = int(raw)
|
||||
except (TypeError, ValueError):
|
||||
continue
|
||||
if account_id > 0:
|
||||
values.append(account_id)
|
||||
return list(dict.fromkeys(values))
|
||||
|
||||
|
||||
def _query_workbench_rows(
|
||||
db: Session, account_ids: list[int], *, user_id: int | None = None
|
||||
):
|
||||
query = db.query(DouyuWorkbenchAccount).filter(
|
||||
DouyuWorkbenchAccount.handbook_scope == QUERY_WORKBENCH_SCOPE,
|
||||
DouyuWorkbenchAccount.account_id.in_(account_ids),
|
||||
)
|
||||
if user_id is not None:
|
||||
query = query.filter(DouyuWorkbenchAccount.user_id == user_id)
|
||||
return query
|
||||
|
||||
|
||||
def mark_workbench_queried(
|
||||
db: Session,
|
||||
account_ids: Iterable[int],
|
||||
*,
|
||||
queried_at: datetime | None = None,
|
||||
commit: bool = True,
|
||||
) -> int:
|
||||
"""一键查询成功后把工作台行标记为「已查询」。
|
||||
|
||||
只更新已经在查询工作台里的账号;其它工作台(精英/电竞/小店)不参与。
|
||||
"""
|
||||
ids = _normalized_ids(account_ids)
|
||||
if not ids:
|
||||
return 0
|
||||
moment = queried_at or datetime.now(UTC)
|
||||
rows = _query_workbench_rows(db, ids).all()
|
||||
for row in rows:
|
||||
row.data_state = DATA_STATE_QUERIED
|
||||
row.queried_at = moment
|
||||
if rows and commit:
|
||||
db.commit()
|
||||
return len(rows)
|
||||
|
||||
|
||||
def mark_workbench_stale(
|
||||
db: Session,
|
||||
account_ids: Iterable[int],
|
||||
*,
|
||||
relogin_at: datetime | None = None,
|
||||
commit: bool = True,
|
||||
) -> int:
|
||||
"""人工重登后把工作台行标记为「待重新查询」。
|
||||
|
||||
重登可能失败(CK 仍失效),此时界面优先展示 CK 状态,`stale` 只在 CK 恢复
|
||||
有效后才作为「重登成功、待重新查询」参与批量查询。
|
||||
"""
|
||||
ids = _normalized_ids(account_ids)
|
||||
if not ids:
|
||||
return 0
|
||||
moment = relogin_at or datetime.now(UTC)
|
||||
rows = _query_workbench_rows(db, ids).all()
|
||||
for row in rows:
|
||||
row.data_state = DATA_STATE_STALE
|
||||
row.relogin_at = moment
|
||||
if rows and commit:
|
||||
db.commit()
|
||||
return len(rows)
|
||||
|
||||
|
||||
def pending_query_ids(
|
||||
db: Session,
|
||||
*,
|
||||
user_id: int,
|
||||
visible_account_ids: Iterable[int] | None = None,
|
||||
) -> dict[str, list[int] | int]:
|
||||
"""返回本工作台里「需要查询」的账号:导入未查 + 重登待重查,且当前有 CK。
|
||||
|
||||
无 CK 的账号查了也会失败,交给重登流程,不进批量查询。
|
||||
"""
|
||||
visible = _normalized_ids(visible_account_ids or [])
|
||||
query = (
|
||||
db.query(
|
||||
DouyuWorkbenchAccount.account_id,
|
||||
DouyuWorkbenchAccount.data_state,
|
||||
Account.cookie,
|
||||
)
|
||||
.join(Account, Account.id == DouyuWorkbenchAccount.account_id)
|
||||
.filter(
|
||||
DouyuWorkbenchAccount.user_id == user_id,
|
||||
DouyuWorkbenchAccount.handbook_scope == QUERY_WORKBENCH_SCOPE,
|
||||
DouyuWorkbenchAccount.data_state.in_(
|
||||
(DATA_STATE_IMPORTED, DATA_STATE_STALE)
|
||||
),
|
||||
Account.cookie.isnot(None),
|
||||
Account.cookie != "",
|
||||
or_(Account.status.is_(None), Account.status != "invalid"),
|
||||
or_(
|
||||
Account.ck_check_status.is_(None),
|
||||
Account.ck_check_status.notin_(("invalid", "error")),
|
||||
),
|
||||
)
|
||||
)
|
||||
if visible:
|
||||
query = query.filter(DouyuWorkbenchAccount.account_id.in_(visible))
|
||||
imported_ids: list[int] = []
|
||||
stale_ids: list[int] = []
|
||||
for account_id, data_state, _cookie in query.order_by(
|
||||
DouyuWorkbenchAccount.sort_order.asc(), DouyuWorkbenchAccount.id.asc()
|
||||
).all():
|
||||
if data_state == DATA_STATE_STALE:
|
||||
stale_ids.append(int(account_id))
|
||||
else:
|
||||
imported_ids.append(int(account_id))
|
||||
return {
|
||||
"imported_ids": imported_ids,
|
||||
"stale_ids": stale_ids,
|
||||
"account_ids": imported_ids + stale_ids,
|
||||
"total": len(imported_ids) + len(stale_ids),
|
||||
}
|
||||
|
||||
|
||||
def workbench_state_map(
|
||||
db: Session,
|
||||
*,
|
||||
user_id: int,
|
||||
account_ids: Iterable[int],
|
||||
handbook_scope: str = QUERY_WORKBENCH_SCOPE,
|
||||
) -> dict[int, DouyuWorkbenchAccount]:
|
||||
"""批量读取工作台行状态,供账号列表回填数据来源标记。"""
|
||||
ids = _normalized_ids(account_ids)
|
||||
if not ids:
|
||||
return {}
|
||||
rows = (
|
||||
db.query(DouyuWorkbenchAccount)
|
||||
.filter(
|
||||
DouyuWorkbenchAccount.user_id == user_id,
|
||||
DouyuWorkbenchAccount.handbook_scope == handbook_scope,
|
||||
DouyuWorkbenchAccount.account_id.in_(ids),
|
||||
)
|
||||
.all()
|
||||
)
|
||||
return {int(row.account_id): row for row in rows}
|
||||
@@ -452,6 +452,25 @@ class DouyuBatchRunnerCore:
|
||||
message=message,
|
||||
)
|
||||
db.commit()
|
||||
if (
|
||||
status == "success"
|
||||
and task.account_id
|
||||
and str(task.task_type or "") == "query_assets_and_bind"
|
||||
):
|
||||
# 查询工作台靠这行区分「导入后的旧数据」与「一键查询后的新数据」。
|
||||
try:
|
||||
from .douyu_query_workbench import mark_workbench_queried
|
||||
|
||||
mark_workbench_queried(
|
||||
db,
|
||||
[int(task.account_id)],
|
||||
queried_at=task.finished_at,
|
||||
)
|
||||
except Exception: # noqa: BLE001 - 工作台状态不能反噬任务结果
|
||||
logger.exception(
|
||||
"[douyu] 标记查询工作台已查询失败: task_id={}", task_id
|
||||
)
|
||||
db.rollback()
|
||||
self._push_task_event(task)
|
||||
if account is not None and event_context is not None:
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user