perf(douyu): 优化任务列表查询 SQL
This commit is contained in:
+147
-25
@@ -291,6 +291,103 @@ def _visible_tasks_query(db: Session, current: User):
|
||||
raise HTTPException(status_code=403, detail="无权查看斗鱼任务")
|
||||
|
||||
|
||||
def _list_visible_tasks_for_support(
|
||||
db: Session,
|
||||
current: User,
|
||||
*,
|
||||
batch_id: str | None,
|
||||
handbook_scope: str | None,
|
||||
limit: int,
|
||||
offset: int = 0,
|
||||
) -> list[DouyuTask]:
|
||||
"""按归属/创建人和工作台范围拆成索引分支,合并后加载完整任务。"""
|
||||
owner_filters: list[tuple[Any, ...]] = [
|
||||
(DouyuTask.account_assigned_to == current.id,),
|
||||
(DouyuTask.created_by == current.id,),
|
||||
]
|
||||
scope_filters: list[tuple[Any, ...]]
|
||||
if handbook_scope:
|
||||
scope_filters = [
|
||||
(DouyuTask.handbook_scope == handbook_scope,),
|
||||
(
|
||||
DouyuTask.handbook_scope == "legacy",
|
||||
DouyuTask.task_type.in_(DOUYU_HANDBOOK_TASK_TYPES[handbook_scope]),
|
||||
),
|
||||
]
|
||||
else:
|
||||
scope_filters = [()]
|
||||
|
||||
take = offset + limit
|
||||
task_ids: list[int] = []
|
||||
for owner_filter in owner_filters:
|
||||
for scope_filter in scope_filters:
|
||||
query = db.query(DouyuTask.id).filter(*owner_filter, *scope_filter)
|
||||
if batch_id:
|
||||
query = query.filter(DouyuTask.batch_id == batch_id)
|
||||
rows = query.order_by(DouyuTask.id.desc()).limit(take).all()
|
||||
task_ids.extend(int(row[0]) for row in rows)
|
||||
|
||||
page_ids = sorted(set(task_ids), reverse=True)[offset : offset + limit]
|
||||
if not page_ids:
|
||||
return []
|
||||
|
||||
account_loader = joinedload(DouyuTask.account).load_only(
|
||||
Account.id,
|
||||
Account.username,
|
||||
Account.uid,
|
||||
Account.nickname,
|
||||
Account.assigned_to,
|
||||
)
|
||||
tasks_by_id = {
|
||||
task.id: task
|
||||
for task in (
|
||||
db.query(DouyuTask)
|
||||
.options(account_loader)
|
||||
.filter(DouyuTask.id.in_(page_ids))
|
||||
.all()
|
||||
)
|
||||
}
|
||||
return [tasks_by_id[task_id] for task_id in page_ids if task_id in tasks_by_id]
|
||||
|
||||
|
||||
def _count_visible_tasks_for_support(
|
||||
db: Session,
|
||||
current: User,
|
||||
*,
|
||||
batch_id: str | None,
|
||||
handbook_scope: str | None,
|
||||
) -> int:
|
||||
"""用索引分支精确计算归属/创建人并集的任务总数。"""
|
||||
if handbook_scope:
|
||||
scope_filters: list[tuple[Any, ...]] = [
|
||||
(DouyuTask.handbook_scope == handbook_scope,),
|
||||
(
|
||||
DouyuTask.handbook_scope == "legacy",
|
||||
DouyuTask.task_type.in_(DOUYU_HANDBOOK_TASK_TYPES[handbook_scope]),
|
||||
),
|
||||
]
|
||||
else:
|
||||
scope_filters = [()]
|
||||
|
||||
def count_owner(
|
||||
owner_filter: tuple[Any, ...], scope_filter: tuple[Any, ...]
|
||||
) -> int:
|
||||
query = db.query(func.count(DouyuTask.id)).filter(*owner_filter, *scope_filter)
|
||||
if batch_id:
|
||||
query = query.filter(DouyuTask.batch_id == batch_id)
|
||||
return int(query.scalar() or 0)
|
||||
|
||||
assigned_filter = (DouyuTask.account_assigned_to == current.id,)
|
||||
created_filter = (DouyuTask.created_by == current.id,)
|
||||
both_filter = assigned_filter + created_filter
|
||||
total = 0
|
||||
for scope_filter in scope_filters:
|
||||
total += count_owner(assigned_filter, scope_filter)
|
||||
total += count_owner(created_filter, scope_filter)
|
||||
total -= count_owner(both_filter, scope_filter)
|
||||
return total
|
||||
|
||||
|
||||
def _require_task_account_access(
|
||||
db: Session,
|
||||
current: User,
|
||||
@@ -996,33 +1093,58 @@ def list_tasks(
|
||||
current: User = Depends(require_permission("douyu:task")),
|
||||
):
|
||||
"""查看斗鱼任务记录,默认只返回最近 100 条。"""
|
||||
query = _visible_tasks_query(db, current)
|
||||
if batch_id:
|
||||
query = query.filter(DouyuTask.batch_id == batch_id)
|
||||
if handbook_scope:
|
||||
# 旧任务没有归属字段,按历史任务类型继续展示,但绝不会触发自动二维码弹窗。
|
||||
query = query.filter(
|
||||
or_(
|
||||
DouyuTask.handbook_scope == handbook_scope,
|
||||
(DouyuTask.handbook_scope == "legacy")
|
||||
& DouyuTask.task_type.in_(DOUYU_HANDBOOK_TASK_TYPES[handbook_scope]),
|
||||
)
|
||||
)
|
||||
total = None
|
||||
if page is not None:
|
||||
total = (
|
||||
query.enable_eagerloads(False)
|
||||
.order_by(None)
|
||||
.with_entities(func.count(DouyuTask.id))
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
query = query.order_by(DouyuTask.id.desc())
|
||||
if page is not None:
|
||||
query = query.offset((page - 1) * page_size).limit(page_size)
|
||||
offset = (page - 1) * page_size if page is not None else 0
|
||||
|
||||
if _can_view_all(current):
|
||||
# 管理员保留原有全量查询路径,避免改变管理端的可见范围。
|
||||
query = _visible_tasks_query(db, current)
|
||||
if batch_id:
|
||||
query = query.filter(DouyuTask.batch_id == batch_id)
|
||||
if handbook_scope:
|
||||
query = query.filter(
|
||||
or_(
|
||||
DouyuTask.handbook_scope == handbook_scope,
|
||||
(DouyuTask.handbook_scope == "legacy")
|
||||
& DouyuTask.task_type.in_(
|
||||
DOUYU_HANDBOOK_TASK_TYPES[handbook_scope]
|
||||
),
|
||||
)
|
||||
)
|
||||
if page is not None:
|
||||
total = (
|
||||
query.enable_eagerloads(False)
|
||||
.order_by(None)
|
||||
.with_entities(func.count(DouyuTask.id))
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
query = query.order_by(DouyuTask.id.desc())
|
||||
if page is not None:
|
||||
query = query.offset(offset).limit(page_size)
|
||||
else:
|
||||
query = query.limit(page_size)
|
||||
tasks = query.all()
|
||||
else:
|
||||
query = query.limit(page_size)
|
||||
result = [_task_out(task, include_detail=include_detail) for task in query.all()]
|
||||
if not user_has_permission(current, "account:view_assigned"):
|
||||
raise HTTPException(status_code=403, detail="无权查看斗鱼任务")
|
||||
tasks = _list_visible_tasks_for_support(
|
||||
db,
|
||||
current,
|
||||
batch_id=batch_id,
|
||||
handbook_scope=handbook_scope,
|
||||
limit=page_size,
|
||||
offset=offset,
|
||||
)
|
||||
if page is not None:
|
||||
total = _count_visible_tasks_for_support(
|
||||
db,
|
||||
current,
|
||||
batch_id=batch_id,
|
||||
handbook_scope=handbook_scope,
|
||||
)
|
||||
|
||||
result = [_task_out(task, include_detail=include_detail) for task in tasks]
|
||||
if page is not None:
|
||||
return {
|
||||
"items": result,
|
||||
|
||||
Reference in New Issue
Block a user