Skip to content

Commit

Permalink
Converted get_invalid_count to a static method in log_summary.py
Browse files Browse the repository at this point in the history
  • Loading branch information
OPSergio committed Oct 31, 2024
1 parent f40aeef commit b3ce77a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
12 changes: 9 additions & 3 deletions relecov_tools/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@ def send_mail(validate_file, receiver_email, attachments, email_psswd):
if not validate_data:
raise ValueError(
"Error: Validation data could not be loaded.")

submitting_institution_code = list(validate_data.keys())[0]

invalid_count = relecov_tools.log_summary.LogSum.get_invalid_count(validate_data)

email_sender = relecov_tools.mail.EmailSender(config)

Expand All @@ -299,8 +303,10 @@ def send_mail(validate_file, receiver_email, attachments, email_psswd):
if add_info:
additional_info = click.prompt("Enter additional information")

email_body, email_receiver_from_json = email_sender.render_email_template(
additional_info
email_body, email_receiver_from_json, institution_name = email_sender.render_email_template(
additional_info,
invalid_count=invalid_count,
submitting_institution_code = submitting_institution_code
)

if email_body is None:
Expand All @@ -313,7 +319,7 @@ def send_mail(validate_file, receiver_email, attachments, email_psswd):
if not final_receiver_email:
raise ValueError("Error: Could not obtain the recipient's email address.")

subject = f"Informe de Validación de Muestras - {email_receiver_from_json}"
subject = f"Informe de Validación de Muestras - {institution_name}"

email_sender.send_email(final_receiver_email, subject, email_body, attachments)

Expand Down
23 changes: 23 additions & 0 deletions relecov_tools/log_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,26 @@ def create_error_summary(
log.error("Error exporting logs to file: %s", str(e))
f.write(str(final_logs))
return

@staticmethod
def get_invalid_count(validation_logs):
"""
Counts the number of invalid samples in the logs data by checking the `valid` field.
Args:
validation_logs (dict): Dictionary containing the validation logs.
Returns:
dict: Dictionary with entry_key as keys and counts of invalid samples as values.
"""
invalid_counts = {}
for entry_key, entry_value in validation_logs.items():
if "samples" in entry_value:
samples = entry_value["samples"]
for sample_key, sample_value in samples.items():
if "valid" in sample_value and not sample_value["valid"]:
if invalid_counts.get(entry_key):
invalid_counts[entry_key] += 1
else:
invalid_counts[entry_key] = 1
return invalid_counts
22 changes: 5 additions & 17 deletions relecov_tools/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,6 @@ def __init__(self, config):
raise FileNotFoundError(
f"The template file could not be found in path {self.template_path}.")

def get_invalid_count(self):
invalid_count = 0

for entry_key, entry_value in self.validate_data.items():
if "samples" in entry_value:
samples = entry_value["samples"]
for sample_key, sample_value in samples.items():
if "valid" in sample_value and not sample_value["valid"]:
invalid_count += 1
return invalid_count

def get_institution_info(
self, institution_code, institutions_file="institutions.json"
):
Expand All @@ -53,11 +42,10 @@ def get_institution_info(
print(f"No information found for code {institution_code}")
return None

def render_email_template(self, additional_info="", validate_data=None):

submitting_institution_code = list(self.validate_data.keys())[0]
invalid_count = self.get_invalid_count()

def render_email_template(self, additional_info="",
invalid_count= None,
submitting_institution_code=None):

institution_info = self.get_institution_info(submitting_institution_code)
if not institution_info:
print("Error: The information could not be obtained from the institution.")
Expand All @@ -75,7 +63,7 @@ def render_email_template(self, additional_info="", validate_data=None):
additional_info=additional_info,
)

return email_template, email_receiver
return email_template, email_receiver, institution_name

def send_email(self, receiver_email, subject, body, attachments):
credentials = relecov_tools.utils.read_yml_file(self.yaml_cred_path)
Expand Down

0 comments on commit b3ce77a

Please sign in to comment.