Skip to content

Commit

Permalink
fix(plugin-chart-table): unnecessary post_processing in raw records m…
Browse files Browse the repository at this point in the history
…ode (apache#925)
  • Loading branch information
ktmud authored and zhaoyongjie committed Nov 24, 2021
1 parent 8e90932 commit 6d44d24
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,27 @@ export default function buildQuery(formData: TableChartFormData) {
let { metrics, orderby } = baseQueryObject;
let postProcessing: PostProcessingRule[] = [];

// orverride orderby with timeseries metric when in aggregation mode
if (queryMode === QueryMode.aggregate && timeseriesLimitMetric && orderDesc != null) {
orderby = [[timeseriesLimitMetric, !orderDesc]];
}

if (percentMetrics) {
const percentMetricLabels = percentMetrics.map(getMetricLabel);
metrics = removeDuplicates(metrics.concat(percentMetrics), getMetricLabel);
postProcessing = [
{
operation: 'contribution',
options: {
columns: percentMetricLabels,
rename_columns: percentMetricLabels.map(x => `%${x}`),
if (queryMode === QueryMode.aggregate) {
// orverride orderby with timeseries metric when in aggregation mode
if (timeseriesLimitMetric && orderDesc != null) {
orderby = [[timeseriesLimitMetric, !orderDesc]];
}
// add postprocessing for percent metrics only when in aggregation mode
if (percentMetrics && percentMetrics.length > 0) {
const percentMetricLabels = percentMetrics.map(getMetricLabel);
metrics = removeDuplicates(metrics.concat(percentMetrics), getMetricLabel);
postProcessing = [
{
operation: 'contribution',
options: {
columns: percentMetricLabels,
rename_columns: percentMetricLabels.map(x => `%${x}`),
},
},
},
];
];
}
}

return [
{
...baseQueryObject,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* 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 { QueryMode } from '@superset-ui/core';
import buildQuery from '../src/buildQuery';
import { TableChartFormData } from '../src/types';

const basicFormData: TableChartFormData = {
viz_type: 'table',
datasource: '11__table',
};

describe('plugin-chart-table', () => {
describe('buildQuery', () => {
it('should add post-processing in aggregate mode', () => {
const query = buildQuery({
...basicFormData,
query_mode: QueryMode.aggregate,
metrics: ['aaa'],
percent_metrics: ['pct_abc'],
}).queries[0];
expect(query.metrics).toEqual(['aaa', 'pct_abc']);
expect(query.post_processing).toEqual([
{
operation: 'contribution',
options: {
columns: ['pct_abc'],
rename_columns: ['%pct_abc'],
},
},
]);
});

it('should not add post-processing when there is not percent metric', () => {
const query = buildQuery({
...basicFormData,
query_mode: QueryMode.aggregate,
metrics: ['aaa'],
percent_metrics: [],
}).queries[0];
expect(query.metrics).toEqual(['aaa']);
expect(query.post_processing).toEqual([]);
});

it('should not add post-processing in raw records mode', () => {
const query = buildQuery({
...basicFormData,
query_mode: QueryMode.raw,
metrics: ['aaa'],
columns: ['rawcol'],
percent_metrics: ['ccc'],
}).queries[0];
expect(query.metrics).toEqual([]);
expect(query.columns).toEqual(['rawcol']);
expect(query.post_processing).toEqual([]);
});
});
});

0 comments on commit 6d44d24

Please sign in to comment.