Skip to content
This repository has been archived by the owner on Dec 10, 2021. It is now read-only.

Commit

Permalink
fix(plugin-chart-table): refine ordering logic (#930)
Browse files Browse the repository at this point in the history
  • Loading branch information
villebro authored Feb 3, 2021
1 parent 84f8e45 commit f0d1bd5
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 9 deletions.
38 changes: 30 additions & 8 deletions plugins/plugin-chart-table/src/buildQuery.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,44 @@
/**
* 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 { buildQueryContext, getMetricLabel, QueryMode, removeDuplicates } from '@superset-ui/core';
import { PostProcessingRule } from '@superset-ui/core/src/query/types/PostProcessing';
import { TableChartFormData } from './types';
import { extractTimeseriesLimitMetric } from './utils/extractOrderby';

export default function buildQuery(formData: TableChartFormData) {
const {
percent_metrics: percentMetrics,
timeseries_limit_metric: timeseriesLimitMetric,
query_mode: queryMode,
order_desc: orderDesc,
} = formData;
const { percent_metrics: percentMetrics, order_desc: orderDesc = null } = formData;
let { query_mode: queryMode } = formData;
const timeseriesLimitMetric = extractTimeseriesLimitMetric(formData.timeseries_limit_metric);
return buildQueryContext(formData, baseQueryObject => {
let { metrics, orderby } = baseQueryObject;
if (queryMode === undefined && metrics.length > 0) {
queryMode = QueryMode.aggregate;
}
let postProcessing: PostProcessingRule[] = [];

if (queryMode === QueryMode.aggregate) {
// orverride orderby with timeseries metric when in aggregation mode
if (timeseriesLimitMetric && orderDesc != null) {
orderby = [[timeseriesLimitMetric, !orderDesc]];
if (timeseriesLimitMetric.length > 0 && orderDesc != null) {
orderby = [[timeseriesLimitMetric[0], !orderDesc]];
} else if (timeseriesLimitMetric.length === 0 && metrics?.length > 0 && orderDesc != null) {
// default to ordering by first metric when no sort order has been specified
orderby = [[metrics[0], !orderDesc]];
}
// add postprocessing for percent metrics only when in aggregation mode
if (percentMetrics && percentMetrics.length > 0) {
Expand Down
2 changes: 1 addition & 1 deletion plugins/plugin-chart-table/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export type TableChartFormData = QueryFormData & {
page_length?: string | number | null; // null means auto-paginate
metrics?: QueryFormMetric[] | null;
percent_metrics?: QueryFormMetric[] | null;
timeseries_limit_metric?: QueryFormMetric | null;
timeseries_limit_metric?: QueryFormMetric[] | QueryFormMetric | null;
groupby?: QueryFormMetric[] | null;
all_columns?: QueryFormMetric[] | null;
order_desc?: boolean;
Expand Down
31 changes: 31 additions & 0 deletions plugins/plugin-chart-table/src/utils/extractOrderby.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* 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 { QueryFormMetric } from '@superset-ui/core';

export function extractTimeseriesLimitMetric(
timeSeriesLimitMetric?: QueryFormMetric[] | QueryFormMetric | null,
): QueryFormMetric[] {
if (timeSeriesLimitMetric === undefined || timeSeriesLimitMetric === null) {
return [];
}
if (Array.isArray(timeSeriesLimitMetric)) {
return timeSeriesLimitMetric;
}
return [timeSeriesLimitMetric];
}
33 changes: 33 additions & 0 deletions plugins/plugin-chart-table/test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* 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 { extractTimeseriesLimitMetric } from '../src/utils/extractOrderby';

describe('utils', () => {
it('should add post-processing in aggregate mode', () => {
expect(extractTimeseriesLimitMetric(undefined)).toEqual([]);
expect(extractTimeseriesLimitMetric(null)).toEqual([]);
expect(extractTimeseriesLimitMetric([])).toEqual([]);
expect(extractTimeseriesLimitMetric('my_metric')).toEqual(['my_metric']);
expect(extractTimeseriesLimitMetric(['my_metric'])).toEqual(['my_metric']);
expect(extractTimeseriesLimitMetric(['my_metric_1', 'my_metric_2'])).toEqual([
'my_metric_1',
'my_metric_2',
]);
});
});

1 comment on commit f0d1bd5

@vercel
Copy link

@vercel vercel bot commented on f0d1bd5 Feb 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.