Skip to content

Commit

Permalink
add slack channels to header data logs
Browse files Browse the repository at this point in the history
  • Loading branch information
eschutho committed Jul 26, 2024
1 parent 27dde2a commit d58780e
Show file tree
Hide file tree
Showing 3 changed files with 302 additions and 0 deletions.
10 changes: 10 additions & 0 deletions superset/commands/report/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,20 +367,30 @@ def _get_log_data(self) -> HeaderDataType:
chart_id = None
dashboard_id = None
report_source = None
slack_channels = None
if self._report_schedule.chart:
report_source = ReportSourceFormat.CHART
chart_id = self._report_schedule.chart_id
else:
report_source = ReportSourceFormat.DASHBOARD
dashboard_id = self._report_schedule.dashboard_id

if self._report_schedule.recipients:
slack_channels = [
recipient.recipient_config_json
for recipient in self._report_schedule.recipients
if recipient.type
in [ReportRecipientType.SLACK, ReportRecipientType.SLACKV2]
]

log_data: HeaderDataType = {
"notification_type": self._report_schedule.type,
"notification_source": report_source,
"notification_format": self._report_schedule.report_format,
"chart_id": chart_id,
"dashboard_id": dashboard_id,
"owners": self._report_schedule.owners,
"slack_channels": slack_channels,
}
return log_data

Expand Down
1 change: 1 addition & 0 deletions superset/utils/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ class HeaderDataType(TypedDict):
notification_source: str | None
chart_id: int | None
dashboard_id: int | None
slack_channels: list[str] | None


class DatasourceDict(TypedDict):
Expand Down
291 changes: 291 additions & 0 deletions tests/unit_tests/commands/report/execute_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,291 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

from superset.commands.report.execute import BaseReportState
from superset.reports.models import (
ReportRecipientType,
ReportSchedule,
ReportSourceFormat,
)
from superset.utils.core import HeaderDataType

# Returns correct log data when report schedule has a chart
# def test_log_data_with_chart( mocker):

# # Mocking the report schedule
# mock_report_schedule = mocker.Mock(spec=ReportSchedule)
# mock_report_schedule.chart = True
# mock_report_schedule.chart_id = 123
# mock_report_schedule.dashboard_id = None
# mock_report_schedule.type = "report_type"
# mock_report_schedule.report_format = "report_format"
# mock_report_schedule.owners = [1, 2]
# mock_report_schedule.recipients = []


# # Initializing the class and setting the report schedule
# class_instance = BaseReportState(mock_report_schedule, 'January 1, 2021', 'execution_id_example')
# class_instance._report_schedule = mock_report_schedule

# # Calling the method
# result = class_instance._get_log_data(),

# # Expected result
# expected_result: HeaderDataType = {
# "notification_type": "report_type",
# "notification_source": ReportSourceFormat.CHART,
# "notification_format": "report_format",
# "chart_id": 123,
# "dashboard_id": None,
# "owners": [1, 2],
# "slack_channels": None,
# }
# # Assertions
# assert result == expected_result
# mock_report_schedule.recipients = [
# mocker.Mock(type=ReportRecipientType.SLACK, recipient_config_json="channel_1"),
# mocker.Mock(type=ReportRecipientType.SLACKV2, recipient_config_json="channel_2")
# ]


# Returns correct log data when report schedule has no chart or dashboard
def test_log_data_with_chart(mocker):
# Mocking the report schedule
mock_report_schedule = mocker.Mock(spec=ReportSchedule)
mock_report_schedule.chart = True
mock_report_schedule.chart_id = 123
mock_report_schedule.dashboard_id = None
mock_report_schedule.type = "report_type"
mock_report_schedule.report_format = "report_format"
mock_report_schedule.owners = [1, 2]
mock_report_schedule.recipients = []

# Initializing the class and setting the report schedule
class_instance = BaseReportState(
mock_report_schedule, "January 1, 2021", "execution_id_example"
)
class_instance._report_schedule = mock_report_schedule

# Calling the method
result = class_instance._get_log_data()

# Expected result
expected_result: HeaderDataType = {
"notification_type": "report_type",
"notification_source": ReportSourceFormat.CHART,
"notification_format": "report_format",
"chart_id": 123,
"dashboard_id": None,
"owners": [1, 2],
"slack_channels": None,
}

# Assertions
assert result == expected_result


def test_log_data_with_dashboard(mocker):
# Mocking the report schedule
mock_report_schedule = mocker.Mock(spec=ReportSchedule)
mock_report_schedule.chart = False
mock_report_schedule.chart_id = None
mock_report_schedule.dashboard_id = 123
mock_report_schedule.type = "report_type"
mock_report_schedule.report_format = "report_format"
mock_report_schedule.owners = [1, 2]
mock_report_schedule.recipients = []

# Initializing the class and setting the report schedule
class_instance = BaseReportState(
mock_report_schedule, "January 1, 2021", "execution_id_example"
)
class_instance._report_schedule = mock_report_schedule

# Calling the method
result = class_instance._get_log_data()

# Expected result
expected_result: HeaderDataType = {
"notification_type": "report_type",
"notification_source": ReportSourceFormat.DASHBOARD,
"notification_format": "report_format",
"chart_id": None,
"dashboard_id": 123,
"owners": [1, 2],
"slack_channels": None,
}

# Assertions
assert result == expected_result


def test_log_data_with_email_recipients(mocker):
# Mocking the report schedule
mock_report_schedule = mocker.Mock(spec=ReportSchedule)
mock_report_schedule.chart = False
mock_report_schedule.chart_id = None
mock_report_schedule.dashboard_id = 123
mock_report_schedule.type = "report_type"
mock_report_schedule.report_format = "report_format"
mock_report_schedule.owners = [1, 2]
mock_report_schedule.recipients = []
mock_report_schedule.recipients = [
mocker.Mock(type=ReportRecipientType.EMAIL, recipient_config_json="email_1"),
mocker.Mock(type=ReportRecipientType.EMAIL, recipient_config_json="email_2"),
]

# Initializing the class and setting the report schedule
class_instance = BaseReportState(
mock_report_schedule, "January 1, 2021", "execution_id_example"
)
class_instance._report_schedule = mock_report_schedule

# Calling the method
result = class_instance._get_log_data()

# Expected result
expected_result: HeaderDataType = {
"notification_type": "report_type",
"notification_source": ReportSourceFormat.DASHBOARD,
"notification_format": "report_format",
"chart_id": None,
"dashboard_id": 123,
"owners": [1, 2],
"slack_channels": [],
}

# Assertions
assert result == expected_result


def test_log_data_with_slack_recipients(mocker):
# Mocking the report schedule
mock_report_schedule = mocker.Mock(spec=ReportSchedule)
mock_report_schedule.chart = False
mock_report_schedule.chart_id = None
mock_report_schedule.dashboard_id = 123
mock_report_schedule.type = "report_type"
mock_report_schedule.report_format = "report_format"
mock_report_schedule.owners = [1, 2]
mock_report_schedule.recipients = []
mock_report_schedule.recipients = [
mocker.Mock(type=ReportRecipientType.SLACK, recipient_config_json="channel_1"),
mocker.Mock(type=ReportRecipientType.SLACK, recipient_config_json="channel_2"),
]

# Initializing the class and setting the report schedule
class_instance = BaseReportState(
mock_report_schedule, "January 1, 2021", "execution_id_example"
)
class_instance._report_schedule = mock_report_schedule

# Calling the method
result = class_instance._get_log_data()

# Expected result
expected_result: HeaderDataType = {
"notification_type": "report_type",
"notification_source": ReportSourceFormat.DASHBOARD,
"notification_format": "report_format",
"chart_id": None,
"dashboard_id": 123,
"owners": [1, 2],
"slack_channels": ["channel_1", "channel_2"],
}

# Assertions
assert result == expected_result


# Returns correct log data when report schedule has no owners
def test_log_data_no_owners(mocker):
# Mocking the report schedule
mock_report_schedule = mocker.Mock(spec=ReportSchedule)
mock_report_schedule.chart = False
mock_report_schedule.chart_id = None
mock_report_schedule.dashboard_id = 123
mock_report_schedule.type = "report_type"
mock_report_schedule.report_format = "report_format"
mock_report_schedule.owners = []
mock_report_schedule.recipients = [
mocker.Mock(type=ReportRecipientType.SLACK, recipient_config_json="channel_1"),
mocker.Mock(type=ReportRecipientType.SLACK, recipient_config_json="channel_2"),
]

# Initializing the class and setting the report schedule
class_instance = BaseReportState(
mock_report_schedule, "January 1, 2021", "execution_id_example"
)
class_instance._report_schedule = mock_report_schedule

# Calling the method
result = class_instance._get_log_data()

# Expected result
expected_result: HeaderDataType = {
"notification_type": "report_type",
"notification_source": ReportSourceFormat.DASHBOARD,
"notification_format": "report_format",
"chart_id": None,
"dashboard_id": 123,
"owners": [],
"slack_channels": ["channel_1", "channel_2"],
}

# Assertions
assert result == expected_result


# Handles missing or null values in report schedule attributes gracefully
def test_log_data_with_missing_values(mocker):
# Mocking the report schedule
mock_report_schedule = mocker.Mock(spec=ReportSchedule)
mock_report_schedule.chart = None
mock_report_schedule.chart_id = None
mock_report_schedule.dashboard_id = None
mock_report_schedule.type = "report_type"
mock_report_schedule.report_format = "report_format"
mock_report_schedule.owners = [1, 2]
mock_report_schedule.recipients = [
mocker.Mock(type=ReportRecipientType.SLACK, recipient_config_json="channel_1"),
mocker.Mock(
type=ReportRecipientType.SLACKV2, recipient_config_json="channel_2"
),
]

# Initializing the class and setting the report schedule
class_instance = BaseReportState(
mock_report_schedule, "January 1, 2021", "execution_id_example"
)
class_instance._report_schedule = mock_report_schedule

# Calling the method
result = class_instance._get_log_data()

# Expected result
expected_result: HeaderDataType = {
"notification_type": "report_type",
"notification_source": ReportSourceFormat.DASHBOARD,
"notification_format": "report_format",
"chart_id": None,
"dashboard_id": None,
"owners": [1, 2],
"slack_channels": ["channel_1", "channel_2"],
}

# Assertions
assert result == expected_result

0 comments on commit d58780e

Please sign in to comment.