Skip to content

Commit

Permalink
feat(backend): 全局搜索资源池主机数据调整 TencentBlueKing#8156
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzhw8 committed Nov 26, 2024
1 parent be7f047 commit f035a88
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 38 deletions.
3 changes: 3 additions & 0 deletions dbm-ui/backend/components/gse/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class _GseApi(BaseApi):
MODULE = _("管控平台")
BASE = GSE_APIGW_DOMAIN

class Constants:
GSE_AGENT_RUNNING_CODE = 2

def __init__(self):
self.get_agent_status = self.generate_data_api(
method="POST",
Expand Down
2 changes: 0 additions & 2 deletions dbm-ui/backend/db_services/dbresource/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
RESOURCE_IMPORT_TASK_FIELD = "{user}_resource_import_task_field"
RESOURCE_IMPORT_EXPIRE_TIME = 7 * 24 * 60 * 60

# gse正常的状态码为2
GSE_AGENT_RUNNING_CODE = 2

# 集群对应的规格筛选类
SPEC_FILTER_FACTORY = {
Expand Down
41 changes: 40 additions & 1 deletion dbm-ui/backend/db_services/dbresource/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
from django.utils.translation import ugettext as _

from backend.components.dbresource.client import DBResourceApi
from backend.components.gse.client import GseApi
from backend.db_meta.enums.spec import SpecClusterType, SpecMachineType
from backend.db_meta.models import Spec
from backend.db_meta.models import Spec, AppCache
from backend.db_services.dbresource.exceptions import SpecOperateException
from backend.db_services.ipchooser.query.resource import ResourceQueryHelper


class ClusterSpecFilter(object):
Expand Down Expand Up @@ -411,3 +413,40 @@ def spec_resource_count(cls, bk_biz_id: int, bk_cloud_id: int, spec_ids: List[in
spec_apply_count = DBResourceApi.apply_count(params=spec_count_params)
spec_apply_count = {k.split("_")[0]: v for k, v in spec_apply_count.items()}
return spec_apply_count

@staticmethod
def resource_list(params):
def _format_resource_fields(data, _cloud_info, _biz_infos):
data.update(
{
"bk_cloud_name": _cloud_info[str(data["bk_cloud_id"])]["bk_cloud_name"],
"bk_host_innerip": data["ip"],
"bk_mem": data.pop("dram_cap"),
"bk_cpu": data.pop("cpu_num"),
"bk_disk": data.pop("total_storage_cap"),
"resource_type": data.pop("rs_type"),
"for_biz": {
"bk_biz_id": data["dedicated_biz"],
"bk_biz_name": _biz_infos.get(data["dedicated_biz"]),
},
"agent_status": int(
(data.pop("gse_agent_status_code") == GseApi.Constants.GSE_AGENT_RUNNING_CODE)
),
}
)
return data

resource_data = DBResourceApi.resource_list(params=params)
if not resource_data["details"]:
return {"count": 0, "results": []}

# 获取云区域信息和业务信息
cloud_info = ResourceQueryHelper.search_cc_cloud(get_cache=True)
for_biz_ids = [data["dedicated_biz"] for data in resource_data["details"] if data["dedicated_biz"]]
for_biz_infos = AppCache.batch_get_app_attr(bk_biz_ids=for_biz_ids, attr_name="bk_biz_name")
# 格式化资源池字段信息
for data in resource_data.get("details") or []:
_format_resource_fields(data, cloud_info, for_biz_infos)

resource_data["results"] = resource_data.pop("details")
return resource_data
10 changes: 10 additions & 0 deletions dbm-ui/backend/db_services/dbresource/views/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# -*- coding:utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at https://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""
34 changes: 1 addition & 33 deletions dbm-ui/backend/db_services/dbresource/views/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from backend.db_meta.models import AppCache
from backend.db_meta.models.machine import DeviceClass
from backend.db_services.dbresource.constants import (
GSE_AGENT_RUNNING_CODE,
RESOURCE_IMPORT_EXPIRE_TIME,
RESOURCE_IMPORT_TASK_FIELD,
SWAGGER_TAG,
Expand Down Expand Up @@ -115,38 +114,7 @@ class DBResourceViewSet(viewsets.SystemViewSet):
resource_meta=None,
)
def resource_list(self, request):
def _format_resource_fields(data, _cloud_info, _biz_infos):
data.update(
{
"bk_cloud_name": _cloud_info[str(data["bk_cloud_id"])]["bk_cloud_name"],
"bk_host_innerip": data["ip"],
"bk_mem": data.pop("dram_cap"),
"bk_cpu": data.pop("cpu_num"),
"bk_disk": data.pop("total_storage_cap"),
"resource_type": data.pop("rs_type"),
"for_biz": {
"bk_biz_id": data["dedicated_biz"],
"bk_biz_name": _biz_infos.get(data["dedicated_biz"]),
},
"agent_status": int((data.pop("gse_agent_status_code") == GSE_AGENT_RUNNING_CODE)),
}
)
return data

resource_data = DBResourceApi.resource_list(params=self.params_validate(self.get_serializer_class()))
if not resource_data["details"]:
return Response({"count": 0, "results": []})

# 获取云区域信息和业务信息
cloud_info = ResourceQueryHelper.search_cc_cloud(get_cache=True)
for_biz_ids = [data["dedicated_biz"] for data in resource_data["details"] if data["dedicated_biz"]]
for_biz_infos = AppCache.batch_get_app_attr(bk_biz_ids=for_biz_ids, attr_name="bk_biz_name")
# 格式化资源池字段信息
for data in resource_data.get("details") or []:
_format_resource_fields(data, cloud_info, for_biz_infos)

resource_data["results"] = resource_data.pop("details")
return Response(resource_data)
return Response(ResourceHandler.resource_list(self.params_validate(self.get_serializer_class())))

@common_swagger_auto_schema(
operation_summary=_("获取DBA业务下的主机信息"),
Expand Down
4 changes: 2 additions & 2 deletions dbm-ui/backend/db_services/quick_search/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
from django.db.models.functions import Concat
from django.forms import model_to_dict

from backend.components.dbresource.client import DBResourceApi
from backend.db_meta.enums import ClusterType
from backend.db_meta.models import Cluster, Machine, ProxyInstance, StorageInstance
from backend.db_services.dbresource.handlers import ResourceHandler
from backend.db_services.quick_search import constants
from backend.db_services.quick_search.constants import FilterType, ResourceType
from backend.flow.models import FlowTree
Expand Down Expand Up @@ -285,4 +285,4 @@ def filter_ticket(self, keyword_list: list):
return results

def filter_resource_pool(self, keyword_list: list):
return DBResourceApi.resource_list({"hosts": keyword_list, "limit": self.limit, "offset": 0})["details"]
return ResourceHandler().resource_list({"hosts": keyword_list, "limit": self.limit, "offset": 0})["results"]

0 comments on commit f035a88

Please sign in to comment.