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 10, 2025
1 parent 035f68f commit 915dbf4
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 10 deletions.
14 changes: 13 additions & 1 deletion dbm-ui/backend/db_services/mongodb/resources/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@

from django.db.models import CharField, ExpressionWrapper, F, Prefetch, Q, QuerySet, Value
from django.db.models.functions import Concat
from django.forms import model_to_dict
from django.utils.translation import ugettext_lazy as _

from backend.db_meta.enums import ClusterType, MachineType
from backend.db_meta.models import AppCache, NosqlStorageSetDtl, StorageInstanceTuple
from backend.db_meta.enums.spec import SpecClusterType
from backend.db_meta.models import AppCache, NosqlStorageSetDtl, Spec, StorageInstanceTuple
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 Down Expand Up @@ -98,6 +100,11 @@ def _filter_cluster_hook(
),
)

# 预取remote的spec
kwargs["remote_spec_map"] = {
spec.spec_id: spec for spec in Spec.objects.filter(spec_cluster_type=SpecClusterType.MongoDB)
}

return super()._filter_cluster_hook(
bk_biz_id,
cluster_queryset,
Expand Down Expand Up @@ -187,6 +194,10 @@ def _to_cluster_representation(
if k in ip_port_list:
machine_map[group_name].extend(v)

# 获取集群规格
spec_id = cluster.storages[0].machine.spec_id
spec = kwargs["remote_spec_map"].get(spec_id)

cluster_extra_info = {
"machine_instance_num": machine_instance_num,
"mongodb_machine_pair": mongodb_machine_pair,
Expand All @@ -200,6 +211,7 @@ def _to_cluster_representation(
"shard_node_count": shard_node_count,
"temporary_info": cls.get_temporary_cluster_info(cluster, TicketType.MONGODB_RESTORE),
"disaster_tolerance_level": cluster.disaster_tolerance_level,
"cluster_spec": model_to_dict(spec) if spec else None,
}
cluster_info = super()._to_cluster_representation(
cluster,
Expand Down
5 changes: 3 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 @@ -103,15 +103,16 @@ def _filter_cluster_hook(
# 提前预取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)}
kwargs["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
42 changes: 38 additions & 4 deletions dbm-ui/backend/db_services/mysql/resources/tendbsingle/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@
"""
from typing import Any, Dict, List

from django.db.models import F
from django.db.models import F, QuerySet
from django.forms import model_to_dict
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.cluster_type import ClusterType
from backend.db_meta.models import AppCache, StorageInstance
from backend.db_meta.enums.spec import SpecClusterType
from backend.db_meta.models import AppCache, Spec, 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
from backend.db_services.dbbase.resources.register import register_resource_decorator


Expand All @@ -43,6 +46,30 @@ def get_topo_graph(cls, bk_biz_id: int, cluster_id: int) -> dict:
graph = scan_cluster(cluster).to_dict()
return graph

@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.MySQL)}
return super()._filter_cluster_hook(
bk_biz_id,
cluster_queryset,
proxy_queryset,
storage_queryset,
limit,
offset,
remote_spec_map=remote_spec_map,
**kwargs,
)

@classmethod
def _to_cluster_representation(
cls,
Expand All @@ -54,11 +81,17 @@ def _to_cluster_representation(
cloud_info: Dict[str, Any],
biz_info: AppCache,
cluster_stats_map: Dict[str, Dict[str, int]],
**kwargs
**kwargs,
) -> Dict[str, Any]:
"""将集群对象转为可序列化的 dict 结构"""
masters = [m.simple_desc for m in cluster.storages if m.instance_inner_role == InstanceInnerRole.ORPHAN]
cluster_role_info = {"masters": masters}
# 获取集群规格
spec_id = cluster.storages[0].machine.spec_id
spec = kwargs["remote_spec_map"].get(spec_id)
cluster_extra_info = {
"cluster_spec": model_to_dict(spec) if spec else None,
}

cluster_info = super()._to_cluster_representation(
cluster,
Expand All @@ -69,9 +102,10 @@ def _to_cluster_representation(
cloud_info,
biz_info,
cluster_stats_map,
**kwargs
**kwargs,
)
cluster_info.update(cluster_role_info)
cluster_info.update(cluster_extra_info)
return cluster_info

@classmethod
Expand Down
8 changes: 7 additions & 1 deletion dbm-ui/backend/db_services/sqlserver/resources/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"""
from django.db.models import QuerySet

from backend.db_meta.enums.spec import SpecClusterType
from backend.db_meta.models import Spec
from backend.db_meta.models.storage_set_dtl import SqlserverClusterSyncMode
from backend.db_services.dbbase.resources import query
from backend.db_services.dbbase.resources.register import register_resource_decorator
Expand Down Expand Up @@ -37,7 +39,11 @@ def _filter_cluster_hook(
mode.cluster_id: mode.sync_mode
for mode in SqlserverClusterSyncMode.objects.filter(cluster_id__in=cluster_queryset)
}
# 预取remote的spec
kwargs["remote_spec_map"] = {
spec.spec_id: spec for spec in Spec.objects.filter(spec_cluster_type=SpecClusterType.Sqlserver)
}
cluster_infos = super()._filter_cluster_hook(
bk_biz_id, cluster_queryset, proxy_queryset, storage_queryset, limit, offset
bk_biz_id, cluster_queryset, proxy_queryset, storage_queryset, limit, offset, **kwargs
)
return cluster_infos
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"""
from typing import Any, Dict, List

from django.forms import model_to_dict
from django.utils.translation import ugettext_lazy as _

from backend.db_meta.api.cluster.sqlserverha.detail import scan_cluster
Expand Down Expand Up @@ -62,6 +63,15 @@ def _to_cluster_representation(
slaves = [m.simple_desc for m in cluster.storages if m.instance_inner_role == InstanceInnerRole.SLAVE]
cluster_role_info = {"masters": masters, "slaves": slaves}
sync_mode_info = {"sync_mode": cls.db_sync_mode_map.get(cluster.id, "")}
# 补充cluster_spec参数
master_storages = list(filter(lambda storage: storage.instance_inner_role == "master", cluster.storages))
# 获取master规格
if master_storages:
spec_id = master_storages[0].machine.spec_id
spec = kwargs["remote_spec_map"].get(spec_id)
else:
spec = None
cluster_spec_info = {"cluster_spec": model_to_dict(spec) if spec else None}
cluster_info = super()._to_cluster_representation(
cluster,
cluster_entry,
Expand All @@ -73,5 +83,5 @@ def _to_cluster_representation(
cluster_stats_map,
**kwargs
)
cluster_info.update({**cluster_role_info, **sync_mode_info})
cluster_info.update({**cluster_role_info, **sync_mode_info, **cluster_spec_info})
return cluster_info
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"""
from typing import Any, Dict, List

from django.forms import model_to_dict
from django.utils.translation import ugettext_lazy as _

from backend.db_meta.api.cluster.sqlserversingle.detail import scan_cluster
Expand Down Expand Up @@ -58,6 +59,12 @@ def _to_cluster_representation(
storages = [m.simple_desc for m in cluster.storages if m.instance_inner_role == InstanceInnerRole.ORPHAN]
cluster_role_info = {"storages": storages}
sync_mode_info = {"sync_mode": cls.db_sync_mode_map.get(cluster.id, "")}
# 获取集群规格
spec_id = cluster.storages[0].machine.spec_id
spec = kwargs["remote_spec_map"].get(spec_id)
cluster_extra_info = {
"cluster_spec": model_to_dict(spec) if spec else None,
}
cluster_info = super()._to_cluster_representation(
cluster,
cluster_entry,
Expand All @@ -69,5 +76,5 @@ def _to_cluster_representation(
cluster_stats_map,
**kwargs
)
cluster_info.update({**cluster_role_info, **sync_mode_info})
cluster_info.update({**cluster_role_info, **sync_mode_info, **cluster_extra_info})
return cluster_info

0 comments on commit 915dbf4

Please sign in to comment.