From 5a53468c5fd2ed81df937bed752d17a3b3296d32 Mon Sep 17 00:00:00 2001 From: iSecloud <869820505@qq.com> Date: Fri, 17 Jan 2025 10:47:00 +0800 Subject: [PATCH] =?UTF-8?q?fix(backend):=20=E4=BF=AE=E5=A4=8D=E5=B7=B2?= =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=8D=95=E6=8D=AE=E6=B2=A1=E5=8F=91=E9=80=81?= =?UTF-8?q?=E9=80=9A=E7=9F=A5=20#9117?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dbm-ui/backend/core/notify/handlers.py | 4 +-- dbm-ui/backend/ticket/handler.py | 37 +++++++++++++++++--------- dbm-ui/backend/ticket/models/ticket.py | 6 ++--- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/dbm-ui/backend/core/notify/handlers.py b/dbm-ui/backend/core/notify/handlers.py index 5e6b824efa..f0aa6dc6ca 100644 --- a/dbm-ui/backend/core/notify/handlers.py +++ b/dbm-ui/backend/core/notify/handlers.py @@ -300,11 +300,11 @@ def render_msg_template(self, msg_type: str): else: template = jinja_env.from_string(TODO_TEMPLATE) - biz_name = AppCache.get_biz_name(self.bk_biz_id) + biz = AppCache.objects.get(bk_biz_id=self.bk_biz_id) ticket_operators = self.ticket.get_current_operators() payload = { "ticket_type": TicketType.get_choice_label(self.ticket.ticket_type), - "biz_name": f"{biz_name}(#{self.bk_biz_id}, {biz_name})", + "biz_name": f"{biz.bk_biz_name}(#{self.bk_biz_id}, {biz.db_app_abbr})", "cluster_domains": ",".join(self.clusters), "remark": self.ticket.remark, "creator": self.ticket.creator, diff --git a/dbm-ui/backend/ticket/handler.py b/dbm-ui/backend/ticket/handler.py index 1d906344be..ffbe028202 100644 --- a/dbm-ui/backend/ticket/handler.py +++ b/dbm-ui/backend/ticket/handler.py @@ -11,6 +11,7 @@ import itertools import json import logging +from collections import defaultdict from typing import Dict, List from django.db import transaction @@ -60,16 +61,23 @@ def add_related_object(cls, ticket_data: List[Dict]) -> List[Dict]: # 单据关联对象映射表 ticket_id_obj_ids_map: Dict[int, Dict[str, List[int]]] = {} + # 这里的快照数据需要以单据维度分割,因为不同单据的集群信息可能不同 + snapshot_cluster_domain_map = defaultdict(dict) + snapshot_instance_ip_port_map = defaultdict(dict) + # 查询单据对应的集群列表、实例列表等 - cluster_id_immute_domain_map, instance_id_ip_port_map = {}, {} for ticket in Ticket.objects.filter(id__in=ticket_ids): clusters = ticket.details.get("clusters", {}) - cluster_id_immute_domain_map.update( + snapshot_cluster_domain_map[ticket.id].update( {int(cluster_id): info["immute_domain"] for cluster_id, info in clusters.items()} ) + instances = ticket.details.get("instances", {}) if isinstance(instances, dict): - instance_id_ip_port_map.update({int(inst_id): info["instance"] for inst_id, info in instances.items()}) + snapshot_instance_ip_port_map[ticket.id].update( + {int(inst_id): info["instance"] for inst_id, info in instances.items()} + ) + ticket_id_obj_ids_map[ticket.id] = { "cluster_ids": fetch_cluster_ids(ticket.details), "instance_ids": fetch_instance_ids(ticket.details), @@ -77,27 +85,30 @@ def add_related_object(cls, ticket_data: List[Dict]) -> List[Dict]: # 补充关联对象信息 for item in ticket_data: - ticket_cluster_ids = ticket_id_obj_ids_map[item["id"]]["cluster_ids"] - if ticket_cluster_ids: + ticket_id = item["id"] + cluster_ids = ticket_id_obj_ids_map[ticket_id]["cluster_ids"] + instance_ids = ticket_id_obj_ids_map[ticket_id]["instance_ids"] + + if cluster_ids: item["related_object"] = { "title": _("集群"), "objects": [ - cluster_id_immute_domain_map.get(cluster_id) - for cluster_id in ticket_cluster_ids - if cluster_id_immute_domain_map.get(cluster_id) + snapshot_cluster_domain_map[ticket_id][cluster_id] + for cluster_id in cluster_ids + if cluster_id in snapshot_cluster_domain_map[ticket_id] ], } - ticket_instance_ids = ticket_id_obj_ids_map[item["id"]]["instance_ids"] - if ticket_instance_ids: + if instance_ids: item["related_object"] = { "title": _("实例"), "objects": [ - instance_id_ip_port_map.get(instance_id) - for instance_id in ticket_instance_ids - if instance_id_ip_port_map.get(instance_id) + snapshot_instance_ip_port_map[ticket_id][inst_id] + for inst_id in instance_ids + if inst_id in snapshot_instance_ip_port_map[ticket_id] ], } + return ticket_data @classmethod diff --git a/dbm-ui/backend/ticket/models/ticket.py b/dbm-ui/backend/ticket/models/ticket.py index bc072b95ef..883cbf1e13 100644 --- a/dbm-ui/backend/ticket/models/ticket.py +++ b/dbm-ui/backend/ticket/models/ticket.py @@ -156,9 +156,9 @@ def get_terminate_reason(self): def get_current_operators(self): # 获取当前流程处理人和协助人 running_todo = self.todo_of_ticket.filter(status=TodoStatus.TODO).first() - if not running_todo: - return [] - return {"operators": running_todo.operators, "helpers": running_todo.helpers} + operators = running_todo.operators if running_todo else [] + helpers = running_todo.helpers if running_todo else [] + return {"operators": operators, "helpers": helpers} def update_details(self, **kwargs): self.details.update(kwargs)