From 95f890c53cd0b69b79d77e07077f48e5c7df2b0d Mon Sep 17 00:00:00 2001 From: ayazabbas <30928485+ayazabbas@users.noreply.github.com> Date: Thu, 13 Jun 2024 10:49:00 +0100 Subject: [PATCH] fix how delayed events are sent/resolved (#78) * fix how delayed events are sent/resolved * bump version --- pyproject.toml | 2 +- pyth_observer/dispatch.py | 25 ++++++++++++++----------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index bffa397..08c6c34 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ ignore_missing_imports = true [tool.poetry] name = "pyth-observer" -version = "0.2.13" +version = "0.2.14" description = "Alerts and stuff" authors = [] readme = "README.md" diff --git a/pyth_observer/dispatch.py b/pyth_observer/dispatch.py index 6338bd4..d34036c 100644 --- a/pyth_observer/dispatch.py +++ b/pyth_observer/dispatch.py @@ -89,11 +89,10 @@ async def run(self, states: List[State]): "failures": 1, "last_window_failures": None, "sent": False, - "event_type": event_type, } else: alert["failures"] += 1 - self.delayed_events[alert_identifier] = event + self.delayed_events[f"{event_type}-{alert_identifier}"] = event continue # Skip sending immediately for ZendutyEvent or TelegramEvent sent_events.append(event.send()) @@ -188,10 +187,7 @@ async def process_zenduty_events(self, current_time): ): logger.debug(f"Resolving Zenduty alert {identifier}") resolved = True - if ( - info["sent"] - and info.get("event_type", "ZendutyEvent") == "ZendutyEvent" - ): + if info["sent"]: response = await send_zenduty_alert( identifier, identifier, resolved=True ) @@ -212,16 +208,23 @@ async def process_zenduty_events(self, current_time): logger.debug(f"Raising Zenduty alert {identifier}") self.open_alerts[identifier]["sent"] = True self.open_alerts[identifier]["last_alert"] = current_time.isoformat() - event = self.delayed_events.get(identifier) - if event: - to_alert.append(event.send()) + for event_type in ["ZendutyEvent", "TelegramEvent"]: + key = f"{event_type}-{identifier}" + event = self.delayed_events.get(key) + if event: + to_alert.append(event.send()) + # Send the alerts that were delayed due to thresholds await asyncio.gather(*to_alert) + + # Remove alerts that have been resolved for identifier in to_remove: if self.open_alerts.get(identifier): del self.open_alerts[identifier] - if self.delayed_events.get(identifier): - del self.delayed_events[identifier] + for event_type in ["ZendutyEvent", "TelegramEvent"]: + key = f"{event_type}-{identifier}" + if self.delayed_events.get(key): + del self.delayed_events[key] with open(self.open_alerts_file, "w") as file: json.dump(self.open_alerts, file)