Skip to content

Commit

Permalink
fix(backend): 集群列表补充集群规格 #9024
Browse files Browse the repository at this point in the history
  • Loading branch information
ygcyao committed Jan 13, 2025
1 parent 035f68f commit 66326f9
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 54 deletions.
30 changes: 2 additions & 28 deletions dbm-ui/backend/db_services/bigdata/resources/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
)
30 changes: 29 additions & 1 deletion dbm-ui/backend/db_services/dbbase/resources/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -237,6 +247,7 @@ class BaseListRetrieveResource(CommonQueryResourceMixin):

fields = [{"name": _("业务"), "key": "bk_biz_name"}]
cluster_types = []
storage_spec_role = None

@classmethod
@abc.abstractmethod
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion dbm-ui/backend/db_services/mongodb/resources/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"},
Expand Down
11 changes: 3 additions & 8 deletions dbm-ui/backend/db_services/mysql/resources/tendbcluster/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"},
Expand Down Expand Up @@ -85,18 +85,13 @@ 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,
proxy_queryset,
storage_queryset,
limit,
offset,
remote_spec_map=remote_spec_map,
**kwargs,
)

Expand Down
9 changes: 3 additions & 6 deletions dbm-ui/backend/db_services/mysql/resources/tendbha/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"},
Expand Down Expand Up @@ -102,16 +102,13 @@ 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,
proxy_queryset,
storage_queryset,
limit,
offset,
remote_spec_map=remote_spec_map,
**kwargs,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,6 +27,7 @@ class ListRetrieveResource(query.ListRetrieveResource):
"""查看 mysql 单点部署的资源"""

cluster_types = [ClusterType.TenDBSingle]
storage_spec_role = InstanceRole.ORPHAN

fields = [
{"name": _("集群名"), "key": "cluster_name"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -109,16 +108,13 @@ 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,
proxy_queryset,
storage_queryset,
limit,
offset,
redis_spec_map=redis_spec_map,
**kwargs,
)

Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,6 +26,7 @@ class ListRetrieveResource(SqlserverListRetrieveResource):
"""查看 sqlserver ha 架构的资源"""

cluster_types = [ClusterType.SqlserverHA]
storage_spec_role = InstanceRole.BACKEND_MASTER

fields = [
{"name": _("集群名"), "key": "cluster_name"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,6 +26,7 @@ class ListRetrieveResource(SqlserverListRetrieveResource):
"""查看 sqlserver ha 架构的资源"""

cluster_types = [ClusterType.SqlserverSingle]
storage_spec_role = InstanceRole.ORPHAN

fields = [
{"name": _("集群名"), "key": "cluster_name"},
Expand Down

0 comments on commit 66326f9

Please sign in to comment.