-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Global mode to CategoryWidget & PieWidget (#370)
- Loading branch information
Sergio Clebal
authored
Apr 19, 2022
1 parent
b37a86a
commit 4a6fbdb
Showing
9 changed files
with
313 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 76 additions & 35 deletions
111
packages/react-widgets/__tests__/models/CategoryModel.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 81 additions & 22 deletions
103
packages/react-widgets/__tests__/models/HistogramModel.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,55 @@ | ||
import { executeSQL } from '@carto/react-api/'; | ||
import { AggregationTypes } from '@carto/react-core/'; | ||
import { Methods, executeTask } from '@carto/react-workers'; | ||
import { | ||
formatOperationColumn, | ||
formatTableNameWithFilters, | ||
wrapModelCall | ||
} from './utils'; | ||
|
||
export const getCategories = async (props) => { | ||
const { | ||
column, | ||
operationColumn, | ||
operation, | ||
joinOperation, | ||
filters, | ||
filtersLogicalOperator, | ||
dataSource | ||
} = props; | ||
|
||
return executeTask(dataSource, Methods.FEATURES_CATEGORY, { | ||
filters, | ||
filtersLogicalOperator, | ||
export function getCategories(props) { | ||
return wrapModelCall(props, fromLocal, fromRemote); | ||
} | ||
|
||
// From local | ||
function fromLocal(props) { | ||
const { source, column, operationColumn, operation, joinOperation } = props; | ||
|
||
return executeTask(source.id, Methods.FEATURES_CATEGORY, { | ||
filters: source.filters, | ||
filtersLogicalOperator: source.filtersLogicalOperator, | ||
operation, | ||
joinOperation, | ||
column, | ||
operationColumn: operationColumn || column | ||
}); | ||
}; | ||
} | ||
|
||
// From remote | ||
function fromRemote(props) { | ||
const { source, abortController } = props; | ||
const { credentials, connection } = source; | ||
|
||
const query = buildSqlQueryToGetCategories(props); | ||
|
||
return executeSQL({ | ||
credentials, | ||
query, | ||
connection, | ||
opts: { abortController } | ||
}); | ||
} | ||
|
||
function buildSqlQueryToGetCategories(props) { | ||
const { column, operation, operationColumn, joinOperation } = props; | ||
|
||
const selectValueClause = `${operation}(${ | ||
operation === AggregationTypes.COUNT | ||
? '*' | ||
: formatOperationColumn(operationColumn || column, joinOperation) | ||
}) as value`; | ||
|
||
return `SELECT COALESCE(${column}, 'null') as name, ${selectValueClause} FROM ${formatTableNameWithFilters( | ||
props | ||
)} GROUP BY ${column}`.trim(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,54 @@ | ||
import { executeSQL } from '@carto/react-api'; | ||
import { Methods, executeTask } from '@carto/react-workers'; | ||
import { formatTableNameWithFilters, wrapModelCall } from './utils'; | ||
|
||
export const getHistogram = async (props) => { | ||
const { column, operation, ticks, filters, filtersLogicalOperator, dataSource } = props; | ||
export function getHistogram(props) { | ||
return wrapModelCall(props, fromLocal, fromRemote); | ||
} | ||
|
||
return executeTask(dataSource, Methods.FEATURES_HISTOGRAM, { | ||
filters, | ||
filtersLogicalOperator, | ||
// From local | ||
function fromLocal(props) { | ||
const { source, column, operation, ticks } = props; | ||
|
||
return executeTask(source.id, Methods.FEATURES_HISTOGRAM, { | ||
filters: source.filters, | ||
filtersLogicalOperator: source.filtersLogicalOperator, | ||
operation, | ||
column, | ||
ticks | ||
}); | ||
}; | ||
} | ||
|
||
// From remote | ||
async function fromRemote(props) { | ||
const { source, ticks, abortController } = props; | ||
const { credentials, connection } = source; | ||
|
||
const query = buildSqlQueryToGetHistogram(props); | ||
|
||
const data = await executeSQL({ | ||
credentials, | ||
query, | ||
connection, | ||
opts: { abortController } | ||
}); | ||
|
||
const result = Array(ticks.length + 1).fill(0); | ||
data.forEach(({ tick, value }) => (result[tick] = value)); | ||
|
||
return result; | ||
} | ||
|
||
function buildSqlQueryToGetHistogram(props) { | ||
const { column, operation, ticks } = props; | ||
|
||
const caseTicks = ticks.map((t, index) => `WHEN ${column} < ${t} THEN ${index}`); | ||
caseTicks.push(`ELSE ${ticks.length}`); | ||
|
||
const selectValueClause = `${operation}(${column}) as value`; | ||
const selectTickClause = `CASE ${caseTicks.join(' ')} END as tick`; | ||
|
||
const subQuery = `SELECT ${selectTickClause}, ${column} FROM ${formatTableNameWithFilters(props)}`; | ||
|
||
return `SELECT tick, ${selectValueClause} FROM (${subQuery}) q GROUP BY tick`; | ||
} |
Oops, something went wrong.