From 2382ce195f8b57801fedf925e766b141696d853b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E5=BB=BA=E5=86=9B?= Date: Tue, 25 Nov 2025 21:35:26 +0800 Subject: [PATCH] fix(event): Fix Duplicate event notification delivery Previously, if multiple subscription rules matched an event and pointed to the same notification target (e.g., the same webhook endpoint or chat ID), multiple identical notifications would be sent. This change modifies the `create_notice_details` service to track sent notifications using a set of unique tuples `(event_id, channel, target)`. This ensures that a notification is delivered only once per unique target, even if triggered by multiple subscriptions. Change-Id: Ibcba09bc62d43d64ddca4862abf44232b74d43bc --- veaiops/handler/services/event/subscribe.py | 58 ++++++++++++--------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/veaiops/handler/services/event/subscribe.py b/veaiops/handler/services/event/subscribe.py index f3e5c9bd..c251c1d7 100644 --- a/veaiops/handler/services/event/subscribe.py +++ b/veaiops/handler/services/event/subscribe.py @@ -93,40 +93,50 @@ async def create_notice_details(event: Event, subscribes: List[Subscribe]) -> Li """ logger.info(f"start create notice details. event_id={event.id} matched_subscribes={subscribes}") notice_details = [] + seen_notifications = set() for subscribe in subscribes: # 2.1 Webhook notification if subscribe.enable_webhook is True and subscribe.webhook_endpoint: - logger.info(f"create notice detail with webhook endpoint {subscribe.webhook_endpoint} meet") - notice_details.append( - EventNoticeDetail( - event_main_id=event.id, - notice_channel=ChannelType.Webhook, - target=subscribe.webhook_endpoint, - extra={"headers": subscribe.webhook_headers}, - status=EventStatus.INITIAL, + key = (event.id, ChannelType.Webhook, subscribe.webhook_endpoint) + if key not in seen_notifications: + logger.info(f"create notice detail with webhook endpoint {subscribe.webhook_endpoint} meet") + notice_details.append( + EventNoticeDetail( + event_main_id=event.id, + notice_channel=ChannelType.Webhook, + target=subscribe.webhook_endpoint, + extra={"headers": subscribe.webhook_headers}, + status=EventStatus.INITIAL, + ) ) - ) + seen_notifications.add(key) # 2.2 Channel notifications for strategy in subscribe.inform_strategy_ids: logger.info(f"create notice detail with channel strategy {strategy}") for chat_id in strategy.chat_ids: - logger.info( - f"create notice detail with channel strategy {strategy} channel{strategy.channel} chat_id {chat_id}" - ) - notice_details.append( - EventNoticeDetail( - event_main_id=event.id, - notice_channel=ChannelType(strategy.channel), - target=chat_id, - extra={ - "bot_id": strategy.bot_id, - "msg_id": event.raw_data.msg_id if isinstance(event.raw_data, AgentNotification) else "", - "chat_id": chat_id, - }, - status=EventStatus.INITIAL, + key = (event.id, ChannelType(strategy.channel), chat_id) + if key not in seen_notifications: + logger.info( + f"create notice detail with channel strategy {strategy} " + f"channel {strategy.channel} chat_id {chat_id}" ) - ) + notice_details.append( + EventNoticeDetail( + event_main_id=event.id, + notice_channel=ChannelType(strategy.channel), + target=chat_id, + extra={ + "bot_id": strategy.bot_id, + "msg_id": event.raw_data.msg_id + if isinstance(event.raw_data, AgentNotification) + else "", + "chat_id": chat_id, + }, + status=EventStatus.INITIAL, + ) + ) + seen_notifications.add(key) return notice_details