Skip to content

Commit

Permalink
fix(backend): 修复已完成单据没发送通知 TencentBlueKing#9117
Browse files Browse the repository at this point in the history
  • Loading branch information
iSecloud committed Jan 17, 2025
1 parent bdb6824 commit f2bbda7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
4 changes: 2 additions & 2 deletions dbm-ui/backend/core/notify/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
37 changes: 24 additions & 13 deletions dbm-ui/backend/ticket/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import itertools
import json
import logging
from collections import defaultdict
from typing import Dict, List

from django.db import transaction
Expand Down Expand Up @@ -60,44 +61,54 @@ 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),
}

# 补充关联对象信息
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
Expand Down
6 changes: 3 additions & 3 deletions dbm-ui/backend/ticket/models/ticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f2bbda7

Please sign in to comment.