perf: 优化 MySQL 查询并接入 pytest

This commit is contained in:
yml2213
2026-08-30 18:26:54 +08:00
parent 1413b4f030
commit 9497e1ace6
10 changed files with 178 additions and 24 deletions
+10 -5
View File
@@ -5,7 +5,7 @@ from concurrent.futures import ThreadPoolExecutor, as_completed
from datetime import datetime, timezone
from fastapi import APIRouter, Depends, HTTPException, Query
from fastapi.responses import StreamingResponse
from sqlalchemy import case, or_
from sqlalchemy import case, func, or_
from sqlalchemy.orm import Session, defer, joinedload
import io
import csv
@@ -145,7 +145,7 @@ def list_cookies(
total = None
if page is not None:
total = query.order_by(None).count()
total = query.order_by(None).with_entities(func.count(LoginTask.id)).scalar() or 0
query = _order_cookie_tasks(query, selected_names)
if page is not None:
query = query.offset((page - 1) * page_size).limit(page_size)
@@ -209,8 +209,13 @@ def cookies_summary(
query = _visible_cookie_tasks_query(db, current)
if user_has_permission(current, "login:view_all"):
query = query.join(Account, LoginTask.account_id == Account.id)
total = query.count()
assigned_count = query.filter(Account.assigned_to.isnot(None)).count()
total = query.with_entities(func.count(LoginTask.id)).scalar() or 0
assigned_count = (
query.filter(Account.assigned_to.isnot(None))
.with_entities(func.count(LoginTask.id))
.scalar()
or 0
)
return {
"total": total,
"assigned_count": assigned_count,
@@ -242,7 +247,7 @@ def list_cookie_operations(
Account.tag.ilike(f"%{search_text}%"),
))
total = query.order_by(None).count()
total = query.order_by(None).with_entities(func.count(LoginTask.id)).scalar() or 0
tasks = (
query.order_by(LoginTask.finished_at.desc(), LoginTask.id.desc())
.offset((page - 1) * page_size)