feat: add workflow notification dispatcher
This commit is contained in:
114
review_agent/notifications/records.py
Normal file
114
review_agent/notifications/records.py
Normal file
@@ -0,0 +1,114 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from django.utils import timezone
|
||||
|
||||
from review_agent.models import WorkflowNotificationRecord
|
||||
|
||||
from .context import NotificationContext
|
||||
from .message_builder import absolute_result_url
|
||||
from .recipient import ResolvedFeishuTarget
|
||||
|
||||
|
||||
def existing_success_record(context: NotificationContext) -> WorkflowNotificationRecord | None:
|
||||
return (
|
||||
WorkflowNotificationRecord.objects.filter(
|
||||
dedupe_key=context.dedupe_key,
|
||||
send_status=WorkflowNotificationRecord.SendStatus.SUCCESS,
|
||||
)
|
||||
.order_by("-created_at", "-id")
|
||||
.first()
|
||||
)
|
||||
|
||||
|
||||
def create_disabled_record(
|
||||
context: NotificationContext,
|
||||
target: ResolvedFeishuTarget,
|
||||
message_summary: str,
|
||||
) -> WorkflowNotificationRecord:
|
||||
return _create_record(
|
||||
context,
|
||||
target,
|
||||
channel=WorkflowNotificationRecord.Channel.DISABLED,
|
||||
send_status=WorkflowNotificationRecord.SendStatus.DISABLED,
|
||||
message_summary=message_summary,
|
||||
error_code="notify_disabled",
|
||||
error_message="FEISHU_NOTIFY_ENABLED 未启用",
|
||||
)
|
||||
|
||||
|
||||
def create_failed_record(
|
||||
context: NotificationContext,
|
||||
target: ResolvedFeishuTarget,
|
||||
message_summary: str,
|
||||
*,
|
||||
error_code: str,
|
||||
error_message: str,
|
||||
request_duration_ms: int | None = None,
|
||||
) -> WorkflowNotificationRecord:
|
||||
return _create_record(
|
||||
context,
|
||||
target,
|
||||
channel=WorkflowNotificationRecord.Channel.FEISHU_API,
|
||||
send_status=WorkflowNotificationRecord.SendStatus.FAILED,
|
||||
message_summary=message_summary,
|
||||
error_code=error_code,
|
||||
error_message=error_message,
|
||||
request_duration_ms=request_duration_ms,
|
||||
)
|
||||
|
||||
|
||||
def create_success_record(
|
||||
context: NotificationContext,
|
||||
target: ResolvedFeishuTarget,
|
||||
message_summary: str,
|
||||
*,
|
||||
external_message_id: str,
|
||||
request_duration_ms: int | None = None,
|
||||
) -> WorkflowNotificationRecord:
|
||||
return _create_record(
|
||||
context,
|
||||
target,
|
||||
channel=WorkflowNotificationRecord.Channel.FEISHU_API,
|
||||
send_status=WorkflowNotificationRecord.SendStatus.SUCCESS,
|
||||
message_summary=message_summary,
|
||||
external_message_id=external_message_id,
|
||||
request_duration_ms=request_duration_ms,
|
||||
sent_at=timezone.now(),
|
||||
)
|
||||
|
||||
|
||||
def _create_record(
|
||||
context: NotificationContext,
|
||||
target: ResolvedFeishuTarget,
|
||||
*,
|
||||
channel: str,
|
||||
send_status: str,
|
||||
message_summary: str,
|
||||
error_code: str = "",
|
||||
error_message: str = "",
|
||||
external_message_id: str = "",
|
||||
request_duration_ms: int | None = None,
|
||||
sent_at=None,
|
||||
) -> WorkflowNotificationRecord:
|
||||
return WorkflowNotificationRecord.objects.create(
|
||||
workflow_type=context.workflow_type,
|
||||
workflow_batch_id=context.workflow_batch_id,
|
||||
workflow_batch_no=context.workflow_batch_no,
|
||||
workflow_status=context.workflow_status,
|
||||
dedupe_key=context.dedupe_key,
|
||||
trigger_user_id=context.trigger_user_id,
|
||||
channel=channel,
|
||||
target=target.display_name,
|
||||
at_display_name=target.display_name,
|
||||
at_identifier_type=target.identifier_type,
|
||||
at_identifier_masked=target.masked_identifier,
|
||||
send_status=send_status,
|
||||
message_title=context.title,
|
||||
message_summary=message_summary,
|
||||
result_url=absolute_result_url(context.result_path),
|
||||
external_message_id=external_message_id,
|
||||
error_code=error_code,
|
||||
error_message=error_message[:1000],
|
||||
request_duration_ms=request_duration_ms,
|
||||
sent_at=sent_at,
|
||||
)
|
||||
Reference in New Issue
Block a user