"""Run bug notifications scripts.""" import argparse from datetime import date, timedelta from dotenv import load_dotenv load_dotenv() from src.logic import ( # noqa: E402 encoding_order_delay, encoding_order_discrepancies, ) from src.utils import slack # noqa: E402 def output_message(is_success, message, success_text, error_title, do_print, do_slack): if is_success: if do_print: print(success_text) if do_slack: slack.output_success(success_text) else: if do_print: print(error_title) print(message) if do_slack: slack.output_error(error_title, message) if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("--days", type=int, default=1, help="Number of days to look back (from today).") parser.add_argument( "--print", type=bool, default=True, help="Print output to console.", action=argparse.BooleanOptionalAction ) parser.add_argument( "--slack", type=bool, default=False, help="Send output to Slack. Requires SLACK_BOT_USER_OAUTH_TOKEN and SLACK_CHANNEL_ID to be set.", action=argparse.BooleanOptionalAction, ) args = parser.parse_args() if args.slack: if not slack.config.SLACK_BOT_USER_OAUTH_TOKEN: parser.error( "SLACK_BOT_USER_OAUTH_TOKEN is not set in environment variable or retrievable from Secrets Manager." ) if not slack.config.SLACK_CHANNEL_ID: parser.error("SLACK_CHANNEL_ID is not set in environment variables.") end_date = date.today() start_date = end_date - timedelta(args.days) date_difference = (end_date - start_date).days is_success, message = encoding_order_discrepancies.run(start_date, end_date) output_message( is_success, message, f"There is no incomplete encoding order found in last {date_difference} day(s).", "Incomplete encoding order(s)", args.print, args.slack, ) is_success, message = encoding_order_delay.run("physical", start_date, end_date) output_message( is_success, message, f"There is no missing encoding order found for physical products in last {date_difference} day(s).", "Missing encoding order(s) for physical products", args.print, args.slack, ) is_success, message = encoding_order_delay.run("digital", start_date, end_date) output_message( is_success, message, f"There is no missing encoding order found for digital products in last {date_difference} day(s).", "Missing encoding order(s) for digital products", args.print, args.slack, )