-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Global mode to TimeSeriesWidget #377
Merged
VictorVelarde
merged 19 commits into
master
from
feature/sc-219452/timeserieswidget-global-remote-mode
Apr 20, 2022
Merged
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
123bd1a
FormulaWidget remote mode for global [sc-219448]
2ea547a
Fixed bugs and make improvements from review
0d311a2
Fix multiple columns join aggregation
f152a42
Improvements in model checking
704ea47
Remove SAFE_ADD to keep compatibility between C3 providers
abe7f80
Fix global param
2aa39f6
Add tests
76b5382
Merge branch 'master' into feature/sc-219448/formulawidget-remote-mod…
383d64a
Update packages/react-widgets/__tests__/models/utils.test.js
6cbd542
Update utils.test.js
0ef4f93
Improvement in utils test
64dc004
Merge branch 'master' into feature/sc-219448/formulawidget-remote-mod…
6fc571f
Update CHANGELOG.md
b37a86a
Update CHANGELOG.md
4a6fbdb
Add Global mode to CategoryWidget & PieWidget (#370)
a18f05f
Time Series global
16aeee9
Merge branch 'master' of github.com:CartoDB/carto-react into feature/…
77c35e7
Add tests
e761718
Update CHANGELOG.md
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
12 changes: 6 additions & 6 deletions
12
packages/react-core/src/operations/constants/GroupDateTypes.d.ts
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,8 +1,8 @@ | ||
export enum GroupDateTypes { | ||
YEARS = 'years', | ||
MONTHS = 'months', | ||
WEEKS = 'weeks', | ||
DAYS = 'days', | ||
HOURS = 'hours', | ||
MINUTES = 'minutes' | ||
YEARS = 'year', | ||
MONTHS = 'month', | ||
WEEKS = 'week', | ||
DAYS = 'day', | ||
HOURS = 'hour', | ||
MINUTES = 'minute' | ||
} |
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,212 @@ | ||
import { executeSQL } from '@carto/react-api/'; | ||
import { | ||
AggregationTypes, | ||
getMonday, | ||
GroupDateTypes, | ||
groupValuesByDateColumn | ||
} from '@carto/react-core/'; | ||
import { Methods, executeTask } from '@carto/react-workers'; | ||
import { | ||
formatOperationColumn, | ||
formatTableNameWithFilters, | ||
wrapModelCall | ||
} from './utils'; | ||
|
||
export const getTimeSeries = async (props) => { | ||
const { | ||
filters, | ||
dataSource, | ||
column, | ||
operationColumn, | ||
joinOperation, | ||
operation, | ||
stepSize | ||
} = props; | ||
export function getTimeSeries(props) { | ||
return wrapModelCall(props, fromLocal, fromRemote); | ||
} | ||
|
||
return executeTask(dataSource, Methods.FEATURES_TIME_SERIES, { | ||
filters, | ||
// From local | ||
function fromLocal({ | ||
source, | ||
column, | ||
operationColumn, | ||
joinOperation, | ||
operation, | ||
stepSize | ||
}) { | ||
return executeTask(source.id, Methods.FEATURES_TIME_SERIES, { | ||
filters: source.filters, | ||
filtersLogicalOperator: source.filtersLogicalOperator, | ||
column, | ||
stepSize, | ||
operationColumn: operationColumn || column, | ||
joinOperation, | ||
operation | ||
}); | ||
} | ||
|
||
// From remote | ||
const STEP_SIZE_TO_GROUP_QUERY_MAP = { | ||
[GroupDateTypes.YEARS]: [GroupDateTypes.YEARS], | ||
[GroupDateTypes.MONTHS]: [GroupDateTypes.YEARS, GroupDateTypes.MONTHS], | ||
[GroupDateTypes.DAYS]: [ | ||
GroupDateTypes.YEARS, | ||
GroupDateTypes.MONTHS, | ||
GroupDateTypes.DAYS | ||
], | ||
[GroupDateTypes.HOURS]: [ | ||
GroupDateTypes.YEARS, | ||
GroupDateTypes.MONTHS, | ||
GroupDateTypes.DAYS, | ||
GroupDateTypes.HOURS | ||
], | ||
[GroupDateTypes.MINUTES]: [ | ||
GroupDateTypes.YEARS, | ||
GroupDateTypes.MONTHS, | ||
GroupDateTypes.DAYS, | ||
GroupDateTypes.HOURS, | ||
GroupDateTypes.MINUTES | ||
] | ||
}; | ||
|
||
const FORMAT_NAME_BY_STEP_SIZE = { | ||
[GroupDateTypes.YEARS]: (row) => | ||
Date.UTC(row[formatStepSizeColumn(GroupDateTypes.YEARS)], 0), | ||
[GroupDateTypes.MONTHS]: (row) => | ||
Date.UTC( | ||
row[formatStepSizeColumn(GroupDateTypes.YEARS)], | ||
row[formatStepSizeColumn(GroupDateTypes.MONTHS)] - 1 | ||
), | ||
[GroupDateTypes.DAYS]: (row) => | ||
Date.UTC( | ||
row[formatStepSizeColumn(GroupDateTypes.YEARS)], | ||
row[formatStepSizeColumn(GroupDateTypes.MONTHS)] - 1, | ||
row[formatStepSizeColumn(GroupDateTypes.DAYS)] | ||
), | ||
[GroupDateTypes.HOURS]: (row) => | ||
Date.UTC( | ||
row[formatStepSizeColumn(GroupDateTypes.YEARS)], | ||
row[formatStepSizeColumn(GroupDateTypes.MONTHS)] - 1, | ||
row[formatStepSizeColumn(GroupDateTypes.DAYS)], | ||
row[formatStepSizeColumn(GroupDateTypes.HOURS)] | ||
), | ||
[GroupDateTypes.MINUTES]: (row) => | ||
Date.UTC( | ||
row[formatStepSizeColumn(GroupDateTypes.YEARS)], | ||
row[formatStepSizeColumn(GroupDateTypes.MONTHS)] - 1, | ||
row[formatStepSizeColumn(GroupDateTypes.DAYS)], | ||
row[formatStepSizeColumn(GroupDateTypes.HOURS)], | ||
row[formatStepSizeColumn(GroupDateTypes.MINUTES)] | ||
) | ||
}; | ||
|
||
function groupWeeklyAverageValuesByDateColumn(data) { | ||
const groups = data.reduce((acc, item) => { | ||
const value = item.name; | ||
const formattedValue = new Date(value); | ||
const groupKey = getMonday(formattedValue); | ||
|
||
if (!isNaN(groupKey)) { | ||
let groupedValues = acc.get(groupKey); | ||
if (!groupedValues) { | ||
groupedValues = []; | ||
acc.set(groupKey, groupedValues); | ||
} | ||
|
||
const aggregatedValue = item.value * item._grouped_count; | ||
|
||
const isValid = aggregatedValue !== null && aggregatedValue !== undefined; | ||
|
||
if (isValid) { | ||
groupedValues.push([aggregatedValue, item._grouped_count]); | ||
acc.set(groupKey, groupedValues); | ||
} | ||
} | ||
|
||
return acc; | ||
}, new Map()); | ||
|
||
return [...groups.entries()].map(([name, value]) => ({ | ||
name, | ||
value: | ||
value.filter((_, idx) => idx % 2 === 0).reduce((acc, [val]) => acc + val, 0) / | ||
value.filter((_, idx) => idx % 2 !== 0).reduce((acc, [val]) => acc + val, 0) | ||
})); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This final part is a bit obscure, can you comment (or use some better variable names)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree. I'm going to improve it now. |
||
} | ||
|
||
// In order to keep compatibility with the different providers, we group the data in the query using | ||
// multiple extract fns. After that, we must build the date using the resulting _agg_* columns | ||
function formatRemoteData(data, props) { | ||
const { stepSize, operation } = props; | ||
|
||
const dateFormatter = | ||
FORMAT_NAME_BY_STEP_SIZE[ | ||
stepSize === GroupDateTypes.WEEKS ? GroupDateTypes.DAYS : stepSize | ||
]; | ||
|
||
const formattedData = data.map((row) => ({ | ||
value: row.value, | ||
name: dateFormatter(row), | ||
_grouped_count: row._grouped_count | ||
})); | ||
|
||
// For weeks, each provider behavies differently, so we need to group the data in the local | ||
if (stepSize === GroupDateTypes.WEEKS) { | ||
// For any other AggregationTypes it's easy, but AVG should be made using a pondered average | ||
if (operation === AggregationTypes.AVG) { | ||
return groupWeeklyAverageValuesByDateColumn(formattedData); | ||
} else { | ||
return groupValuesByDateColumn({ | ||
data: formattedData, | ||
keysColumn: 'name', | ||
valuesColumns: ['value'], | ||
operation: stepSize === AggregationTypes.COUNT ? AggregationTypes.SUM : stepSize | ||
}); | ||
} | ||
} else { | ||
return formattedData; | ||
} | ||
} | ||
|
||
async function fromRemote(props) { | ||
const { source, abortController } = props; | ||
const { credentials, connection } = source; | ||
|
||
const query = buildSqlQueryToGetTimeSeries(props); | ||
|
||
const data = await executeSQL({ | ||
credentials, | ||
query, | ||
connection, | ||
opts: { abortController } | ||
}); | ||
|
||
return formatRemoteData(data, props); | ||
} | ||
|
||
function buildSqlQueryToGetTimeSeries(props) { | ||
const { column, operation, operationColumn, joinOperation, stepSize } = props; | ||
|
||
const stepSizesToGroupBy = | ||
STEP_SIZE_TO_GROUP_QUERY_MAP[ | ||
stepSize === GroupDateTypes.WEEKS ? GroupDateTypes.DAYS : stepSize | ||
]; | ||
|
||
if (!stepSizesToGroupBy) throw new Error(`${stepSize} not supported`); | ||
|
||
const selectClause = stepSizesToGroupBy | ||
.map((_stepSize) => { | ||
return `extract(${_stepSize} from cast(${column} as timestamp)) as ${formatStepSizeColumn( | ||
_stepSize | ||
)}`; | ||
}) | ||
.join(); | ||
|
||
const selectValueClause = `${operation}(${ | ||
operation === AggregationTypes.COUNT | ||
? '*' | ||
: formatOperationColumn(operationColumn || column, joinOperation) | ||
}) as value`; | ||
|
||
const byColumns = stepSizesToGroupBy.map(formatStepSizeColumn).join(); | ||
|
||
return `SELECT ${selectClause}, ${selectValueClause}, COUNT(*) as _grouped_count FROM ${formatTableNameWithFilters( | ||
props | ||
)} GROUP BY ${byColumns} ORDER BY ${byColumns}`.trim(); | ||
} | ||
|
||
// Aux | ||
function formatStepSizeColumn(stepSize) { | ||
return `_agg_${stepSize}`; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of curiosity, why this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because in all the providers, these operations are singular :(