Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from datetime import datetime
from typing import Dict, List, Optional

from elementary.clients.slack.schema import SlackMessageSchema
from elementary.clients.slack.slack_message_builder import SlackMessageBuilder
from elementary.monitor.api.tests.schema import TestResultSummarySchema
from elementary.monitor.data_monitoring.schema import SelectorFilterSchema
from elementary.utils.time import convert_utc_time_to_timezone

TAG_PREFIX = "#"


class SlackReportSummaryMessageBuilder(SlackMessageBuilder):
Expand Down Expand Up @@ -44,9 +44,6 @@ def _add_title_to_slack_alert(
bucket_website_url: Optional[str] = None,
filter: SelectorFilterSchema = SelectorFilterSchema(),
):
current_time = convert_utc_time_to_timezone(datetime.utcnow()).strftime(
"%Y-%m-%d | %H:%M"
)
env_text = (
":construction: Development"
if env == "dev"
Expand All @@ -56,9 +53,7 @@ def _add_title_to_slack_alert(
totals = self._get_test_results_totals(test_results)

title_blocks = [
self.create_header_block(
f":mag: Monitoring summary ({current_time} | {env_text})"
),
self.create_header_block(f":mag: Monitoring summary ({env_text})"),
self.create_text_section_block(summary_filter_text),
]

Expand All @@ -73,7 +68,6 @@ def _add_title_to_slack_alert(
self.create_fields_section_block(
[
f":white_check_mark: Passed: {totals.get('passed', 0)}",
f":wrench: Schema changes: {totals.get('schema_changes', 0)}",
f":small_red_triangle: Failed: {totals.get('failed', 0)}",
f":exclamation: Errors: {totals.get('error', 0)}",
f":Warning: Warning: {totals.get('warning', 0)}",
Expand Down Expand Up @@ -108,8 +102,12 @@ def _add_preview_to_slack_alert(self, test_results: List[TestResultSummarySchema
subscribers = []
for test in test_results:
if test.status != "pass":
formatted_tags = [
tag if tag.startswith(TAG_PREFIX) else f"{TAG_PREFIX}{tag}"
for tag in test.tags
]
owners.extend(test.owners)
tags.extend(test.tags)
tags.extend(formatted_tags)
subscribers.extend(test.subscribers)

tags_text = self.prettify_and_dedup_list(tags) if tags else "_No tags_"
Expand Down Expand Up @@ -139,18 +137,19 @@ def _add_details_to_slack_alert(
error_tests_details = []
failed_tests_details = []
warning_tests_details = []
schema_changes_tests_details = []
for test in test_results:
if test.test_type == "schema_change" and test.status != "pass":
schema_changes_tests_details.extend(
if test.status == "error":
error_tests_details.extend(
self._get_test_result_details_block(test, include_description)
)
elif test.status == "error":
error_tests_details.extend(self._get_test_result_details_block(test))
elif test.status == "fail":
failed_tests_details.extend(self._get_test_result_details_block(test))
failed_tests_details.extend(
self._get_test_result_details_block(test, include_description)
)
else:
warning_tests_details.extend(self._get_test_result_details_block(test))
warning_tests_details.extend(
self._get_test_result_details_block(test, include_description)
)

details_blocks = []
if failed_tests_details:
Expand All @@ -165,13 +164,6 @@ def _add_details_to_slack_alert(
details_blocks.append(self.create_divider_block())
details_blocks.extend(warning_tests_details)

if schema_changes_tests_details:
details_blocks.append(
self.create_text_section_block(":wrench: *Schema changes*")
)
details_blocks.append(self.create_divider_block())
details_blocks.extend(schema_changes_tests_details)

if error_tests_details:
details_blocks.append(
self.create_text_section_block(":exclamation: *Error*")
Expand Down Expand Up @@ -217,11 +209,9 @@ def _get_test_result_details_block(
def _get_test_results_totals(
test_results: List[TestResultSummarySchema],
) -> Dict[str, int]:
totals = dict(passed=0, failed=0, error=0, warning=0, schema_changes=0)
totals = dict(passed=0, failed=0, error=0, warning=0)
for test in test_results:
if test.test_type == "schema_change" and test.status != "pass":
totals["schema_changes"] += 1
elif test.status == "pass":
if test.status == "pass":
totals["passed"] += 1
elif test.status == "error":
totals["error"] += 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
def test_get_test_results_totals(test_results_summary):
message_builder = SlackReportSummaryMessageBuilder()
totals = message_builder._get_test_results_totals(test_results_summary)
assert totals.get("schema_changes") == 2
assert totals.get("passed") == 4
assert totals.get("error") == 1
assert totals.get("failed") == 2
assert totals.get("warning") == 1
assert totals.get("failed") == 3
assert totals.get("warning") == 2


def test_add_details_to_slack_alert_attachments_limit(test_results_summary):
Expand All @@ -28,22 +27,20 @@ def test_add_details_to_slack_alert_attachments_limit(test_results_summary):
)
assert ":small_red_triangle: *Failed tests*" in attachments_as_string
assert ":warning: *Warning*" in attachments_as_string
assert ":wrench: *Schema changes*" in attachments_as_string
assert ":exclamation: *Error*" in attachments_as_string

message_builder = SlackReportSummaryMessageBuilder()
message_builder._add_details_to_slack_alert((test_results_summary * 5)[0:37])
message_builder._add_details_to_slack_alert((test_results_summary * 5)[0:39])
attachments_as_string = json.dumps(
message_builder.slack_message.get("attachments")[0].get("blocks")
)
assert ":small_red_triangle: *Failed tests*" in attachments_as_string
assert ":warning: *Warning*" in attachments_as_string
assert ":wrench: *Schema changes*" in attachments_as_string
assert ":exclamation: *Error*" in attachments_as_string

# Over attachments limitation
message_builder = SlackReportSummaryMessageBuilder()
message_builder._add_details_to_slack_alert((test_results_summary * 5)[0:38])
message_builder._add_details_to_slack_alert((test_results_summary * 5)[0:40])
Comment thread
RoiTabach marked this conversation as resolved.
attachments_as_string = json.dumps(
message_builder.slack_message.get("attachments")[0].get("blocks")
)
Expand Down