-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add slack message on TSV load complete (#369)
* Create a stubbed task to report load completion * Add logging for pull_data task duration * Add logging for record_count of rows upserted * Update task dependencies * Remove unnecessary xcom push and use default * Update message, remove unneeded logger config * Use existing provider_name variable * Update push_output_paths_wrapper test with expected number of xcoms * Send slack message * Fix broken formatting * Clean up tests * Italicize note in the slack message * Change name of slack override variable to be more descriptive * Put all slack messages behind the environment check * Update send message mock in tests * Better default when duration is none
- Loading branch information
Showing
8 changed files
with
165 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import logging | ||
|
||
from common.slack import send_message | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def report_completion(provider_name, media_type, duration, record_count): | ||
""" | ||
Send a Slack notification when the load_data task has completed. | ||
Messages are only sent out in production and if a Slack connection is defined. | ||
In all cases the data is logged. | ||
""" | ||
|
||
# This happens when the task is manually set to `success` in Airflow before | ||
# completing. | ||
duration = "_No data_" if duration == "None" else duration | ||
|
||
message = f""" | ||
*Provider*: `{provider_name}` | ||
*Media Type*: `{media_type}` | ||
*Number of Records Upserted*: {record_count} | ||
*Duration of data pull task*: {duration} | ||
* _Duration includes time taken to pull data of all media types._ | ||
""" | ||
send_message(message, username="Airflow DAG Load Data Complete") | ||
logger.info(message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from unittest import mock | ||
|
||
import pytest | ||
from common.loader.reporting import report_completion | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def send_message_mock() -> mock.MagicMock: | ||
with mock.patch("common.slack.SlackMessage.send") as SendMessageMock: | ||
yield SendMessageMock | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"should_send_message", | ||
[True, False], | ||
) | ||
def test_report_completion(should_send_message): | ||
with mock.patch( | ||
"common.slack.should_send_message", return_value=should_send_message | ||
): | ||
report_completion("Jamendo", "Audio", None, 100) | ||
# Send message is only called if `should_send_message` is True. | ||
send_message_mock.called = should_send_message |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters