From 66326f920bc741ba85c06184ff826d528989b16d Mon Sep 17 00:00:00 2001 From: ycggyao Date: Mon, 13 Jan 2025 14:48:50 +0800 Subject: [PATCH] =?UTF-8?q?fix(backend):=20=E9=9B=86=E7=BE=A4=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E8=A1=A5=E5=85=85=E9=9B=86=E7=BE=A4=E8=A7=84=E6=A0=BC?= =?UTF-8?q?=20#9024?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../db_services/bigdata/resources/query.py | 30 ++----------------- .../db_services/dbbase/resources/query.py | 30 ++++++++++++++++++- .../db_services/mongodb/resources/query.py | 3 +- .../mysql/resources/tendbcluster/query.py | 11 ++----- .../mysql/resources/tendbha/query.py | 9 ++---- .../mysql/resources/tendbsingle/query.py | 3 +- .../redis/resources/redis_cluster/query.py | 10 ++----- .../sqlserver/resources/sqlserver_ha/query.py | 3 +- .../resources/sqlserver_single/query.py | 3 +- 9 files changed, 48 insertions(+), 54 deletions(-) diff --git a/dbm-ui/backend/db_services/bigdata/resources/query.py b/dbm-ui/backend/db_services/bigdata/resources/query.py index 51417b0c5f..9bd85ae75b 100644 --- a/dbm-ui/backend/db_services/bigdata/resources/query.py +++ b/dbm-ui/backend/db_services/bigdata/resources/query.py @@ -12,17 +12,15 @@ from operator import itemgetter from typing import Any, Dict, List -from django.db.models import Count, F, Q, QuerySet +from django.db.models import Count, F, Q from django.forms import model_to_dict from django.utils.translation import ugettext_lazy as _ -from backend.db_meta.enums.spec import SpecClusterType -from backend.db_meta.models import AppCache, Spec +from backend.db_meta.models import AppCache from backend.db_meta.models.cluster import Cluster from backend.db_meta.models.instance import StorageInstance from backend.db_proxy.models import ClusterExtension from backend.db_services.dbbase.resources import query -from backend.db_services.dbbase.resources.query import ResourceList from backend.db_services.ipchooser.query.resource import ResourceQueryHelper from backend.ticket.constants import TICKET_RUNNING_STATUS_SET from backend.ticket.models import InstanceOperateRecord @@ -271,27 +269,3 @@ def _to_nodes_list( # 对创建时间或者实例数量进行排序 return query.ResourceList(count=count, data=paginated_group_list) - - @classmethod - def _filter_cluster_hook( - cls, - bk_biz_id, - cluster_queryset: QuerySet, - proxy_queryset: QuerySet, - storage_queryset: QuerySet, - limit: int, - offset: int, - **kwargs, - ) -> ResourceList: - # 预取remote的spec - remote_spec_map = {spec.spec_id: spec for spec in Spec.objects.filter(spec_cluster_type=SpecClusterType.Doris)} - return super()._filter_cluster_hook( - bk_biz_id, - cluster_queryset, - proxy_queryset, - storage_queryset, - limit, - offset, - remote_spec_map=remote_spec_map, - **kwargs, - ) diff --git a/dbm-ui/backend/db_services/dbbase/resources/query.py b/dbm-ui/backend/db_services/dbbase/resources/query.py index 356d5dbd62..393d65c4fd 100644 --- a/dbm-ui/backend/db_services/dbbase/resources/query.py +++ b/dbm-ui/backend/db_services/dbbase/resources/query.py @@ -13,13 +13,23 @@ import attr from django.db.models import F, Prefetch, Q, QuerySet +from django.forms import model_to_dict from django.http import HttpResponse 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.comm import SystemTagEnum -from backend.db_meta.models import AppCache, Cluster, ClusterEntry, DBModule, Machine, ProxyInstance, StorageInstance +from backend.db_meta.models import ( + AppCache, + Cluster, + ClusterEntry, + DBModule, + Machine, + ProxyInstance, + Spec, + StorageInstance, +) from backend.db_services.dbbase.instances.handlers import InstanceHandler from backend.db_services.dbbase.resources.query_base import ( build_q_for_domain_by_cluster, @@ -237,6 +247,7 @@ class BaseListRetrieveResource(CommonQueryResourceMixin): fields = [{"name": _("业务"), "key": "bk_biz_name"}] cluster_types = [] + storage_spec_role = None @classmethod @abc.abstractmethod @@ -516,6 +527,12 @@ def _filter_cluster_hook( # 获取集群统计信息,只需要获取一次 cluster_stats_map = Cluster.get_cluster_stats(bk_biz_id, cls.cluster_types) + # 预取remote的spec + db_types = set([ClusterType.cluster_type_to_db_type(cluster_type) for cluster_type in cls.cluster_types]) + kwargs["remote_spec_map"] = { + spec.spec_id: spec for spec in Spec.objects.filter(spec_cluster_type__in=db_types) + } + for cluster in cluster_list: cluster_info = cls._to_cluster_representation( cluster=cluster, @@ -556,9 +573,19 @@ def _to_cluster_representation( @param cluster_entry_map: key 是 cluster.id, value 是当前集群对应的 entry 映射 @param cluster_operate_records_map: key 是 cluster.id, value 是当前集群对应的 操作记录 映射 """ + spec = None cluster_entry_map_value = cluster_entry_map.get(cluster.id, {}) bk_cloud_name = cloud_info.get(str(cluster.bk_cloud_id), {}).get("bk_cloud_name", "") + # 补充集群规格信息 + if cls.storage_spec_role is not None: + storage_spec = next( + (storage for storage in cluster.storages if storage.instance_role == cls.storage_spec_role), None + ) + if storage_spec: + spec_id = storage_spec.machine.spec_id + spec = kwargs["remote_spec_map"].get(spec_id) + return { "id": cluster.id, "phase": cluster.phase, @@ -589,6 +616,7 @@ def _to_cluster_representation( "updater": cluster.updater, "create_at": datetime2str(cluster.create_at), "update_at": datetime2str(cluster.update_at), + "cluster_spec": model_to_dict(spec) if spec else None, } @classmethod diff --git a/dbm-ui/backend/db_services/mongodb/resources/query.py b/dbm-ui/backend/db_services/mongodb/resources/query.py index 3018ea1bfa..429cd9af5c 100644 --- a/dbm-ui/backend/db_services/mongodb/resources/query.py +++ b/dbm-ui/backend/db_services/mongodb/resources/query.py @@ -14,7 +14,7 @@ from django.db.models.functions import Concat from django.utils.translation import ugettext_lazy as _ -from backend.db_meta.enums import ClusterType, MachineType +from backend.db_meta.enums import ClusterType, InstanceRole, MachineType from backend.db_meta.models import AppCache, NosqlStorageSetDtl, StorageInstanceTuple from backend.db_meta.models.cluster import Cluster from backend.db_meta.models.instance import ProxyInstance, StorageInstance @@ -31,6 +31,7 @@ class MongoDBListRetrieveResource(query.ListRetrieveResource): """查看 mysql dbha 架构的资源""" cluster_types = [ClusterType.MongoReplicaSet, ClusterType.MongoShardedCluster] + storage_spec_role = InstanceRole.MONGO_M1 fields = [ {"name": _("主域名"), "key": "domain"}, {"name": _("IP"), "key": "ip"}, diff --git a/dbm-ui/backend/db_services/mysql/resources/tendbcluster/query.py b/dbm-ui/backend/db_services/mysql/resources/tendbcluster/query.py index 1200065629..add7d63cab 100644 --- a/dbm-ui/backend/db_services/mysql/resources/tendbcluster/query.py +++ b/dbm-ui/backend/db_services/mysql/resources/tendbcluster/query.py @@ -17,11 +17,10 @@ from backend.configuration.constants import DBType from backend.db_meta.api.cluster.tendbcluster.detail import scan_cluster -from backend.db_meta.enums import InstanceInnerRole, TenDBClusterSpiderRole +from backend.db_meta.enums import InstanceInnerRole, InstanceRole, TenDBClusterSpiderRole from backend.db_meta.enums.cluster_type import ClusterType -from backend.db_meta.enums.spec import SpecClusterType from backend.db_meta.exceptions import DBMetaException -from backend.db_meta.models import AppCache, Spec +from backend.db_meta.models import AppCache from backend.db_meta.models.cluster import Cluster from backend.db_meta.models.instance import ProxyInstance, StorageInstance from backend.db_services.dbbase.resources import query @@ -36,6 +35,7 @@ class ListRetrieveResource(query.ListRetrieveResource): cluster_type = ClusterType.TenDBCluster cluster_types = [ClusterType.TenDBCluster] + storage_spec_role = InstanceRole.REMOTE_MASTER fields = [ {"name": _("集群名"), "key": "cluster_name"}, @@ -85,10 +85,6 @@ def _filter_cluster_hook( "as_ejector__tendbclusterstorageset", "as_receiver__tendbclusterstorageset" ) proxy_queryset = proxy_queryset.prefetch_related("tendbclusterspiderext") - # 预取remote的spec - remote_spec_map = { - spec.spec_id: spec for spec in Spec.objects.filter(spec_cluster_type=SpecClusterType.TenDBCluster) - } return super()._filter_cluster_hook( bk_biz_id, cluster_queryset, @@ -96,7 +92,6 @@ def _filter_cluster_hook( storage_queryset, limit, offset, - remote_spec_map=remote_spec_map, **kwargs, ) diff --git a/dbm-ui/backend/db_services/mysql/resources/tendbha/query.py b/dbm-ui/backend/db_services/mysql/resources/tendbha/query.py index 60df864877..5baa3242e4 100644 --- a/dbm-ui/backend/db_services/mysql/resources/tendbha/query.py +++ b/dbm-ui/backend/db_services/mysql/resources/tendbha/query.py @@ -15,10 +15,9 @@ from django.utils.translation import ugettext_lazy as _ from backend.db_meta.api.cluster.tendbha.detail import scan_cluster -from backend.db_meta.enums import ClusterEntryRole, InstanceInnerRole +from backend.db_meta.enums import ClusterEntryRole, InstanceInnerRole, InstanceRole from backend.db_meta.enums.cluster_type import ClusterType -from backend.db_meta.enums.spec import SpecClusterType -from backend.db_meta.models import AppCache, Spec, StorageInstance +from backend.db_meta.models import AppCache, StorageInstance from backend.db_meta.models.cluster import Cluster from backend.db_services.dbbase.resources import query from backend.db_services.dbbase.resources.query import ResourceList @@ -31,6 +30,7 @@ class ListRetrieveResource(query.ListRetrieveResource): """查看 mysql dbha 架构的资源""" cluster_types = [ClusterType.TenDBHA] + storage_spec_role = InstanceRole.BACKEND_MASTER fields = [ {"name": _("集群名"), "key": "cluster_name"}, @@ -102,8 +102,6 @@ def _filter_cluster_hook( ) -> ResourceList: # 提前预取storage的tuple storage_queryset = storage_queryset.prefetch_related("as_receiver__ejector") - # 预取remote的spec - remote_spec_map = {spec.spec_id: spec for spec in Spec.objects.filter(spec_cluster_type=SpecClusterType.MySQL)} return super()._filter_cluster_hook( bk_biz_id, cluster_queryset, @@ -111,7 +109,6 @@ def _filter_cluster_hook( storage_queryset, limit, offset, - remote_spec_map=remote_spec_map, **kwargs, ) diff --git a/dbm-ui/backend/db_services/mysql/resources/tendbsingle/query.py b/dbm-ui/backend/db_services/mysql/resources/tendbsingle/query.py index 571db3c3dc..a5a7a73890 100644 --- a/dbm-ui/backend/db_services/mysql/resources/tendbsingle/query.py +++ b/dbm-ui/backend/db_services/mysql/resources/tendbsingle/query.py @@ -14,7 +14,7 @@ from django.utils.translation import ugettext_lazy as _ from backend.db_meta.api.cluster.tendbsingle.detail import scan_cluster -from backend.db_meta.enums import InstanceInnerRole +from backend.db_meta.enums import InstanceInnerRole, InstanceRole from backend.db_meta.enums.cluster_type import ClusterType from backend.db_meta.models import AppCache, StorageInstance from backend.db_meta.models.cluster import Cluster @@ -27,6 +27,7 @@ class ListRetrieveResource(query.ListRetrieveResource): """查看 mysql 单点部署的资源""" cluster_types = [ClusterType.TenDBSingle] + storage_spec_role = InstanceRole.ORPHAN fields = [ {"name": _("集群名"), "key": "cluster_name"}, diff --git a/dbm-ui/backend/db_services/redis/resources/redis_cluster/query.py b/dbm-ui/backend/db_services/redis/resources/redis_cluster/query.py index 12d627e4c4..229a51982d 100644 --- a/dbm-ui/backend/db_services/redis/resources/redis_cluster/query.py +++ b/dbm-ui/backend/db_services/redis/resources/redis_cluster/query.py @@ -22,8 +22,7 @@ from backend.db_meta.api.cluster.tendisssd.handler import TendisSSDClusterHandler from backend.db_meta.enums import ClusterEntryType, InstanceRole from backend.db_meta.enums.cluster_type import ClusterType -from backend.db_meta.enums.spec import SpecClusterType -from backend.db_meta.models import AppCache, Machine, Spec +from backend.db_meta.models import AppCache, Machine from backend.db_meta.models.cluster import Cluster from backend.db_services.dbbase.resources import query from backend.db_services.dbbase.resources.query import ResourceList @@ -109,8 +108,6 @@ def _filter_cluster_hook( storage_queryset = storage_queryset.prefetch_related( "machine", "nosqlstoragesetdtl_set", "as_receiver", "as_ejector" ) - # 预取remote的spec - redis_spec_map = {spec.spec_id: spec for spec in Spec.objects.filter(spec_cluster_type=SpecClusterType.Redis)} return super()._filter_cluster_hook( bk_biz_id, cluster_queryset, @@ -118,7 +115,6 @@ def _filter_cluster_hook( storage_queryset, limit, offset, - redis_spec_map=redis_spec_map, **kwargs, ) @@ -194,10 +190,10 @@ def _to_cluster_representation( # 补充集群的规格和容量信息 cluster_spec = cluster_capacity = "" - redis_spec_map = kwargs["redis_spec_map"] + remote_spec_map = kwargs["remote_spec_map"] if machine_list: spec_id = cluster.storages[0].machine.spec_id - spec = redis_spec_map.get(spec_id) + spec = remote_spec_map.get(spec_id) cluster_spec = model_to_dict(spec) if spec else {} cluster_capacity = spec.capacity * machine_pair_cnt if spec else 0 diff --git a/dbm-ui/backend/db_services/sqlserver/resources/sqlserver_ha/query.py b/dbm-ui/backend/db_services/sqlserver/resources/sqlserver_ha/query.py index 1dabe55f5c..15185ddf90 100644 --- a/dbm-ui/backend/db_services/sqlserver/resources/sqlserver_ha/query.py +++ b/dbm-ui/backend/db_services/sqlserver/resources/sqlserver_ha/query.py @@ -13,7 +13,7 @@ from django.utils.translation import ugettext_lazy as _ from backend.db_meta.api.cluster.sqlserverha.detail import scan_cluster -from backend.db_meta.enums import InstanceInnerRole +from backend.db_meta.enums import InstanceInnerRole, InstanceRole from backend.db_meta.enums.cluster_type import ClusterType from backend.db_meta.models import AppCache from backend.db_meta.models.cluster import Cluster @@ -26,6 +26,7 @@ class ListRetrieveResource(SqlserverListRetrieveResource): """查看 sqlserver ha 架构的资源""" cluster_types = [ClusterType.SqlserverHA] + storage_spec_role = InstanceRole.BACKEND_MASTER fields = [ {"name": _("集群名"), "key": "cluster_name"}, diff --git a/dbm-ui/backend/db_services/sqlserver/resources/sqlserver_single/query.py b/dbm-ui/backend/db_services/sqlserver/resources/sqlserver_single/query.py index 7b4e483aa9..58d684f22a 100644 --- a/dbm-ui/backend/db_services/sqlserver/resources/sqlserver_single/query.py +++ b/dbm-ui/backend/db_services/sqlserver/resources/sqlserver_single/query.py @@ -13,7 +13,7 @@ from django.utils.translation import ugettext_lazy as _ from backend.db_meta.api.cluster.sqlserversingle.detail import scan_cluster -from backend.db_meta.enums import InstanceInnerRole +from backend.db_meta.enums import InstanceInnerRole, InstanceRole from backend.db_meta.enums.cluster_type import ClusterType from backend.db_meta.models import AppCache from backend.db_meta.models.cluster import Cluster @@ -26,6 +26,7 @@ class ListRetrieveResource(SqlserverListRetrieveResource): """查看 sqlserver ha 架构的资源""" cluster_types = [ClusterType.SqlserverSingle] + storage_spec_role = InstanceRole.ORPHAN fields = [ {"name": _("集群名"), "key": "cluster_name"},