alert_email
frequenz.lib.notebooks.alerts.alert_email ¤
This module provides functionality for generating email alert notifications.
It includes functions for formatting and structuring alert-related emails, such as: - Generating a summary of alerts per microgrid (optionally grouped by component ID). - Creating an HTML table representation of alert details. - Constructing a complete alert email with formatted content. - Sorting alerts by severity (optional) and applying color-coded styling. - Generating structured JSON output for alerts. - Filtering groups with no errors or warnings (optional, enabled by default).
Example Usage:¤
import pandas as pd
from frequenz.lib.notebooks.alerts.alert_email import AlertEmailConfig, generate_alert_email
def example():
# Example alert records dataframe
alert_records = pd.DataFrame(
[
{
"microgrid_id": 1,
"component_id": 1,
"state_type": "error",
"state_value": "UNDERVOLTAGE",
"start_time": "2025-03-14 15:06:30",
"end_time": "2025-03-14 17:00:00",
},
{
"microgrid_id": 2,
"component_id": 1,
"state_type": "state",
"state_value": "DISCHARGING",
"start_time": "2025-03-14 15:06:30",
"end_time": None,
},
]
)
# Configuration for email generation
alert_email_config = AlertEmailConfig(
notebook_url="http://alerts.example.com",
displayed_rows=10,
sort_by_severity=True,
group_by_component=False,
filter_no_alerts=True,
)
# Generate the HTML body of the alert email
html_email = generate_alert_email(alert_records=alert_records, config=alert_email_config)
# Output the HTML or send it via email
print(html_email)
Classes¤
frequenz.lib.notebooks.alerts.alert_email.AlertEmailConfig
dataclass
¤
Configuration for generating alert emails.
Source code in frequenz/lib/notebooks/alerts/alert_email.py
frequenz.lib.notebooks.alerts.alert_email.AlertPlotType ¤
Possible plot types for alert visualisations.
Source code in frequenz/lib/notebooks/alerts/alert_email.py
frequenz.lib.notebooks.alerts.alert_email.ExportOptions
dataclass
¤
Configuration for exporting and/or displaying plots.
Source code in frequenz/lib/notebooks/alerts/alert_email.py
frequenz.lib.notebooks.alerts.alert_email.ImageExportFormat ¤
Functions¤
frequenz.lib.notebooks.alerts.alert_email.compute_time_since ¤
Calculate the time elapsed since a given timestamp (start or end time).
PARAMETER | DESCRIPTION |
---|---|
row
|
DataFrame row containing timestamps.
TYPE:
|
ts_column
|
Column name ("start_time" or "end_time") to compute from.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
str
|
Time elapsed as a formatted string (e.g., "3h 47m", "2d 5h"). |
Source code in frequenz/lib/notebooks/alerts/alert_email.py
frequenz.lib.notebooks.alerts.alert_email.generate_alert_email ¤
generate_alert_email(
alert_records: DataFrame,
config: AlertEmailConfig,
checks: list[str] | None = None,
) -> str
Generate a full HTML email for alerts.
PARAMETER | DESCRIPTION |
---|---|
alert_records
|
DataFrame containing alert records.
TYPE:
|
config
|
Configuration object for email generation.
TYPE:
|
checks
|
A list of conditions checked by the alert system. |
RETURNS | DESCRIPTION |
---|---|
str
|
Full HTML email body. |
Source code in frequenz/lib/notebooks/alerts/alert_email.py
frequenz.lib.notebooks.alerts.alert_email.generate_alert_json ¤
generate_alert_json(
alert_records: DataFrame,
group_by_component: bool = False,
) -> dict[str, Any]
Generate a JSON representation of the alert data.
The data can be optionally grouped by component ID
PARAMETER | DESCRIPTION |
---|---|
alert_records
|
DataFrame containing alert records.
TYPE:
|
group_by_component
|
Whether to group alerts by component ID.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
dict[str, Any]
|
Dictionary representing the alert data in JSON format. |
Source code in frequenz/lib/notebooks/alerts/alert_email.py
frequenz.lib.notebooks.alerts.alert_email.generate_alert_summary ¤
generate_alert_summary(
alert_records: DataFrame,
group_by_component: bool = False,
filter_no_alerts: bool = True,
) -> str
Generate a summary of alerts per microgrid, optionally grouped by component ID.
PARAMETER | DESCRIPTION |
---|---|
alert_records
|
DataFrame containing alert records.
TYPE:
|
group_by_component
|
Whether to group alerts by component ID.
TYPE:
|
filter_no_alerts
|
Whether to exclude groups with zero errors and warnings.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
str
|
HTML summary string. |
Source code in frequenz/lib/notebooks/alerts/alert_email.py
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
|
frequenz.lib.notebooks.alerts.alert_email.generate_alert_table ¤
generate_alert_table(
alert_records: DataFrame,
displayed_rows: int = 20,
sort_by_severity: bool = False,
) -> str
Generate a formatted HTML table for alert details with color-coded severity levels.
PARAMETER | DESCRIPTION |
---|---|
alert_records
|
DataFrame containing alert records.
TYPE:
|
displayed_rows
|
Number of rows to display.
TYPE:
|
sort_by_severity
|
Whether to sort alerts by severity.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
str
|
HTML string of the table with color-coded rows. |
Source code in frequenz/lib/notebooks/alerts/alert_email.py
frequenz.lib.notebooks.alerts.alert_email.generate_check_status ¤
Generate a clean HTML bullet list summarising what was checked.
PARAMETER | DESCRIPTION |
---|---|
checks
|
A list of plain text items (e.g. conditions, rules). |
RETURNS | DESCRIPTION |
---|---|
str
|
An HTML unordered list. |
Source code in frequenz/lib/notebooks/alerts/alert_email.py
frequenz.lib.notebooks.alerts.alert_email.generate_no_alerts_email ¤
Generate an HTML email when no alerts are found.
PARAMETER | DESCRIPTION |
---|---|
checks
|
A list of conditions checked. |
notebook_url
|
Optional link to manage preferences.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
str
|
HTML email body. |
Source code in frequenz/lib/notebooks/alerts/alert_email.py
frequenz.lib.notebooks.alerts.alert_email.plot_alerts ¤
plot_alerts(
records: DataFrame,
*,
plot_type: str | AlertPlotType = SUMMARY,
export_options: ExportOptions | None = None,
**kwargs: Any
) -> list[str] | None
Generate alert visualisations and optionally save them as image files.
Behaviour based on export_options
format
and show
fields:
- format=None, show=False: Do nothing.
- format=None, show=True: Display plots only (default).
- format=[...], show=False: Save plots to multiple formats only.
- format=[...], show=True: Save plots to multiple formats and display them.
PARAMETER | DESCRIPTION |
---|---|
records
|
DataFrame containing alert records with expected columns "microgrid_id", "component_id", "state_value", and "start_time".
TYPE:
|
plot_type
|
Which plot to create. Options: - 'summary': Plot of alert counts per microgrid/component. - 'state_transitions': Plot of state transitions over time. - 'all': Generate both types.
TYPE:
|
export_options
|
Configuration for exporting and/or displaying the plots.
TYPE:
|
**kwargs
|
Additional arguments for the plot functions. - 'stacked': Whether to use a stacked bar representation per microgrid (only for 'summary' plot type).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
list[str] | None
|
List of file paths if plots are exported, otherwise None. |