Skip to content

Commit 5f571b0

Browse files
thinker8581weilaaa
authored andcommitted
fix(event): Fix Duplicate event notification delivery (#34)
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
1 parent cd1be06 commit 5f571b0

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

veaiops/handler/services/event/subscribe.py

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -93,40 +93,50 @@ async def create_notice_details(event: Event, subscribes: List[Subscribe]) -> Li
9393
"""
9494
logger.info(f"start create notice details. event_id={event.id} matched_subscribes={subscribes}")
9595
notice_details = []
96+
seen_notifications = set()
9697
for subscribe in subscribes:
9798
# 2.1 Webhook notification
9899
if subscribe.enable_webhook is True and subscribe.webhook_endpoint:
99-
logger.info(f"create notice detail with webhook endpoint {subscribe.webhook_endpoint} meet")
100-
notice_details.append(
101-
EventNoticeDetail(
102-
event_main_id=event.id,
103-
notice_channel=ChannelType.Webhook,
104-
target=subscribe.webhook_endpoint,
105-
extra={"headers": subscribe.webhook_headers},
106-
status=EventStatus.INITIAL,
100+
key = (event.id, ChannelType.Webhook, subscribe.webhook_endpoint)
101+
if key not in seen_notifications:
102+
logger.info(f"create notice detail with webhook endpoint {subscribe.webhook_endpoint} meet")
103+
notice_details.append(
104+
EventNoticeDetail(
105+
event_main_id=event.id,
106+
notice_channel=ChannelType.Webhook,
107+
target=subscribe.webhook_endpoint,
108+
extra={"headers": subscribe.webhook_headers},
109+
status=EventStatus.INITIAL,
110+
)
107111
)
108-
)
112+
seen_notifications.add(key)
109113

110114
# 2.2 Channel notifications
111115
for strategy in subscribe.inform_strategy_ids:
112116
logger.info(f"create notice detail with channel strategy {strategy}")
113117
for chat_id in strategy.chat_ids:
114-
logger.info(
115-
f"create notice detail with channel strategy {strategy} channel{strategy.channel} chat_id {chat_id}"
116-
)
117-
notice_details.append(
118-
EventNoticeDetail(
119-
event_main_id=event.id,
120-
notice_channel=ChannelType(strategy.channel),
121-
target=chat_id,
122-
extra={
123-
"bot_id": strategy.bot_id,
124-
"msg_id": event.raw_data.msg_id if isinstance(event.raw_data, AgentNotification) else "",
125-
"chat_id": chat_id,
126-
},
127-
status=EventStatus.INITIAL,
118+
key = (event.id, ChannelType(strategy.channel), chat_id)
119+
if key not in seen_notifications:
120+
logger.info(
121+
f"create notice detail with channel strategy {strategy} "
122+
f"channel {strategy.channel} chat_id {chat_id}"
128123
)
129-
)
124+
notice_details.append(
125+
EventNoticeDetail(
126+
event_main_id=event.id,
127+
notice_channel=ChannelType(strategy.channel),
128+
target=chat_id,
129+
extra={
130+
"bot_id": strategy.bot_id,
131+
"msg_id": event.raw_data.msg_id
132+
if isinstance(event.raw_data, AgentNotification)
133+
else "",
134+
"chat_id": chat_id,
135+
},
136+
status=EventStatus.INITIAL,
137+
)
138+
)
139+
seen_notifications.add(key)
130140

131141
return notice_details
132142

0 commit comments

Comments
 (0)