|
1 | 1 | # License: MIT
|
2 | 2 | # Copyright © 2025 Frequenz Energy-as-a-Service GmbH
|
3 | 3 |
|
4 |
| -"""This module provides functionality for generating email alert notifications.""" |
| 4 | +""" |
| 5 | +This module provides functionality for generating email alert notifications. |
| 6 | +
|
| 7 | +It includes functions for formatting and structuring alert-related emails, |
| 8 | +such as: |
| 9 | + - Generating a summary of alerts per microgrid (optionally grouped by |
| 10 | + component ID). |
| 11 | + - Creating an HTML table representation of alert details. |
| 12 | + - Constructing a complete alert email with formatted content. |
| 13 | + - Sorting alerts by severity (optional) and applying color-coded styling. |
| 14 | + - Generating structured JSON output for alerts. |
| 15 | + - Filtering groups with no errors or warnings (optional, enabled by default). |
| 16 | +
|
| 17 | +### Example Usage: |
| 18 | +```python |
| 19 | +import pandas as pd |
| 20 | +from frequenz.lib.notebooks.alerts.alert_email import generate_alert_email |
| 21 | +
|
| 22 | +# Example alert records dataframe |
| 23 | +alert_records = pd.DataFrame( |
| 24 | + [ |
| 25 | + { |
| 26 | + "microgrid_id": 1, |
| 27 | + "component_id": 1, |
| 28 | + "state_type": "error", |
| 29 | + "state_value": "UNDERVOLTAGE", |
| 30 | + "start_time": "2025-03-14 15:06:30", |
| 31 | + "end_time": "2025-03-14 17:00:00", |
| 32 | + }, |
| 33 | + { |
| 34 | + "microgrid_id": 2, |
| 35 | + "component_id": 1, |
| 36 | + "state_type": "state", |
| 37 | + "state_value": "DISCHARGING", |
| 38 | + "start_time": "2025-03-14 15:06:30", |
| 39 | + "end_time": None, |
| 40 | + }, |
| 41 | + ] |
| 42 | +) |
| 43 | +
|
| 44 | +email_html = generate_alert_email( |
| 45 | + alert_records=alert_records, |
| 46 | + notebook_url="http://alerts.example.com", |
| 47 | + displayed_rows=10, |
| 48 | + sort_by_severity=True, |
| 49 | + group_by_component=False, |
| 50 | + filter_no_alerts=True, |
| 51 | +) |
| 52 | +
|
| 53 | +# Print or send the email content |
| 54 | +print(email_html) |
| 55 | +``` |
| 56 | +""" |
5 | 57 | import html
|
6 | 58 | from datetime import timedelta
|
7 | 59 | from typing import TYPE_CHECKING, Any
|
|
0 commit comments