This repository was archived by the owner on Dec 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 265
feat: add resample operator to advanced analytic #1349
Merged
zhaoyongjie
merged 8 commits into
apache-superset:master
from
zhaoyongjie:resample_operator
Sep 17, 2021
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cc30919
chore: add resample operator to advanced analytic
zhaoyongjie 33e9a0d
wip
zhaoyongjie b28f411
minor improvement
zhaoyongjie 6e4c942
wip
zhaoyongjie 7452ba2
fix UT
zhaoyongjie c8c9360
minor fix
zhaoyongjie 4b5d7a9
udates
zhaoyongjie cdebdb6
fix type
zhaoyongjie 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
This file contains hidden or 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
43 changes: 43 additions & 0 deletions
43
packages/superset-ui-chart-controls/src/operators/resampleOperator.ts
This file contains hidden or 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,43 @@ | ||
| /* eslint-disable camelcase */ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitationsxw | ||
| * under the License. | ||
| */ | ||
| import { PostProcessingResample } from '@superset-ui/core'; | ||
| import { PostProcessingFactory } from './types'; | ||
| import { TIME_COLUMN } from './utils'; | ||
|
|
||
| export const resampleOperator: PostProcessingFactory<PostProcessingResample | undefined> = ( | ||
| formData, | ||
| queryObject, | ||
| ) => { | ||
| const resampleZeroFill = formData.resample_method === 'zerofill'; | ||
| const resampleMethod = resampleZeroFill ? 'asfreq' : formData.resample_method; | ||
| const resampleRule = formData.resample_rule; | ||
| if (resampleMethod && resampleRule) { | ||
| return { | ||
| operation: 'resample', | ||
| options: { | ||
| method: resampleMethod, | ||
| rule: resampleRule, | ||
| fill_value: resampleZeroFill ? 0 : null, | ||
| time_column: TIME_COLUMN, | ||
| }, | ||
| }; | ||
| } | ||
| return undefined; | ||
| }; |
This file contains hidden or 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
88 changes: 88 additions & 0 deletions
88
packages/superset-ui-chart-controls/test/utils/operators/resampleOperator.test.ts
This file contains hidden or 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,88 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| import { QueryObject, SqlaFormData } from '@superset-ui/core'; | ||
| import { resampleOperator } from '../../../src'; | ||
|
|
||
| const formData: SqlaFormData = { | ||
| metrics: ['count(*)', { label: 'sum(val)', expressionType: 'SQL', sqlExpression: 'sum(val)' }], | ||
| time_range: '2015 : 2016', | ||
| granularity: 'month', | ||
| datasource: 'foo', | ||
| viz_type: 'table', | ||
| }; | ||
| const queryObject: QueryObject = { | ||
| metrics: ['count(*)', { label: 'sum(val)', expressionType: 'SQL', sqlExpression: 'sum(val)' }], | ||
| time_range: '2015 : 2016', | ||
| granularity: 'month', | ||
| post_processing: [ | ||
| { | ||
| operation: 'pivot', | ||
| options: { | ||
| index: ['__timestamp'], | ||
| columns: ['nation'], | ||
| aggregates: { | ||
| 'count(*)': { | ||
| operator: 'sum', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| describe('resampleOperator', () => { | ||
| it('should skip resampleOperator', () => { | ||
| expect(resampleOperator(formData, queryObject)).toEqual(undefined); | ||
| expect(resampleOperator({ ...formData, resample_method: 'ffill' }, queryObject)).toEqual( | ||
| undefined, | ||
| ); | ||
| expect(resampleOperator({ ...formData, resample_rule: '1D' }, queryObject)).toEqual(undefined); | ||
| }); | ||
|
|
||
| it('should do resample', () => { | ||
| expect( | ||
| resampleOperator({ ...formData, resample_method: 'ffill', resample_rule: '1D' }, queryObject), | ||
| ).toEqual({ | ||
| operation: 'resample', | ||
| options: { | ||
| method: 'ffill', | ||
| rule: '1D', | ||
| fill_value: null, | ||
| time_column: '__timestamp', | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('should do zerofill resample', () => { | ||
| expect( | ||
| resampleOperator( | ||
| { ...formData, resample_method: 'zerofill', resample_rule: '1D' }, | ||
| queryObject, | ||
| ), | ||
| ).toEqual({ | ||
| operation: 'resample', | ||
| options: { | ||
| method: 'asfreq', | ||
| rule: '1D', | ||
| fill_value: 0, | ||
| time_column: '__timestamp', | ||
| }, | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.