Alert configuration and delivery lifecycle
Insights analytical alerts watch the result of an Insights Query v3 on a schedule and send a message when a condition matches. An alert keeps the query link, trigger rule, schedule, message, and delivery target together so the same analytical result can reach email, a webhook, or Telegram. See also Alerts and notifications.
Alert model
An Insights Alert is the saved product object that connects one query to one notification channel. It remains disabled or enabled independently of the query and records the last scheduled run for the next due-time check.
Configuration surface
The alert setup asks for the information needed to answer three questions: which query should be watched, when should it be checked, and where should the result go. The main configuration groups are:
| Group | Purpose |
|---|---|
| Query and rule | Selects the query and defines the condition that can trigger a delivery. |
| Schedule | Uses an hourly, daily, weekly, monthly, or custom Cron schedule. |
| Channel target | Holds email recipients, a Telegram chat ID, or a webhook URL with an optional token. |
| Message | Defines the Markdown and template text sent after the condition matches. |
| State | Enables or disables the alert and records the last execution. |
The setup dialog offers a field/operator/value rule for common cases. It can switch to a custom expression when the simple rule is not enough. The dialog also provides separate actions to create or update an alert, enable or disable it, and send a test delivery. frontend/…/AlertSetupDialog.vue• L17-115 insights/…/insights_alert.json• L9-28
References: frontend/…/AlertSetupDialog.vue• L118-291 insights/…/insights_alert.json• L30-148
Validation boundary
The server is the final validation boundary. For an enabled alert it checks access to the linked query, checks channel-specific target data, and evaluates the condition before it accepts the saved configuration. Email targets must contain at least one valid address. Webhook targets need an HTTPS URL with a host, while the token is optional. insights/…/insights_alert.py• L77-104 insights/…/insights_alert.py• L127-144
The browser prevents incomplete setup actions, but it does not replace server checks. A disabled alert can be saved without running the enabled-alert checks, then must pass them when it is enabled. frontend/…/AlertSetupDialog.vue• L44-67 insights/…/insights_alert.py• L77-83
References: frontend/…/AlertSetupDialog.vue• L44-67 insights/…/insights_alert.py• L77-144 insights/…/test_insights_alert.py• L397-420 insights/…/test_insights_alert.py• L114-132
Persistence and execution identity
Alert changes use the Frappe document API. The linked query is set when a new alert is saved from the query result view, and the alert is enabled as part of a successful create or update. The server stores the webhook token as a protected password field; the client treats the returned value as masked and does not use local storage for the alert document. frontend/…/AlertSetupDialog.vue• L69-87 frontend/…/alert.ts• L7-30 frontend/…/alert.ts• L73-79
The alert also stores permission_user, which names the user whose query permissions apply during unattended evaluation. The value is set when an alert is created or when its disabled state changes. Ordinary edits do not transfer that execution identity to the editor. This keeps scheduled results within the access scope that was explicitly enabled. insights/…/insights_alert.py• L106-125 insights/permission_user.py• L4-18
Alert access follows ownership or write access to the linked query. A send also checks that the execution user may send the query's rows, which requires the needed workbook, query, and download rights. Deleting a query removes its linked alerts, so an alert cannot remain pointed at a deleted analytical source. insights/…/insights_alert.py• L146-175 insights/permissions.py• L376-399 insights/…/insights_query_v3.py• L71-77
References: frontend/…/AlertSetupDialog.vue• L69-114 frontend/…/alert.ts• L7-79 insights/…/insights_alert.py• L106-175 insights/permission_user.py• L25-49 insights/permissions.py• L376-399 insights/…/insights_query_v3.py• L71-77
Evaluation lifecycle
Alert evaluation separates the trigger check from the data used in the notification. The first query run asks whether the condition is true. Only after a match does the alert read the query again to build the message context and deliver the notification.
Scheduled evaluation
The scheduler scans enabled alerts and checks each alert's next due time. Standard frequencies map to fixed Cron rules. A custom Cron value controls the same due-time calculation. Before a due alert runs, the scheduler records the start time and commits it. It then evaluates and delivers under the stored permission_user, so a killed or failed run does not repeat on every scheduler tick. insights/hooks.py• L164-169 insights/…/insights_alert.py• L354-417
The lifecycle is shown below.
flowchart TD
A[Scheduler tick] --> B[Load enabled alerts]
B --> C{Alert due?}
C -- No --> B
C -- Yes --> D[Run condition under permission user]
D --> E{Condition matches?}
E -- No --> F[Commit scheduler progress]
E -- Yes --> G[Run query for message context]
G --> H[Render notification]
H --> I[Deliver through selected channel]
I --> F
D -. ordinary failure .-> J[Log failure and record execution]
G -. failure .-> J
I -. failure .-> J
D -. send refusal or unreadable schedule .-> K[Disable alert and tell workbook owner]
K --> B
J --> B
Manual test and send actions use the same evaluation and delivery path. A forced test bypasses both the condition result and the due-time check so a user can check a configured channel even when the condition is false or the alert is not currently due. Manual sends still require the execution user to retain the rights needed to send the query rows. insights/…/insights_alert.py• L155-194 insights/…/insights_alert.py• L381-384
References: insights/hooks.py• L164-169 insights/…/insights_alert.py• L155-194 insights/…/insights_alert.py• L354-465 insights/…/test_insights_alert.py• L163-299 insights/…/test_insights_alert.py• L631-645
Trigger evaluation
The linked query builds the analytical result and applies the alert expression to it. The alert therefore triggers when its condition is true for the result, which can include whole-result checks such as a count. insights/…/insights_query_v3.py• L332-344 insights/…/test_insights_alert.py• L696-713
The condition may use the simple expression produced by the setup controls or a custom expression. The query builder evaluates that expression against the query result, so the alert rule depends on the selected query's available fields and current data. frontend/…/AlertSetupDialog.vue• L35-42 insights/…/insights_query_v3.py• L332-337
References: frontend/…/AlertSetupDialog.vue• L23-42 insights/…/insights_alert.py• L266-279 insights/…/insights_query_v3.py• L332-344 insights/…/test_insights_alert.py• L572-629 insights/…/test_insights_alert.py• L696-713
Message context
After a trigger matches, the alert reads the linked query again and builds a context for the message. The context exposes the query title, alert title, a capped row list, the full row count, and an HTML table representation of the rows. {{ rows }} is treated as a request for the table form, while {{ count }} gives the full number of matching rows. insights/…/insights_alert.py• L280-334 frontend/…/AlertSetupDialog.vue• L262-290
User message templates run in a restricted Jinja environment. They can use the supplied context and basic filters but do not receive general Frappe utilities or internal globals. Email converts the rendered Markdown to HTML and wraps it in the alert email template. Telegram and webhook delivery use the rendered text directly. insights/…/insights_alert.py• L280-300 insights/…/insights_alert.py• L528-550
References: insights/…/insights_alert.py• L280-334 insights/…/insights_alert.py• L528-550 insights/…/alert.html• L1-61 frontend/…/AlertSetupDialog.vue• L262-290 insights/…/test_insights_alert.py• L646-655 insights/…/test_insights_alert.py• L715-751
Delivery channels
The selected channel receives one rendered message after a successful trigger check. Each channel has a different target contract, but all channels share the same query context and alert title.
Email delivery
Email delivery sends the rendered HTML to the validated comma-separated recipient list through the site's outgoing mail service. The subject identifies the alert, and the body footer identifies the alert, site, and author so the message can be traced back to its source. Replies use the alert author's validated email when one is available, while the footer escapes the alert title before it is shown. insights/…/insights_alert.py• L247-264 insights/…/alert.html• L54-59
References: insights/…/insights_alert.py• L247-264 insights/…/alert.html• L54-59 insights/…/test_insights_alert.py• L367-489
Webhook delivery
Webhook delivery posts a versioned JSON event to the configured public HTTPS endpoint. An optional bearer token travels in the Authorization header rather than in the URL. The payload includes the rendered message under both message and content, plus context with the alert title, query title, total row count, trigger time, and up to 100 rows. A truncated flag tells the receiver when the row list is capped. insights/…/insights_alert.py• L200-245
The outbound request rejects non-HTTPS URLs, private or otherwise non-public destinations, redirects, and proxy use. The address check happens when the connection opens, not only when the alert is saved. This protects a user-selected target from resolving into the Insights server's own network. insights/http.py• L3-29 insights/http.py• L50-62 insights/http.py• L125-166
References: insights/…/insights_alert.py• L127-144 insights/…/insights_alert.py• L200-245 insights/http.py• L3-29 insights/http.py• L125-166 insights/…/test_insights_alert.py• L59-160
Telegram delivery
Telegram delivery sends the rendered text to the configured chat through the Telegram bot configured in Insights Settings. The server requires a bot token and limits the message to Telegram's supported message length. The current setup dialog does not expose Telegram as a selectable channel, but the saved alert model and server path still support it. insights/…/insights_alert.py• L196-198 insights/…/insights_alert.py• L508-526 insights/…/insights_alert.json• L52-57
References: insights/…/insights_alert.py• L196-198 insights/…/insights_alert.py• L508-526 insights/…/insights_alert.json• L52-57 frontend/…/AlertSetupDialog.vue• L177-200 insights/…/test_insights_alert.py• L983-1012
API interactions
The query result view is the main user entry point for alert management. It loads alerts for the current query, opens an existing alert or a new setup form, and delegates document saves and server-side actions to the Frappe API.
Alert discovery
The alert list calls insights.api.alerts.get_alerts with the current query name. The API returns alerts linked to that query, and the UI presents their status, owner, and last execution before opening a selected alert. insights/…/alerts.py• L1-12 frontend/…/alert.ts• L33-46 frontend/…/QueryAlertsDialog.vue• L19-87
References: insights/…/alerts.py• L1-12 frontend/…/alert.ts• L33-46 frontend/…/QueryAlertsDialog.vue• L19-87
Alert editing and testing
The result table shows the alert entry only when its host view enables alerts. The setup dialog uses document save for create, update, enable, and disable operations. It calls the whitelisted test_alert method for a forced delivery, while the Desk form exposes the equivalent send_alert action. frontend/…/QueryDataTable.vue• L207-254 frontend/…/AlertSetupDialog.vue• L69-114 insights/…/insights_alert.js• L4-19
References: frontend/…/QueryDataTable.vue• L207-254 frontend/…/AlertSetupDialog.vue• L69-144 insights/…/insights_alert.py• L155-194 insights/…/insights_alert.py• L381-384 insights/…/insights_alert.js• L4-19
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the alert field contract aligned across the DocType definition, the TypeScript alert model, and the setup form. A new channel or field must have server validation and a usable client path.
- Keep the condition syntax aligned between the simple-rule builder, custom expression editor, and query expression evaluator. A syntax change can make existing saved alerts fail validation or trigger on different data.
- Treat the webhook JSON shape and payload version as an external contract. Changes to row limits, context names, or authentication headers affect receivers outside Insights.
- Preserve the distinction between trigger evaluation and message-context execution. A change that adds filters or pagination to one path can make the alert condition disagree with the delivered rows.
- When changing the enabled-state flow, preserve the rule that
permission_userchanges on enable transitions rather than on ordinary edits. - When changing scheduled failure handling, preserve execution recording after rollback so a failed endpoint does not run again on every scheduler tick.
- Keep the rule that a manual send checks both the execution user's workbook rights and the query's download rights. Read access alone must not send query rows to recipients.
- Keep webhook authentication optional. Receivers that authenticate by a secret URL must not receive an unexpected bearer header.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| An enabled alert has no readable linked query or has an invalid condition | Save or evaluation fails before a notification is sent | Server validation checks query access and evaluates the condition. |
| A manual sender can read an alert but cannot edit its workbook or download the query rows | No notification is sent | Send-time permission checks require the execution user's send and download rights. |
| An email recipient is empty or malformed | The alert cannot be saved | Recipient parsing and address validation run during validation. |
| A webhook URL is non-HTTPS, resolves to a non-public address, redirects, or needs a proxy | Delivery is refused | Save-time URL checks and connection-time public-address checks reject the request. |
| A webhook endpoint returns an HTTP error or cannot be reached | The delivery fails and the scheduler logs the error | The request has a bounded timeout and converts the failure to an alert error. |
| A scheduled delivery fails | The alert is marked as executed and waits for its next schedule instead of retrying every tick | The scheduler records execution after rollback. |
| A scheduled run loses the execution user's rights or holds a Cron value the scheduler cannot read | The alert is disabled and the workbook owner is told | Refused sends and unreadable schedules use the stop path. |
| A webhook result contains more than 100 rows | The receiver does not get the full result set | The payload reports the full count and sets truncated when rows are capped. |
| The configured Telegram bot token is missing | Telegram delivery fails | The channel checks Insights Settings before sending. |
Key files
| File | Why to read it |
|---|---|
| insights/…/insights_alert.py | Defines the alert lifecycle from validation through scheduling, evaluation, rendering, and channel delivery. |
| insights/…/insights_alert.json | Defines the saved alert contract and channel-dependent fields. |
| insights/…/insights_query_v3.py | Shows how alert conditions use the analytical query and how query deletion cleans up linked alerts. |
| frontend/…/AlertSetupDialog.vue | Shows the user flow for configuration, validation feedback, save, enable/disable, and test delivery. |
| frontend/…/QueryAlertsDialog.vue | Shows how alerts are discovered and selected from a query result. |
| frontend/…/alert.ts | Defines the client alert model and API-backed document access. |
| insights/…/alerts.py | Defines the query-scoped alert discovery endpoint. |
| insights/http.py | Defines the safety boundary for webhook delivery to user-selected URLs. |
| insights/…/alert.html | Defines the HTML wrapper and trace footer for email notifications. |
| insights/…/test_insights_alert.py | Pins the delivery contracts for webhook payloads, email attribution, conditions, schedules, forced sends, permissions, failure recording, and Telegram messages. |