From 5a235ca676aa2ade461975b6c2a19c984b635d1d Mon Sep 17 00:00:00 2001 From: Kamil Gabryjelski Date: Thu, 29 Jul 2021 11:54:51 +0200 Subject: [PATCH] feat(plugin-chart-pivot-table): add sort by result to data pane (#1243) * feat(plugin-chart-pivot-table): add sort by result to data pane * Use getMetricLabel helper function --- .../src/plugin/buildQuery.ts | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/superset-frontend/temporary_superset_ui/superset-ui/plugins/plugin-chart-pivot-table/src/plugin/buildQuery.ts b/superset-frontend/temporary_superset_ui/superset-ui/plugins/plugin-chart-pivot-table/src/plugin/buildQuery.ts index 5ca1813681f45..ef81cf184209c 100644 --- a/superset-frontend/temporary_superset_ui/superset-ui/plugins/plugin-chart-pivot-table/src/plugin/buildQuery.ts +++ b/superset-frontend/temporary_superset_ui/superset-ui/plugins/plugin-chart-pivot-table/src/plugin/buildQuery.ts @@ -16,21 +16,40 @@ * specific language governing permissions and limitations * under the License. */ -import { buildQueryContext, ensureIsArray, normalizeOrderBy } from '@superset-ui/core'; +import { + buildQueryContext, + ensureIsArray, + getMetricLabel, + normalizeOrderBy, +} from '@superset-ui/core'; import { PivotTableQueryFormData } from '../types'; export default function buildQuery(formData: PivotTableQueryFormData) { - const { groupbyColumns = [], groupbyRows = [], order_desc = true } = formData; + const { + groupbyColumns = [], + groupbyRows = [], + order_desc = true, + timeseries_limit_metric, + } = formData; const groupbySet = new Set([ ...ensureIsArray(groupbyColumns), ...ensureIsArray(groupbyRows), ]); return buildQueryContext(formData, baseQueryObject => { const queryObject = normalizeOrderBy({ ...baseQueryObject, order_desc }); + const { metrics } = queryObject; + const orderBy = ensureIsArray(timeseries_limit_metric); + if ( + orderBy.length && + !metrics?.find(metric => getMetricLabel(metric) === getMetricLabel(orderBy[0])) + ) { + metrics?.push(orderBy[0]); + } return [ { ...queryObject, columns: [...groupbySet], + metrics, }, ]; });