From dd26af0e2065477602155163492e062178c72cc8 Mon Sep 17 00:00:00 2001 From: burnout87 Date: Tue, 10 Oct 2023 09:42:57 +0200 Subject: [PATCH 1/3] dedicated loger for email_helper.py to use in case no logger is provided to function --- cdci_data_analysis/analysis/email_helper.py | 26 ++++++++++----------- cdci_data_analysis/analysis/time_helper.py | 4 ++-- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/cdci_data_analysis/analysis/email_helper.py b/cdci_data_analysis/analysis/email_helper.py index 2e1b755e2..395c7f151 100644 --- a/cdci_data_analysis/analysis/email_helper.py +++ b/cdci_data_analysis/analysis/email_helper.py @@ -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 @@ -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 @@ -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 @@ -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}, @@ -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, ' @@ -397,7 +397,7 @@ def send_job_email( attachment=api_code_email_attachment, sentry_dsn=sentry_dsn) - 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 @@ -502,7 +502,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): diff --git a/cdci_data_analysis/analysis/time_helper.py b/cdci_data_analysis/analysis/time_helper.py index bf32911f1..75719a906 100644 --- a/cdci_data_analysis/analysis/time_helper.py +++ b/cdci_data_analysis/analysis/time_helper.py @@ -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 From 55d34b0d08216520c6d3fd0e240baeeb5241c046 Mon Sep 17 00:00:00 2001 From: burnout87 Date: Tue, 10 Oct 2023 09:47:36 +0200 Subject: [PATCH 2/3] no need for sentry_dsn --- cdci_data_analysis/analysis/email_helper.py | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/cdci_data_analysis/analysis/email_helper.py b/cdci_data_analysis/analysis/email_helper.py index 395c7f151..3e0c3edea 100644 --- a/cdci_data_analysis/analysis/email_helper.py +++ b/cdci_data_analysis/analysis/email_helper.py @@ -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() @@ -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) @@ -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; @@ -394,8 +391,7 @@ 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, logger, sending_time=sending_time, first_submitted_time=time_request) @@ -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 @@ -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() @@ -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() From 26fdb7495c25c71ca01514de34efbb087573722b Mon Sep 17 00:00:00 2001 From: burnout87 Date: Tue, 10 Oct 2023 10:00:43 +0200 Subject: [PATCH 3/3] send_incident_report_email args --- cdci_data_analysis/flask_app/app.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/cdci_data_analysis/flask_app/app.py b/cdci_data_analysis/flask_app/app.py index 983dcf2e2..f683e07c0 100644 --- a/cdci_data_analysis/flask_app/app.py +++ b/cdci_data_analysis/flask_app/app.py @@ -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: @@ -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: