|
22 | 22 | from backend.components.bkchat.client import BkChatApi |
23 | 23 | from backend.configuration.constants import BizSettingsEnum |
24 | 24 | from backend.configuration.models import BizSettings |
25 | | -from backend.core.notify.constants import DEFAULT_BIZ_NOTIFY_CONFIG, MsgType |
| 25 | +from backend.core.notify.constants import DEFAULT_BIZ_AI_NOTIFY_CONFIG, DEFAULT_BIZ_NOTIFY_CONFIG, MsgType |
26 | 26 | from backend.core.notify.exceptions import NotifyBaseException |
27 | | -from backend.core.notify.template import FAILED_TEMPLATE, FINISHED_TEMPLATE, TERMINATE_TEMPLATE, TODO_TEMPLATE |
| 27 | +from backend.core.notify.template import ( |
| 28 | + AI_TASK_GUARDIAN_TEMPLATE, |
| 29 | + FAILED_TEMPLATE, |
| 30 | + FINISHED_TEMPLATE, |
| 31 | + TERMINATE_TEMPLATE, |
| 32 | + TODO_TEMPLATE, |
| 33 | +) |
28 | 34 | from backend.db_meta.models import AppCache |
29 | 35 | from backend.env import DEFAULT_USERNAME |
30 | 36 | from backend.exceptions import ApiResultError |
@@ -344,6 +350,29 @@ def render_msg_template(self, msg_type: str): |
344 | 350 | content = textwrap.dedent(template.render(payload)) |
345 | 351 | return title, content |
346 | 352 |
|
| 353 | + def render_msg_template_for_ai_task(self, ai_result: str): |
| 354 | + """ |
| 355 | + 拼装单据值守推送的信息内容,以及标题 |
| 356 | + """ |
| 357 | + biz = AppCache.objects.get(bk_biz_id=self.bk_biz_id) |
| 358 | + title = _("「DBM」:检测到您的{ticket_type}单据「{ticket_id}」有风险").format( |
| 359 | + ticket_type=TicketType.get_choice_label(self.ticket.ticket_type), |
| 360 | + ticket_id=self.ticket.id, |
| 361 | + ) |
| 362 | + # 渲染通知内容 |
| 363 | + jinja_env = Environment() |
| 364 | + template = jinja_env.from_string(AI_TASK_GUARDIAN_TEMPLATE) |
| 365 | + payload = { |
| 366 | + "creator": self.ticket.creator, |
| 367 | + "biz_name": f"{biz.bk_biz_name}(#{self.bk_biz_id}, {biz.db_app_abbr})", |
| 368 | + "cluster_domains": ",".join(self.clusters), |
| 369 | + "submit_time": self.ticket.create_at.astimezone().strftime("%Y-%m-%d %H:%M:%S%z"), |
| 370 | + "running_time": f"{self.ticket.get_cost_time()}s", |
| 371 | + "ai_result": ai_result, |
| 372 | + } |
| 373 | + content = textwrap.dedent(template.render(payload)) |
| 374 | + return title, content |
| 375 | + |
347 | 376 | def send_msg(self): |
348 | 377 | # 获取单据通知设置,优先: 单据配置 > 业务配置 > 默认业务配置 |
349 | 378 | if self.phase in self.ticket.msg_config: |
@@ -378,8 +407,41 @@ def send_msg(self): |
378 | 407 | except (ApiResultError, Exception) as e: |
379 | 408 | logger.error(_("[{}]消息发送失败,错误信息: {}").format(MsgType.get_choice_label(msg_type), e)) |
380 | 409 |
|
| 410 | + def send_msg_of_ai_task_guardian(self, ai_result: str): |
| 411 | + """ |
| 412 | + 定义AI单据值守推送消息的逻辑 |
| 413 | + @param ai_result: 调用智能体之后的结果信息 |
| 414 | + """ |
| 415 | + biz_notify_config = BizSettings.get_setting_value( |
| 416 | + self.bk_biz_id, key=BizSettingsEnum.NOTIFY_CONFIG, default=DEFAULT_BIZ_AI_NOTIFY_CONFIG |
| 417 | + ) |
| 418 | + send_msg_config = biz_notify_config["AI_TASK_GUARDIAN"] |
| 419 | + |
| 420 | + send_msg_types = [msg_type for msg_type in send_msg_config if send_msg_config.get(msg_type)] |
| 421 | + for msg_type in send_msg_types: |
| 422 | + notify_class, context = self.get_notify_class(msg_type) |
| 423 | + |
| 424 | + # 如果是群机器人通知,则接受者为群ID |
| 425 | + if msg_type == MsgType.WECOM_ROBOT: |
| 426 | + self.receivers = send_msg_config.get(MsgType.WECOM_ROBOT.value, []) |
| 427 | + |
| 428 | + # 拼接信息通知 |
| 429 | + title, content = self.render_msg_template_for_ai_task(ai_result=ai_result) |
| 430 | + |
| 431 | + try: |
| 432 | + notify_class(title, content, self.receivers).send_msg(msg_type, context=context) |
| 433 | + except (ApiResultError, Exception) as e: |
| 434 | + logger.error(_("[{}]消息发送失败,错误信息: {}").format(MsgType.get_choice_label(msg_type), e)) |
| 435 | + |
381 | 436 |
|
382 | 437 | @shared_task |
383 | 438 | def send_msg(ticket_id: int, deadline: int = None): |
384 | 439 | # 可异步发送消息,非阻塞路径默认不抛出异常 |
385 | 440 | NotifyAdapter(ticket_id, deadline).send_msg() |
| 441 | + |
| 442 | + |
| 443 | +@shared_task |
| 444 | +def send_msg_for_ai_task_guardian(ticket_id: int, ai_result: str, deadline: int = None): |
| 445 | + # 可异步发送消息,非阻塞路径默认不抛出异常 |
| 446 | + # AI单据值守消息通道专属 |
| 447 | + NotifyAdapter(ticket_id, deadline).send_msg_of_ai_task_guardian(ai_result=ai_result) |
0 commit comments