The semantic layer names the measures, dimensions and hierarchies of your business data once, as metadata, so every reader asks for "revenue by customer by month" instead of re-spelling operations, fields and filters. Reports, the KPIs over them, the report builder, Droot AI, plan models, commission formulas and BI tools all read the same definitions, and every read runs as the person asking: their companies, their screens, their field rights.
Semantic models
A semantic model measures the records of one stored model. An app ships them in metadata/semantic-models.json; an
administrator defines or changes them on POST /api/admin/semantic-models (needs metadata.write and every company).
{
"key": "sales.revenue",
"label": "Sales Revenue",
"model": "accounting.customer-invoice-line",
"filter": { "status": "posted" },
"dimensions": [
{ "key": "customer", "label": "Customer", "field": "invoice.customer" },
{ "key": "item", "label": "Item", "field": "item" },
{ "key": "invoiceYear", "label": "Year", "field": "invoice.invoiceDate", "bucket": "year" },
{ "key": "invoiceMonth", "label": "Month", "field": "invoice.invoiceDate", "bucket": "month" }
],
"measures": [
{ "key": "revenue", "label": "Revenue", "op": "sum", "field": "netMinor", "format": "money" },
{ "key": "cost", "label": "Cost", "op": "sum", "field": "costMinor", "format": "money" },
{ "key": "margin", "label": "Margin", "formula": "revenue - cost", "scale": 0, "format": "money" },
{ "key": "marginPct", "label": "Margin %", "formula": "round((revenue - cost) * 10000 / revenue)", "scale": 0, "format": "percent" }
],
"hierarchies": [{ "key": "time", "label": "Time", "levels": ["invoiceYear", "invoiceMonth"] }]
}
model: a stored model (not virtual) of the app or of one of its dependencies.filter: always applied, in the record filter language (filter tokens such as$todayare allowed).dimensions: the report dimension shape (a field, a related-field path through lookups, a datebucket, a status axisby: "category", an organisation roll-upby: "hierarchy"), with alabeland a requiredkey.measures: either an aggregate (the report measure shape:op,field,where,calc,weight,exprof two fields) or aformulaover other measures of the model. Formulas use the calculated field grammar (+ - * /, numbers,abs,round,min,max,coalesce) and are worked out after the read on every row and on the totals, exactly as a report'sformulameasure is (decimals, never floating point; the money rules below, checked when the model is defined). A plain figure is decimal text rounded half-up once toscale(default 2); a formula over amounts of a currency field is money, an amount per currency ({ "INR": "60.25" }, as a money measure answers) atscaledecimals. Formulas can't be sorted on, and they don't count toward a query's ten aggregate measures (their operands do).formatsays how a figure reads:number,money(minor units),percent(basis points),countorduration.hierarchies: drill paths, two to six dimension keys from the top level down.- Dimension and measure keys share one namespace (a query and a BI column name either by key).
A tenant may change a semantic model's label, description and readModel on an installed one; the rest is the
app's.
Reading
| Endpoint | What |
|---|---|
GET /api/semantic/models |
The semantic models you may read, with the dimensions, measures and hierarchies your role reads |
GET /api/semantic/models/:key |
One of them |
POST /api/semantic/models/:key/query |
Measures by dimensions, or one level of a hierarchy |
A semantic model is readable when its model is: a screen you may open lists its records (or you hold data.raw). A
dimension or measure on a field your role can't read is left out of the catalogue, and a query naming it is refused.
{ "measures": ["revenue", "margin"], "dimensions": ["customer"], "filter": { "item": "<id>" }, "sort": { "revenue": -1 }, "limit": 20 }
filternames dimension keys (mapped to their fields; a bucketed dimension takes a{ "gte", "lt" }range) or the model's own fields, in the record filter language.drill: { "hierarchy": "time", "level": 1, "at": { "invoiceYear": { "gte": "...", "lt": "..." } } }reads one level of a hierarchy under the values of the levels above it (each row returns its bucketrangesto drill with).- At most three dimensions and ten aggregate measures per query (a formula counts its operands).
The answer has dimensions and measures (columns with labels and formats), rows (key, ranges, values),
totals, total (groups across pages), labels (display text of lookup and user values) and the calendar dates
were bucketed by.
Reports, KPIs and dashboards
A report view (kind: "report") holds its own report.measures; KPIs and dashboard widgets read them. Three shapes
build a figure from others:
-
calc: arithmetic over one record's fields, before totalling ({ "key": "net", "op": "sum", "calc": "amount - coalesce(discount, 0)" }). -
expr: exactly two figures (sub,add,mulBp,ratioBp). A table sorts by them like any measure (by the figure in the primary currency for money). -
formula: any expression over other measures of the same report, by key, in the calculated field grammar, worked out on every row and on the totals from their own figures:{ "measures": [ { "key": "revenue", "op": "sum", "field": "netTotal" }, { "key": "cost", "op": "sum", "field": "costTotal" }, { "key": "margin", "op": "sum", "formula": "(revenue - cost) / revenue * 100", "scale": 1, "format": "percent", "label": "Margin %" } ] }Money plus or minus money in the same currency stays money, money divided by money is a ratio, figures in different currencies are never combined, and a division by zero is no value. A report reusing a semantic model (
semantic) gets the model's formula measures as report formulas.
A KPI (kpi kind) names one report measure (viewKey, measure) and grades it (goal, target, warn, bad,
rangeLow, rangeHigh), optionally over a periodField with a compare period. A KPI, and a dashboard's kpi, chart
or table widget, gives a report's prompts with params: { "region": "north" } (every required param without a
default). A dashboard kpi widget is a stat card: its main figure plus up to four figures (a measure, a KPI, another
report's measure, or a formula over main and the other figures), drawn in a style (plain, split, progress, ring
or list).
Define the report first, then the KPI, then the dashboard: each is checked against what the one before it defines.
Droot AI proposes all three as one changeset and checks the figures first with preview_figures, which runs the
proposed definitions as the user in a transaction it rolls back.
Analytics read model
A semantic model with "readModel": true keeps an analytics read model: one facts document per record of its model
(its company, each dimension's value with related fields already followed, each measure's contribution), kept current
by the domain event stream. When a record is created, changed, deleted or restored its facts are recomputed, and so
are the facts of the records that reach it through a dimension's path (a customer's segment changing moves its
invoices' facts). Past 2,000 such records the read model is rebuilt instead.
Queries read the facts when the read model is ready for the current definition, the reader's access is restricted by
company alone and the filter names dimensions only; otherwise they read the records. The figures are the same either
way: the answer's source says which (read-model with the time of its last event, or live). A definition change
rebuilds it in the background, a page of records at a time; queries read the records meanwhile. A filter or measure
condition that names a filter token (such as $today) or an organisation roll-up dimension can't be kept as facts.
| Endpoint | What |
|---|---|
GET /api/admin/analytics/read-models |
Each read model's status, rows, last build and last event (needs metadata.write) |
POST /api/admin/analytics/read-models/:key/rebuild |
Rebuild one from the records |
Data warehouse feed
GET /api/analytics/feed/:key?cursor=&limit= hands on a read model's changes incrementally, oldest first: each row is
a record's dimension values and measure contributions, or its tombstone (deleted: true) when it was deleted or no
longer matches the filter. Store next and pass it as cursor on the next call; the first call starts from the
beginning. Changes are handed on once they are five seconds old, so a slower commit never lands behind your cursor.
Formula measures are not in the feed: compute them from their operands in the warehouse. Use an API key with
records.read; the feed applies its companies and its role's field rights, and refuses a key whose access is
restricted by more than company.
OData for Power BI, Tableau and Excel
/odata/v4 is a read-only OData v4 service: each read model you may read is an entity set (the key with dots and
dashes as _, so sales.revenue is sales_revenue), one entity per record keyed by RecordId, with Company, the
dimensions (a lookup's display text beside it as <key>Label) and the measures' per-record contributions for the tool
to sum. It supports $select, $filter (eq ne gt ge lt le, and, or, parentheses; text in quotes, numbers,
dates, true, false, null), $orderby, $top, $skip, $count=true and @odata.nextLink paging of 1,000.
In Power BI choose Get Data, OData feed, enter https://<your server>/odata/v4, then Basic sign-in with any user name
and an API key as the password. Excel (Data, From OData Feed) and Tableau (OData connector) work the same way.