From a593dbf820541cf1827834536e81173bfcaf1d5a 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 5ca1813681f4..ef81cf184209 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, }, ]; });