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
+11 -6
View File
@@ -55,9 +55,14 @@ def dashboard_summary(
"""返回首页所需的全部轻量统计。"""
douyu_accounts = 0
if user_has_permission(current, "account:view_all"):
douyu_accounts = db.query(Account).count()
douyu_accounts = db.query(func.count(Account.id)).scalar() or 0
elif user_has_permission(current, "account:view_assigned"):
douyu_accounts = db.query(Account).filter(Account.assigned_to == current.id).count()
douyu_accounts = (
db.query(func.count(Account.id))
.filter(Account.assigned_to == current.id)
.scalar()
or 0
)
login_tasks = _empty_task_summary()
if any(user_has_permission(current, permission) for permission in (
@@ -79,7 +84,7 @@ def dashboard_summary(
)
if not user_has_permission(current, "login:view_all"):
cookie_query = cookie_query.filter(Account.assigned_to == current.id)
cookies = cookie_query.count()
cookies = cookie_query.with_entities(func.count(LoginTask.id)).scalar() or 0
huya_accounts = 0
can_view_huya_accounts = any(user_has_permission(current, permission) for permission in (
@@ -91,7 +96,7 @@ def dashboard_summary(
huya_account_query = db.query(HuyaAccount)
if not _can_view_huya_all(current):
huya_account_query = huya_account_query.filter(HuyaAccount.assigned_to == current.id)
huya_accounts = huya_account_query.count()
huya_accounts = huya_account_query.with_entities(func.count(HuyaAccount.id)).scalar() or 0
huya_tasks = _empty_task_summary()
huya_goods = 0
@@ -101,8 +106,8 @@ def dashboard_summary(
if not _can_view_huya_all(current):
huya_task_query = huya_task_query.filter(HuyaAccount.assigned_to == current.id)
huya_tasks = _task_summary(huya_task_query, HuyaTask)
huya_goods = db.query(HuyaGoodsSnapshot).count()
huya_recharge_goods = db.query(HuyaRechargeGoodsSnapshot).count()
huya_goods = db.query(func.count(HuyaGoodsSnapshot.id)).scalar() or 0
huya_recharge_goods = db.query(func.count(HuyaRechargeGoodsSnapshot.id)).scalar() or 0
return {
"douyu": {