-
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 18 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
189 changes: 189 additions & 0 deletions
189
packages/react-widgets/__tests__/models/TimeSeriesModel.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
import { getTimeSeries } from '../../src/models/TimeSeriesModel'; | ||
import { AggregationTypes, GroupDateTypes } from '@carto/react-core'; | ||
import { Methods, executeTask } from '@carto/react-workers'; | ||
|
||
const MOCK_WORKER_RESULT = [ | ||
Date.UTC(1970, 0, 1, 0, 0), | ||
Date.UTC(1970, 0, 1, 0, 30), | ||
Date.UTC(1970, 1, 1, 0, 0), | ||
Date.UTC(1970, 1, 1, 0, 30), | ||
Date.UTC(1971, 0, 1, 1, 0) | ||
].map((name) => ({ name, value: 1 })); | ||
|
||
jest.mock('@carto/react-workers', () => ({ | ||
executeTask: jest | ||
.fn() | ||
.mockImplementation(() => new Promise((resolve) => resolve(MOCK_WORKER_RESULT))), | ||
Methods: { | ||
FEATURES_TIME_SERIES: 'featuresTimeSeries' | ||
} | ||
})); | ||
|
||
const mockedExecuteSQL = jest.fn(); | ||
|
||
jest.mock('@carto/react-api', () => ({ | ||
executeSQL: (...args) => mockedExecuteSQL(...args) | ||
})); | ||
|
||
describe('getTimeSeries', () => { | ||
describe('local mode', () => { | ||
test('should work correctly', async () => { | ||
const props = { | ||
source: { | ||
id: '__test__', | ||
type: 'query', | ||
data: 'SELECT * FROM test', | ||
credentials: { | ||
apiVersion: 'v2' | ||
} | ||
}, | ||
operation: AggregationTypes.SUM, | ||
column: 'date_column', | ||
operationColumn: 'opt_column', | ||
stepSize: GroupDateTypes.DAYS | ||
}; | ||
|
||
const data = await getTimeSeries(props); | ||
|
||
expect(data).toBe(MOCK_WORKER_RESULT); | ||
|
||
expect(executeTask).toHaveBeenCalledWith( | ||
props.source.id, | ||
Methods.FEATURES_TIME_SERIES, | ||
{ | ||
filters: props.source.filters, | ||
filtersLogicalOperator: props.source.filtersLogicalOperator, | ||
operation: props.operation, | ||
joinOperation: props.joinOperation, | ||
column: props.column, | ||
operationColumn: props.operationColumn, | ||
stepSize: props.stepSize | ||
} | ||
); | ||
}); | ||
}); | ||
|
||
describe('global mode', () => { | ||
const DEFAULT_PROPS = { | ||
source: { | ||
id: '__test__', | ||
type: 'table', | ||
data: '__test__', | ||
credentials: { | ||
apiVersion: 'v3', | ||
accessToken: '__test_token__' | ||
}, | ||
connection: '__test_connection__' | ||
}, | ||
operation: AggregationTypes.COUNT, | ||
column: 'date_column', | ||
operationColumn: 'opt_column', | ||
global: true | ||
}; | ||
|
||
describe('no-week stepSize', () => { | ||
const MOCK_API_RESULT = [ | ||
{ | ||
['_agg_' + GroupDateTypes.YEARS]: 1970, | ||
['_agg_' + GroupDateTypes.MONTHS]: 1, | ||
value: 2 | ||
}, | ||
{ | ||
['_agg_' + GroupDateTypes.YEARS]: 1970, | ||
['_agg_' + GroupDateTypes.MONTHS]: 2, | ||
value: 3 | ||
} | ||
]; | ||
|
||
const RESULT = [ | ||
{ name: Date.UTC(1970, 0, 1, 0, 0), value: 2 }, | ||
{ name: Date.UTC(1970, 1, 1, 0, 0), value: 3 } | ||
]; | ||
|
||
test('should work correctly', async () => { | ||
mockedExecuteSQL.mockImplementation( | ||
() => new Promise((resolve) => resolve(MOCK_API_RESULT)) | ||
); | ||
|
||
const props = { | ||
...DEFAULT_PROPS, | ||
stepSize: GroupDateTypes.MONTHS | ||
}; | ||
|
||
const data = await getTimeSeries(props); | ||
|
||
expect(data).toEqual(RESULT); | ||
|
||
expect(mockedExecuteSQL).toHaveBeenCalledWith({ | ||
credentials: props.source.credentials, | ||
query: `SELECT extract(year from cast(date_column as timestamp)) as _agg_year,extract(month from cast(date_column as timestamp)) as _agg_month,count(*) as value FROM __test__ GROUP BY _agg_year,_agg_month ORDER BY _agg_year,_agg_month`, | ||
connection: props.source.connection, | ||
opts: { | ||
abortController: undefined | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
describe('week stepSize', () => { | ||
const MOCK_API_RESULT = [ | ||
{ | ||
['_agg_' + GroupDateTypes.YEARS]: 1970, | ||
['_agg_' + GroupDateTypes.MONTHS]: 1, | ||
['_agg_' + GroupDateTypes.DAYS]: 1, | ||
value: 2, | ||
_grouped_count: 10 | ||
}, | ||
{ | ||
['_agg_' + GroupDateTypes.YEARS]: 1970, | ||
['_agg_' + GroupDateTypes.MONTHS]: 1, | ||
['_agg_' + GroupDateTypes.DAYS]: 2, | ||
value: 3, | ||
_grouped_count: 20 | ||
} | ||
]; | ||
|
||
const RESULTS = { | ||
[AggregationTypes.AVG]: [{ name: -259200000, value: 2.6666666666666665 }], | ||
[AggregationTypes.SUM]: [{ name: -259200000, value: 5 }], | ||
[AggregationTypes.MIN]: [{ name: -259200000, value: 2 }], | ||
[AggregationTypes.MAX]: [{ name: -259200000, value: 3 }], | ||
[AggregationTypes.COUNT]: [{ name: -259200000, value: 5 }] | ||
}; | ||
|
||
const RESULTS_QUERIES = { | ||
[AggregationTypes.AVG]: `SELECT extract(year from cast(date_column as timestamp)) as _agg_year,extract(month from cast(date_column as timestamp)) as _agg_month,extract(day from cast(date_column as timestamp)) as _agg_day,avg(opt_column) as value,count(*) as _grouped_count FROM __test__ GROUP BY _agg_year,_agg_month,_agg_day ORDER BY _agg_year,_agg_month,_agg_day`, | ||
[AggregationTypes.SUM]: `SELECT extract(year from cast(date_column as timestamp)) as _agg_year,extract(month from cast(date_column as timestamp)) as _agg_month,extract(day from cast(date_column as timestamp)) as _agg_day,sum(opt_column) as value FROM __test__ GROUP BY _agg_year,_agg_month,_agg_day ORDER BY _agg_year,_agg_month,_agg_day`, | ||
[AggregationTypes.MIN]: `SELECT extract(year from cast(date_column as timestamp)) as _agg_year,extract(month from cast(date_column as timestamp)) as _agg_month,extract(day from cast(date_column as timestamp)) as _agg_day,min(opt_column) as value FROM __test__ GROUP BY _agg_year,_agg_month,_agg_day ORDER BY _agg_year,_agg_month,_agg_day`, | ||
[AggregationTypes.MAX]: `SELECT extract(year from cast(date_column as timestamp)) as _agg_year,extract(month from cast(date_column as timestamp)) as _agg_month,extract(day from cast(date_column as timestamp)) as _agg_day,max(opt_column) as value FROM __test__ GROUP BY _agg_year,_agg_month,_agg_day ORDER BY _agg_year,_agg_month,_agg_day`, | ||
[AggregationTypes.COUNT]: `SELECT extract(year from cast(date_column as timestamp)) as _agg_year,extract(month from cast(date_column as timestamp)) as _agg_month,extract(day from cast(date_column as timestamp)) as _agg_day,count(*) as value FROM __test__ GROUP BY _agg_year,_agg_month,_agg_day ORDER BY _agg_year,_agg_month,_agg_day` | ||
}; | ||
|
||
test('should work correctly', () => { | ||
mockedExecuteSQL.mockImplementation( | ||
() => new Promise((resolve) => resolve(MOCK_API_RESULT)) | ||
); | ||
|
||
const props = { | ||
...DEFAULT_PROPS, | ||
stepSize: GroupDateTypes.WEEKS | ||
}; | ||
|
||
Object.keys(RESULTS).forEach((operation) => { | ||
getTimeSeries({ ...props, operation }).then((data) => | ||
expect(data).toEqual(RESULTS[operation]) | ||
); | ||
|
||
expect(mockedExecuteSQL).toHaveBeenCalledWith({ | ||
credentials: props.source.credentials, | ||
query: RESULTS_QUERIES[operation], | ||
connection: props.source.connection, | ||
opts: { | ||
abortController: undefined | ||
} | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
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 :(