To allow user to define sources easily, the Front-PS team proposes to create a new /sources
folders where the definition of the sources are setted. This new folder will be encountered inside a new /data
folder that will also contains the /models
. The data
folder and its structure is aimed to split the front / back parts of the application.
└── data
├── models
└── sources
// Firstly, we define the ID of the source
export const KPI_SOURCE_ID = 'kpiSource'
// Use constants for query columns aliases to
// avoid problem with future changes.
export const KPI_SOURCE_COLUMNS = {
NAME: 'name',
REVENUE: 'revenue'
}
// Define the source structure that will be passed to `addSource` reducer
// Remember: it's important to use aliases with the constants defined above.
export const kpiSource = {
id: KPI_SOURCE_ID,
data: `
SELECT
states.name as ${KPI_SOURCE_COLUMNS.NAME},
SUM(stores.revenue) as ${KPI_SOURCE_COLUMNS.REVENUE},
states.the_geom_webmercator
FROM ne_50m_admin_1_states as states
JOIN retail_stores as stores ON ST_Intersects(states.the_geom_webmercator, stores.the_geom_webmercator)
GROUP BY states.name, states.the_geom_webmercator
`,
}
// After the definition of the source, define the widgets properties
// needed to make them work. In this case, we have two widgets:
export const KPI_SOURCE_WIDGETS = {
TOTAL_REVENUE: {
column: KPI_SOURCE_COLUMNS.REVENUE,
operation: AggregationTypes.SUM
},
REVENUE_BY_STATE: {
column: KPI_SOURCE_COLUMNS.NAME,
operationColumn: KPI_SOURCE_COLUMNS.REVENUE,
operation: AggregationTypes.SUM
}
}