"""Logic layer for bulk ingestion slack messages.""" from typing import Any import flask from notifications.config import ENVIRONMENT from notifications.connectors import slack from notifications.connectors.redis import client as redis_client def ts_redis_key(execution_arn: str) -> str: """Create redis key from execution arn.""" return f'bulk_ingestion_slack_ts:{execution_arn}' def bulk_ingest_started(payload: dict[str, Any]) -> None: """Send a Slack notification indicating the start of ingestion.""" execution_arn = payload.get('execution_arn', None) assert execution_arn # noqa: S101 ts = slack.send_bulk_message( body=flask.render_template( 'slack/bulk_ingestion/ingestion_start.jinja', **payload, env=ENVIRONMENT ), color='#d5d5d5', ) redis_client.set(ts_redis_key(execution_arn), ts, ex=2592000) # TTL: 1 month (30 days) def bulk_ingest_succeeded(payload: dict[str, Any]) -> None: """Send a Slack notification indicating the successful completion of ingestion.""" execution_arn = payload.get('execution_arn', None) assert execution_arn # noqa: S101 slack.send_bulk_message( body=flask.render_template( 'slack/bulk_ingestion/ingestion_success.jinja', **payload, env=ENVIRONMENT ), color='#2aad73', ) redis_client.delete(ts_redis_key(execution_arn)) def bulk_ingest_failed(payload: dict[str, Any]) -> None: """Send a Slack notification indicating the failure of ingestion.""" execution_arn = payload.get('execution_arn', None) assert execution_arn # noqa: S101 slack.send_bulk_message( body=flask.render_template( 'slack/bulk_ingestion/ingestion_failure.jinja', **payload, env=ENVIRONMENT ), color='#900003', ) redis_client.delete(ts_redis_key(execution_arn)) def bulk_ingest_product_failed(payload: dict[str, Any]) -> None: """Send a Slack notification indicating the failure of a product ingestion.""" execution_arn = payload.get('execution_arn', None) assert execution_arn # noqa: S101 ts = redis_client.get(ts_redis_key(execution_arn)) assert ts # noqa: S101 slack.send_bulk_message( body=flask.render_template( 'slack/bulk_ingestion/ingestion_product_failure.jinja', **payload, env=ENVIRONMENT ), color='#900003', thread_ts=str(ts), )