Skip to content

Commit dc2348e

Browse files
committed
Refactor alert message handling to support multiple links. Introduced LinksLineBlock for rendering multiple links with optional icons, and updated AlertMessageBuilder to utilize this new structure.
1 parent ec86dcc commit dc2348e

3 files changed

Lines changed: 39 additions & 10 deletions

File tree

elementary/messages/block_builders.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,30 @@ def ItalicTextLineBlock(*, text: SimpleLineBlock) -> LineBlock:
8282
return TextLineBlock(text=text, style=TextStyle.ITALIC)
8383

8484

85+
def LinkInlineBlocks(
86+
*, text: str, url: str, icon: Optional[Icon] = None
87+
) -> list[InlineBlock]:
88+
inlines: list[InlineBlock] = []
89+
if icon:
90+
inlines.append(IconBlock(icon=icon))
91+
inlines.append(LinkBlock(text=text, url=url))
92+
return inlines
93+
94+
8595
def LinkLineBlock(*, text: str, url: str) -> LineBlock:
86-
return LineBlock(inlines=[LinkBlock(text=text, url=url)])
96+
return LineBlock(inlines=LinkInlineBlocks(text=text, url=url))
97+
98+
99+
def LinksLineBlock(*, links: list[tuple[str, str, Optional[Icon]]]) -> LineBlock:
100+
return LineBlock(
101+
inlines=sum(
102+
[
103+
LinkInlineBlocks(text=text, url=url, icon=icon)
104+
for text, url, icon in links
105+
],
106+
[],
107+
)
108+
)
87109

88110

89111
def SummaryLineBlock(

elementary/monitor/alerts/alert_messages/builder.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
FactsBlock,
1010
ItalicTextLineBlock,
1111
JsonCodeBlock,
12-
LinkLineBlock,
12+
LinksLineBlock,
1313
MentionLineBlock,
1414
NonPrimaryFactBlock,
1515
PrimaryFactBlock,
@@ -104,7 +104,7 @@ def _get_run_alert_subtitle_block(
104104
detected_at_str: Optional[str] = None,
105105
suppression_interval: Optional[int] = None,
106106
env: Optional[str] = None,
107-
report_link: Optional[ReportLinkData] = None,
107+
links: list[ReportLinkData] = [],
108108
) -> LinesBlock:
109109
summary = []
110110
summary.append((type.capitalize() + ":", name))
@@ -117,9 +117,11 @@ def _get_run_alert_subtitle_block(
117117
summary.append(("Suppression interval:", str(suppression_interval)))
118118
subtitle_lines = [SummaryLineBlock(summary=summary)]
119119

120-
if report_link:
120+
if links:
121121
subtitle_lines.append(
122-
LinkLineBlock(text="View in Elementary", url=report_link.url)
122+
LinksLineBlock(
123+
links=[(link.text, link.url, link.icon) for link in links]
124+
)
123125
)
124126
return LinesBlock(lines=subtitle_lines)
125127

@@ -138,6 +140,7 @@ def _get_run_alert_subtitle_blocks(
138140
elif isinstance(alert, ModelAlertModel):
139141
asset_type = "snapshot" if alert.materialization == "snapshot" else "model"
140142
asset_name = alert.alias
143+
report_link = alert.get_report_link()
141144
return [
142145
self._get_run_alert_subtitle_block(
143146
type=asset_type,
@@ -146,7 +149,7 @@ def _get_run_alert_subtitle_blocks(
146149
detected_at_str=alert.detected_at_str,
147150
suppression_interval=alert.suppression_interval,
148151
env=alert.env,
149-
report_link=alert.get_report_link(),
152+
links=[report_link] if report_link else [],
150153
)
151154
]
152155

elementary/monitor/data_monitoring/alerts/integrations/utils/report_link.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from enum import Enum
22
from typing import Optional
33

4+
from elementary.messages.blocks import Icon
45
from elementary.utils.pydantic_shim import BaseModel
56

67
TEST_RUNS_LINK_TEXT = "View test runs"
@@ -10,6 +11,7 @@
1011
class ReportLinkData(BaseModel):
1112
url: str
1213
text: str
14+
icon: Optional[Icon] = None
1315

1416

1517
class ReportPath(Enum):
@@ -31,9 +33,11 @@ def _get_run_history_report_link(
3133
url = f"{formatted_report_url}/report/{path.value}/{unique_id}/"
3234
report_link = ReportLinkData(
3335
url=url,
34-
text=TEST_RUNS_LINK_TEXT
35-
if path == ReportPath.TEST_RUNS
36-
else MODEL_RUNS_LINK_TEXT,
36+
text=(
37+
TEST_RUNS_LINK_TEXT
38+
if path == ReportPath.TEST_RUNS
39+
else MODEL_RUNS_LINK_TEXT
40+
),
3741
)
3842

3943
return report_link
@@ -62,7 +66,7 @@ def get_model_test_runs_link(
6266

6367
if model_unique_id and report_url:
6468
formatted_report_url = _get_formatted_report_url(report_url)
65-
url = f'{formatted_report_url}/report/{ReportPath.TEST_RUNS.value}/?treeNode={{"id":"{model_unique_id}"}}'
69+
url = f'{formatted_report_url}/report/{ReportPath.TEST_RUNS.value}/?treeNode={{"id":"{model_unique_id}"}}' # noqa: E231
6670
report_link = ReportLinkData(url=url, text=TEST_RUNS_LINK_TEXT)
6771

6872
return report_link

0 commit comments

Comments
 (0)