Skip to content

Commit

Permalink
fix(backend): 导出接口调整为json格式返回 #2812
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzhw8 committed Dec 26, 2023
1 parent 320d170 commit c54a073
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 43 deletions.
1 change: 1 addition & 0 deletions .github/workflows/auto_create_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ jobs:
title: 'chore: update medium.lock #1'
body: ''
branch: '${{ steps.get-current-branch.outputs.current_branch }}'
base: "v1.3.0"

- name: Auto Add Release File
id: add-release-file
Expand Down
72 changes: 35 additions & 37 deletions dbm-ui/backend/db_services/dbbase/resources/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@
specific language governing permissions and limitations under the License.
"""
import abc
from typing import Dict, List
from typing import Dict, List, Union

import attr
from django.db.models import Q
from django.http import HttpResponse
from django.utils.translation import ugettext_lazy as _

from backend.db_meta.enums import ClusterEntryType
from backend.db_meta.models import Cluster, ClusterEntry, Machine, ProxyInstance, StorageInstance
from backend.flow.utils.dns_manage import DnsManage
from backend.utils.excel import ExcelHandler


@attr.s
Expand Down Expand Up @@ -114,7 +112,7 @@ def get_fields(cls) -> List[Dict[str, str]]:
return cls.fields

@classmethod
def export_cluster(cls, bk_biz_id: int, cluster_ids: list) -> HttpResponse:
def export_cluster(cls, bk_biz_id: int, cluster_ids: list) -> Dict[str, List]:
# 获取所有符合条件的集群对象
clusters = Cluster.objects.prefetch_related(
"storageinstance_set", "proxyinstance_set", "storageinstance_set__machine", "proxyinstance_set__machine"
Expand All @@ -123,19 +121,20 @@ def export_cluster(cls, bk_biz_id: int, cluster_ids: list) -> HttpResponse:
clusters = clusters.filter(id__in=cluster_ids)

# 初始化用于存储Excel数据的字典列表
excel_data_dict__list = []
headers = [
"cluster_id",
"cluster_name",
"cluster_alias",
"cluster_type",
"master_domain",
"major_version",
"region",
"disaster_tolerance_level",
{"id": "cluster_id", "name": _("集群 ID")},
{"id": "cluster_name", "name": _("集群名称")},
{"id": "cluster_alias", "name": _("集群别名")},
{"id": "cluster_type", "name": _("集群类型")},
{"id": "master_domain", "name": _("主域名")},
{"id": "major_version", "name": _("主版本")},
{"id": "region", "name": _("地域")},
{"id": "disaster_tolerance_level", "name": _("容灾级别")},
]

def fill_instances_to_cluster_info(_cluster_info: Dict, instances: List):
def fill_instances_to_cluster_info(
_cluster_info: Dict, instances: List[Union[StorageInstance, ProxyInstance]]
):
"""
把实例信息填充到集群信息中
"""
Expand All @@ -148,10 +147,11 @@ def fill_instances_to_cluster_info(_cluster_info: Dict, instances: List):
cluster_info[role] += f"\n{ins.machine.ip}#{ins.port}"
else:
if role not in headers:
headers.append(role)
headers.append({"id": role, "name": role})
cluster_info[role] = f"{ins.machine.ip}#{ins.port}"

# 遍历所有的集群对象
data_list = []
for cluster in clusters:
# 创建一个空字典来保存当前集群的信息
cluster_info = {
Expand All @@ -167,13 +167,12 @@ def fill_instances_to_cluster_info(_cluster_info: Dict, instances: List):
fill_instances_to_cluster_info(cluster_info, cluster.storageinstance_set.all())
fill_instances_to_cluster_info(cluster_info, cluster.proxyinstance_set.all())

# 将当前集群的信息追加到excel_data_dict__list列表中
excel_data_dict__list.append(cluster_info)
wb = ExcelHandler.serialize(excel_data_dict__list, header=headers, match_header=True)
return ExcelHandler.response(wb, f"biz_{bk_biz_id}_clusters.xlsx")
# 将当前集群的信息追加到data_list列表中
data_list.append(cluster_info)
return {"headers": headers, "data_list": data_list}

@classmethod
def export_instance(cls, bk_biz_id: int, bk_host_ids: list) -> HttpResponse:
def export_instance(cls, bk_biz_id: int, bk_host_ids: list) -> Dict[str, List]:
# 查询实例
query_condition = Q(bk_biz_id=bk_biz_id)
if bk_host_ids:
Expand All @@ -185,26 +184,26 @@ def export_instance(cls, bk_biz_id: int, bk_host_ids: list) -> HttpResponse:
query_condition
)
headers = [
"bk_host_id",
"bk_cloud_id",
"ip",
"ip_port",
"instance_role",
"bk_idc_city_name",
"bk_idc_name",
"cluster_id",
"cluster_name",
"cluster_alias",
"cluster_type",
"master_domain",
"major_version",
{"id": "bk_host_id", "name": _("主机 ID")},
{"id": "bk_cloud_id", "name": _("云区域 ID")},
{"id": "ip", "name": _("IP")},
{"id": "ip_port", "name": _("IP 端口")},
{"id": "instance_role", "name": _("实例角色")},
{"id": "bk_idc_city_name", "name": _("城市")},
{"id": "bk_idc_name", "name": _("机房")},
{"id": "cluster_id", "name": _("集群 ID")},
{"id": "cluster_name", "name": _("集群名称")},
{"id": "cluster_alias", "name": _("集群别名")},
{"id": "cluster_type", "name": _("集群类型")},
{"id": "master_domain", "name": _("主域名")},
{"id": "major_version", "name": _("主版本")},
]
# 插入数据
excel_data_dict__list = []
data_list = []
for instances in [storages, proxies]:
for ins in instances:
for cluster in ins.cluster.all():
excel_data_dict__list.append(
data_list.append(
{
"bk_host_id": ins.machine.bk_host_id,
"bk_cloud_id": ins.machine.bk_cloud_id,
Expand All @@ -221,5 +220,4 @@ def export_instance(cls, bk_biz_id: int, bk_host_ids: list) -> HttpResponse:
"major_version": cluster.major_version,
}
)
wb = ExcelHandler.serialize(excel_data_dict__list, header=headers, match_header=True)
return ExcelHandler.response(wb, f"biz_{bk_biz_id}_instances.xlsx")
return {"headers": headers, "data_list": data_list}
4 changes: 2 additions & 2 deletions dbm-ui/backend/db_services/dbbase/resources/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ def get_topo_graph(self, request, bk_biz_id: int, cluster_id: int):
def export_cluster(self, request, bk_biz_id: int):
"""导出集群数据为 excel 文件"""
cluster_ids = request.POST.get("cluster_ids")
return self.query_class.export_cluster(bk_biz_id, cluster_ids)
return Response(self.query_class.export_cluster(bk_biz_id, cluster_ids))

@action(methods=["POST"], detail=False, url_path="export_instance")
def export_instance(self, request, bk_biz_id: int):
"""导出实例数据为 excel 文件"""
bk_host_ids = request.POST.get("bk_host_ids")
return self.query_class.export_instance(bk_biz_id, bk_host_ids)
return Response(self.query_class.export_instance(bk_biz_id, bk_host_ids))

def _paginate_resource_list(self, request, bk_biz_id: int):
return self.paginator.paginate_resource_list(request, bk_biz_id, self)
4 changes: 2 additions & 2 deletions helm-charts/bk-dbm/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,5 @@ dependencies:
description: A Helm chart for bk-dbm
name: bk-dbm
type: application
version: 1.3.0-alpha.29
appVersion: 1.3.0-alpha.29
version: 1.3.0-alpha.30
appVersion: 1.3.0-alpha.30
2 changes: 1 addition & 1 deletion helm-charts/bk-dbm/charts/dbconfig/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apiVersion: v2
appVersion: 0.0.1-alpha.71
appVersion: 0.0.1-alpha.72
description: A Helm chart for dbconfig
name: dbconfig
type: application
Expand Down
2 changes: 1 addition & 1 deletion helm-charts/bk-dbm/charts/dbm/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apiVersion: v2
appVersion: 1.3.0-alpha.229
appVersion: 1.3.0-alpha.231
description: A Helm chart for dbm
name: dbm
type: application
Expand Down

0 comments on commit c54a073

Please sign in to comment.