Dashboard queries and semantic modeling
Dashboard query work turns a widget request into data that the server and UI can use. A semantic model gives that request shared names for source fields and business measures. The system resolves the model, builds SQL, sends it to a query backend, caches live results, and returns tabular data for widgets or exports.
This page sits under Dashboard domain model and pipeline. Read it with Dashboard definition lifecycle, Dashboard rendering and export, and Dashboard theming and templating for the surrounding dashboard flow.
Semantic data model
Dashboard data request
A dashboard data request describes what a widget needs rather than how to read the source. It can name dimensions and their time grain, metrics, filters, segments, sort rules, and a row limit. A named query stores this description for reuse across widgets. pkg/…/model.go• L79-90 pkg/…/semantic_query.go• L76-108
The same request fields can come from YAML or from a .dashboard.tsx definition. The TSX loader turns the dashboard tree into the same dashboard and widget query model, so query resolution does not depend on the authoring format. pkg/…/jsloader.go• L23-57 pkg/…/jsloader.go• L431-447
The same dashboard can also use author-written SQL or fixed inline rows. These sources fit the same result contract, so the UI does not need a different data shape for semantic, SQL, or static widgets. pkg/…/model.go• L98-130 pkg/…/api.go• L306-345
References: pkg/…/model.go• L79-90 pkg/…/semantic_query.go• L76-108 pkg/…/jsloader.go• L23-57 pkg/…/jsloader.go• L431-447 pkg/…/model.go• L98-130 pkg/…/api.go• L306-345
Semantic model catalog
A semantic model is the shared description of a data source. It defines the source table plus named dimensions, metrics, time granularities, and segments. Queries use these names instead of repeating source expressions in each widget. testdata/…/sales.yml• L1-34
Project loading makes the model catalog available to each dashboard. A dashboard can select one model as its default, name a model directly, or use an alias that maps a dashboard name to a project model. The full catalog is also available when a query refers to a dimension on a joined model. pkg/…/project.go• L10-20 pkg/…/model.go• L514-538 pkg/…/semantic_query.go• L9-18
YAML and TSX dashboards receive this same catalog context. Invalid models stay marked as invalid during loading. A dashboard that refers to one fails with a useful model error, while a regular dashboard that does not use it can still load. pkg/…/jsloader.go• L25-55 pkg/…/project.go• L60-74 pkg/…/model.go• L520-538
References: pkg/…/project.go• L10-20 pkg/…/model.go• L514-538 pkg/…/semantic_query.go• L9-18 pkg/…/jsloader.go• L25-55 pkg/…/project.go• L60-74 pkg/…/model.go• L520-538
Query definition validation
Dashboard validation checks semantic model references and asks the semantic engine to generate SQL before a dashboard is used. This catches missing models and invalid semantic fields at definition time rather than during a user request. Named semantic queries and direct semantic widgets use the same model and catalog rules. pkg/…/validator.go• L156-176 pkg/…/validator.go• L382-401 pkg/…/validator.go• L810-833
Validation also keeps static data separate from executable queries. A widget with inline rows must have named columns and matching row values. It cannot also declare SQL, a named query, or semantic fields. pkg/…/validator.go• L338-379
References: pkg/…/validator.go• L156-176 pkg/…/validator.go• L382-401 pkg/…/validator.go• L810-833 pkg/…/validator.go• L338-379
Query resolution
Source selection
Query resolution creates one executable job for each data-bearing widget. It skips text, divider, and image widgets. It returns inline rows directly and resolves semantic or SQL sources for the remaining widgets. pkg/…/api.go• L254-303
The source order is deliberate:
- A named query is used when the widget has a query reference.
- Direct semantic fields are used when the widget or dashboard supplies model context and semantic fields.
- Inline SQL is used when no semantic source applies.
- Inline data is returned without a backend call.
Connection selection follows the same context. A query-level connection overrides the dashboard connection for a named query. A widget-level connection overrides the dashboard connection for direct semantic or SQL work. Filter option queries use their own connection when one is supplied, then fall back to the dashboard connection. pkg/…/semantic_query.go• L20-73 pkg/…/model.go• L589-624
References: pkg/…/api.go• L254-303 pkg/…/semantic_query.go• L20-73 pkg/…/model.go• L589-624
Filter context
Filter context supplies the values for one dashboard request. The server starts with dashboard defaults and replaces them with values from the request. Before query rendering, the dashboard normalizes date expressions, date-range presets, YAML date values, and scalar values that echo YAML defaults. pkg/…/api.go• L147-176 pkg/…/model.go• L410-437 pkg/…/filter_values.go• L12-64
The normalized values are then checked against the dashboard filter definitions. Unknown names and unexpected shapes fail closed. Dates must be valid calendar dates. Date ranges must contain an ordered start and end. Select values must use allowed options when the dashboard declares them. pkg/…/filter_values.go• L113-200
The context can affect both a semantic query and the model expressions behind it. This lets one dashboard request apply the same user choice to dimensions, metrics, segments, and source expressions. The system renders these values before SQL generation. pkg/…/semantic.go• L84-176
Filter option queries use the same semantic job path as widget queries. An option request supplies its dimension, optional metric, connection, and sort order to that path, so it uses the same model aliases and model catalog as dashboard data queries. pkg/…/semantic_query.go• L156-170
References: pkg/…/api.go• L147-176 pkg/…/model.go• L410-437 pkg/…/filter_values.go• L12-64 pkg/…/filter_values.go• L113-200 pkg/…/semantic.go• L84-176 pkg/…/semantic_query.go• L156-170
SQL generation and backend access
Semantic SQL compilation
Semantic compilation combines the selected model, the complete model catalog, and the request's semantic fields. The semantic engine resolves names and relationships, then produces SQL for the chosen connection. pkg/…/semantic.go• L12-50
The flow from a dashboard request to a backend job is:
flowchart LR
A[Widget or named query] --> B[Resolve model and connection]
B --> C[Apply filter context]
C --> D[Semantic engine generates SQL]
D --> E[Widget job]
E --> F[Query backend]
Joined dimensions can receive qualified names in generated SQL. The result layer maps those output names back to the names used by dashboard encodings, so a widget can find its requested dimension. pkg/…/semantic.go• L53-81 pkg/…/api.go• L17-30
References: pkg/…/semantic.go• L12-50 pkg/…/semantic.go• L53-81 pkg/…/api.go• L17-30
Query backend
The query backend is the boundary between dashboard work and a data source. It accepts a connection name and SQL and returns a tabular result. DAC's standard backend delegates this work to the Bruin query command, while the interface also allows other backend implementations. pkg/…/backend.go• L5-14 pkg/…/bruin.go• L11-27
The Bruin backend sends JSON-output query commands to the Bruin CLI. It can include the selected connection, project config file, and target environment. For live execution, it converts returned column metadata and rows into the shared query result. For dry runs, it also reports the resolved connection and query validity, and it surfaces structured Bruin errors when the command returns one. pkg/…/bruin.go• L21-30 pkg/…/bruin.go• L45-120
The live server uses one cached backend for dashboard queries. Server-side builds use the same job and result path but create a direct backend for the build operation. This keeps the semantic contract consistent across interactive and generated views. pkg/…/server.go• L95-130 pkg/…/render.go• L60-64 pkg/…/render.go• L98-122
TSX definitions can also call query() while the dashboard is loaded. A backend supplied to the loader runs that SQL and returns rows that can shape the dashboard. Without a backend, the loader returns an empty result so the dashboard structure can still be validated. pkg/…/jsloader.go• L150-170 pkg/…/jsloader.go• L67-72
References: pkg/…/backend.go• L5-14 pkg/…/bruin.go• L11-27 pkg/…/bruin.go• L45-120 pkg/…/server.go• L95-130 pkg/…/render.go• L60-64 pkg/…/render.go• L98-122 pkg/…/jsloader.go• L150-170 pkg/…/jsloader.go• L67-72
Result delivery
Query result contract
Every executed result has a list of columns and positional rows. Each column can include a name and a source type. A widget result also carries the generated SQL for diagnostics or an error string when that widget's query fails. pkg/…/result.go• L3-12 pkg/…/api.go• L136-145
Static widget data follows the same columns-and-rows shape. The validator requires unique, non-empty column names and one value for each declared column. This shared contract lets metric, chart, table, export, and UI consumers work from the same data without knowing whether the source was semantic SQL, direct SQL, or inline rows. pkg/…/model.go• L384-391 pkg/…/validator.go• L338-379 frontend/…/dashboard.ts• L261-269
References: pkg/…/result.go• L3-12 pkg/…/api.go• L136-145 pkg/…/model.go• L384-391 pkg/…/validator.go• L338-379 frontend/…/dashboard.ts• L261-269
Server delivery paths
The server exposes three dashboard data paths for different UI needs. Batch delivery waits for all widget jobs and returns a map keyed by stable widget IDs. Stream delivery sends each result as newline-delimited JSON when it completes. Single-widget delivery supports lazy loading. All three paths use the same resolution and execution rules. pkg/…/api.go• L147-203 pkg/…/api.go• L353-435 pkg/…/api.go• L437-482
The server runs several jobs at once with a limit on active work. A failure is kept with the affected widget result in batch and stream responses, which allows other widgets to remain usable. pkg/…/api.go• L182-201 pkg/…/api.go• L323-345
References: pkg/…/api.go• L147-203 pkg/…/api.go• L353-435 pkg/…/api.go• L437-482 pkg/…/api.go• L182-201 pkg/…/api.go• L323-345
UI data consumption
The UI sends the current filter map with a dashboard data request. It can use the complete response, accept widget results as a stream, or request one widget on demand. In a built static dashboard, the UI reads the baked widget data instead of calling the server. frontend/…/client.ts• L66-80 frontend/…/client.ts• L83-159 frontend/…/client.ts• L162-177
Widgets receive the shared data shape and use column names to select their values. The semantic result rename step is important here because a mismatch between generated output names and widget fields would look like missing data in the UI. frontend/…/dashboard.ts• L261-269 pkg/…/semantic.go• L53-81
References: frontend/…/client.ts• L66-80 frontend/…/client.ts• L83-159 frontend/…/client.ts• L162-177 frontend/…/dashboard.ts• L261-269 pkg/…/semantic.go• L53-81
Cache and freshness
Query result cache
The live server caches successful backend results for a short time. A cache entry is identified by both connection and SQL, so the same generated query can be reused while a different filter or connection receives its own result. pkg/…/cache.go• L11-29 pkg/…/cache.go• L33-58
The default live-server cache lifetime is five minutes. Errors are not stored as results. An expired entry causes a new backend request. pkg/…/server.go• L95-102 pkg/…/cache.go• L41-58
References: pkg/…/cache.go• L11-29 pkg/…/cache.go• L33-58 pkg/…/server.go• L95-102
Source change invalidation
The server clears the query cache when watched dashboard or semantic source files change. This prevents a changed model or filter expression from being hidden by an old generated result. pkg/…/server.go• L214-228
The cache does not replace request filter handling. The filter context still changes the generated SQL, and that SQL becomes part of the cache identity. pkg/…/cache.go• L68-71 pkg/…/api.go• L169-176
References: pkg/…/server.go• L214-228 pkg/…/cache.go• L68-71 pkg/…/api.go• L169-176
Change checklist
When these files or sections are changed, remember to consider these:
- A change to semantic model names or output expressions can affect model resolution, generated SQL, and widget column lookup together.
- A change to filter defaults, normalization, or validation can change generated SQL and therefore the cache key and result freshness.
- A change to the semantic engine's qualified-column naming must stay aligned with the server's result-column rename rule.
- A change to the widget result shape must stay aligned across the backend result, server response, static build payload, and frontend
WidgetDatatype. - A change to connection precedence must remain consistent for named queries, direct semantic widgets, direct SQL widgets, and filter option queries.
- A change to TSX query fields or load-time
query()behavior must stay aligned with the dashboard model and with validation when no backend is available. - A change to Bruin CLI output or command flags must stay aligned with backend result parsing, dry-run validation, connection selection, config-file loading, and environment selection.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A widget names a model that is missing or invalid | Job resolution fails before the widget can query its backend | Keep model references and aliases aligned with the project semantic model catalog. |
| A semantic query uses a joined dimension but result-name remapping is stale | The backend returns data but the widget cannot find the requested dimension column | Keep qualified output naming and ColumnRenames behavior aligned. |
| A filter contains an unknown name or invalid shape | The request is rejected before template rendering or SQL generation | Normalize client echoes and validate values against the dashboard filter definitions. |
| A date range has invalid dates or a start after its end | The request cannot produce a valid filter context | Use canonical YYYY-MM-DD values and an ordered range. |
| A backend query fails | The affected widget receives an error result instead of rows | Inspect the returned query and error while other widget results can still load. |
| Bruin returns malformed JSON or a CLI error | The query or dry-run cannot become a shared result and the widget receives an execution error | Keep Bruin JSON output valid and preserve the command's stdout and stderr for diagnosis. |
| Source files change while an old cache entry remains | The dashboard can show data from the prior model or query | Ensure file watching remains active or wait for the cache TTL to expire. |
| A TSX load-time query needs a backend during validation | Dynamic dashboard content is empty even though the definition can still load | Supply a query function for data-driven loading when the dashboard needs real load-time rows. |
Key files
| File | Why to read it |
|---|---|
| pkg/…/semantic_query.go | Shows how widget, named-query, and filter-option fields become semantic jobs. |
| pkg/…/model.go | Defines dashboard query fields, model lookup, connection fallback, inline result shape, and source rules. |
| pkg/…/filter_values.go | Defines filter normalization and fail-closed value checks before query rendering. |
| pkg/…/project.go | Shows how the project semantic model catalog is discovered and tracked. |
| pkg/…/jsloader.go | Shows how TSX dashboard definitions and load-time query results become the shared dashboard model. |
| pkg/…/validator.go | Shows definition checks for semantic models, query sources, filter types, and inline result data. |
| pkg/…/semantic.go | Shows the boundary between filter-aware semantic models, SQL generation, and output-column names. |
| pkg/…/api.go | Shows job creation, execution, result shaping, and batch, stream, and lazy delivery. |
| pkg/…/backend.go | Defines the backend contract for sending SQL to a data source. |
| pkg/…/bruin.go | Shows how the standard backend invokes Bruin and maps query and dry-run JSON into shared results. |
| pkg/…/cache.go | Defines live query cache identity, expiry, and invalidation. |
| frontend/…/client.ts | Shows how server and static data reach the UI. |
| frontend/…/dashboard.ts | Defines the semantic query and widget result shapes used by the UI. |