Skip to content

Commit

Permalink
using GroupedAlert directly
Browse files Browse the repository at this point in the history
  • Loading branch information
MikaKerman committed Oct 10, 2024
1 parent 6ee5466 commit 7abcef2
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 62 deletions.
2 changes: 0 additions & 2 deletions elementary/monitor/alerts/grouped_alerts/__init__.py
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
7 changes: 0 additions & 7 deletions elementary/monitor/alerts/grouped_alerts/all_in_one.py

This file was deleted.

1 change: 0 additions & 1 deletion elementary/monitor/alerts/grouped_alerts/grouping_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@
class GroupingType(str, Enum):
BY_ALERT = "alert"
BY_TABLE = "table"
ALL_IN_ONE = "all_in_one"
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -29,7 +25,7 @@ def _get_alert_template(
ModelAlertModel,
SourceFreshnessAlertModel,
GroupedByTableAlerts,
AllInOneAlert,
GroupedAlert,
],
*args,
**kwargs,
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -105,7 +101,7 @@ def send_alert(
ModelAlertModel,
SourceFreshnessAlertModel,
GroupedByTableAlerts,
AllInOneAlert,
GroupedAlert,
],
*args,
**kwargs,
Expand All @@ -129,7 +125,7 @@ def _group_alerts(
ModelAlertModel,
SourceFreshnessAlertModel,
GroupedByTableAlerts,
AllInOneAlert,
GroupedAlert,
]
]:
flattened_alerts: List[
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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[
Expand All @@ -908,38 +900,38 @@ 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",
bullet_icon="exclamation",
)
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))

Expand All @@ -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[
Expand Down Expand Up @@ -981,41 +973,41 @@ 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",
bullet_icon="exclamation",
)
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 = [
Expand All @@ -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
Expand Down Expand Up @@ -1055,7 +1047,7 @@ def _get_fallback_template(
ModelAlertModel,
SourceFreshnessAlertModel,
GroupedByTableAlerts,
AllInOneAlert,
GroupedAlert,
],
*args,
**kwargs,
Expand Down Expand Up @@ -1098,7 +1090,7 @@ def _fix_owners_and_subscribers(
ModelAlertModel,
SourceFreshnessAlertModel,
GroupedByTableAlerts,
AllInOneAlert,
GroupedAlert,
],
):
if isinstance(alert, GroupedAlert):
Expand All @@ -1118,7 +1110,7 @@ def send_alert(
ModelAlertModel,
SourceFreshnessAlertModel,
GroupedByTableAlerts,
AllInOneAlert,
GroupedAlert,
],
*args,
**kwargs,
Expand Down Expand Up @@ -1163,7 +1155,7 @@ def _get_integration_params(
ModelAlertModel,
SourceFreshnessAlertModel,
GroupedByTableAlerts,
AllInOneAlert,
GroupedAlert,
],
*args,
**kwargs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = ""
Expand Down Expand Up @@ -590,7 +590,7 @@ def _get_fallback_template(
ModelAlertModel,
SourceFreshnessAlertModel,
GroupedByTableAlerts,
AllInOneAlert,
GroupedAlert,
],
*args,
**kwargs,
Expand All @@ -614,7 +614,7 @@ def send_alert(
ModelAlertModel,
SourceFreshnessAlertModel,
GroupedByTableAlerts,
AllInOneAlert,
GroupedAlert,
],
*args,
**kwargs,
Expand Down

0 comments on commit 7abcef2

Please sign in to comment.