优化get_local_apps函数
This commit is contained in:
parent
1c99d58d64
commit
9c23d4bbfe
@ -7,6 +7,48 @@ from jcloud.api.client import dashboard_whitelist
|
|||||||
from jcloud.utils import get_current_team
|
from jcloud.utils import get_current_team
|
||||||
|
|
||||||
|
|
||||||
|
def _get_team_user_names(apps):
|
||||||
|
"""批量获取团队关联用户名称,避免N+1查询问题"""
|
||||||
|
if not apps:
|
||||||
|
return {}
|
||||||
|
|
||||||
|
# 收集所有团队ID
|
||||||
|
team_ids = [app.team for app in apps if app.team]
|
||||||
|
if not team_ids:
|
||||||
|
return {}
|
||||||
|
|
||||||
|
# 批量获取团队信息
|
||||||
|
teams = jingrow.get_all(
|
||||||
|
"Team",
|
||||||
|
filters={"name": ["in", team_ids]},
|
||||||
|
fields=["name", "user"]
|
||||||
|
)
|
||||||
|
|
||||||
|
# 收集所有用户ID
|
||||||
|
user_ids = [team.user for team in teams if team.user]
|
||||||
|
if not user_ids:
|
||||||
|
return {}
|
||||||
|
|
||||||
|
# 批量获取用户信息
|
||||||
|
users = jingrow.get_all(
|
||||||
|
"User",
|
||||||
|
filters={"name": ["in", user_ids]},
|
||||||
|
fields=["name"]
|
||||||
|
)
|
||||||
|
|
||||||
|
# 构建映射关系
|
||||||
|
team_to_user = {team.name: team.user for team in teams if team.user}
|
||||||
|
user_names = {user.name: user.name for user in users}
|
||||||
|
|
||||||
|
# 返回团队到用户名称的映射
|
||||||
|
result = {}
|
||||||
|
for team_id, user_id in team_to_user.items():
|
||||||
|
if user_id in user_names:
|
||||||
|
result[team_id] = user_names[user_id]
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
@jingrow.whitelist(allow_guest=True)
|
@jingrow.whitelist(allow_guest=True)
|
||||||
def get_local_apps(filters=None, order_by=None, limit_start=None, limit_page_length=None):
|
def get_local_apps(filters=None, order_by=None, limit_start=None, limit_page_length=None):
|
||||||
"""获取本地应用列表"""
|
"""获取本地应用列表"""
|
||||||
@ -38,6 +80,9 @@ def get_local_apps(filters=None, order_by=None, limit_start=None, limit_page_len
|
|||||||
limit_page_length=limit_page_length
|
limit_page_length=limit_page_length
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# 批量获取团队用户名称
|
||||||
|
team_user_names = _get_team_user_names(apps)
|
||||||
|
|
||||||
# 格式化返回数据
|
# 格式化返回数据
|
||||||
result = []
|
result = []
|
||||||
for app in apps:
|
for app in apps:
|
||||||
@ -49,7 +94,7 @@ def get_local_apps(filters=None, order_by=None, limit_start=None, limit_page_len
|
|||||||
"category": app.category,
|
"category": app.category,
|
||||||
"enabled": app.enabled,
|
"enabled": app.enabled,
|
||||||
"public": app.public,
|
"public": app.public,
|
||||||
"team": app.team,
|
"team": team_user_names.get(app.team),
|
||||||
"status": app.status,
|
"status": app.status,
|
||||||
"repository_url": app.repository_url,
|
"repository_url": app.repository_url,
|
||||||
"file_url": app.file_url,
|
"file_url": app.file_url,
|
||||||
@ -71,7 +116,9 @@ def get_local_app(name):
|
|||||||
|
|
||||||
app = jingrow.get_pg("Local App", name)
|
app = jingrow.get_pg("Local App", name)
|
||||||
|
|
||||||
# 获取应用详情
|
# 获取团队关联用户的名称
|
||||||
|
team_user_names = _get_team_user_names([app])
|
||||||
|
|
||||||
app_data = {
|
app_data = {
|
||||||
"name": app.name,
|
"name": app.name,
|
||||||
"app_name": app.app_name,
|
"app_name": app.app_name,
|
||||||
@ -81,7 +128,7 @@ def get_local_app(name):
|
|||||||
"category": app.category,
|
"category": app.category,
|
||||||
"enabled": app.enabled,
|
"enabled": app.enabled,
|
||||||
"public": app.public,
|
"public": app.public,
|
||||||
"team": app.team,
|
"team": team_user_names.get(app.team),
|
||||||
"status": app.status,
|
"status": app.status,
|
||||||
"repository_url": app.repository_url,
|
"repository_url": app.repository_url,
|
||||||
"file_url": app.file_url,
|
"file_url": app.file_url,
|
||||||
@ -214,6 +261,9 @@ def search_local_apps(query, filters=None, limit=20):
|
|||||||
limit=limit
|
limit=limit
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# 批量获取团队用户名称
|
||||||
|
team_user_names = _get_team_user_names(apps)
|
||||||
|
|
||||||
# 格式化返回数据
|
# 格式化返回数据
|
||||||
result = []
|
result = []
|
||||||
for app in apps:
|
for app in apps:
|
||||||
@ -225,7 +275,7 @@ def search_local_apps(query, filters=None, limit=20):
|
|||||||
"category": app.category,
|
"category": app.category,
|
||||||
"enabled": app.enabled,
|
"enabled": app.enabled,
|
||||||
"public": app.public,
|
"public": app.public,
|
||||||
"team": app.team,
|
"team": team_user_names.get(app.team),
|
||||||
"status": app.status,
|
"status": app.status,
|
||||||
"repository_url": app.repository_url,
|
"repository_url": app.repository_url,
|
||||||
"file_url": app.file_url,
|
"file_url": app.file_url,
|
||||||
@ -235,4 +285,4 @@ def search_local_apps(query, filters=None, limit=20):
|
|||||||
}
|
}
|
||||||
result.append(app_data)
|
result.append(app_data)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
Loading…
x
Reference in New Issue
Block a user