Application runtime and integration
Frappe Insights is a Frappe app with a Vue client that runs inside a site and uses Frappe for identity, HTTP requests, document access, and real-time events. The runtime joins the server route, boot data, browser session, client router, and shared services so that the analytical features work as one site-aware application.
Frappe application boundary
App registration
The Frappe integration gives the app a site entry point, a configurable URL, permission checks, lifecycle hooks, and scheduled work. The default path is /insights, but a site can move the app when that path is already in use. The path is read when the hook module loads, so a bench process serving several sites must keep this shared setting coherent.
The integration also registers the app in the Frappe app screen and exposes the app page through route rules. Permission checks remain part of the Frappe boundary, before a user reaches the client or its data features.
References: insights/hooks.py• L14-33 insights/hooks.py• L286-301
Desk document integration
Insights can render selected Frappe desk documents through Insights content. When this mode is enabled, desk pages link to the related Insights dashboard or chart and use the same app path to build links. Frappe permission checks still control access to the desk document, while the embedded Insights view checks access to its own content.
This joins standard Frappe desk records with Insights views without changing the main browser entry point. User actions on desk documents can therefore show the same analytical content as the Insights app.
References: insights/hooks.py• L173-205 insights/desk.py• L4-29 insights/desk.py• L40-80
Site boot data
The app page is a no-cache HTML entry that supplies the values the client cannot know by itself. Boot data carries the site name, CSRF token, socket port, deployment type, and effective Insights path. The client uses these values to form requests and real-time connections for the current site.
The built client is served as an asset and copied into the Frappe web directory as the app entry. This keeps local development and the deployed Frappe page on the same client startup path.
References: insights/…/_insights.py• L9-28 frontend/vite.config.js• L52-87 frontend/package.json• L9-14
Frontend startup and navigation
Client bootstrap
The frontend bootstrap creates the Vue application and installs the shared state, router, Frappe request adapter, UI services, translation service, and global controllers. It mounts the application at #app, while App.vue supplies the common shell for pages, dialogs, and notifications.
The startup also restores the saved theme before the first useful view and enables telemetry after a logged-in session is known. Global error handling reports an unhandled client error without changing the server state.
References: frontend/…/main.ts• L1-48 frontend/index.html• L8-27 frontend/…/App.vue• L1-23 frontend/…/globals.ts• L25-48
Route model
The router maps the client URL space to the main user journeys: dashboards, workbooks, data sources, the managed data store, templates, shared views, login, and not-found pages. Workbook child routes keep query, chart, and dashboard views under their workbook context.
The router base comes from the effective app path in boot data. This allows links and browser history to work when a site uses a path other than /insights.
The following diagram shows how a browser request reaches the Frappe page and then becomes a client view.
flowchart LR
B[Browser request] --> R[Frappe route rule]
R --> H[Insights HTML entry]
H --> D[Boot data and client assets]
D --> V[Vue router]
V --> P[Dashboard, workbook, source, store, or shared view]
References: insights/hooks.py• L23-30 insights/hooks.py• L286-291 insights/…/insights.js• L1-7 frontend/…/app_path.ts• L1-10 frontend/…/router.ts• L5-121
Route access
Before each route change, the client initializes the session and separates guest views from protected views. Shared charts and dashboards can open for guests. Other views send an unauthenticated browser to Frappe login in production or to the local login view during development.
The client also watches request responses for a lost session. A forbidden response combined with a guest cookie clears the local session and returns the user to login. This keeps the visible route aligned with the server session after expiry or logout in another tab.
References: frontend/…/router.ts• L123-154 frontend/…/session.ts• L35-70
Session and real-time behavior
Session lifecycle
The client session is the local view of Frappe identity and Insights access. It starts with the browser cookie, then asks the server for role and site information such as admin status, download rights, locale, demo data, and fiscal year settings.
Login and logout both clear the local view and reload the page after the Frappe call. A reload makes the browser cookie and the booted application state agree before the next route decision.
The session distinguishes three useful states:
| State | Meaning | Client effect |
|---|---|---|
| Guest | No signed-in Frappe user | Only guest views can open |
| Signed-in user | A valid user cookie is present | The client loads user and site access data |
| Authorized user | The user has Insights user or admin access | Protected Insights views can operate |
References: frontend/…/session.ts• L4-18 frontend/…/session.ts• L35-99 insights/…/__init__.py• L29-131
Socket connection
The real-time service provides one shared Socket.IO connection for the client. It uses the current host, site name, and socket port from the browser environment, sends site credentials, and retries a limited number of times when the connection drops.
Components register event listeners through a helper that removes each listener when the component leaves the page. The application uses the insights_notification event to show a toast only when the event targets the current user.
References: frontend/…/socket.ts• L1-21 frontend/…/globals.ts• L44-48 frontend/…/index.ts• L351-357 frontend/…/App.vue• L36-54 insights/__init__.py• L39-60
Translation service
Translation loading
The translation service gives the client one __ function for messages and labels. It fetches translations from Frappe once through a cached resource, then stores the message map in the browser for use by Vue components and shared helpers.
The server selects the language from the signed-in user when possible. For guests or users without a language value, it falls back to the site language. Message arguments use numbered placeholders and remain unchanged when a value is missing.
References: frontend/…/translation.ts• L4-48 insights/…/translations.py• L1-12
Server request and public access handling
API boundary
The API boundary exposes Frappe-whitelisted calls for client actions and keeps type and role checks close to those calls. Most Insights actions require the Insights User role. A smaller set of guest calls supports shared content, site display settings, invitation acceptance, name lookup for renamed public resources, and translation loading.
The View endpoints also support guest rendering of public dashboards and charts. Each dashboard or chart reference passes through one read check, so a guest can receive only content marked public. The response contains the data needed to render the view while the chart query and its operations stay on the server. Drill access is a separate signed-in capability, even when the chart itself is public.
Document reads and document methods follow a separate public-access path. The server first uses normal Frappe permission checks. It can then allow only explicitly public document types and methods, such as reading a shared dashboard or executing a query attached to public content.
The same boundary handles user file previews and imports. It limits uploads to supported table formats and places imported rows in the managed uploads source so later analytical work follows the normal data-source permission path.
References: insights/…/view.py• L4-17 insights/…/view.py• L63-110 insights/…/view.py• L207-240 insights/…/view.py• L357-425 insights/decorators.py• L13-44 insights/decorators.py• L119-167 insights/…/__init__.py• L63-85 insights/…/__init__.py• L103-131 insights/…/__init__.py• L160-237 insights/…/__init__.py• L240-359 insights/…/shared.py• L6-32
Workbook access and sharing
Workbook APIs return only resources that the current user can access. They support views for owned workbooks, shared workbooks, and the full accessible set, while also exposing the sharing state needed by the client.
Sharing stays within the Insights user population. A workbook can be shared with named Insights users or with the organization for view or edit access.
References: insights/…/workbooks.py• L10-28 insights/…/workbooks.py• L30-89 insights/…/workbooks.py• L129-247
Embedded view policy
The page renderer handles the public and shared app paths used for embedded content. When the site sets allowed origins, it adds a frame-ancestor policy so the browser can enforce which other sites may embed those views.
Public access is still tied to a public dashboard, chart, or query relationship. A preview key can provide a controlled exception for server-side dashboard image generation. Preview images are available only while the dashboard has stable rows for every reader and its current preview is valid. On-demand dashboard images use a bounded width and a rate limit because each request starts a browser render.
References: insights/utils.py• L15-16 insights/utils.py• L165-194 insights/…/view.py• L155-204 insights/…/shared.py• L35-60 insights/hooks.py• L286
Outbound HTTP
User-selected webhook URLs are treated as untrusted destinations. The HTTP helper accepts only HTTPS URLs, checks that the destination resolves to public network space when the connection opens, refuses redirects, and does not use proxies or local credential files.
This boundary protects the Frappe server from being used to reach private network services. It is used by webhook alerts, which send a versioned payload with a bounded row set and a bearer token in the request header.
References: insights/http.py• L3-29 insights/http.py• L50-87 insights/http.py• L125-166 insights/…/insights_alert.py• L56-79 insights/…/insights_alert.py• L109-150
Server lifecycle and cache behavior
Application hooks
Frappe hooks connect application events to Insights behavior beyond a single HTTP request. Installation and migration hooks prepare site data, user changes keep the Insights admin team aligned, scheduled jobs run alerts and data maintenance, and the page renderer serves the app entry and embedded views.
The hooks also define permission checks for Insights documents and validate shares and workbook member rows when Frappe changes them. The app exposes its whitelisted method source through Frappe discovery endpoints for open-source API clients. These rules keep document access and background work inside the same application boundary. Deployment and ordered migration behavior are described in Deployment, setup, and migrations.
References: insights/hooks.py• L90-101 insights/hooks.py• L119-141 insights/hooks.py• L158-229 insights/hooks.py• L286-301
Request cleanup
Data-source connections can live for the duration of one request so repeated work in that request can reuse them. The after_request hook closes those connections and removes them from the site-local connection map. This prevents a request from leaving remote database handles behind.
The same cleanup rule also wraps internal connection scopes used during data-source updates. It makes connection release part of the request lifecycle rather than a caller-specific choice.
References: insights/hooks.py• L90-95 insights/…/insights_data_source_v3.py• L244-276 insights/…/insights_data_source_v3.py• L484-503 insights/__init__.py• L28-34
Cache utilities
The cache layer supports short-lived reuse of values that are costly to compute. It creates stable digest keys from query inputs and backend identity, prefixes entries for the app, and lets callers force a fresh value or select an expiry.
Query execution uses this pattern for repeated code results and query results. A System Manager can clear the app cache when stored values no longer match the current data or configuration.
References: insights/cache_utils.py• L8-34 insights/…/ibis_utils.py• L749-763 insights/…/ibis_utils.py• L928-951
Related operational pages
Deployment, setup, demo provisioning, fixtures, and ordered data migrations are covered in Deployment, setup, and migrations. Upgrade and legacy migration behavior is covered in Upgrade and legacy migration. For the rules that decide which signed-in users can use Insights resources, see Authorization and user administration. For the managed data store and its administration flow, see Data store administration.
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the configured
insights_pathconsistent across Frappe route rules, app-screen links, boot data, and the Vue router base. - Keep desk island links and desk permission hooks aligned with the enabled desk integration mode.
- Keep socket boot values and the Socket.IO URL contract aligned when the site or socket service changes.
- Keep guest route metadata aligned with the server-side public document and method allowlists.
- Keep View reference resolution and public visibility rules aligned so guest endpoints do not reveal refused charts or query details.
- Keep translation language fallback behavior aligned between the client resource and the Frappe user and system settings.
- Keep request cleanup aligned with any new data-source connection cache so connections do not outlive a request.
- Keep workbook share validation aligned with the ownership and organization-share rules used by the client.
- Keep webhook payload versioning and public-destination checks aligned when alert delivery changes.
- Keep cache keys aligned with every query input and backend identity that can change a result.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The app path changes in only one layer | Frappe serves the page but browser routes or app links point to the wrong path | Treat the hook value, boot field, route base, and route rules as one contract |
| Different sites in one bench require different paths while the hook module is shared | The route and app link can use the path selected when the hook module first loads | Keep the shared runtime configuration coherent for the process that serves the sites |
| A desk document link is enabled without the matching boot path or permission rules | The desk page can point to a missing view or expose a view with the wrong access result | Change the desk hook set, app path, and permission integration as one unit |
| A protected API call returns 403 after the cookie expires | The client can show a stale protected view | The fetch wrapper resets the session and routes to login when the browser is a Guest |
| A public chart reader requests drill data | The request is refused even though the saved chart can be viewed | Keep drill access separate from public chart rendering and require sign-in |
| Socket site or port data is wrong | Notifications and other real-time events do not reach the current site | Build the connection from boot site data and use the configured socket port |
| A public document gains a new guest method without an allowlist entry | Guests still receive a permission error, or an unsafe method could be exposed if checks are bypassed | Allow only explicit public document types and methods |
| A webhook target resolves to a private address or redirects | Alert delivery is refused instead of reaching the selected endpoint | Require HTTPS, check the address at connect time, and reject redirects |
| A cached query result outlives a source or query change | The user sees data from an old computation | Include query inputs and backend identity in keys and clear the Insights cache when needed |
| A dashboard image request exceeds the width or rate limit | The render is refused before a server-side browser consumes more resources | Keep image width bounds and rate limits aligned with the image endpoint contract |
Key files
| File | Why to read it |
|---|---|
| insights/hooks.py | Defines the Frappe app boundary, route rules, desk integration, lifecycle hooks, schedules, permission integration, and API discovery setting. |
| insights/…/_insights.py | Shows the boot data that connects a Frappe site to the client runtime. |
| frontend/…/main.ts | Shows the client services and startup order. |
| frontend/…/router.ts | Defines navigation, guest views, protected views, and session recovery. |
| frontend/…/session.ts | Defines the browser view of Frappe identity and Insights access. |
| frontend/…/socket.ts and frontend/…/App.vue | Explain the shared real-time connection and user notification flow. |
| frontend/…/translation.ts and insights/…/translations.py | Explain how the client gets language-specific messages. |
| insights/…/__init__.py, insights/…/shared.py, and insights/decorators.py | Define the HTTP API boundary, upload path, and split between protected and public access. |
| insights/…/view.py | Defines the public and signed-in view surface for dashboards, charts, filters, exports, previews, images, and drill actions. |
| insights/…/workbooks.py | Defines accessible workbook lists and user or organization sharing behavior. |
| insights/http.py and insights/…/insights_alert.py | Define safe outbound webhook delivery and its payload contract. |
| insights/…/insights_data_source_v3.py | Shows request-scoped data-source connection cleanup. |
| insights/cache_utils.py | Defines shared cache keys, expiry, and administrative cache reset. |