From 9c23d4bbfe34c1e259ef59bfebed804a879f3072 Mon Sep 17 00:00:00 2001 From: jingrow Date: Thu, 23 Oct 2025 02:24:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96get=5Flocal=5Fapps=E5=87=BD?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jcloud/api/local_app.py | 60 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 5 deletions(-) diff --git a/jcloud/api/local_app.py b/jcloud/api/local_app.py index 7c3d2cf..dd4de81 100644 --- a/jcloud/api/local_app.py +++ b/jcloud/api/local_app.py @@ -7,6 +7,48 @@ from jcloud.api.client import dashboard_whitelist 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) 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 ) + # 批量获取团队用户名称 + team_user_names = _get_team_user_names(apps) + # 格式化返回数据 result = [] 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, "enabled": app.enabled, "public": app.public, - "team": app.team, + "team": team_user_names.get(app.team), "status": app.status, "repository_url": app.repository_url, "file_url": app.file_url, @@ -71,7 +116,9 @@ def get_local_app(name): app = jingrow.get_pg("Local App", name) - # 获取应用详情 + # 获取团队关联用户的名称 + team_user_names = _get_team_user_names([app]) + app_data = { "name": app.name, "app_name": app.app_name, @@ -81,7 +128,7 @@ def get_local_app(name): "category": app.category, "enabled": app.enabled, "public": app.public, - "team": app.team, + "team": team_user_names.get(app.team), "status": app.status, "repository_url": app.repository_url, "file_url": app.file_url, @@ -214,6 +261,9 @@ def search_local_apps(query, filters=None, limit=20): limit=limit ) + # 批量获取团队用户名称 + team_user_names = _get_team_user_names(apps) + # 格式化返回数据 result = [] for app in apps: @@ -225,7 +275,7 @@ def search_local_apps(query, filters=None, limit=20): "category": app.category, "enabled": app.enabled, "public": app.public, - "team": app.team, + "team": team_user_names.get(app.team), "status": app.status, "repository_url": app.repository_url, "file_url": app.file_url, @@ -235,4 +285,4 @@ def search_local_apps(query, filters=None, limit=20): } result.append(app_data) - return result + return result \ No newline at end of file