From 369aafd9ae35c1cb12ff02be5701a07f3122c71d Mon Sep 17 00:00:00 2001 From: Lily Kuang Date: Wed, 26 Apr 2023 10:17:32 -0700 Subject: [PATCH] feat: add verbose map to get /dataset/ endpoint (#23655) --- .../Chart/DrillBy/DrillByChart.test.tsx | 19 +++++++++++ .../components/Chart/DrillBy/DrillByChart.tsx | 4 +++ .../Chart/DrillBy/DrillByMenuItems.tsx | 4 ++- .../components/Chart/DrillBy/DrillByModal.tsx | 1 + .../src/components/Chart/types.ts | 4 ++- .../src/hooks/apiResources/datasets.ts | 32 +++++++++++++++++++ 6 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 superset-frontend/src/hooks/apiResources/datasets.ts diff --git a/superset-frontend/src/components/Chart/DrillBy/DrillByChart.test.tsx b/superset-frontend/src/components/Chart/DrillBy/DrillByChart.test.tsx index 497e2bb065..a7d314a20a 100644 --- a/superset-frontend/src/components/Chart/DrillBy/DrillByChart.test.tsx +++ b/superset-frontend/src/components/Chart/DrillBy/DrillByChart.test.tsx @@ -23,6 +23,24 @@ import { noOp } from 'src/utils/common'; import DrillByChart from './DrillByChart'; const chart = chartQueries[sliceId]; +const dataset = { + changed_on_humanized: '01-01-2001', + created_on_humanized: '01-01-2001', + description: 'desc', + table_name: 'my_dataset', + owners: [ + { + first_name: 'Sarah', + last_name: 'Connor', + }, + ], + columns: [ + { + column_name: 'gender', + }, + { column_name: 'name' }, + ], +}; const setup = (overrides: Record = {}, result?: any) => render( @@ -31,6 +49,7 @@ const setup = (overrides: Record = {}, result?: any) => onContextMenu={noOp} inContextMenu={false} result={result} + dataset={dataset} />, { useRedux: true, diff --git a/superset-frontend/src/components/Chart/DrillBy/DrillByChart.tsx b/superset-frontend/src/components/Chart/DrillBy/DrillByChart.tsx index 5e3f9d3c1f..91faa05c9c 100644 --- a/superset-frontend/src/components/Chart/DrillBy/DrillByChart.tsx +++ b/superset-frontend/src/components/Chart/DrillBy/DrillByChart.tsx @@ -24,10 +24,12 @@ import { css, ContextMenuFilters, } from '@superset-ui/core'; +import { Dataset } from '../types'; interface DrillByChartProps { formData: BaseFormData & { [key: string]: any }; result: QueryData[]; + dataset: Dataset; onContextMenu: ( offsetX: number, offsetY: number, @@ -39,6 +41,7 @@ interface DrillByChartProps { export default function DrillByChart({ formData, result, + dataset, onContextMenu, inContextMenu, }: DrillByChartProps) { @@ -56,6 +59,7 @@ export default function DrillByChart({ disableErrorBoundary chartType={formData.viz_type} enableNoResults + datasource={dataset} formData={formData} queriesData={result} hooks={hooks} diff --git a/superset-frontend/src/components/Chart/DrillBy/DrillByMenuItems.tsx b/superset-frontend/src/components/Chart/DrillBy/DrillByMenuItems.tsx index 17e013d29f..a199602797 100644 --- a/superset-frontend/src/components/Chart/DrillBy/DrillByMenuItems.tsx +++ b/superset-frontend/src/components/Chart/DrillBy/DrillByMenuItems.tsx @@ -44,6 +44,7 @@ import { cachedSupersetGet, supersetGetCache, } from 'src/utils/cachedSupersetGet'; +import { useVerboseMap } from 'src/hooks/apiResources/datasets'; import { MenuItemTooltip } from '../DisabledMenuItemTooltip'; import DrillByModal from './DrillByModal'; import { getSubmenuYOffset } from '../utils'; @@ -115,6 +116,7 @@ export const DrillByMenuItems = ({ ?.behaviors.find(behavior => behavior === Behavior.DRILL_BY), [formData.viz_type], ); + const verboseMap = useVerboseMap(dataset); useEffect(() => { if (handlesDimensionContextMenu && hasDrillBy) { @@ -270,7 +272,7 @@ export const DrillByMenuItems = ({ drillByConfig={drillByConfig} formData={formData} onHideModal={closeModal} - dataset={dataset!} + dataset={{ ...dataset!, verbose_map: verboseMap }} /> )} diff --git a/superset-frontend/src/components/Chart/DrillBy/DrillByModal.tsx b/superset-frontend/src/components/Chart/DrillBy/DrillByModal.tsx index df754f66a9..afccef4b65 100644 --- a/superset-frontend/src/components/Chart/DrillBy/DrillByModal.tsx +++ b/superset-frontend/src/components/Chart/DrillBy/DrillByModal.tsx @@ -407,6 +407,7 @@ export default function DrillByModal({ )} {drillByDisplayMode === DrillByType.Chart && chartDataResult && ( ; }; diff --git a/superset-frontend/src/hooks/apiResources/datasets.ts b/superset-frontend/src/hooks/apiResources/datasets.ts new file mode 100644 index 0000000000..cd44b2426e --- /dev/null +++ b/superset-frontend/src/hooks/apiResources/datasets.ts @@ -0,0 +1,32 @@ +/* eslint-disable no-underscore-dangle */ +/** + * 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 { Column, Metric, ensureIsArray } from '@superset-ui/core'; +import { Dataset } from 'src/components/Chart/types'; + +export const useVerboseMap = (dataset?: Dataset) => { + const verbose_map = {}; + ensureIsArray(dataset?.columns).forEach((column: Column) => { + verbose_map[column.column_name] = column.verbose_name || column.column_name; + }); + ensureIsArray(dataset?.metrics).forEach((metric: Metric) => { + verbose_map[metric.metric_name] = metric.verbose_name || metric.metric_name; + }); + return verbose_map; +};