Skip to content

Commit

Permalink
use alert fields in new builder
Browse files Browse the repository at this point in the history
  • Loading branch information
ofek1weiss committed Feb 18, 2025
1 parent c7a628f commit ba47099
Show file tree
Hide file tree
Showing 20 changed files with 2,059 additions and 135 deletions.
2 changes: 1 addition & 1 deletion elementary/monitor/alerts/alert_messages/alert_fields.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from enum import Enum


class AlertFields(str, Enum):
class AlertField(str, Enum):
TABLE = "table"
COLUMN = "column"
DESCRIPTION = "description"
Expand Down
34 changes: 18 additions & 16 deletions elementary/monitor/alerts/alert_messages/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
TextStyle,
)
from elementary.messages.message_body import Color, MessageBlock, MessageBody
from elementary.monitor.alerts.alert_messages.alert_fields import AlertFields
from elementary.monitor.alerts.alert_messages.alert_fields import AlertField
from elementary.monitor.alerts.alerts_groups.alerts_group import AlertsGroup
from elementary.monitor.alerts.alerts_groups.base_alerts_group import BaseAlertsGroup
from elementary.monitor.alerts.alerts_groups.grouped_by_table import (
Expand Down Expand Up @@ -205,13 +205,13 @@ def _get_details_blocks(
)
)
fact_blocks = []
if table and AlertFields.TABLE in fields:
if table and AlertField.TABLE in fields:
fact_blocks.append(
PrimaryFactBlock(
(TextLineBlock(text="Table"), TextLineBlock(text=table))
)
)
if column and AlertFields.COLUMN in fields:
if column and AlertField.COLUMN in fields:
fact_blocks.append(
NonPrimaryFactBlock(
(TextLineBlock(text="Column"), TextLineBlock(text=column))
Expand All @@ -233,22 +233,22 @@ def _get_details_blocks(
if subscribers
else ItalicTextLineBlock(text="No subscribers")
)
if AlertFields.TAGS in fields:
if AlertField.TAGS in fields:
fact_blocks.append(
NonPrimaryFactBlock((TextLineBlock(text="Tags"), tags_line))
)
if AlertFields.OWNERS in fields:
if AlertField.OWNERS in fields:
fact_blocks.append(
NonPrimaryFactBlock((TextLineBlock(text="Owners"), owners_line))
)
if AlertFields.SUBSCRIBERS in fields:
if AlertField.SUBSCRIBERS in fields:
fact_blocks.append(
NonPrimaryFactBlock(
(TextLineBlock(text="Subscribers"), subscribers_line)
)
)

if description and AlertFields.DESCRIPTION in fields:
if description and AlertField.DESCRIPTION in fields:
fact_blocks.append(
PrimaryFactBlock(
(TextLineBlock(text="Description"), TextLineBlock(text=description))
Expand All @@ -273,7 +273,7 @@ def _get_result_blocks(
fields: List[str] = [],
) -> List[MessageBlock]:
result_blocks: List[MessageBlock] = []
if result_message and AlertFields.RESULT_MESSAGE in fields:
if result_message and AlertField.RESULT_MESSAGE in fields:
result_blocks.append(
LinesBlock(
lines=[
Expand All @@ -286,7 +286,7 @@ def _get_result_blocks(
)
if (
result_sample or anomalous_value
) and AlertFields.TEST_RESULTS_SAMPLE in fields:
) and AlertField.TEST_RESULTS_SAMPLE in fields:
result_blocks.append(
LinesBlock(
lines=[
Expand Down Expand Up @@ -315,7 +315,7 @@ def _get_result_blocks(
result_blocks.append(
JsonCodeBlock(content=result_sample),
)
if result_query and AlertFields.TEST_QUERY in fields:
if result_query and AlertField.TEST_QUERY in fields:
result_blocks.append(
LinesBlock(
lines=[
Expand Down Expand Up @@ -343,7 +343,7 @@ def _get_test_alert_config_blocks(
self, test_params: Optional[Dict[str, Any]], fields: List[str]
) -> List[MessageBlock]:
config_blocks: List[MessageBlock] = []
if test_params and AlertFields.TEST_PARAMS in fields:
if test_params and AlertField.TEST_PARAMS in fields:
config_blocks.append(
LinesBlock(
lines=[
Expand Down Expand Up @@ -547,12 +547,13 @@ def _get_alert_result_blocks(
title = "Result"

if isinstance(alert, TestAlertModel):
is_anomaly_detection = alert.test_type == "anomaly_detection"
result_blocks = self._get_result_blocks(
result_message=alert.error_message,
result_sample=alert.test_rows_sample,
anomalous_value=(
alert.other if alert.test_type == "anomaly_detection" else None
result_sample=(
alert.test_rows_sample if not is_anomaly_detection else None
),
anomalous_value=(alert.other if is_anomaly_detection else None),
result_query=alert.test_results_query,
fields=fields,
)
Expand Down Expand Up @@ -629,10 +630,11 @@ def _get_alert_groups_blocks(
def build(
self,
alert: AlertType,
fields: Optional[List[str]] = None,
) -> MessageBody:
color = self._get_alert_color(alert)
fields = fields or [field.value for field in AlertFields]

fields = alert.alert_fields if not isinstance(alert, BaseAlertsGroup) else None
fields = fields or [field.value for field in AlertField]

blocks: List[MessageBlock] = []

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,8 @@ def _send_alert(
else:
# New messaging integration path - converts alerts to message bodies
alert_message_builder = AlertMessageBuilder()
fields = alert.alert_fields if not isinstance(alert, AlertsGroup) else None
alert_message_body = alert_message_builder.build(
alert=alert,
fields=fields,
)
try:
self._send_message(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
{
"type": "AdaptiveCard",
"body": [
{
"type": "Container",
"separator": true,
"items": [
{
"type": "Container",
"items": [
{
"type": "TextBlock",
"text": "Failure: \"test_short_name\" test failed on test_table.test_column",
"weight": "bolder",
"size": "large",
"wrap": true
}
],
"style": "Attention"
},
{
"type": "TextBlock",
"text": "**Test:** test_short_name | **Status:** fail | **Time:** N/A",
"wrap": true
}
]
},
{
"type": "Container",
"separator": true,
"items": [
{
"type": "TextBlock",
"text": "\u2139\ufe0f **Details**",
"wrap": true
},
{
"type": "FactSet",
"facts": [
{
"title": "Table",
"value": "test_table"
},
{
"title": "Tags",
"value": "tag1, tag2"
},
{
"title": "Owners",
"value": "owner1, owner2"
},
{
"title": "Subscribers",
"value": "_No subscribers_"
},
{
"title": "Description",
"value": "Test description"
}
]
}
]
},
{
"type": "Container",
"separator": true,
"items": [
{
"type": "ActionSet",
"actions": [
{
"type": "Action.ToggleVisibility",
"title": "Test Result",
"targetElements": [
"expandable-00000000-0000-0000-0000-000000000001"
]
}
]
},
{
"type": "Container",
"id": "expandable-00000000-0000-0000-0000-000000000001",
"items": [
{
"type": "Container",
"separator": true,
"items": [
{
"type": "TextBlock",
"text": "**Result Message**",
"wrap": true
},
{
"type": "Container",
"style": "emphasis",
"items": [
{
"type": "RichTextBlock",
"inlines": [
{
"type": "TextRun",
"text": "Test error message",
"fontType": "Monospace"
}
]
}
]
},
{
"type": "TextBlock",
"text": "\ud83d\udd0e **Test Results Sample**",
"wrap": true
},
{
"type": "Container",
"style": "emphasis",
"items": [
{
"type": "RichTextBlock",
"inlines": [
{
"type": "TextRun",
"text": "{\n \"column1\": \"value1\",\n \"column2\": \"value2\"\n}",
"fontType": "Monospace"
}
]
}
]
},
{
"type": "TextBlock",
"text": "**Test Results Query**",
"wrap": true
},
{
"type": "Container",
"style": "emphasis",
"items": [
{
"type": "RichTextBlock",
"inlines": [
{
"type": "TextRun",
"text": "select 1",
"fontType": "Monospace"
}
]
}
]
}
]
}
],
"isVisible": false
},
{
"type": "ActionSet",
"actions": [
{
"type": "Action.ToggleVisibility",
"title": "Test Configuration",
"targetElements": [
"expandable-00000000-0000-0000-0000-000000000002"
]
}
]
},
{
"type": "Container",
"id": "expandable-00000000-0000-0000-0000-000000000002",
"items": [
{
"type": "Container",
"separator": true,
"items": [
{
"type": "TextBlock",
"text": "\ud83d\udee0\ufe0f **Test Parameters**",
"wrap": true
},
{
"type": "Container",
"style": "emphasis",
"items": [
{
"type": "RichTextBlock",
"inlines": [
{
"type": "TextRun",
"text": "{\n \"param1\": \"value1\",\n \"param2\": \"value2\"\n}",
"fontType": "Monospace"
}
]
}
]
}
]
}
],
"isVisible": false
}
]
}
],
"version": "1.5"
}
Loading

0 comments on commit ba47099

Please sign in to comment.