forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add resample operator to advanced analytic (apache#1349)
* chore: add resample operator to advanced analytic * wip * minor improvement * wip * fix UT * minor fix * udates * fix type
- Loading branch information
1 parent
538971d
commit 7f65b6f
Showing
6 changed files
with
190 additions
and
1 deletion.
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
43 changes: 43 additions & 0 deletions
43
...rset_ui/superset-ui/packages/superset-ui-chart-controls/src/operators/resampleOperator.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 |
---|---|---|
@@ -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 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
...rset-ui/packages/superset-ui-chart-controls/test/utils/operators/resampleOperator.test.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 |
---|---|---|
@@ -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 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