Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the duplicate email issue in enterprise reporting pipeline #563

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions enterprise_reporting/send_enterprise_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'],
Expand Down Expand Up @@ -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(
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion enterprise_reporting/tests/test_send_enterprise_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions enterprise_reporting/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 2 additions & 5 deletions enterprise_reporting/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""


import datetime
import logging
import os
import re
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
Loading