Dynamic Data Sources
We are kicking off a beta and would love for you to be involved! Sign up here if you want to join in.
Use Case
In some scenario, you want to point the dashboards to different data source (database or data warehouse) dynamically, based on who's viewing the report or whether it's in production or dev mode.
Thanks to its programmable nature, Holistics can support this capability natively. This will enable popular use cases such as:
- Clients Dashboarding: Build the same set of models/datasets/dashboards for clients but different data source for each client underneath.
- Dev/Prod environment: Dynamically switch the underlying data sources (from dev → prod and vice versa) based on the environment that analysts are working on
- Dynamic data sources for embedded analytics: Embedded analytics but different customers use different databases.
Approach
In Holistics, you can specify a function/expression in the data source definition (of dataset or data model).
// In a dataset
Dataset sales {
models: [ orders ]
data_source_name: function_or_expression_here
}
// Or in a data model
Model orders {
data_source_name: function_or_expression_here
dimension order_id { ... }
dimension user_id { ... }
...
}
The below example use an "if expression" to tell Holistics to use different database when doing development vs when deployed.
Dataset sales {
label: 'Dynamic Client Dataset'
models: [ ... ]
relationships: [ ... ]
data_source_name:
if (H.git.current_branch == 'master') {
'production'
} else if (H.git.current_branch == 'staging') {
'staging'
} else {
'develop'
}
}
Notes:
- Ensure data sources connected: You need to make sure you have defined the 3 data sources named
production
,staging
, anddevelop
. - Run-time evaluation: The expression is evaluated at run-time (i.e when dashboard is viewed, or queries are generated to send to database.
data_source_name
is only available when defining data model or dataset, not dashboard. The dataset's data source will override whatever defined in data model.
Available Variables
While not exhaustive, here are some common system variables you can use:
User Attributes
H.current_user.data_source
→ this will return thedata_source
user attribute value of the current user. You can use this to set data source dynamically by the user's viewing the dashboard.- Currently, only
H.current_user.data_source
attribute is supported. We plan to support more attributes in the future. - Note that you need to define this user attribute first in the Users Management (/manage/users) before you can use it.
Holistics built-in variables
H.git.current_branch
→ this will return the name of the current branchH.git.is_production
→ this will returntrue
when in Reporting tab, or in Development tab with production mode enabled.
For example, this is a sample setup that:
- In Production: use the
data_source
user attribute value of the current user - In Development: use the data source
develop
Dataset sales {
data_source_name:
if (H.git.current_branch == 'master') {
H.current_user.data_source
} else {
'develop'
}
}
Example: Dynamical Data Source at User Level
Suppose you have many different customers who want the same set of reports. You maintain different data source for each customer. You want each customer when logging in to Holistics will be able to see the same reports but pointing to their respective database.
Here's how you can utilize user attributes in Holistics to achive that.
1. Connect databases
First, connect to all of your customers' databases.
2. Define new user attribute & set values for each user
Go to Users (or Group Management) in Holistics, define a new attribute name data_source
. You can do this either at the user level, or usergroup's level.
Once done, to go each user and set the corresponding data_source
value for them. For more information, refer to User Attributes
3. Write dynamic code to set data source
Dataset dynamic_client_dataset {
label: 'Dynamic Client Dataset'
// The underlying data source will be dynamically switched based on who use it
data_source_name: H.current_user.data_source
models: [ revenue ]
relationships: [ ]
}
4. Making sure that it works as expected
To make sure your setup work correctly, go to "Organization Settings > View and Edit as" under App Settings to test with each account. For more details, please refer to Log In As Another User
By selecting a particular user or user group, the corresponding value of their user attribute (in this case, the data source name) will be applied to the data_source_name
property of the dataset (and model). This allows the same dashboards to display different data for different users.
Please be aware that testing this setup in the Development environment is not possible, particularly for users without access to the Modeling layer (such as Explorers or Viewers).
Therefore, you must first publish your changes to the Production environment before utilizing the "View and Edit as" option.