优化首页统计和任务请求

This commit is contained in:
yml2213
2026-08-05 10:41:40 +08:00
parent 2fda47c631
commit 335f5cf57b
11 changed files with 421 additions and 160 deletions
+44 -11
View File
@@ -236,10 +236,12 @@ def task_types(current: User = Depends(require_permission("douyu:task"))):
return SUPPORTED_DOUYU_TASK_TYPES
@router.get("/accounts", response_model=list[DouyuTaskAccountOut])
@router.get("/accounts")
def list_task_accounts(
search: str = Query(""),
ids: str = Query(""),
page: int | None = Query(None, ge=1),
page_size: int = Query(50, ge=1, le=200),
db: Session = Depends(get_db),
current: User = Depends(get_current_user),
):
@@ -262,8 +264,18 @@ def list_task_accounts(
Account.game_name.ilike(pattern),
Account.esports_game_name.ilike(pattern),
))
rows = query.order_by(Account.id.desc()).limit(500).all()
return [_account_out(account) for account in rows]
total = None
if page is not None:
total = query.order_by(None).count()
query = query.order_by(Account.id.desc())
if page is not None:
query = query.offset((page - 1) * page_size).limit(page_size)
else:
query = query.limit(500)
result = [_account_out(account) for account in query.all()]
if page is not None:
return {"items": result, "total": total or 0, "page": page, "page_size": page_size}
return result
@router.get("/config", response_model=DouyuConfigOut)
@@ -366,25 +378,46 @@ async def create_task_batch(
return {"batch_id": batch_id, "count": count, "success": True}
@router.get("/tasks", response_model=list[DouyuTaskOut])
@router.get("/tasks")
def list_tasks(
batch_id: str | None = None,
include_detail: bool = Query(False, description="是否返回完整任务结果(默认否,轮询请保持 false)"),
page: int | None = Query(None, ge=1),
page_size: int = Query(100, ge=1, le=500),
db: Session = Depends(get_db),
current: User = Depends(require_permission("douyu:task")),
):
"""查看斗鱼任务记录。"""
cleanup_orphan_douyu_tasks(
"""查看斗鱼任务记录,默认只返回最近 100 条"""
query = _visible_tasks_query(db, current)
if batch_id:
query = query.filter(DouyuTask.batch_id == batch_id)
total = None
if page is not None:
total = query.enable_eagerloads(False).order_by(None).count()
query = query.order_by(DouyuTask.id.desc())
if page is not None:
query = query.offset((page - 1) * page_size).limit(page_size)
else:
query = query.limit(page_size)
result = [_task_out(task, include_detail=include_detail) for task in query.all()]
if page is not None:
return {"items": result, "total": total or 0, "page": page, "page_size": page_size}
return result
@router.post("/tasks/cleanup-orphans")
def cleanup_orphan_tasks(
db: Session = Depends(get_db),
current: User = Depends(require_permission("douyu:task")),
):
"""手动清理没有内存执行器接管的斗鱼任务。"""
cleaned = cleanup_orphan_douyu_tasks(
db,
active_batch_ids=douyu_batch_registry.active_ids(),
statuses=("pending", "running"),
message="任务已中断(无执行器接管)",
)
query = _visible_tasks_query(db, current)
if batch_id:
query = query.filter(DouyuTask.batch_id == batch_id)
rows = query.order_by(DouyuTask.id.desc()).limit(300).all()
return [_task_out(task, include_detail=include_detail) for task in rows]
return {"message": f"已清理 {cleaned} 个残留任务", "cleaned": cleaned, "success": True}
@router.get("/tasks/{task_id}", response_model=DouyuTaskOut)