Alerts¶
Alerts are data threshold notifications, if a value meets a specific requirement, selected users get notified.
For now, the checks can only be executed on publish but we might want to configure scheduled checks later (for live data).
Alert structure¶
The alerts are handled using a pydantic model: AlertModel, who inherits from BaseAlert (used as the payload sent by the frontend)
They perform all the validation needed to build the alert.
Here is an exemple payload for the BaseAlert:
{
"name": "My alert",
"domain": "selected_domain",
"condition": {"column": "qty", "operator": "lt", "value": 10},
"email": {"groups": ["manager"]},
"story_attachment": {
"url": "https://andrea.toucantoco.guru/demo/story_id",
"elementSelector": "css",
"lang": "en",
"width": 1,
"height": 1,
"deviceScaleFactor": 1,
}
}
email and story_attachment are optional.
The BaseModel payload is then converted to an AlertModel using this method:
AlertModel.create(creator='user@email.com', created_at=datetime.utcnow(), base_alert=base_alert)
In this example, an alert is set on the domain "selected_domain", it will: - Check if the condition is matched (column "qty" is less than 10), if so: - Send an email containing the story attached to every user of the "manager" group.
Alerts check¶
We retrieve all the alerts and process them one by one by executing the specified query.
If any data is found matching the query, the alert is triggered and the notifications are sent.
This check is done by the check_alert() function.
Email throttle¶
To avoid spamming the users, a throttle is set up on emails to avoid sending the alerts more than once a day.
The filtering is set up using the filter_users_throttle() function.
There is currently no throttle on the MS Teams alerts.