Skip to content

Commit

Permalink
Merge pull request #596 from oda-hub/proper-email-logger
Browse files Browse the repository at this point in the history
Proper email logger
  • Loading branch information
burnout87 authored Oct 11, 2023
2 parents 1c2bbf6 + 53b02ed commit 4f1868b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 31 deletions.
45 changes: 20 additions & 25 deletions cdci_data_analysis/analysis/email_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
from jinja2 import Environment, FileSystemLoader
from bs4 import BeautifulSoup


from ..app_logging import app_logging
from ..analysis.exceptions import BadRequest, MissingRequestParameter
from ..analysis.hash import make_hash
from ..analysis.time_helper import validate_time

from datetime import datetime

logger = logging.getLogger()
email_helper_logger = app_logging.getLogger('email_helper')

num_email_sending_max_tries = 5
email_sending_retry_sleep_s = .5
Expand All @@ -48,7 +48,7 @@ def timestamp2isot(timestamp_or_string: typing.Union[str, float]):
try:
timestamp_or_string = validate_time(timestamp_or_string).strftime("%Y-%m-%d %H:%M:%S")
except (ValueError, OverflowError, TypeError, OSError) as e:
logger.warning(f'Error when constructing the datetime object from the timestamp {timestamp_or_string}:\n{e}')
email_helper_logger.warning(f'Error when constructing the datetime object from the timestamp {timestamp_or_string}:\n{e}')
raise EMailNotSent(f"Email not sent: {e}")

return timestamp_or_string
Expand Down Expand Up @@ -101,8 +101,8 @@ def compress_request_url_params(request_url, consider_args=['selected_catalog',

if len(v_json) > 100:
v = "z:" + base64.b64encode(zlib.compress(v_json.encode())).decode()
logger.info("compressing long %.50s...", v_json)
logger.info("compressed into %.500s...", v)
email_helper_logger.info("compressing long %.50s...", v_json)
email_helper_logger.info("compressed into %.500s...", v)

compressed_qs[k] = v

Expand Down Expand Up @@ -154,7 +154,7 @@ def wrap_python_code(code, max_length=100, max_str_length=None):
else:
code = new_code

logger.debug("\033[31mwrapped: %s\033[0m", code)
email_helper_logger.debug("\033[31mwrapped: %s\033[0m", code)

mode = black.Mode(
target_versions={black.TargetVersion.PY38},
Expand Down Expand Up @@ -195,15 +195,15 @@ def get_first_submitted_email_time(scratch_dir):
validate_time(f_name_split[3])
first_submitted_email_time = float(f_name_split[3])
except (ValueError, OverflowError, TypeError, OSError) as e:
logger.warning(f'Error when extracting the time of the first submitted email.'
f'The value extracted {first_submitted_email_time} raised the following error:\n{e}')
email_helper_logger.warning(f'Error when extracting the time of the first submitted email.'
f'The value extracted {first_submitted_email_time} raised the following error:\n{e}')
first_submitted_email_time = None
sentry.capture_message(f'Error when extracting the time of the first submitted email.'
f'The value extracted {first_submitted_email_time} raised the following error:\n{e}')
else:
logger.warning(f'Error when extracting the time of the first submitted email: '
f'the name of the email file has been found not properly formatted, therefore, '
f'the time of the first submitted email could not be extracted.')
email_helper_logger.warning(f'Error when extracting the time of the first submitted email: '
f'the name of the email file has been found not properly formatted, therefore, '
f'the time of the first submitted email could not be extracted.')
first_submitted_email_time = None
sentry.capture_message(f'Error when extracting the time of the first submitted email: '
f'the name of the email file has been found not properly formatted, therefore, '
Expand All @@ -220,8 +220,7 @@ def send_incident_report_email(
decoded_token,
incident_content=None,
incident_time=None,
scratch_dir=None,
sentry_dsn=None):
scratch_dir=None):

sending_time = time_.time()

Expand Down Expand Up @@ -264,8 +263,7 @@ def send_incident_report_email(
scratch_dir=scratch_dir,
smtp_server_password=config.smtp_server_password,
sending_time=sending_time,
logger=logger,
sentry_dsn=sentry_dsn)
logger=logger)

store_incident_report_email_info(message, scratch_dir, sending_time=sending_time)

Expand All @@ -286,8 +284,7 @@ def send_job_email(
time_request=None,
request_url="",
api_code="",
scratch_dir=None,
sentry_dsn=None):
scratch_dir=None):
sending_time = time_.time()

# let's get the needed email template;
Expand Down Expand Up @@ -394,10 +391,9 @@ def send_job_email(
sending_time=sending_time,
scratch_dir=scratch_dir,
logger=logger,
attachment=api_code_email_attachment,
sentry_dsn=sentry_dsn)
attachment=api_code_email_attachment)

store_status_email_info(message, status, scratch_dir, sending_time=sending_time, first_submitted_time=time_request)
store_status_email_info(message, status, scratch_dir, logger, sending_time=sending_time, first_submitted_time=time_request)

return message

Expand All @@ -416,8 +412,7 @@ def send_email(smtp_server,
logger,
sending_time=None,
scratch_dir=None,
attachment=None,
sentry_dsn=None
attachment=None
):

server = None
Expand Down Expand Up @@ -502,7 +497,7 @@ def send_email(smtp_server,
server.quit()


def store_status_email_info(message, status, scratch_dir, sending_time=None, first_submitted_time=None):
def store_status_email_info(message, status, scratch_dir, logger, sending_time=None, first_submitted_time=None):
path_email_history_folder = os.path.join(scratch_dir, 'email_history')
current_time = time_.time()
if not os.path.exists(path_email_history_folder):
Expand Down Expand Up @@ -605,7 +600,7 @@ def log_email_sending_info(logger, status, time_request, scratch_dir, job_id, ad
logger.info(f"logging email sending attempt into {email_history_log_fn} file")


def is_email_to_send_run_query(logger, status, time_original_request, scratch_dir, job_id, config, decoded_token=None, sentry_dsn=None):
def is_email_to_send_run_query(logger, status, time_original_request, scratch_dir, job_id, config, decoded_token=None):
log_additional_info_obj = {}
sending_ok = False
time_check = time_.time()
Expand Down Expand Up @@ -692,7 +687,7 @@ def is_email_to_send_run_query(logger, status, time_original_request, scratch_di
return sending_ok


def is_email_to_send_callback(logger, status, time_original_request, scratch_dir, config, job_id, decoded_token=None, sentry_dsn=None):
def is_email_to_send_callback(logger, status, time_original_request, scratch_dir, config, job_id, decoded_token=None):
log_additional_info_obj = {}
sending_ok = False
time_check = time_.time()
Expand Down
4 changes: 2 additions & 2 deletions cdci_data_analysis/analysis/time_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

from ..app_logging import app_logging

logger = app_logging.getLogger('drupal_helper')
time_helper_logger = app_logging.getLogger('time_helper')

def validate_time(timestamp_to_validate):
try:
datetime_obj = datetime.fromtimestamp(float(timestamp_to_validate))
except (ValueError, OverflowError, TypeError, OSError) as e:
logger.warning(f'Error when constructing the datetime object from the timestamp {timestamp_to_validate}:\n{e}')
time_helper_logger.warning(f'Error when constructing the datetime object from the timestamp {timestamp_to_validate}:\n{e}')
raise
return datetime_obj

Expand Down
5 changes: 1 addition & 4 deletions cdci_data_analysis/flask_app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1007,8 +1007,6 @@ def report_incident():
app_config = app.config.get('conf')
secret_key = app_config.secret_key

sentry_dsn = sentry.sentry_url

output, output_code = tokenHelper.validate_token_from_request(token=token, secret_key=secret_key)

if output_code is not None:
Expand All @@ -1030,8 +1028,7 @@ def report_incident():
decoded_token=decoded_token,
incident_content=incident_content,
incident_time=incident_time,
scratch_dir=scratch_dir,
sentry_dsn=sentry_dsn
scratch_dir=scratch_dir
)
report_incident_status = 'incident report email successfully sent'
except email_helper.EMailNotSent as e:
Expand Down

0 comments on commit 4f1868b

Please sign in to comment.