Skip to content

Commit

Permalink
fix(backend): 全站搜索、集群列表优化 #8719
Browse files Browse the repository at this point in the history
  • Loading branch information
ygcyao committed Jan 8, 2025
1 parent e808e3d commit e3d89dc
Show file tree
Hide file tree
Showing 16 changed files with 221 additions and 67 deletions.
4 changes: 2 additions & 2 deletions dbm-ui/backend/db_services/bigdata/resources/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def _to_cluster_representation(
cls,
cluster: Cluster,
cluster_entry: List[Dict[str, str]],
db_module_names_map: Dict[int, str],
db_module_config_map: Dict[int, Dict[str, str]],
cluster_entry_map: Dict[int, Dict[str, str]],
cluster_operate_records_map: Dict[int, List],
cloud_info: Dict[str, Any],
Expand All @@ -118,7 +118,7 @@ def _to_cluster_representation(
cluster_info = super()._to_cluster_representation(
cluster,
cluster_entry,
db_module_names_map,
db_module_config_map,
cluster_entry_map,
cluster_operate_records_map,
cloud_info,
Expand Down
36 changes: 27 additions & 9 deletions dbm-ui/backend/db_services/dbbase/resources/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
from django.http import HttpResponse
from django.utils.translation import ugettext_lazy as _

from backend.configuration.constants import DBType
from backend.constants import IP_PORT_DIVIDER
from backend.db_meta.enums import ClusterEntryType, ClusterType, InstanceRole
from backend.db_meta.enums.comm import SystemTagEnum
from backend.db_meta.models import AppCache, Cluster, ClusterEntry, DBModule, Machine, ProxyInstance, StorageInstance
from backend.db_services.cmdb.biz import list_modules_by_biz
from backend.db_services.dbbase.instances.handlers import InstanceHandler
from backend.db_services.dbbase.resources.query_base import (
build_q_for_domain_by_cluster,
Expand Down Expand Up @@ -485,7 +487,11 @@ def _filter_cluster_hook(
# 预取proxy_queryset,storage_queryset,clusterentry_set,加块查询效率
cluster_list = cluster_queryset[offset : limit + offset].prefetch_related(
Prefetch("proxyinstance_set", queryset=proxy_queryset.select_related("machine"), to_attr="proxies"),
Prefetch("storageinstance_set", queryset=storage_queryset.select_related("machine"), to_attr="storages"),
Prefetch(
"storageinstance_set",
queryset=storage_queryset.select_related("machine").prefetch_related("as_ejector", "as_receiver"),
to_attr="storages",
),
Prefetch("clusterentry_set", to_attr="entries"),
"tag_set",
)
Expand All @@ -496,14 +502,26 @@ def _filter_cluster_hook(
# 获取集群与访问入口的映射
cluster_entry_map = ClusterEntry.get_cluster_entry_map(cluster_ids)

# 获取DB模块的映射信息
db_module_names_map = {
module["db_module_id"]: module["db_module_name"]
# 获取DB模块dbconfig的映射信息
db_module_config_map = {
module["db_module_id"]: {
"bk_biz_id": module["bk_biz_id"],
"db_module_id": module["db_module_id"],
"name": module["db_module_name"],
"alias_name": module["alias_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"
"bk_biz_id", "db_module_id", "db_module_name", "alias_name"
)
}

# 补充db类型为mysql、tendbcluster、sqlserver的dbconfig信息
for cluster_type in cls.cluster_types:
db_type = ClusterType.cluster_type_to_db_type(cluster_type)
if db_type in [DBType.MySQL, DBType.TenDBCluster, DBType.Sqlserver]:
list_module = list_modules_by_biz(bk_biz_id, cluster_type)
db_module_config_map.update({module["db_module_id"]: module for module in list_module})

# 获取集群操作记录的映射关系
cluster_operate_records_map = ClusterOperateRecord.get_cluster_records_map(cluster_ids)

Expand All @@ -523,7 +541,7 @@ def _filter_cluster_hook(
{"cluster_entry_type": entry.cluster_entry_type, "entry": entry.entry, "role": entry.role}
for entry in cluster.entries
],
db_module_names_map=db_module_names_map,
db_module_config_map=db_module_config_map,
cluster_entry_map=cluster_entry_map,
cluster_operate_records_map=cluster_operate_records_map,
cloud_info=cloud_info,
Expand All @@ -540,7 +558,7 @@ def _to_cluster_representation(
cls,
cluster: Cluster,
cluster_entry: List[Dict[str, str]],
db_module_names_map: Dict[int, str],
db_module_config_map: Dict[int, Dict[str, str]],
cluster_entry_map: Dict[int, Dict[str, str]],
cluster_operate_records_map: Dict[int, List],
cloud_info: Dict[str, Any],
Expand All @@ -552,7 +570,7 @@ def _to_cluster_representation(
将集群对象转为可序列化的 dict 结构
@param cluster: model Cluster 对象, 增加了 storages 和 proxies 属性
@param cluster_entry: 集群的访问入口列表
@param db_module_names_map: key 是 db_module_id, value 是 db_module_name
@param db_module_config_map: key 是 db_module_id, value 是 当前集群对应的 dbconfig 映射
@param cluster_entry_map: key 是 cluster.id, value 是当前集群对应的 entry 映射
@param cluster_operate_records_map: key 是 cluster.id, value 是当前集群对应的 操作记录 映射
"""
Expand Down Expand Up @@ -583,7 +601,7 @@ def _to_cluster_representation(
"major_version": cluster.major_version,
"region": cluster.region,
"city": cluster.region,
"db_module_name": db_module_names_map.get(cluster.db_module_id, ""),
"db_module_infos": db_module_config_map.get(cluster.db_module_id, {}),
"db_module_id": cluster.db_module_id,
"creator": cluster.creator,
"updater": cluster.updater,
Expand Down
23 changes: 23 additions & 0 deletions dbm-ui/backend/db_services/dbbase/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from backend.configuration.constants import DBType
from backend.db_dirty.models import DirtyMachine
from backend.db_meta.enums import ClusterPhase, ClusterType
from backend.db_meta.models import Cluster
from backend.db_services.dbbase.constants import ResourceType
from backend.db_services.dbbase.resources.serializers import ListClusterEntriesSLZ, ListResourceSLZ
from backend.db_services.ipchooser.query.resource import ResourceQueryHelper
Expand Down Expand Up @@ -211,3 +212,25 @@ class QueryClusterCapSerializer(serializers.Serializer):
class QueryClusterCapResponseSerializer(serializers.Serializer):
class Meta:
swagger_schema_fields = {"example": {"cluster1": {"used": 1, "total": 2, "in_use": 50}}}


class UpdateClusterAliasSerializer(serializers.Serializer):
bk_biz_id = serializers.IntegerField(help_text=_("业务ID"))
cluster_id = serializers.IntegerField(help_text=_("集群ID"))
new_alias = serializers.CharField(help_text=_("新集群别名"))

def validate(self, attrs):
bk_biz_id = attrs.get("bk_biz_id")
cluster_id = attrs.get("cluster_id")
new_alias = attrs.get("new_alias")

try:
cluster = Cluster.objects.get(bk_biz_id=bk_biz_id, id=cluster_id)
except Cluster.DoesNotExist:
raise serializers.ValidationError(_("Cluster with the given ID does not exist."))

# 验证新别名不能与原集群名相同
if cluster.alias == new_alias:
raise serializers.ValidationError(_("The new alias cannot be the same as the current alias."))

return attrs
17 changes: 17 additions & 0 deletions dbm-ui/backend/db_services/dbbase/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from backend.db_services.dbbase.instances.yasg_slz import CheckInstancesResSLZ, CheckInstancesSLZ
from backend.db_services.dbbase.resources import register
from backend.db_services.dbbase.resources.query import ListRetrieveResource, ResourceList
from backend.db_services.dbbase.resources.serializers import ClusterSLZ
from backend.db_services.dbbase.serializers import (
ClusterDbTypeSerializer,
ClusterEntryFilterSerializer,
Expand All @@ -45,6 +46,7 @@
QueryClusterCapSerializer,
QueryClusterInstanceCountSerializer,
ResourceAdministrationSerializer,
UpdateClusterAliasSerializer,
WebConsoleResponseSerializer,
WebConsoleSerializer,
)
Expand Down Expand Up @@ -407,3 +409,18 @@ def query_cluster_stat(self, request, *args, **kwargs):
cluster_stat_map = {cluster_domain_map[domain]: cap for domain, cap in cluster_stat_map.items()}

return Response(cluster_stat_map)

@common_swagger_auto_schema(
operation_summary=_("更新集群别名"),
request_body=UpdateClusterAliasSerializer(),
tags=[SWAGGER_TAG],
)
@action(methods=["POST"], detail=False, serializer_class=UpdateClusterAliasSerializer)
def update_cluster_alias(self, request):
validated_data = self.params_validate(self.get_serializer_class())
"""更新集群别名"""
cluster = Cluster.objects.get(bk_biz_id=validated_data["bk_biz_id"], id=validated_data["cluster_id"])
cluster.alias = validated_data["new_alias"]
cluster.save(update_fields=["alias"])
serializer = ClusterSLZ(cluster)
return Response(serializer.data)
4 changes: 2 additions & 2 deletions dbm-ui/backend/db_services/mongodb/resources/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def _to_cluster_representation(
cls,
cluster: Cluster,
cluster_entry: List[Dict[str, str]],
db_module_names_map: Dict[int, str],
db_module_config_map: Dict[int, Dict[str, str]],
cluster_entry_map: Dict[int, Dict[str, str]],
cluster_operate_records_map: Dict[int, List],
cloud_info: Dict[str, Any],
Expand Down Expand Up @@ -204,7 +204,7 @@ def _to_cluster_representation(
cluster_info = super()._to_cluster_representation(
cluster,
cluster_entry,
db_module_names_map,
db_module_config_map,
cluster_entry_map,
cluster_operate_records_map,
cloud_info,
Expand Down
9 changes: 9 additions & 0 deletions dbm-ui/backend/db_services/mongodb/resources/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
)
from backend.db_services.mongodb.resources import constants, yasg_slz
from backend.db_services.mongodb.resources.query import MongoDBListRetrieveResource
from backend.iam_app.dataclass import ResourceEnum
from backend.iam_app.dataclass.actions import ActionEnum
from backend.iam_app.handlers.drf_perm.base import DBManagePermission

Expand Down Expand Up @@ -103,6 +104,14 @@ class MongoDBViewSet(ResourceViewSet):

list_perm_actions = [ActionEnum.MONGODB_VIEW, ActionEnum.MONGODB_ENABLE_DISABLE, ActionEnum.MONGODB_DESTROY]
list_instance_perm_actions = [ActionEnum.MONGODB_VIEW]
list_external_perm_actions = [ActionEnum.ACCESS_ENTRY_EDIT]

@staticmethod
def _external_perm_param_field(kwargs):
return {
ResourceEnum.BUSINESS.id: kwargs["bk_biz_id"],
ResourceEnum.DBTYPE.id: kwargs["view_class"].db_type.value,
}

@common_swagger_auto_schema(
operation_summary=_("获取实例的角色类型"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def _to_cluster_representation(
cls,
cluster: Cluster,
cluster_entry: List[Dict[str, str]],
db_module_names_map: Dict[int, str],
db_module_config_map: Dict[int, Dict[str, str]],
cluster_entry_map: Dict[int, Dict[str, str]],
cluster_operate_records_map: Dict[int, List],
cloud_info: Dict[str, Any],
Expand Down Expand Up @@ -162,7 +162,7 @@ def get_remote_infos(insts: List[StorageInstance]):
cluster_info = super()._to_cluster_representation(
cluster,
cluster_entry,
db_module_names_map,
db_module_config_map,
cluster_entry_map,
cluster_operate_records_map,
cloud_info,
Expand Down
4 changes: 2 additions & 2 deletions dbm-ui/backend/db_services/mysql/resources/tendbha/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def _to_cluster_representation(
cls,
cluster: Cluster,
cluster_entry: List[Dict[str, str]],
db_module_names_map: Dict[int, str],
db_module_config_map: Dict[int, Dict[str, str]],
cluster_entry_map: Dict[int, Dict[str, str]],
cluster_operate_records_map: Dict[int, List],
cloud_info: Dict[str, Any],
Expand Down Expand Up @@ -155,7 +155,7 @@ def _to_cluster_representation(
cluster_info = super()._to_cluster_representation(
cluster,
cluster_entry,
db_module_names_map,
db_module_config_map,
cluster_entry_map,
cluster_operate_records_map,
cloud_info,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def _to_cluster_representation(
cls,
cluster: Cluster,
cluster_entry: List[Dict[str, str]],
db_module_names_map: Dict[int, str],
db_module_config_map: Dict[int, Dict[str, str]],
cluster_entry_map: Dict[int, Dict[str, str]],
cluster_operate_records_map: Dict[int, List],
cloud_info: Dict[str, Any],
Expand All @@ -63,7 +63,7 @@ def _to_cluster_representation(
cluster_info = super()._to_cluster_representation(
cluster,
cluster_entry,
db_module_names_map,
db_module_config_map,
cluster_entry_map,
cluster_operate_records_map,
cloud_info,
Expand Down
3 changes: 1 addition & 2 deletions dbm-ui/backend/db_services/quick_search/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@


class ResourceType(str, StructuredEnum):
CLUSTER_NAME = EnumField("cluster_name", _("集群名"))
CLUSTER_DOMAIN = EnumField("cluster_domain", _("集群域名"))
ENTRY = EnumField("entry", _("访问入口"))
INSTANCE = EnumField("instance", _("实例"))
TICKET = EnumField("ticket", _("单号"))
TASK = EnumField("task", _("任务"))
Expand Down
Loading

0 comments on commit e3d89dc

Please sign in to comment.