Skip to content

Notification Service

Costas Yiallourides edited this page Apr 15, 2025 · 3 revisions

Overview

The Notification Service module provides a flexible and robust way to send alert notifications. It supports one-off and periodic email notifications, complete with attachment handling, retry mechanisms, and a configurable scheduler for interval-based alerts.

Features

  • One-off Notifications: Send immediate, single notification alerts.
  • Periodic Notifications: Automatically send notifications at regular intervals using the built-in scheduler.
  • Attachment Support: Attach files (e.g., CSVs, logs, images) to notifications.
  • Retry Mechanisms: Handle failed attempts with configurable retries and backoff delays.
  • Customizable Configuration: Use dedicated dataclasses for flexible setup (e.g., EmailConfig and SchedulerConfig).
  • Ease of Use: Create notification instances with minimal setup and start sending alerts instantly.

Notification Utilities

The notification_utils.py module provides a set of utility functions to support testing, validating, and previewing email notifications. It is primarily intended for use in interactive tools like notebooks or Streamlit apps, where users need to configure or debug email-based alerts in real time.

Key functionalities include:

  • Sending Test Emails: Automatically generate and send a sample email using a given EmailConfig, helping verify SMTP configuration.
  • Validating Configurations: Check the completeness of an EmailConfig and optionally test SMTP connectivity or validate attachment paths.
  • Email Preview Generation: Wraps HTML email bodies with styling and attachment info for easy previewing in browsers or notebook cells.

These utilities are especially useful during development and debugging of notification features, ensuring smooth setup and quick feedback on email configurations.

Usage

1. Configuration

The EmailConfig and SchedulerConfig dataclasses are used to configure email notifications and scheduling.

Example:

from notification_service import EmailConfig, SchedulerConfig

# Email and Scheduler configuration
email_config_dict = {
    "subject": "Critical Alert",
    "message": "Inverter is in error mode",
    "recipients": ["[email protected]"],
    "smtp_server": "smtp.example.com",
    "smtp_port": 587,
    "smtp_user": "[email protected]",
    "smtp_password": "password",
    "from_email": "[email protected]",
    "attachments": ["alert_records.csv"],
    "scheduler": {
        "send_immediately": True,
        "interval": 60,
        "duration": 3600,
    },
}

2. Creating a Notification

Create an EmailNotification object using the configuration.

from notification_service import EmailNotification

# Create an email notification instance
email_notification = EmailNotification.from_dict(email_config)

3. Sending Notifications

One-off Notification: Send a single email notification immediately.

email_notification.send()

Periodic Notifications: Start sending notifications periodically.

# Start the scheduler
email_notification.start_scheduler()

# Stop the scheduler after some time
import time
time.sleep(300)  # Keep it running for 5 minutes
email_notification.stop_scheduler()

Considerations

  • SMTP Server Requirements: A valid SMTP server and credentials are required to send email notifications.
  • Attachments: Ensure attached files exist and are accessible when sending notifications.

Future Integrations

It is planned to extend the service to support additional channels with Slack being the first priority.

Clone this wiki locally