villebro commented on a change in pull request #13575:
URL: https://github.com/apache/superset/pull/13575#discussion_r592818978



##########
File path: 
superset-frontend/src/explore/components/controls/DndColumnSelectControl/DndFilterSelect.tsx
##########
@@ -17,12 +17,12 @@
  * under the License.
  */
 import React, { useEffect, useMemo, useState } from 'react';
-import { logging, SupersetClient } from '@superset-ui/core';
+import { logging, SupersetClient, t } from '@superset-ui/core';
 import { ColumnMeta, Metric } from '@superset-ui/chart-controls';

Review comment:
       Bycatch: I believe `Metric` is originally defined in `@superset-ui/core` 
(probably better to import from there, as I'm unsure why it's exported by 
`@superset-ui/chart-controls`)

##########
File path: 
superset-frontend/src/explore/components/controls/DndColumnSelectControl/DndMetricSelect.tsx
##########
@@ -0,0 +1,301 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with work for additional information
+ * regarding copyright ownership.  The ASF licenses file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use 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 React, { useCallback, useEffect, useMemo, useState } from 'react';
+import { ensureIsArray, Metric, t } from '@superset-ui/core';
+import { ColumnMeta } from '@superset-ui/chart-controls';
+import { isEqual } from 'lodash';
+import { usePrevious } from 'src/common/hooks/usePrevious';
+import AdhocMetric from '../MetricControl/AdhocMetric';
+import AdhocMetricPopoverTrigger from 
'../MetricControl/AdhocMetricPopoverTrigger';
+import MetricDefinitionValue from '../MetricControl/MetricDefinitionValue';
+import { OptionValueType } from './types';
+import { DatasourcePanelDndItem } from '../../DatasourcePanel/types';
+import { DndItemType } from '../../DndItemType';
+import DndSelectLabel from './DndSelectLabel';
+import { savedMetricType } from '../MetricControl/types';
+
+const isDictionaryForAdhocMetric = (value: any) =>
+  value && !(value instanceof AdhocMetric) && value.expressionType;
+
+const coerceAdhocMetrics = (value: any) => {
+  if (!value) {
+    return [];
+  }
+  if (!Array.isArray(value)) {
+    if (isDictionaryForAdhocMetric(value)) {
+      return [new AdhocMetric(value)];
+    }
+    return [value];
+  }
+  return value.map(val => {
+    if (isDictionaryForAdhocMetric(val)) {
+      return new AdhocMetric(val);
+    }
+    return val;
+  });
+};
+
+const getOptionsForSavedMetrics = (
+  savedMetrics: savedMetricType[],
+  currentMetricValues: (string | AdhocMetric)[],
+  currentMetric?: string,
+) =>
+  savedMetrics?.filter(savedMetric =>
+    Array.isArray(currentMetricValues)
+      ? !currentMetricValues.includes(savedMetric.metric_name ?? '') ||
+        savedMetric.metric_name === currentMetric
+      : savedMetric,
+  ) ?? [];
+
+const columnsContainAllMetrics = (
+  value: (string | AdhocMetric | ColumnMeta)[],
+  columns: ColumnMeta[],
+  savedMetrics: savedMetricType[],
+) => {
+  const columnNames = new Set(
+    [...(columns || []), ...(savedMetrics || [])]
+      // eslint-disable-next-line camelcase
+      .map(
+        item =>
+          (item as ColumnMeta).column_name ||
+          (item as savedMetricType).metric_name,
+      ),
+  );
+
+  return (
+    ensureIsArray(value)
+      .filter(metric => metric)
+      // find column names
+      .map(metric =>
+        (metric as AdhocMetric).column
+          ? (metric as AdhocMetric).column.column_name
+          : (metric as ColumnMeta).column_name || metric,
+      )
+      .filter(name => name && typeof name === 'string')
+      .every(name => columnNames.has(name))

Review comment:
       It would be nice to add type guards for the types in `value` to be able 
to separate between the different metrics types (`string | AdhocMetric | 
ColumnMeta`). We could then add a `getMetricName(metric)` helper function which 
would return the name based on which type it is.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to