feat(login): 增加登录任务账号信息与筛选
This commit is contained in:
+160
-18
@@ -15,7 +15,7 @@ from fastapi import (
|
||||
)
|
||||
from fastapi.responses import StreamingResponse
|
||||
from sqlalchemy import and_, func, or_
|
||||
from sqlalchemy.orm import Session, load_only
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..database import SessionLocal, get_db
|
||||
from ..deps import authenticate_websocket, get_current_user, require_permission
|
||||
@@ -89,6 +89,43 @@ def _apply_status_filter(query, status: str | None):
|
||||
)
|
||||
|
||||
|
||||
def _apply_login_task_filters(
|
||||
query,
|
||||
*,
|
||||
status: str | None = None,
|
||||
search: str = "",
|
||||
tag: str = "",
|
||||
assigned_user_id: int | None = None,
|
||||
assign_status: str = "",
|
||||
ck_status: str = "",
|
||||
account_status: str = "",
|
||||
):
|
||||
"""给登录任务查询叠加账号维度筛选,列表/统计/导出/批删共用。"""
|
||||
query = _apply_status_filter(query, status)
|
||||
search_text = (search or "").strip()
|
||||
if search_text:
|
||||
query = query.filter(Account.username.ilike(f"%{search_text}%"))
|
||||
tag_text = (tag or "").strip()
|
||||
if tag_text:
|
||||
query = query.filter(Account.tag == tag_text)
|
||||
assign_scope = (assign_status or "").strip()
|
||||
if assign_scope == "unassigned":
|
||||
query = query.filter(Account.assigned_to.is_(None))
|
||||
elif isinstance(assigned_user_id, int) and not isinstance(assigned_user_id, bool):
|
||||
query = query.filter(Account.assigned_to == assigned_user_id)
|
||||
ck_text = (ck_status or "").strip()
|
||||
if ck_text == "unchecked":
|
||||
query = query.filter(
|
||||
or_(Account.ck_check_status.is_(None), Account.ck_check_status == "")
|
||||
)
|
||||
elif ck_text:
|
||||
query = query.filter(Account.ck_check_status == ck_text)
|
||||
account_status_text = (account_status or "").strip()
|
||||
if account_status_text:
|
||||
query = query.filter(Account.status == account_status_text)
|
||||
return query
|
||||
|
||||
|
||||
def _login_task_summary(query):
|
||||
"""按状态汇总登录任务,避免概览页拉完整任务列表。"""
|
||||
rows = (
|
||||
@@ -212,27 +249,45 @@ def _visible_tasks_query(db: Session, current: User, batch_id: str | None):
|
||||
|
||||
|
||||
def _serialize_tasks(db: Session, rows) -> list[LoginTaskOut]:
|
||||
"""把任务行拼上账号名后序列化。"""
|
||||
"""把任务行拼上当前账号信息后序列化。"""
|
||||
account_ids = [t.account_id for t in rows]
|
||||
accounts_map = {}
|
||||
accounts_map: dict[int, tuple] = {}
|
||||
if account_ids:
|
||||
accs = (
|
||||
db.query(Account)
|
||||
.options(load_only(Account.id, Account.username))
|
||||
for row in (
|
||||
db.query(
|
||||
Account.id,
|
||||
Account.username,
|
||||
Account.tag,
|
||||
Account.assigned_to,
|
||||
Account.status,
|
||||
Account.ck_check_status,
|
||||
Account.ck_checked_at,
|
||||
Account.cookie_updated_at,
|
||||
User.username,
|
||||
)
|
||||
.outerjoin(User, Account.assigned_to == User.id)
|
||||
.filter(Account.id.in_(account_ids))
|
||||
.all()
|
||||
)
|
||||
accounts_map = {a.id: a.username for a in accs}
|
||||
):
|
||||
accounts_map[int(row[0])] = row
|
||||
|
||||
result = []
|
||||
for t in rows:
|
||||
attempt_status = t.attempt_status or ""
|
||||
account = accounts_map.get(int(t.account_id))
|
||||
result.append(
|
||||
LoginTaskOut(
|
||||
id=t.id,
|
||||
batch_id=t.attempt_batch_id or t.batch_id,
|
||||
account_id=t.account_id,
|
||||
account_username=accounts_map.get(t.account_id, ""),
|
||||
account_username=(account[1] if account else "") or "",
|
||||
account_tag=(account[2] if account else "") or "",
|
||||
assigned_to=(account[3] if account else None),
|
||||
assigned_username=(account[8] if account else "") or "",
|
||||
account_status=(account[4] if account else "") or "",
|
||||
ck_check_status=(account[5] if account else "") or "",
|
||||
ck_checked_at=(account[6] if account else None),
|
||||
cookie_updated_at=(account[7] if account else None),
|
||||
status=attempt_status or t.status,
|
||||
cookie="",
|
||||
message=(t.attempt_message if attempt_status else "")
|
||||
@@ -254,6 +309,12 @@ def list_tasks(
|
||||
page: int | None = None,
|
||||
page_size: int = 50,
|
||||
status: str | None = None,
|
||||
search: str = "",
|
||||
tag: str = "",
|
||||
assigned_user_id: int | None = None,
|
||||
assign_status: str = "",
|
||||
ck_status: str = "",
|
||||
account_status: str = "",
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(get_current_user),
|
||||
):
|
||||
@@ -265,7 +326,16 @@ def list_tasks(
|
||||
传 `page` 时同时返回 `total`,是为了让前端能「全选所有筛选结果」——
|
||||
否则跨页选择无从下手,而任务量早就过了单页能装下的规模。
|
||||
"""
|
||||
query = _apply_status_filter(_visible_tasks_query(db, current, batch_id), status)
|
||||
query = _apply_login_task_filters(
|
||||
_visible_tasks_query(db, current, batch_id),
|
||||
status=status,
|
||||
search=search,
|
||||
tag=tag,
|
||||
assigned_user_id=assigned_user_id,
|
||||
assign_status=assign_status,
|
||||
ck_status=ck_status,
|
||||
account_status=account_status,
|
||||
)
|
||||
|
||||
ordered = query.order_by(LoginTask.id.desc())
|
||||
|
||||
@@ -284,6 +354,35 @@ def list_tasks(
|
||||
}
|
||||
|
||||
|
||||
@router.get("/tasks/filters")
|
||||
def task_filter_options(
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(get_current_user),
|
||||
):
|
||||
"""登录任务列表可用的标签与当前归属客服。"""
|
||||
base = _visible_tasks_query(db, current, None)
|
||||
tags = [
|
||||
row[0]
|
||||
for row in base.with_entities(Account.tag)
|
||||
.filter(Account.tag.isnot(None), Account.tag != "")
|
||||
.distinct()
|
||||
.order_by(Account.tag.asc())
|
||||
.all()
|
||||
]
|
||||
assigned_users = [
|
||||
{"id": int(user_id), "username": username or ""}
|
||||
for user_id, username in (
|
||||
base.with_entities(User.id, User.username)
|
||||
.join(User, Account.assigned_to == User.id)
|
||||
.filter(User.role == "support")
|
||||
.distinct()
|
||||
.order_by(User.username.asc())
|
||||
.all()
|
||||
)
|
||||
]
|
||||
return {"tags": tags, "assigned_users": assigned_users}
|
||||
|
||||
|
||||
def _fmt_dt(value) -> str:
|
||||
"""导出用的时间格式。
|
||||
|
||||
@@ -299,6 +398,12 @@ def _fmt_dt(value) -> str:
|
||||
def export_tasks(
|
||||
batch_id: str | None = None,
|
||||
status: str | None = None,
|
||||
search: str = "",
|
||||
tag: str = "",
|
||||
assigned_user_id: int | None = None,
|
||||
assign_status: str = "",
|
||||
ck_status: str = "",
|
||||
account_status: str = "",
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(get_current_user),
|
||||
):
|
||||
@@ -313,19 +418,28 @@ def export_tasks(
|
||||
- 失败分型复用登录失败那套(`login_failure.classify_failure`),
|
||||
与账号时间线上写的是同一份判据,不会出现「页面上算 A、导出算 B」。
|
||||
"""
|
||||
query = _apply_status_filter(_visible_tasks_query(db, current, batch_id), status)
|
||||
query = _apply_login_task_filters(
|
||||
_visible_tasks_query(db, current, batch_id),
|
||||
status=status,
|
||||
search=search,
|
||||
tag=tag,
|
||||
assigned_user_id=assigned_user_id,
|
||||
assign_status=assign_status,
|
||||
ck_status=ck_status,
|
||||
account_status=account_status,
|
||||
)
|
||||
rows = query.order_by(LoginTask.id.desc()).all()
|
||||
|
||||
account_ids = [t.account_id for t in rows]
|
||||
info: dict[int, tuple[str, str, str]] = {}
|
||||
if account_ids:
|
||||
for acc_id, username, tag, owner in (
|
||||
for acc_id, username, account_tag, owner in (
|
||||
db.query(Account.id, Account.username, Account.tag, User.username)
|
||||
.outerjoin(User, Account.assigned_to == User.id)
|
||||
.filter(Account.id.in_(account_ids))
|
||||
.all()
|
||||
):
|
||||
info[acc_id] = (username or "", tag or "", owner or "")
|
||||
info[acc_id] = (username or "", account_tag or "", owner or "")
|
||||
|
||||
output = io.StringIO()
|
||||
writer = csv.writer(output)
|
||||
@@ -380,13 +494,28 @@ def export_tasks(
|
||||
|
||||
@router.get("/tasks/summary")
|
||||
def tasks_summary(
|
||||
batch_id: str | None = None,
|
||||
status: str | None = None,
|
||||
search: str = "",
|
||||
tag: str = "",
|
||||
assigned_user_id: int | None = None,
|
||||
assign_status: str = "",
|
||||
ck_status: str = "",
|
||||
account_status: str = "",
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(get_current_user),
|
||||
):
|
||||
"""登录任务统计。"""
|
||||
query = db.query(LoginTask).join(Account, LoginTask.account_id == Account.id)
|
||||
if not user_has_permission(current, "login:view_all"):
|
||||
query = query.filter(Account.assigned_to == current.id)
|
||||
query = _apply_login_task_filters(
|
||||
_visible_tasks_query(db, current, batch_id),
|
||||
status=status,
|
||||
search=search,
|
||||
tag=tag,
|
||||
assigned_user_id=assigned_user_id,
|
||||
assign_status=assign_status,
|
||||
ck_status=ck_status,
|
||||
account_status=account_status,
|
||||
)
|
||||
return _login_task_summary(query)
|
||||
|
||||
|
||||
@@ -445,6 +574,12 @@ def delete_tasks(
|
||||
task_ids: str = "",
|
||||
batch_id: str | None = None,
|
||||
status: str | None = None,
|
||||
search: str = "",
|
||||
tag: str = "",
|
||||
assigned_user_id: int | None = None,
|
||||
assign_status: str = "",
|
||||
ck_status: str = "",
|
||||
account_status: str = "",
|
||||
all_matching: bool = False,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("login:batch")),
|
||||
@@ -474,8 +609,15 @@ def delete_tasks(
|
||||
# 所以先按权限与筛选条件查出 id,再按主键删。
|
||||
# 状态筛选与列表/导出共用一份 —— 「清空失败」以前只删 `failed`、
|
||||
# 漏掉 `error`,删完与页面上那个「失败 N」的数还对不上。
|
||||
query = _apply_status_filter(
|
||||
_visible_tasks_query(db, current, batch_id), status
|
||||
query = _apply_login_task_filters(
|
||||
_visible_tasks_query(db, current, batch_id),
|
||||
status=status,
|
||||
search=search,
|
||||
tag=tag,
|
||||
assigned_user_id=assigned_user_id,
|
||||
assign_status=assign_status,
|
||||
ck_status=ck_status,
|
||||
account_status=account_status,
|
||||
)
|
||||
ids = [row[0] for row in query.with_entities(LoginTask.id).all()]
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user