SIENTIAPDE-1172
refactor: streamline email generation and enhance notification handling - Updated email HTML structure to improve clarity and presentation of notifications. - Enhanced notification generator to better manage execution counts and output formatting. - Refactored Email class to improve error handling and logging during email sending. - Adjusted EmailBuilder to efficiently process error, warning, and info models. - Improved test coverage for email and notification functionalities to ensure reliability.
This commit is contained in:
@@ -95,6 +95,8 @@ class Email(BaseActivity):
|
||||
self.logger.error(
|
||||
f"Failed to attach content of {att_name}: {e}")
|
||||
|
||||
raise e
|
||||
|
||||
return msg
|
||||
|
||||
@activity.defn(name="send_email")
|
||||
@@ -113,29 +115,29 @@ class Email(BaseActivity):
|
||||
metadata=metadata)
|
||||
|
||||
for group_name, group_config in receiver_groups.items():
|
||||
|
||||
receivers = ", ".join(group_config['members'])
|
||||
|
||||
self.info(f"Sending email to {group_name}: {receivers}",
|
||||
metadata=metadata)
|
||||
|
||||
msg = MIMEMultipart()
|
||||
msg.attach(MIMEText(group_config['html'], 'html'))
|
||||
msg['From'] = self.sender_email
|
||||
msg['To'] = receivers
|
||||
msg['Subject'] = f"SIENTIA™ {mail_type}"
|
||||
|
||||
msg = self.handle_attachments(
|
||||
[
|
||||
{
|
||||
"filename": f"{notification['trigger']}_{notification['notification_id']}.txt",
|
||||
"content": notification['attachment_content']
|
||||
}
|
||||
for notification in group_config['notifications']
|
||||
if notification['attachment_content']],
|
||||
msg)
|
||||
|
||||
try:
|
||||
|
||||
receivers = ", ".join(group_config['members'])
|
||||
|
||||
self.info(f"Sending email to {group_name}: {receivers}",
|
||||
metadata=metadata)
|
||||
|
||||
msg = MIMEMultipart()
|
||||
msg.attach(MIMEText(group_config['html'], 'html'))
|
||||
msg['From'] = self.sender_email
|
||||
msg['To'] = receivers
|
||||
msg['Subject'] = f"SIENTIA™ {mail_type}"
|
||||
|
||||
msg = self.handle_attachments(
|
||||
[
|
||||
{
|
||||
"filename": f"{notification['trigger']}_{notification['notification_id']}.txt",
|
||||
"attachment_content": notification['attachment_content']
|
||||
}
|
||||
for notification in group_config['notifications']
|
||||
if notification.get('attachment_content') is not None],
|
||||
msg)
|
||||
|
||||
self.server.sendmail(
|
||||
self.sender_email, receivers, msg.as_string())
|
||||
except Exception as e:
|
||||
@@ -146,7 +148,7 @@ class Email(BaseActivity):
|
||||
else:
|
||||
group_config['status'] = 'sent'
|
||||
|
||||
self.info(f"Email sent to {group_name}.",
|
||||
self.info(f"Email sent to {group_name}: {receivers}",
|
||||
metadata=metadata)
|
||||
|
||||
self.info(f"Email sent for {mail_type} mail type.",
|
||||
|
||||
@@ -335,3 +335,5 @@ class SlotManager(Redis):
|
||||
if status == 'sent':
|
||||
key = f"{row['schedule']}:{row['notification_id']}"
|
||||
self.set(key, now, ttl=sent_ttl)
|
||||
|
||||
self.info("Notification cache stored...", metadata=metadata)
|
||||
|
||||
@@ -31,11 +31,14 @@ class EmailBuilder:
|
||||
return {
|
||||
'mail_type': mail_type,
|
||||
'error_events': self.replace_parameters(self.general_template,
|
||||
error_models) if error_models else '',
|
||||
general_events.get(
|
||||
'ERROR')) if error_models else '',
|
||||
'warning_events': self.replace_parameters(self.general_template,
|
||||
warning_models) if warning_models else '',
|
||||
general_events.get(
|
||||
'WARNING')) if warning_models else '',
|
||||
'info_events': self.replace_parameters(self.general_template,
|
||||
info_models) if info_models else '',
|
||||
general_events.get(
|
||||
'INFO')) if info_models else '',
|
||||
}
|
||||
|
||||
def build_email(self, report_data: list[dict], mail_type: str) -> str:
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Notification ID</th>
|
||||
<th>Schedule</th>
|
||||
<th>Block</th>
|
||||
<th>Timestamp</th>
|
||||
<th>Message</th>
|
||||
@@ -14,6 +15,7 @@
|
||||
{% for event in model.events %}
|
||||
<tr>
|
||||
<td>{{ event.notification_id }}</td>
|
||||
<td>{{ event.trigger }}</td>
|
||||
<td>{{ event.block }}</td>
|
||||
<td>{{ event.timestamp }}</td>
|
||||
<td>{{ event.message }}</td>
|
||||
|
||||
@@ -67,14 +67,6 @@ class LoadNotificationPackage:
|
||||
start_to_close_timeout=timedelta(seconds=60),
|
||||
retry_policy=retry_policy
|
||||
)
|
||||
|
||||
if not notification_package:
|
||||
return {
|
||||
'last_timestamp': last_timestamp,
|
||||
'notification_package': [],
|
||||
'sending_configs': await sending_configs_handler
|
||||
}
|
||||
|
||||
sending_configs = await sending_configs_handler
|
||||
|
||||
if not sending_configs or not notification_package:
|
||||
@@ -103,5 +95,5 @@ class LoadNotificationPackage:
|
||||
return {
|
||||
'last_timestamp': last_timestamp,
|
||||
'notification_package': notification_package,
|
||||
'sending_configs': await sending_configs_handler
|
||||
'sending_configs': sending_configs
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user