diff --git a/airflow/dags/mattermost_dags/general/_helpers.py b/airflow/dags/mattermost_dags/general/_helpers.py index bf583b5ea..699242f62 100644 --- a/airflow/dags/mattermost_dags/general/_helpers.py +++ b/airflow/dags/mattermost_dags/general/_helpers.py @@ -3,6 +3,7 @@ from datetime import datetime, timedelta from mattermost.operators.mattermost_operator import MattermostOperator +from tabulate import tabulate task_logger = logging.getLogger('airflow.task') @@ -99,13 +100,11 @@ def hightouch_check_syncs(response): status = fail or warning, fail """ syncs = json.loads(''.join(response)) - failed_syncs = {} + failed_syncs = [] try: if ('data' not in syncs) or len(syncs) == 0: raise HightouchApiException('Invalid response from syncs api') - failed_syncs = { - sync['slug']: sync['status'] for sync in syncs.get('data') if sync['status'] not in ('success', 'disabled') - } + failed_syncs = [sync for sync in syncs.get('data') if sync['status'] not in ('success', 'disabled')] except KeyError as e: task_logger.error('Error in check syncs ...', exc_info=True) raise e @@ -129,7 +128,16 @@ def resolve_hightouch(ti=None, **kwargs): task_logger.info('There are no failed syncs') else: status = ':red_circle:' - message = f"**HIGHTOUCH**: {status}\nFailed syncs: {failed_syncs}" + msg = tabulate( + [ + (f"[{sync['slug']}](https://app.hightouch.com/mattermost-com/syncs/{sync['id']})", sync['status']) + for sync in failed_syncs + ], + headers=['Sync', 'Status'], + tablefmt='github', + ) + + message = f"**HIGHTOUCH**: {status}\n\n{msg}" MattermostOperator(mattermost_conn_id='mattermost', text=message, task_id='resolve_hightouch_message').execute( None ) diff --git a/airflow/tests/general/test_monitoring.py b/airflow/tests/general/test_monitoring.py index af8f94e77..79392736c 100644 --- a/airflow/tests/general/test_monitoring.py +++ b/airflow/tests/general/test_monitoring.py @@ -55,14 +55,19 @@ def test_hightouch_check_syncs_pass(load_data): response = load_data('monitoring/syncs_pass.json') failed_syncs = hightouch_check_syncs(response) # Expected to return empty dict as no loads failed - assert failed_syncs == {} + assert failed_syncs == [] def test_hightouch_check_syncs_fail(load_data): response = load_data('monitoring/syncs_fail.json') failed_syncs = hightouch_check_syncs(response) # Expected to return dict containing failed loads - assert failed_syncs == {'test_sync_1': 'warning'} + assert failed_syncs == [{ + "id": 99999, + "slug": "test_sync_1", + "status": "warning", + "updatedAt": "2022-12-07T12:59:20Z" + }] def test_hightouch_check_syncs_error(load_data):