Alerts and notifications
Insights alerts turn a saved analytical query into a scheduled notification. An alert checks whether the query meets a condition, builds a message from the query result, and sends that message through email, Telegram, or a webhook. The alert also carries the query access context so scheduled work does not bypass the permissions of the person who enabled it. For setup and the full delivery sequence, see Alert configuration and delivery.
Alert model
An alert is a rule attached to one Insights Query v3 record. The rule combines a title, a condition, a schedule, a message, and one delivery channel. The Insights Alert document is the shared boundary between the alert editor, the scheduler, query evaluation, and notification services. insights/…/insights_alert.json
The main alert choices are:
| Concern | Supported choices |
|---|---|
| Schedule | Hourly, daily, weekly, monthly, or a custom cron schedule |
| Channel | Email, Telegram, or webhook |
| Condition | An expression evaluated against the query |
| Message | Markdown text with query result values available to the template |
| State | Enabled or disabled, with last and next execution times |
An alert must pass query access, destination, and condition checks before it can be enabled. When a new alert is created or an alert is enabled again, Insights records the user who performed that action as the permission user; later edits do not replace that identity. Query deletion also removes alerts that refer to that query, which keeps the alert boundary from pointing to an analytical resource that no longer exists. insights/…/insights_alert.py, insights/…/insights_alert.json, insights/…/insights_query_v3.py
References: insights/…/insights_alert.json• L30-148 insights/…/insights_alert.py• L77-125 insights/…/insights_query_v3.py• L71-77
Condition evaluation
The condition answers whether an alert should send. Insights evaluates it as part of the saved query, filters the query to matching rows, and checks whether at least one row remains. This makes a true condition mean that the analytical resource has current data that matches the alert rule rather than merely that the expression is syntactically valid. insights/…/insights_query_v3.py
The same condition is checked when the alert is saved and when it runs. A save with an invalid condition is rejected. A normal send reads the current query result, while a manual send can force delivery for testing even when the condition is false or the alert is not due. insights/…/insights_alert.py, insights/…/insights_alert.py, insights/…/test_insights_alert.py
References: insights/…/insights_query_v3.py• L332-344 insights/…/insights_alert.py• L77-104 insights/…/insights_alert.py• L155-194 insights/…/test_insights_alert.py• L572-655
Schedule evaluation
The scheduler considers only enabled alerts. Each alert turns its selected frequency into a next execution time, or uses its custom cron value, and compares that time with the current time. When an alert is due, Insights records the start of its execution window before it runs the query. This prevents a long or interrupted run from being retried on every scheduler tick. insights/…/insights_alert.py, insights/…/insights_alert.py, insights/hooks.py
The scheduled process follows this high-level sequence:
flowchart TD
A[Scheduled task] --> B[Find enabled alerts]
B --> C{Execution due?}
C -- No --> B
C -- Yes --> D[Reserve execution window]
D --> E[Run query as recorded permission user]
E --> F{Condition matches?}
F -- No --> G[Leave notification unsent]
F -- Yes --> H[Build message and result context]
H --> I[Deliver through selected channel]
I --> J[Complete execution]
I -. Failure .-> K[Record failed run and notify owner]
An execution time marks the scheduler’s run window. Insights records it before the query starts and keeps a separate failure record after a delivery error, so a broken destination does not cause repeated sends on every scheduler tick. The alert owner receives a general failure notice while the detailed error stays in the error log. A refusal caused by lost permission or an unreadable schedule disables the alert and tells the workbook owner. insights/…/insights_alert.py, insights/…/test_insights_alert.py
References: insights/…/insights_alert.py• L353-417 insights/…/insights_alert.py• L439-505 insights/hooks.py• L164-170 insights/…/test_insights_alert.py• L163-299
Notification channels
Each channel adapts the same evaluated alert into a destination-specific notification. Email uses the site’s outgoing mail service. Telegram sends rendered text to a configured chat. A webhook posts a versioned JSON event to an external HTTPS endpoint. insights/…/insights_alert.py
| Channel | Notification boundary | Result context |
|---|---|---|
| Site mail service | Markdown rendered as HTML with the alert, site, and author identified | |
| Telegram | Configured Telegram bot and chat | Rendered text, limited to the service message size |
| Webhook | User-selected public HTTPS endpoint | Versioned event with message and content text, query and alert names, row count, result rows, and a truncation marker |
Webhook delivery limits the rows in the payload while preserving the full result count. It sends the token in an authorization header rather than in the URL. The endpoint must be public, must use HTTPS, and must not redirect. insights/…/insights_alert.py, insights/http.py, insights/http.py
The webhook contract also marks whether the result is truncated. The token is optional and travels in an authorization header when present. Delivery reports an HTTP failure as a validation error and hides transport error details that could expose private endpoint data. A request refused by the outbound safety check keeps its refusal reason so the caller can understand why the request was blocked. insights/…/insights_alert.py, insights/…/test_insights_alert.py
References: insights/…/insights_alert.py• L176-264 insights/…/insights_alert.py• L314-334 insights/http.py• L50-62 insights/http.py• L125-166 insights/…/test_insights_alert.py• L47-160 insights/…/test_insights_alert.py• L983-1012
Permission boundary
An alert stores the user whose permissions define its scheduled query reads when the alert is enabled. This matters because the scheduler itself runs with administrator authority. At send time, Insights switches to the stored user before it evaluates the condition and builds the result message. insights/…/insights_alert.py, insights/…/insights_alert.py
Alert visibility and edit access also follow the alert’s ownership and query access. A user can access an alert they own or an alert linked to a query for which they have the needed access. Manual delivery has a stronger boundary: the sender must be allowed to edit the alert’s workbook and download the query rows. This prevents a read-only user from using an alert as a new data export path. insights/permissions.py, insights/hooks.py, insights/…/insights_alert.py, insights/…/test_insights_alert.py
References: insights/…/insights_alert.py• L106-125 insights/…/insights_alert.py• L146-176 insights/…/insights_alert.py• L386-417 insights/permissions.py• L376-399 insights/hooks.py• L124-144 insights/…/test_insights_alert.py• L791-929
Document and API boundaries
The alert document is the write boundary for alert configuration and the action boundary for manual sends. The alert API provides the read path used by the query interface to list alerts for a selected query. The client keeps the alert shape aligned with the document and loads alert records without local persistence. insights/…/insights_alert.js, insights/…/alerts.py, frontend/…/alert.ts
The API boundary is narrow by design:
get_alerts(query)returns alert records linked to one query.send_alertevaluates and sends one alert from the document action.test_alertforces one delivery path for a configured alert.- The scheduler owns recurring delivery and does not depend on the client being open.
The client’s “Send Alert” action gives an engineer or user a direct way to test delivery. The test action can force a send, but it still uses the manual sender permission and query-download checks. The recurring path remains server-controlled, which separates interactive feedback from scheduled notification behavior. insights/…/insights_alert.js, insights/…/insights_alert.py, insights/…/insights_alert.py
References: insights/…/alerts.py• L6-12 frontend/…/alert.ts• L7-31 insights/…/insights_alert.js• L4-18 insights/…/insights_alert.py• L155-194 insights/…/insights_alert.py• L381-384
Alert configuration and delivery lifecycle
Alert configuration defines the query, rule, schedule, message, and destination. Delivery then uses that saved contract to evaluate current data and notify the destination. The related page Alert configuration and delivery covers the user-facing setup and lifecycle in more detail.
References: insights/…/insights_alert.py• L77-194 insights/…/insights_alert.json• L30-148
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the
Insights Alertdocument fields, the clientInsightsAlerttype, and the query-filtered API response aligned. - Treat webhook payload fields, the
message/contentpair, and the payload version as an external contract. Coordinate changes with webhook receivers. - Preserve the permission-user handoff when changing enable or scheduler behavior. Administrator execution must not widen alert result access.
- Keep manual-send checks aligned with workbook edit and query download permissions. Read access alone must not enable a data export.
- Keep schedule calculation, early execution reservation, and failure recording consistent. A failed delivery or killed query must not create an unintended rapid retry loop.
- Keep channel validation in step with channel delivery. Required destination data must be checked before a scheduled run.
- Keep webhook error messages safe for users while preserving the reason for requests refused by the outbound safety boundary.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Alert condition is empty or invalid | The alert cannot be saved or evaluated | Validate the condition on document save and evaluate it through the query builder |
| Alert query is not readable by the user | The alert could expose query data or cannot be configured safely | Check query access on save and run scheduled work as the recorded permission user |
| Manual sender can read the alert but cannot edit its workbook or download query rows | A test send could bypass the query data boundary or fails before delivery | Check sender authority and query export permission before running the alert |
| Email recipients are empty or malformed | Email delivery is rejected | Require at least one valid address during validation |
| A cron value is not readable by the scheduler | The alert cannot determine its next run and is disabled during scheduled processing | Validate five-field cron syntax and stop an old unreadable value rather than retrying it |
| Webhook uses a non-HTTPS, private, or redirecting destination | The delivery is refused and no external request is accepted | Validate HTTPS and hostname, resolve only public addresses at connection time, and disallow redirects |
| Webhook endpoint is down or returns an error | The run is logged as failed and the notification is not delivered | Roll back the failed work, record the execution time separately, and notify the owner without exposing transport details |
| The stored permission user loses access or becomes disabled | Scheduled delivery cannot safely run and repeats would fail | Disable the alert and tell the workbook owner to enable it again under a valid user |
| Webhook transport error contains endpoint details | A user could see private network or connection data | Report the error class without exposing the transport message |
| Webhook result has more than the payload row limit | The receiver does not get every result row | Include the total count and a truncated marker with the capped row list |
Key files
| File | Why to read it |
|---|---|
| insights/…/insights_alert.json | Defines the alert document contract, channel choices, schedule fields, and destination fields. |
| insights/…/insights_alert.py | Connects validation, condition checks, scheduling, permission context, message creation, and delivery. |
| insights/…/insights_query_v3.py | Shows how a query evaluates an alert expression and how query deletion removes linked alerts. |
| insights/hooks.py | Shows that recurring alert delivery is part of the application scheduler and that alerts use scripted permissions. |
| insights/permissions.py | Explains the ownership and query-access boundary for alert records. |
| insights/…/alerts.py | Defines the query-scoped API read boundary for alerts. |
| frontend/…/alert.ts | Shows the client-facing alert shape and query-linked alert loading behavior. |
| insights/http.py | Defines the safety boundary for outbound webhook requests. |
| insights/…/test_insights_alert.py | Captures the notification contracts for conditions, schedules, channels, sender permissions, webhook payloads, and failed delivery runs. |