Config Builder
The Config Builder is a tool for developers. It requires knowing the store's database tables and writing SQL expressions. You don't need it to view, edit or create reports: the Report builder covers that. Grant the Config Builder permission only to roles that should change the report schema.
This section provides an overview of the Config Builder and explains the structure of a report config.
Every report reads its data from the report schema. The schema lists the datasets reports may read (database tables and their columns) and the explores, which define how those datasets join. The schema shipped with the extension covers orders, order items, invoices, payments, taxes, addresses, carts, products, categories, stock and customers. That is enough for most stores, but many stores keep important sales, marketing or inventory data in their own tables or in the tables of 3rd-party extensions.
The Config Builder lets you extend the schema without changing code. With a config, you can:
- add calculated columns with your own SQL expressions;
- register tables from a 3rd-party or custom extension;
- join those tables to the native Magento tables, so their columns become available in the Report builder;
- change the label, format or visibility of an existing column.
To open it, go to Reports -> Advanced Reports -> Reports and click Config Builder above the reports list. The button is shown to admin roles with the Config Builder permission (see How to restrict access to a specific report).
Configs list
The Config Builder page lists the saved configs with the following columns:
- ID and Title of the config.
- Status:
- Valid: the config is loaded into the report schema.
- Not loading: the config is saved but skipped when the schema is built. See Troubleshooting.
- Invalid JSON: the config text cannot be parsed.
- Datasets, Explores and Columns: what the config declares.
Use the Edit and Delete icons in a row to change or remove a config. Use the search field and the Status, Dataset and Explore filters to find a config. Click Add New Config to create a new one.
Config editor
The config editor contains the following elements:
- Title: the name of the config in the configs list. It is required. If another config already uses the same title, the check shows a warning.
- Insert: adds a ready-made block at a suitable place in the config.
- Templates: Empty config, New dataset and New explore.
- Examples: Calculated money column, Register a table, Join two datasets and Override an EAV attribute.
- Format JSON: re-indents the config.
- Check against schema: checks the config against the full report schema without saving it. Errors are listed under This config would not load, and possible mistakes under Worth a look. For a valid config, the check also lists what the config changes, for example the datasets, columns and relationships it adds or overrides.
- The JSON editor. The line under it shows how many errors and warnings the editor found in the text.
The panel next to the editor has three tabs:
- Schema: browse the datasets and columns of the current schema. Click a column to insert its
dataset.columnreference into the config. - Test: run the
exprof a column over 10 rows of your database, so a mistake shows up before the column is used in a report. Expressions on EAV datasets (products, categories, customers and customer addresses) cannot be tested here. Save the config and check the column in a report instead. - Reference: a short summary of the config format.
Click Save to store the config. The config is checked again on save, and a config that would not load is not saved. A saved config takes effect right away: the extension cleans the cached report schema itself.
The structure of a config
A config is a JSON document with two top-level keys:
{
"datasets": [],
"explores": []
}
datasets: the tables reports may read, with their columns.explores: the join graphs. A report is built on one explore and can only use the columns of the datasets that explore joins.
Configs are merged with the schema shipped by the extension, and with each other, by name. Saved configs are applied after the shipped schema. So a config can add a column to a dataset the extension declares, or override the settings of an existing column, by using the same names.
Datasets
| Key | Description | Required |
|---|---|---|
name | The dataset name. Column references use it as name.column. To add columns to an existing dataset, use its name, for example sales_order. | required |
sql_table_name | The database table, without the table prefix. For example, if your tables use the prefix m2_ and the table is m2_sales_order, enter sales_order. Leave it out when you only add columns to an existing dataset. | for a new dataset |
columns | The columns of the dataset. See Columns. | for a new dataset |
label | The name of the dataset in the column picker. A dataset without a label is hidden from the pickers, unless hidden is set to false. | optional |
group | The group the dataset belongs to, for example Sales, Products or Customers. | optional |
eav_entity_type | Marks an EAV entity: customer, customer_address, catalog_product or catalog_category. | optional |
connection | The database connection: default, sales or checkout. The shipped datasets use sales for order tables and checkout for cart tables. Defaults to default. | optional |
hidden | true keeps the dataset and all its columns out of the pickers. | optional |
remove | true removes a dataset declared elsewhere. See Removing datasets, columns and relationships. | optional |
A dataset with eav_entity_type discovers every attribute of its entity type by itself, with the attribute's label, type and options. In its columns, list only the attributes whose label, type or options you want to override.
Columns
| Key | Description | Required |
|---|---|---|
name | The column name. Without expr, it must be the real database column or EAV attribute code. | required |
kind | dimension (rows are grouped by it) or measure (its values are summarized). Reports group by every dimension column they show, so changing the kind of a column changes every report that shows it. | required |
type | The data type the column returns: string, number, boolean, date, datetime, time or json. Defaults to string. | optional |
value_format | How the value is displayed. See the table below. | optional |
aggregation | For measures only: sum, avg, count, concat or none. | optional |
label | The column name shown in reports. Defaults to name. | optional |
group | The group the column is listed under in the column picker. | optional |
expr | An SQL expression that calculates the value. Every column reference must be fully qualified as dataset.column, for example sales_order.base_grand_total. A reference to another dataset of the explore joins that dataset. | optional |
store_expr | For money columns: the expression used when the report is filtered by a single store, so the value skips currency conversion. | optional |
source_model | The PHP class that provides the options for select and country values. | optional |
timeframes | For date and datetime dimensions: the periods the column can be grouped by, any of dayOfWeek, hourOfDay, date, week, month, quarter and year. | optional |
primary_key | true marks the key column of the dataset. | optional |
unique | true marks a business-unique value, such as a SKU, an email or an order number. | optional |
hidden | true keeps the column out of the pickers. Reports that already show the column keep working. | optional |
attribute_code | For EAV datasets: the attribute to read when name differs from the attribute code. Ignored when expr is set. | optional |
remove | true removes a column declared elsewhere. | optional |
Values of value_format:
| Value | Description |
|---|---|
money | Amount with the currency symbol. Use it with the number type. |
percent | Value with the % symbol. |
number | Number. |
qty | Quantity. |
select | Value with predefined options. Set source_model to show labels instead of raw codes. |
country | Country. For values in the ISO 3166-1 alpha-2 format, use Mirasvit\AdvancedReports\Schema\Type\Options\Directory\Country as source_model. |
store | Store view. The labels come from Magento, so source_model is not needed. |
html | Value output as is. |
Explores
The extension ships two explores: sales, which starts from orders (sales_order), and cart, which starts from carts (quote). To join a new dataset, add a relationship to the explore whose reports should use it.
| Key | Description | Required |
|---|---|---|
name | The explore name. Use sales or cart to add relationships to a shipped explore. | required |
base | The dataset reports on this explore start from. Needed for a new explore only. | for a new explore |
label | The name of the explore. | optional |
relationships | The joins between datasets. | required |
Each relationship has the following keys:
| Key | Description | Required |
|---|---|---|
join | The two datasets to join, for example ["sales_order_item", "sales_order"]. | required |
condition | The SQL join condition, with fully qualified references, for example sales_order_item.order_id = sales_order.entity_id. | required |
type | 11: one row of a dataset matches one row of the other. 1n: one row of the second dataset matches many rows of the first, so list the "many" side first. | required |
name | A name for the relationship, used in error messages and in the result of Check against schema. Recommended format: left-right. | optional |
remove | true removes a relationship declared elsewhere. | optional |
The relationships of an explore must form a tree: there must be exactly one path between any two of its datasets. A config that adds a second path is rejected. If you need the same table joined in another way, declare a separate dataset over the same table (with its own name and the same sql_table_name), or use a separate explore.
Removing datasets, columns and relationships
"remove": true next to a name (or next to the join of an unnamed relationship) removes that item, even if the shipped schema or another config declares it. Don't combine it with other keys of the item.
Removal does not cascade. If you remove a dataset, also remove the relationships that join it, or the config will not load. A saved report that uses a removed column stops opening, and Check against schema names such reports before you save. To only stop offering a column in the pickers, use "hidden": true instead.
Find ready-to-use configs on the Examples of configs page.
Troubleshooting
A config that cannot be loaded, for example one that joins a dataset no other config declares any longer, is skipped. The rest of the schema keeps working. In this case:
- the Advanced Reports pages show the warning Report Builder config(s) could not be loaded - some columns or relations may be missing from reports, with a link to each broken config and the reason;
- the config has the Not loading status in the configs list;
- the config editor shows the reason under This config is not loading.
Open the config, fix it and click Save. Use Check against schema to see what is wrong before saving.