From 35fb51bd669909b341432e107661a2f5d42351c4 Mon Sep 17 00:00:00 2001 From: MikaKerman Date: Thu, 10 Oct 2024 11:24:51 +0300 Subject: [PATCH] using GroupedAlert directly --- .../monitor/alerts/grouped_alerts/__init__.py | 2 - .../alerts/grouped_alerts/all_in_one.py | 7 -- .../alerts/grouped_alerts/grouping_type.py | 1 - .../alerts/integrations/base_integration.py | 20 +++--- .../alerts/integrations/slack/slack.py | 64 ++++++++----------- .../alerts/integrations/teams/teams.py | 8 +-- .../integrations/base_integration_mock.py | 4 ++ 7 files changed, 44 insertions(+), 62 deletions(-) delete mode 100644 elementary/monitor/alerts/grouped_alerts/all_in_one.py diff --git a/elementary/monitor/alerts/grouped_alerts/__init__.py b/elementary/monitor/alerts/grouped_alerts/__init__.py index aedaaab6c..6af686482 100644 --- a/elementary/monitor/alerts/grouped_alerts/__init__.py +++ b/elementary/monitor/alerts/grouped_alerts/__init__.py @@ -1,10 +1,8 @@ -from .all_in_one import AllInOneAlert from .grouped_alert import GroupedAlert from .grouped_by_table import GroupedByTableAlerts from .grouping_type import GroupingType __all__ = [ - "AllInOneAlert", "GroupedAlert", "GroupedByTableAlerts", "GroupingType", diff --git a/elementary/monitor/alerts/grouped_alerts/all_in_one.py b/elementary/monitor/alerts/grouped_alerts/all_in_one.py deleted file mode 100644 index 7a2bccaf9..000000000 --- a/elementary/monitor/alerts/grouped_alerts/all_in_one.py +++ /dev/null @@ -1,7 +0,0 @@ -from elementary.monitor.alerts.grouped_alerts.grouped_alert import GroupedAlert - - -class AllInOneAlert(GroupedAlert): - @property - def summary(self) -> str: - return f"{len(self.alerts)} issues detected" diff --git a/elementary/monitor/alerts/grouped_alerts/grouping_type.py b/elementary/monitor/alerts/grouped_alerts/grouping_type.py index 99b62b6ef..11537b806 100644 --- a/elementary/monitor/alerts/grouped_alerts/grouping_type.py +++ b/elementary/monitor/alerts/grouped_alerts/grouping_type.py @@ -4,4 +4,3 @@ class GroupingType(str, Enum): BY_ALERT = "alert" BY_TABLE = "table" - ALL_IN_ONE = "all_in_one" diff --git a/elementary/monitor/data_monitoring/alerts/integrations/base_integration.py b/elementary/monitor/data_monitoring/alerts/integrations/base_integration.py index 7089504cb..54ce5e654 100644 --- a/elementary/monitor/data_monitoring/alerts/integrations/base_integration.py +++ b/elementary/monitor/data_monitoring/alerts/integrations/base_integration.py @@ -1,11 +1,7 @@ from abc import ABC, abstractmethod from typing import Generator, List, Sequence, Tuple, Union -from elementary.monitor.alerts.grouped_alerts import ( - AllInOneAlert, - GroupedAlert, - GroupedByTableAlerts, -) +from elementary.monitor.alerts.grouped_alerts import GroupedAlert, GroupedByTableAlerts from elementary.monitor.alerts.model_alert import ModelAlertModel from elementary.monitor.alerts.source_freshness_alert import SourceFreshnessAlertModel from elementary.monitor.alerts.test_alert import TestAlertModel @@ -29,7 +25,7 @@ def _get_alert_template( ModelAlertModel, SourceFreshnessAlertModel, GroupedByTableAlerts, - AllInOneAlert, + GroupedAlert, ], *args, **kwargs, @@ -48,8 +44,8 @@ def _get_alert_template( return self._get_source_freshness_template(alert) elif isinstance(alert, GroupedByTableAlerts): return self._get_group_by_table_template(alert) - elif isinstance(alert, AllInOneAlert): - return self._get_all_in_one_template(alert) + elif isinstance(alert, GroupedAlert): + return self._get_grouped_template(alert) @abstractmethod def _get_dbt_test_template(self, alert: TestAlertModel, *args, **kwargs): @@ -80,7 +76,7 @@ def _get_group_by_table_template( raise NotImplementedError @abstractmethod - def _get_all_in_one_template(self, alert: AllInOneAlert, *args, **kwargs): + def _get_grouped_template(self, alert: GroupedAlert, *args, **kwargs): raise NotImplementedError @abstractmethod @@ -105,7 +101,7 @@ def send_alert( ModelAlertModel, SourceFreshnessAlertModel, GroupedByTableAlerts, - AllInOneAlert, + GroupedAlert, ], *args, **kwargs, @@ -129,7 +125,7 @@ def _group_alerts( ModelAlertModel, SourceFreshnessAlertModel, GroupedByTableAlerts, - AllInOneAlert, + GroupedAlert, ] ]: flattened_alerts: List[ @@ -144,7 +140,7 @@ def _group_alerts( if len(flattened_alerts) > threshold: logger.info(f"Grouping {len(flattened_alerts)} alerts into one") return [ - AllInOneAlert(alerts=flattened_alerts), + GroupedAlert(alerts=flattened_alerts), ] return alerts diff --git a/elementary/monitor/data_monitoring/alerts/integrations/slack/slack.py b/elementary/monitor/data_monitoring/alerts/integrations/slack/slack.py index 3832b92c8..cb4b7b590 100644 --- a/elementary/monitor/data_monitoring/alerts/integrations/slack/slack.py +++ b/elementary/monitor/data_monitoring/alerts/integrations/slack/slack.py @@ -9,11 +9,7 @@ from elementary.clients.slack.schema import SlackBlocksType, SlackMessageSchema from elementary.clients.slack.slack_message_builder import MessageColor from elementary.config.config import Config -from elementary.monitor.alerts.grouped_alerts import ( - AllInOneAlert, - GroupedAlert, - GroupedByTableAlerts, -) +from elementary.monitor.alerts.grouped_alerts import GroupedAlert, GroupedByTableAlerts from elementary.monitor.alerts.model_alert import ModelAlertModel from elementary.monitor.alerts.source_freshness_alert import SourceFreshnessAlertModel from elementary.monitor.alerts.test_alert import TestAlertModel @@ -104,18 +100,14 @@ def _get_alert_template( ModelAlertModel, SourceFreshnessAlertModel, GroupedByTableAlerts, - AllInOneAlert, + GroupedAlert, ], *args, **kwargs, ) -> SlackMessageSchema: if self.config.is_slack_workflow: return SlackMessageSchema(text=json.dumps(alert.data, sort_keys=True)) - - if isinstance(alert, AllInOneAlert): - alert_schema = self._get_all_in_one_template(alert) - else: - alert_schema = super()._get_alert_template(alert, *args, **kwargs) + alert_schema = super()._get_alert_template(alert, *args, **kwargs) return self.message_builder.get_slack_message(alert_schema=alert_schema) def _get_dbt_test_template( @@ -886,7 +878,7 @@ def _get_group_by_table_template( title=title_blocks, preview=preview_blocks, details=details_blocks ) - def _add_compact_all_in_one_sub_group_details_block( + def _add_compact_sub_group_details_block( self, details_blocks: list, alerts: Sequence[ @@ -908,29 +900,29 @@ def _add_compact_all_in_one_sub_group_details_block( ) ) - def _get_compact_all_in_one_sub_group_details_block( - self, alert: AllInOneAlert, *args, **kwargs + def _get_compact_sub_group_details_block( + self, alert: GroupedAlert, *args, **kwargs ) -> List[dict]: details_blocks: List[dict] = [] - self._add_compact_all_in_one_sub_group_details_block( + self._add_compact_sub_group_details_block( details_blocks=details_blocks, alerts=alert.model_errors, sub_title="Model Errors", bullet_icon="X", ) - self._add_compact_all_in_one_sub_group_details_block( + self._add_compact_sub_group_details_block( details_blocks=details_blocks, alerts=alert.test_failures, sub_title="Test Failures", bullet_icon="small_red_triangle", ) - self._add_compact_all_in_one_sub_group_details_block( + self._add_compact_sub_group_details_block( details_blocks=details_blocks, alerts=alert.test_warnings, sub_title="Test Warnings", bullet_icon="warning", ) - self._add_compact_all_in_one_sub_group_details_block( + self._add_compact_sub_group_details_block( details_blocks=details_blocks, alerts=alert.test_errors, sub_title="Test Errors", @@ -938,8 +930,8 @@ def _get_compact_all_in_one_sub_group_details_block( ) return details_blocks - def _get_all_in_one_compact_template( - self, alert: AllInOneAlert + def _get_grouped_compact_template( + self, alert: GroupedAlert ) -> SlackAlertMessageSchema: self.message_builder.add_message_color(self._get_color(alert.status)) @@ -949,10 +941,10 @@ def _get_all_in_one_compact_template( ) ] - details_blocks = self._get_compact_all_in_one_sub_group_details_block(alert) + details_blocks = self._get_compact_sub_group_details_block(alert) return SlackAlertMessageSchema(title=title_blocks, details=details_blocks) - def _add_all_in_one_sub_group_details_section( + def _add_sub_group_details_block( self, details_blocks: list, alerts: Sequence[ @@ -981,29 +973,29 @@ def _add_all_in_one_sub_group_details_section( ) details_blocks.append(section) - def _get_all_in_one_sub_group_details_blocks( - self, alert: AllInOneAlert, *args, **kwargs + def _get_sub_group_details_blocks( + self, alert: GroupedAlert, *args, **kwargs ) -> List[dict]: details_blocks: List[dict] = [] - self._add_all_in_one_sub_group_details_section( + self._add_sub_group_details_block( details_blocks=details_blocks, alerts=alert.model_errors, sub_title="Model Errors", bullet_icon="X", ) - self._add_all_in_one_sub_group_details_section( + self._add_sub_group_details_block( details_blocks=details_blocks, alerts=alert.test_failures, sub_title="Test Failures", bullet_icon="small_red_triangle", ) - self._add_all_in_one_sub_group_details_section( + self._add_sub_group_details_block( details_blocks=details_blocks, alerts=alert.test_warnings, sub_title="Test Warnings", bullet_icon="warning", ) - self._add_all_in_one_sub_group_details_section( + self._add_sub_group_details_block( details_blocks=details_blocks, alerts=alert.test_errors, sub_title="Test Errors", @@ -1011,11 +1003,11 @@ def _get_all_in_one_sub_group_details_blocks( ) return details_blocks - def _get_all_in_one_template( - self, alert: AllInOneAlert, *args, **kwargs + def _get_grouped_template( + self, alert: GroupedAlert, *args, **kwargs ) -> SlackAlertMessageSchema: if len(alert.alerts) >= self.COMPACT_SCHEMA_THRESHOLD: - return self._get_all_in_one_compact_template(alert) + return self._get_grouped_compact_template(alert) self.message_builder.add_message_color(self._get_color(alert.status)) title_blocks = [ @@ -1024,7 +1016,7 @@ def _get_all_in_one_template( ), self._get_alert_type_counters_block(alert), ] - details_blocks = self._get_all_in_one_sub_group_details_blocks(alert) + details_blocks = self._get_sub_group_details_blocks(alert) return SlackAlertMessageSchema(title=title_blocks, details=details_blocks) @staticmethod @@ -1055,7 +1047,7 @@ def _get_fallback_template( ModelAlertModel, SourceFreshnessAlertModel, GroupedByTableAlerts, - AllInOneAlert, + GroupedAlert, ], *args, **kwargs, @@ -1098,7 +1090,7 @@ def _fix_owners_and_subscribers( ModelAlertModel, SourceFreshnessAlertModel, GroupedByTableAlerts, - AllInOneAlert, + GroupedAlert, ], ): if isinstance(alert, GroupedAlert): @@ -1118,7 +1110,7 @@ def send_alert( ModelAlertModel, SourceFreshnessAlertModel, GroupedByTableAlerts, - AllInOneAlert, + GroupedAlert, ], *args, **kwargs, @@ -1163,7 +1155,7 @@ def _get_integration_params( ModelAlertModel, SourceFreshnessAlertModel, GroupedByTableAlerts, - AllInOneAlert, + GroupedAlert, ], *args, **kwargs, diff --git a/elementary/monitor/data_monitoring/alerts/integrations/teams/teams.py b/elementary/monitor/data_monitoring/alerts/integrations/teams/teams.py index 6569f3aa6..2684da524 100644 --- a/elementary/monitor/data_monitoring/alerts/integrations/teams/teams.py +++ b/elementary/monitor/data_monitoring/alerts/integrations/teams/teams.py @@ -7,7 +7,7 @@ from elementary.clients.teams.client import TeamsClient from elementary.config.config import Config -from elementary.monitor.alerts.grouped_alerts import AllInOneAlert, GroupedByTableAlerts +from elementary.monitor.alerts.grouped_alerts import GroupedAlert, GroupedByTableAlerts from elementary.monitor.alerts.model_alert import ModelAlertModel from elementary.monitor.alerts.source_freshness_alert import SourceFreshnessAlertModel from elementary.monitor.alerts.test_alert import TestAlertModel @@ -520,7 +520,7 @@ def _get_group_by_table_template( self._get_section("*Test errors*", f"{text}") ) - def _get_all_in_one_template(self, alert: AllInOneAlert, *args, **kwargs): + def _get_grouped_template(self, alert: GroupedAlert, *args, **kwargs): title = f"{self._get_display_name(alert.status)}: {alert.summary}" subtitle = "" @@ -590,7 +590,7 @@ def _get_fallback_template( ModelAlertModel, SourceFreshnessAlertModel, GroupedByTableAlerts, - AllInOneAlert, + GroupedAlert, ], *args, **kwargs, @@ -614,7 +614,7 @@ def send_alert( ModelAlertModel, SourceFreshnessAlertModel, GroupedByTableAlerts, - AllInOneAlert, + GroupedAlert, ], *args, **kwargs, diff --git a/tests/mocks/data_monitoring/alerts/integrations/base_integration_mock.py b/tests/mocks/data_monitoring/alerts/integrations/base_integration_mock.py index fbc9bca25..a99742f44 100644 --- a/tests/mocks/data_monitoring/alerts/integrations/base_integration_mock.py +++ b/tests/mocks/data_monitoring/alerts/integrations/base_integration_mock.py @@ -1,6 +1,7 @@ from typing import Union from elementary.monitor.alerts.grouped_alerts import GroupedByTableAlerts +from elementary.monitor.alerts.grouped_alerts.grouped_alert import GroupedAlert from elementary.monitor.alerts.model_alert import ModelAlertModel from elementary.monitor.alerts.source_freshness_alert import SourceFreshnessAlertModel from elementary.monitor.alerts.test_alert import TestAlertModel @@ -35,6 +36,9 @@ def _get_group_by_table_template( ): return "grouped_by_table" + def _get_grouped_template(self, alert: GroupedAlert, *args, **kwargs): + return "grouped" + def _get_fallback_template( self, alert: Union[