-
Notifications
You must be signed in to change notification settings - Fork 14.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f06db79
commit 3c28cd4
Showing
12 changed files
with
512 additions
and
71 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
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
89 changes: 89 additions & 0 deletions
89
superset-frontend/packages/superset-ui-chart-controls/src/operators/renameOperator.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,89 @@ | ||
/* 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 { | ||
PostProcessingRename, | ||
ensureIsArray, | ||
getMetricLabel, | ||
ComparisionType, | ||
} from '@superset-ui/core'; | ||
import { PostProcessingFactory } from './types'; | ||
import { getMetricOffsetsMap, isValidTimeCompare } from './utils'; | ||
|
||
export const renameOperator: PostProcessingFactory<PostProcessingRename> = ( | ||
formData, | ||
queryObject, | ||
) => { | ||
const metrics = ensureIsArray(queryObject.metrics); | ||
const columns = ensureIsArray(queryObject.columns); | ||
const { x_axis: xAxis } = formData; | ||
// remove or rename top level of column name(metric name) in the MultiIndex when | ||
// 1) only 1 metric | ||
// 2) exist dimentsion | ||
// 3) exist xAxis | ||
// 4) exist time comparison, and comparison type is "actual values" | ||
if ( | ||
metrics.length === 1 && | ||
columns.length > 0 && | ||
(xAxis || queryObject.is_timeseries) && | ||
!( | ||
// todo: we should provide an approach to handle derived metrics | ||
( | ||
isValidTimeCompare(formData, queryObject) && | ||
[ | ||
ComparisionType.Difference, | ||
ComparisionType.Ratio, | ||
ComparisionType.Percentage, | ||
].includes(formData.comparison_type) | ||
) | ||
) | ||
) { | ||
const renamePairs: [string, string | null][] = []; | ||
|
||
if ( | ||
// "actual values" will add derived metric. | ||
// we will rename the "metric" from the metricWithOffset label | ||
// for example: "count__1 year ago" => "1 year ago" | ||
isValidTimeCompare(formData, queryObject) && | ||
formData.comparison_type === ComparisionType.Values | ||
) { | ||
const metricOffsetMap = getMetricOffsetsMap(formData, queryObject); | ||
const timeOffsets = ensureIsArray(formData.time_compare); | ||
[...metricOffsetMap.keys()].forEach(metricWithOffset => { | ||
const offsetLabel = timeOffsets.find(offset => | ||
metricWithOffset.includes(offset), | ||
); | ||
renamePairs.push([metricWithOffset, offsetLabel]); | ||
}); | ||
} | ||
|
||
renamePairs.push([getMetricLabel(metrics[0]), null]); | ||
|
||
return { | ||
operation: 'rename', | ||
options: { | ||
columns: Object.fromEntries(renamePairs), | ||
level: 0, | ||
inplace: true, | ||
}, | ||
}; | ||
} | ||
|
||
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
146 changes: 146 additions & 0 deletions
146
...-frontend/packages/superset-ui-chart-controls/test/utils/operators/renameOperator.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,146 @@ | ||
/** | ||
* 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 { ComparisionType, QueryObject, SqlaFormData } from '@superset-ui/core'; | ||
import { renameOperator } from '@superset-ui/chart-controls'; | ||
|
||
const formData: SqlaFormData = { | ||
x_axis: 'dttm', | ||
metrics: ['count(*)'], | ||
groupby: ['gender'], | ||
time_range: '2015 : 2016', | ||
granularity: 'month', | ||
datasource: 'foo', | ||
viz_type: 'table', | ||
}; | ||
const queryObject: QueryObject = { | ||
is_timeseries: true, | ||
metrics: ['count(*)'], | ||
columns: ['gender', 'dttm'], | ||
time_range: '2015 : 2016', | ||
granularity: 'month', | ||
post_processing: [], | ||
}; | ||
|
||
test('should skip renameOperator if exists multiple metrics', () => { | ||
expect( | ||
renameOperator(formData, { | ||
...queryObject, | ||
...{ | ||
metrics: ['count(*)', 'sum(sales)'], | ||
}, | ||
}), | ||
).toEqual(undefined); | ||
}); | ||
|
||
test('should skip renameOperator if does not exist series', () => { | ||
expect( | ||
renameOperator(formData, { | ||
...queryObject, | ||
...{ | ||
columns: [], | ||
}, | ||
}), | ||
).toEqual(undefined); | ||
}); | ||
|
||
test('should skip renameOperator if does not exist x_axis and is_timeseries', () => { | ||
expect( | ||
renameOperator( | ||
{ | ||
...formData, | ||
...{ x_axis: null }, | ||
}, | ||
{ ...queryObject, ...{ is_timeseries: false } }, | ||
), | ||
).toEqual(undefined); | ||
}); | ||
|
||
test('should skip renameOperator if exists derived metrics', () => { | ||
[ | ||
ComparisionType.Difference, | ||
ComparisionType.Ratio, | ||
ComparisionType.Percentage, | ||
].forEach(type => { | ||
expect( | ||
renameOperator( | ||
{ | ||
...formData, | ||
...{ | ||
comparison_type: type, | ||
time_compare: ['1 year ago'], | ||
}, | ||
}, | ||
{ | ||
...queryObject, | ||
...{ | ||
metrics: ['count(*)'], | ||
}, | ||
}, | ||
), | ||
).toEqual(undefined); | ||
}); | ||
}); | ||
|
||
test('should add renameOperator', () => { | ||
expect(renameOperator(formData, queryObject)).toEqual({ | ||
operation: 'rename', | ||
options: { columns: { 'count(*)': null }, inplace: true, level: 0 }, | ||
}); | ||
}); | ||
|
||
test('should add renameOperator if does not exist x_axis', () => { | ||
expect( | ||
renameOperator( | ||
{ | ||
...formData, | ||
...{ x_axis: null }, | ||
}, | ||
queryObject, | ||
), | ||
).toEqual({ | ||
operation: 'rename', | ||
options: { columns: { 'count(*)': null }, inplace: true, level: 0 }, | ||
}); | ||
}); | ||
|
||
test('should add renameOperator if exist "actual value" time comparison', () => { | ||
expect( | ||
renameOperator( | ||
{ | ||
...formData, | ||
...{ | ||
comparison_type: ComparisionType.Values, | ||
time_compare: ['1 year ago', '1 year later'], | ||
}, | ||
}, | ||
queryObject, | ||
), | ||
).toEqual({ | ||
operation: 'rename', | ||
options: { | ||
columns: { | ||
'count(*)': null, | ||
'count(*)__1 year ago': '1 year ago', | ||
'count(*)__1 year later': '1 year later', | ||
}, | ||
inplace: true, | ||
level: 0, | ||
}, | ||
}); | ||
}); |
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
Oops, something went wrong.