From f035a88b186d6fbd1d9406b1c90848d22974b818 Mon Sep 17 00:00:00 2001 From: durant <826035498@qq.com> Date: Mon, 25 Nov 2024 19:32:22 +0800 Subject: [PATCH] =?UTF-8?q?feat(backend):=20=E5=85=A8=E5=B1=80=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E8=B5=84=E6=BA=90=E6=B1=A0=E4=B8=BB=E6=9C=BA=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E8=B0=83=E6=95=B4=20#8156?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dbm-ui/backend/components/gse/client.py | 3 ++ .../db_services/dbresource/constants.py | 2 - .../db_services/dbresource/handlers.py | 41 ++++++++++++++++++- .../db_services/dbresource/views/__init__.py | 10 +++++ .../db_services/dbresource/views/resource.py | 34 +-------------- .../db_services/quick_search/handlers.py | 4 +- 6 files changed, 56 insertions(+), 38 deletions(-) create mode 100644 dbm-ui/backend/db_services/dbresource/views/__init__.py diff --git a/dbm-ui/backend/components/gse/client.py b/dbm-ui/backend/components/gse/client.py index e871520a91..c49e3f47b0 100644 --- a/dbm-ui/backend/components/gse/client.py +++ b/dbm-ui/backend/components/gse/client.py @@ -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", diff --git a/dbm-ui/backend/db_services/dbresource/constants.py b/dbm-ui/backend/db_services/dbresource/constants.py index d6c6987a6d..a978afbfeb 100644 --- a/dbm-ui/backend/db_services/dbresource/constants.py +++ b/dbm-ui/backend/db_services/dbresource/constants.py @@ -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 = { diff --git a/dbm-ui/backend/db_services/dbresource/handlers.py b/dbm-ui/backend/db_services/dbresource/handlers.py index 3303bf5f1f..0e4a3d843e 100644 --- a/dbm-ui/backend/db_services/dbresource/handlers.py +++ b/dbm-ui/backend/db_services/dbresource/handlers.py @@ -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): @@ -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 diff --git a/dbm-ui/backend/db_services/dbresource/views/__init__.py b/dbm-ui/backend/db_services/dbresource/views/__init__.py new file mode 100644 index 0000000000..5fe769d9f1 --- /dev/null +++ b/dbm-ui/backend/db_services/dbresource/views/__init__.py @@ -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. +""" diff --git a/dbm-ui/backend/db_services/dbresource/views/resource.py b/dbm-ui/backend/db_services/dbresource/views/resource.py index 6484076f15..c0672310c6 100644 --- a/dbm-ui/backend/db_services/dbresource/views/resource.py +++ b/dbm-ui/backend/db_services/dbresource/views/resource.py @@ -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, @@ -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业务下的主机信息"), diff --git a/dbm-ui/backend/db_services/quick_search/handlers.py b/dbm-ui/backend/db_services/quick_search/handlers.py index 0d85c190a9..5ec4929db4 100644 --- a/dbm-ui/backend/db_services/quick_search/handlers.py +++ b/dbm-ui/backend/db_services/quick_search/handlers.py @@ -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 @@ -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"]