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 generate_alert_email
# 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,
},
]
)
email_html = generate_alert_email(
alert_records=alert_records,
notebook_url="http://alerts.example.com",
displayed_rows=10,
sort_by_severity=True,
group_by_component=False,
filter_no_alerts=True,
)
# Print or send the email content
print(email_html)
Functions¤
frequenz.lib.notebooks.alerts.alert_email._format_timedelta ¤
Format a timedelta object into a human-readable string.
PARAMETER | DESCRIPTION |
---|---|
delta
|
Timedelta object representing time difference.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
str
|
Formatted string (e.g., "3h 47m", "2d 5h"). Defaults to "0s" if zero. |
Source code in frequenz/lib/notebooks/alerts/alert_email.py
frequenz.lib.notebooks.alerts.alert_email._parse_and_localize_timestamp ¤
_parse_and_localize_timestamp(timestamp: Any) -> Timestamp
Parse a timestamp, coerce errors to NaT, and localize to UTC if naive.
PARAMETER | DESCRIPTION |
---|---|
timestamp
|
The timestamp value to process.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Timestamp
|
A timezone-aware Pandas Timestamp, or NaT if parsing fails. |
Source code in frequenz/lib/notebooks/alerts/alert_email.py
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,
notebook_url: str = "",
displayed_rows: int = 20,
sort_by_severity: bool = False,
group_by_component: bool = False,
filter_no_alerts: bool = True
) -> str
Generate a full HTML email for alerts.
PARAMETER | DESCRIPTION |
---|---|
alert_records
|
DataFrame containing alert records.
TYPE:
|
notebook_url
|
URL for managing alert preferences.
TYPE:
|
displayed_rows
|
Number of rows to display in the email.
TYPE:
|
sort_by_severity
|
Whether to sort alerts by severity.
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
|
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
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
|
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. |