修复登录任务页面加载慢的问题
- 后端: list_tasks 用 IN 批量查 Account 替代逐条查询,消除 N+1 - 后端: list_accounts 用 joinedload 预加载 assigned_user,消除懒加载 - 前端: 首次加载 accounts 和 tasks 并行请求 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
a996028f5b
commit
accb76b9da
@@ -11,6 +11,7 @@ from ..schemas import AccountImport, AccountAssign, AccountTag, AccountOut, Batc
|
|||||||
from ..deps import get_current_user, require_permission
|
from ..deps import get_current_user, require_permission
|
||||||
from ..permissions import has_permission
|
from ..permissions import has_permission
|
||||||
from sqlalchemy import func
|
from sqlalchemy import func
|
||||||
|
from sqlalchemy.orm import joinedload
|
||||||
|
|
||||||
router = APIRouter(prefix="/api/accounts", tags=["账号管理"])
|
router = APIRouter(prefix="/api/accounts", tags=["账号管理"])
|
||||||
|
|
||||||
@@ -64,7 +65,7 @@ def list_accounts(
|
|||||||
if tag:
|
if tag:
|
||||||
query = query.filter(Account.tag == tag)
|
query = query.filter(Account.tag == tag)
|
||||||
|
|
||||||
accounts = query.order_by(Account.id).all()
|
accounts = query.order_by(Account.id).options(joinedload(Account.assigned_user)).all()
|
||||||
result = []
|
result = []
|
||||||
for acc in accounts:
|
for acc in accounts:
|
||||||
item = AccountOut(
|
item = AccountOut(
|
||||||
|
|||||||
@@ -87,24 +87,28 @@ def list_tasks(
|
|||||||
current: User = Depends(get_current_user),
|
current: User = Depends(get_current_user),
|
||||||
):
|
):
|
||||||
"""查看登录任务列表。"""
|
"""查看登录任务列表。"""
|
||||||
query = db.query(LoginTask)
|
query = db.query(LoginTask).join(Account, LoginTask.account_id == Account.id)
|
||||||
|
|
||||||
# 客服只能看自己账号的任务
|
# 客服只能看自己账号的任务
|
||||||
if not has_permission(current.role, "login:view_all"):
|
if not has_permission(current.role, "login:view_all"):
|
||||||
query = query.join(Account, LoginTask.account_id == Account.id).filter(
|
query = query.filter(Account.assigned_to == current.id)
|
||||||
Account.assigned_to == current.id
|
|
||||||
)
|
|
||||||
|
|
||||||
if batch_id:
|
if batch_id:
|
||||||
query = query.filter(LoginTask.batch_id == batch_id)
|
query = query.filter(LoginTask.batch_id == batch_id)
|
||||||
|
|
||||||
tasks = query.order_by(LoginTask.id.desc()).limit(200).all()
|
rows = query.order_by(LoginTask.id.desc()).limit(200).all()
|
||||||
|
# 批量收集 account_id,一次性查出 username
|
||||||
|
account_ids = [t.account_id for t in rows]
|
||||||
|
accounts_map = {}
|
||||||
|
if account_ids:
|
||||||
|
accs = db.query(Account).filter(Account.id.in_(account_ids)).all()
|
||||||
|
accounts_map = {a.id: a.username for a in accs}
|
||||||
|
|
||||||
result = []
|
result = []
|
||||||
for t in tasks:
|
for t in rows:
|
||||||
acc = db.query(Account).filter(Account.id == t.account_id).first()
|
|
||||||
result.append(LoginTaskOut(
|
result.append(LoginTaskOut(
|
||||||
id=t.id, batch_id=t.batch_id, account_id=t.account_id,
|
id=t.id, batch_id=t.batch_id, account_id=t.account_id,
|
||||||
account_username=acc.username if acc else "",
|
account_username=accounts_map.get(t.account_id, ""),
|
||||||
status=t.status, cookie=t.cookie or "", message=t.message or "",
|
status=t.status, cookie=t.cookie or "", message=t.message or "",
|
||||||
created_by=t.created_by, created_at=t.created_at, finished_at=t.finished_at,
|
created_by=t.created_by, created_at=t.created_at, finished_at=t.finished_at,
|
||||||
))
|
))
|
||||||
|
|||||||
@@ -108,7 +108,7 @@ export default function LoginTasksPage() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
loadAccounts();
|
Promise.all([loadAccounts(), loadTasks()]);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// 日志自动滚动到底部
|
// 日志自动滚动到底部
|
||||||
|
|||||||
Reference in New Issue
Block a user