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:
yml
2026-11-30 20:04:26 +08:00
parent ecb987fff0
commit 6ad8025a86
16 changed files with 1336 additions and 98 deletions
+301
View File
@@ -515,6 +515,22 @@ class TestQueryWorkbenchRouterAndExport:
assert rows[3][12] == "已过期"
assert rows[2][17] == "可换绑"
assert rows[3][17] == "冷却中"
# 导出末尾两列区分「导入后的数据」与「一键查询后的数据」
assert rows[0][-2] == "数据来源"
assert rows[2][-2] == "导入未查询"
assert "导入于" in rows[2][-1]
from web.backend.services.douyu_query_workbench import mark_workbench_queried
mark_workbench_queried(self.session, [self.acc_valid.id])
resp = export_query_workbench_csv(
DouyuQueryWorkbenchExportRequest(ids=[self.acc_valid.id]),
db=self.session,
current=self.admin,
)
rows = list(csv.reader(io.StringIO(resp.body.decode("utf-8-sig"))))
assert rows[1][-2] == "已查询"
assert "查询于" in rows[1][-1]
def test_relogin_requires_login_batch_permission(self):
with pytest.raises(HTTPException) as exc:
@@ -653,3 +669,288 @@ def test_execution_worker_forwards_manual_category_for_relogin_batch(db, monkeyp
assert captured.get("mode") == "relogin"
assert captured.get("trigger_source") == "manual"
assert captured.get("category") == CATEGORY_MANUAL
class _MarkTaskHarness(DouyuBatchRunnerCore):
"""只用于驱动 `_mark_task` 的真实实现,绕过构造器里的代理/客户端依赖。"""
def __init__(self, db):
import threading
self.db = db
self.batch_id = "qw-mark-batch"
self.log_queue = None
self.loop = None
self.log_sink = None
self._hook_local = threading.local()
def _push_log(self, *args, **kwargs):
pass
class TestQueryWorkbenchDataState:
"""工作台必须能区分「导入后的数据」和「一键查询后的数据」。"""
def setup_method(self):
self.engine = get_test_engine()
self.session = make_test_session(self.engine)
self.admin = User(username="state-admin", password_hash="h", role="super_admin")
self.session.add(self.admin)
self.session.flush()
self.acc_a = Account(
username="state_a",
uid="9001",
password="pw",
email="a@e.com",
email_password="ep",
cookie="acf_uid=9001",
ck_check_status="valid",
assigned_to=self.admin.id,
)
self.acc_b = Account(
username="state_b",
uid="9002",
password="pw",
email="b@e.com",
email_password="ep",
cookie="acf_uid=9002",
ck_check_status="valid",
assigned_to=self.admin.id,
)
self.acc_nock = Account(
username="state_nock",
uid="",
password="pw",
email="c@e.com",
email_password="ep",
cookie="",
status="imported",
assigned_to=self.admin.id,
)
self.session.add_all([self.acc_a, self.acc_b, self.acc_nock])
self.session.commit()
def teardown_method(self):
self.session.close()
def _rows(self):
return {
int(row.account_id): row
for row in self.session.query(DouyuWorkbenchAccount).all()
}
def test_import_marks_imported_and_resave_preserves_state(self):
res = update_workbench_accounts(
DouyuWorkbenchAccountsUpdate(
handbook_scope="query", account_ids=[self.acc_a.id, self.acc_b.id]
),
db=self.session,
current=self.admin,
)
assert res["imported_account_ids"] == [self.acc_a.id, self.acc_b.id]
rows = self._rows()
assert rows[self.acc_a.id].data_state == "imported"
assert rows[self.acc_a.id].imported_at is not None
# 一键查询成功后应变成 queried
from web.backend.services.douyu_query_workbench import mark_workbench_queried
assert mark_workbench_queried(self.session, [self.acc_a.id]) == 1
rows = self._rows()
assert rows[self.acc_a.id].data_state == "queried"
assert rows[self.acc_a.id].queried_at is not None
# 再次导入新账号(增删集合)不能把已有行的查询状态重置掉
res = update_workbench_accounts(
DouyuWorkbenchAccountsUpdate(
handbook_scope="query",
account_ids=[self.acc_a.id, self.acc_b.id, self.acc_nock.id],
),
db=self.session,
current=self.admin,
)
assert res["imported_account_ids"] == [self.acc_nock.id]
rows = self._rows()
assert rows[self.acc_a.id].data_state == "queried"
assert rows[self.acc_b.id].data_state == "imported"
# 移出账号只删那一行
update_workbench_accounts(
DouyuWorkbenchAccountsUpdate(
handbook_scope="query", account_ids=[self.acc_a.id]
),
db=self.session,
current=self.admin,
)
assert set(self._rows().keys()) == {self.acc_a.id}
def test_pending_ids_excludes_no_cookie_and_filters_by_state(self):
update_workbench_accounts(
DouyuWorkbenchAccountsUpdate(
handbook_scope="query",
account_ids=[self.acc_a.id, self.acc_b.id, self.acc_nock.id],
),
db=self.session,
current=self.admin,
)
from web.backend.services.douyu_query_workbench import mark_workbench_stale
mark_workbench_stale(self.session, [self.acc_b.id])
from web.backend.routers.douyu import list_query_workbench_pending_ids
pending = list_query_workbench_pending_ids(db=self.session, current=self.admin)
assert pending["imported_ids"] == [self.acc_a.id]
assert pending["stale_ids"] == [self.acc_b.id]
assert pending["total"] == 2
stale_items = list_task_accounts(
search="",
tag="",
ids=",".join(
str(acc_id)
for acc_id in (self.acc_a.id, self.acc_b.id, self.acc_nock.id)
),
account_names="",
include_all=True,
workbench_scope="query",
workbench_state="stale",
page=None,
db=self.session,
current=self.admin,
)
assert [item.username for item in stale_items] == ["state_b"]
assert stale_items[0].workbench_data_state == "stale"
assert stale_items[0].workbench_relogin_at is not None
def test_ids_endpoint_supports_workbench_state_filter(self):
"""「一键查询筛选结果」依靠 /accounts/ids 的跨页筛选。"""
from web.backend.routers.douyu import list_task_account_ids
update_workbench_accounts(
DouyuWorkbenchAccountsUpdate(
handbook_scope="query",
account_ids=[self.acc_a.id, self.acc_b.id],
),
db=self.session,
current=self.admin,
)
from web.backend.services.douyu_query_workbench import mark_workbench_queried
mark_workbench_queried(self.session, [self.acc_b.id])
imported_only = list_task_account_ids(
search="",
tag="",
account_names="",
ck_status="",
include_all=True,
workbench_scope="query",
workbench_state="imported",
db=self.session,
current=self.admin,
)
assert imported_only["account_ids"] == [self.acc_a.id]
queried_only = list_task_account_ids(
search="",
tag="",
account_names="",
ck_status="",
include_all=True,
workbench_scope="query",
workbench_state="queried",
db=self.session,
current=self.admin,
)
assert queried_only["account_ids"] == [self.acc_b.id]
with pytest.raises(HTTPException) as exc:
list_task_account_ids(
search="",
tag="",
account_names="",
ck_status="",
include_all=True,
workbench_scope="query",
workbench_state="bogus",
db=self.session,
current=self.admin,
)
assert exc.value.status_code == 400
def test_relogin_marks_account_as_stale(self):
update_workbench_accounts(
DouyuWorkbenchAccountsUpdate(
handbook_scope="query", account_ids=[self.acc_a.id]
),
db=self.session,
current=self.admin,
)
self.acc_a.ck_check_status = "invalid"
self.session.commit()
res = relogin_query_workbench_invalid_accounts(
DouyuQueryWorkbenchReloginRequest(ids=[self.acc_a.id]),
db=self.session,
current=self.admin,
)
assert res["relogin_count"] == 1
row = self._rows()[self.acc_a.id]
assert row.data_state == "stale"
assert row.relogin_at is not None
def test_import_ck_check_skips_non_operable_ids_without_error(self):
"""导入整批传进来时,无 CK / 登录中的账号应被跳过而不是整批报错。"""
from web.backend.routers.douyu import check_task_accounts_ck
# 只传无 CK 账号:可检测集合为空,直接短路返回,不会发真实探测请求
res = check_task_accounts_ck(
ids=str(self.acc_nock.id),
db=self.session,
current=self.admin,
)
assert res["success"] is True
assert res["checked"] == 0
assert res["skipped"] == 1
assert res["results"] == []
def test_query_task_success_marks_queried_and_failure_keeps_state(self):
update_workbench_accounts(
DouyuWorkbenchAccountsUpdate(
handbook_scope="query", account_ids=[self.acc_a.id]
),
db=self.session,
current=self.admin,
)
runner = _MarkTaskHarness(self.session)
failed_task = DouyuTask(
batch_id="qw-mark-failed",
account_id=self.acc_a.id,
task_type="query_assets_and_bind",
handbook_scope="query",
status="running",
message="",
created_by=self.admin.id,
)
self.session.add(failed_task)
self.session.commit()
runner._mark_task(self.session, failed_task, "failed", "接口全失败", {})
assert self._rows()[self.acc_a.id].data_state == "imported"
ok_task = DouyuTask(
batch_id="qw-mark-ok",
account_id=self.acc_a.id,
task_type="query_assets_and_bind",
handbook_scope="query",
status="running",
message="",
created_by=self.admin.id,
)
self.session.add(ok_task)
self.session.commit()
runner._mark_task(self.session, ok_task, "success", "查询成功", {"points": 1})
row = self._rows()[self.acc_a.id]
assert row.data_state == "queried"
assert row.queried_at is not None