Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(backend): 集群列表查询优化 #7781 #7783

Open
wants to merge 1 commit into
base: v1.5.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions dbm-ui/backend/db_services/dbbase/resources/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
specific language governing permissions and limitations under the License.
"""
import abc
from collections import defaultdict
from typing import Any, Callable, Dict, List, Tuple, Union

import attr
Expand All @@ -17,7 +18,7 @@
from django.utils.translation import ugettext_lazy as _

from backend.constants import IP_PORT_DIVIDER
from backend.db_meta.enums import ClusterEntryType, ClusterType, InstanceRole
from backend.db_meta.enums import ClusterEntryRole, ClusterEntryType, ClusterType, InstanceRole
from backend.db_meta.enums.comm import SystemTagEnum
from backend.db_meta.models import (
AppCache,
Expand Down Expand Up @@ -502,12 +503,13 @@ def _filter_cluster_hook(
cluster_ids = [c.id for c in cluster_queryset]

# 获取集群与访问入口的映射
cluster_entry_map = ClusterEntry.get_cluster_entry_map(cluster_ids)
# cluster_entry_map = ClusterEntry.get_cluster_entry_map(cluster_ids)

# 获取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 @@ -519,19 +521,42 @@ def _filter_cluster_hook(

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

for cluster in cluster_list:
cluster_entry_map.setdefault(cluster.id, {})

# 存储cluster_entry列表
cluster_entries = []

for entry in cluster.entries:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里重复代码太多了,尝试给这个函数多加一个 entries参数吧

def get_cluster_entry_map(cls, cluster_ids: List[int], entries: List[ClusterEntry] = None) -> Dict[int, Dict[str, str]]:

access_entry = entry.entry

if entry.cluster_entry_type != ClusterEntryType.DNS:
cluster_entry_map[cluster.id][entry.cluster_entry_type] = access_entry
# DNS 需额外区分主域名和从域名, entry 中 cluster.immute_domain 为主域名
# 如果entry.role为slave_entry,则为从域名
if access_entry == entry.cluster.immute_domain:
cluster_entry_map[cluster.id]["master_domain"] = access_entry
elif entry.role == ClusterEntryRole.SLAVE_ENTRY:
cluster_entry_map[cluster.id]["slave_domain"] = access_entry

cluster_entries.append(
{"cluster_entry_type": entry.cluster_entry_type, "entry": access_entry, "role": entry.role}
)

# 构建 cluster_info
cluster_info = cls._to_cluster_representation(
cluster=cluster,
cluster_entry=[
{"cluster_entry_type": entry.cluster_entry_type, "entry": entry.entry, "role": entry.role}
for entry in cluster.entries
],
cluster_entry=cluster_entries,
db_module_names_map=db_module_names_map,
cluster_entry_map=cluster_entry_map,
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
Loading