From f77f908704a9549b21e27c34fe12954ff4c4889f Mon Sep 17 00:00:00 2001 From: omar-sarfraz Date: Tue, 11 Feb 2025 18:10:33 +0500 Subject: [PATCH] refactor: moved current est time to global location --- enterprise_reporting/send_enterprise_reports.py | 16 +++++++++++++--- .../tests/test_send_enterprise_reports.py | 2 +- enterprise_reporting/tests/test_utils.py | 1 + enterprise_reporting/utils.py | 7 ++----- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/enterprise_reporting/send_enterprise_reports.py b/enterprise_reporting/send_enterprise_reports.py index 9c61a435..12cd0d6d 100644 --- a/enterprise_reporting/send_enterprise_reports.py +++ b/enterprise_reporting/send_enterprise_reports.py @@ -4,13 +4,15 @@ """ - import argparse +import datetime import logging import os import re import sys +import pytz + from enterprise_reporting.clients.enterprise import EnterpriseAPIClient from enterprise_reporting.reporter import EnterpriseReportSender from enterprise_reporting.utils import is_current_time_in_schedule @@ -55,11 +57,13 @@ def cleanup_files(enterprise_id): os.remove(os.path.join(directory, f)) -def should_deliver_report(args, reporting_config): +def should_deliver_report(args, reporting_config, current_est_time): """Given CLI arguments and the reporting configuration, determine if delivery should happen.""" valid_data_type = reporting_config['data_type'] in (args.data_type or DATA_TYPES) enterprise_customer_specified = bool(args.enterprise_customer) + meets_schedule_requirement = is_current_time_in_schedule( + current_est_time, reporting_config['frequency'], reporting_config['hour_of_day'], reporting_config['day_of_month'], @@ -101,6 +105,12 @@ def process_reports(): LOGGER.error(f'The enterprise {args.enterprise_customer} does not have a reporting configuration.') sys.exit(1) + # We are defining the current est time globally because we want the current time for a job + # to remain same thoughout the job. This ensures that a single report is not processed multiple times. + # See this comment for more details: https://2u-internal.atlassian.net/browse/ENT-9954?focusedCommentId=5356815 + est_timezone = pytz.timezone('US/Eastern') + current_est_time = datetime.datetime.now(est_timezone) + error_raised = False for reporting_config in reporting_configs['results']: LOGGER.info('Checking if {}\'s reporting config for {} data in {} format is ready for processing'.format( @@ -109,7 +119,7 @@ def process_reports(): reporting_config['report_type'], )) - if should_deliver_report(args, reporting_config): + if should_deliver_report(args, reporting_config, current_est_time): if send_data(reporting_config): error_raised = True else: diff --git a/enterprise_reporting/tests/test_send_enterprise_reports.py b/enterprise_reporting/tests/test_send_enterprise_reports.py index 21839bc5..ba9683a1 100644 --- a/enterprise_reporting/tests/test_send_enterprise_reports.py +++ b/enterprise_reporting/tests/test_send_enterprise_reports.py @@ -33,4 +33,4 @@ def test_should_deliver_report(self): Command = namedtuple("Command", "data_type enterprise_customer") args = Command('', '') - assert should_deliver_report(args, reporting_config) + assert should_deliver_report(args, reporting_config, current_est_time) diff --git a/enterprise_reporting/tests/test_utils.py b/enterprise_reporting/tests/test_utils.py index 06b17724..275f1d53 100644 --- a/enterprise_reporting/tests/test_utils.py +++ b/enterprise_reporting/tests/test_utils.py @@ -165,6 +165,7 @@ def test_is_current_time_in_schedule(self): est_timezone = pytz.timezone('US/Eastern') current_est_time = datetime.datetime.now(est_timezone) assert utils.is_current_time_in_schedule( + current_est_time, utils.FREQUENCY_TYPE_DAILY, current_est_time.hour, current_est_time.day, diff --git a/enterprise_reporting/utils.py b/enterprise_reporting/utils.py index 718cd750..917a94ba 100644 --- a/enterprise_reporting/utils.py +++ b/enterprise_reporting/utils.py @@ -3,7 +3,6 @@ """ -import datetime import logging import os import re @@ -16,7 +15,6 @@ import boto3 import pgpy import pyminizip -import pytz from cryptography.fernet import Fernet from fernet_fields.hkdf import derive_fernet_key @@ -149,12 +147,11 @@ def send_email_with_attachment(subject, body, from_email, to_email, attachment_d LOGGER.debug(result) -def is_current_time_in_schedule(frequency, hour_of_day, day_of_month=None, day_of_week=None): +def is_current_time_in_schedule(current_est_time, frequency, hour_of_day, day_of_month=None, day_of_week=None): """ Determine if the current time is in the range specified by this configuration's schedule. """ - est_timezone = pytz.timezone('US/Eastern') - current_est_time = datetime.datetime.now(est_timezone) + current_hour_of_day = current_est_time.hour current_day_of_week = current_est_time.weekday() current_day_of_month = current_est_time.day