Skip to content

Commit

Permalink
fix(backend): 集群列表查询优化 #7781
Browse files Browse the repository at this point in the history
# Reviewed, transaction id: 23476
  • Loading branch information
ygcyao authored and iSecloud committed Nov 15, 2024
1 parent bfc3940 commit 12743dd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
11 changes: 8 additions & 3 deletions dbm-ui/backend/db_services/dbbase/resources/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,10 @@ def _filter_cluster_hook(

# 获取DB模块的映射信息
db_module_names_map = {
module.db_module_id: module.db_module_name
for module in DBModule.objects.filter(bk_biz_id=bk_biz_id, cluster_type__in=cls.cluster_types)
module["db_module_id"]: module["db_module_name"]
for module in DBModule.objects.filter(bk_biz_id=bk_biz_id, cluster_type__in=cls.cluster_types).values(
"db_module_id", "db_module_name"
)
}

# 获取集群操作记录的映射关系
Expand All @@ -490,6 +492,9 @@ def _filter_cluster_hook(

# 将集群的查询结果序列化为集群字典信息
clusters: List[Dict[str, Any]] = []
# 获取集群统计信息,只需要获取一次
cluster_stats_map = Cluster.get_cluster_stats(bk_biz_id, cls.cluster_types)

for cluster in cluster_list:
cluster_info = cls._to_cluster_representation(
cluster=cluster,
Expand All @@ -502,7 +507,7 @@ def _filter_cluster_hook(
cluster_operate_records_map=cluster_operate_records_map,
cloud_info=cloud_info,
biz_info=biz_info,
cluster_stats_map=Cluster.get_cluster_stats(bk_biz_id, cls.cluster_types),
cluster_stats_map=cluster_stats_map,
**kwargs,
)
clusters.append(cluster_info)
Expand Down
2 changes: 1 addition & 1 deletion dbm-ui/backend/ticket/models/ticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ def summary(self):
@classmethod
def get_cluster_records_map(cls, cluster_ids: List[int]):
"""获取集群与操作记录之间的映射关系"""
records = cls.objects.prefetch_related("ticket").filter(
records = cls.objects.select_related("ticket", "flow").filter(
cluster_id__in=cluster_ids, ticket__status=TicketFlowStatus.RUNNING
)
cluster_operate_records_map: Dict[int, List] = defaultdict(list)
Expand Down

0 comments on commit 12743dd

Please sign in to comment.