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)