You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by di...@apache.org on 2022/08/22 18:00:06 UTC

[superset] 02/19: Include additional filters from dashboard context in request.

This is an automated email from the ASF dual-hosted git repository.

diegopucci pushed a commit to branch chore/e2e-tests-drilltodetail-modal
in repository https://gitbox.apache.org/repos/asf/superset.git

commit 8ab13c96ff7f1d173d665a50ffdf2e930301b6ca
Author: Cody Leff <co...@preset.io>
AuthorDate: Thu Aug 4 16:52:10 2022 -0400

    Include additional filters from dashboard context in request.
---
 .../components/DrillDetailPane/DrillDetailPane.tsx | 33 ++++++++++------
 .../dashboard/components/DrillDetailPane/utils.ts  | 46 ++++++++++++++++++++++
 .../components/SliceHeaderControls/index.tsx       |  5 ++-
 3 files changed, 71 insertions(+), 13 deletions(-)

diff --git a/superset-frontend/src/dashboard/components/DrillDetailPane/DrillDetailPane.tsx b/superset-frontend/src/dashboard/components/DrillDetailPane/DrillDetailPane.tsx
index f6b50f9d17..4fb13f9c7f 100644
--- a/superset-frontend/src/dashboard/components/DrillDetailPane/DrillDetailPane.tsx
+++ b/superset-frontend/src/dashboard/components/DrillDetailPane/DrillDetailPane.tsx
@@ -16,7 +16,6 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-
 import React, {
   useState,
   useEffect,
@@ -31,6 +30,7 @@ import {
   GenericDataType,
   t,
   useTheme,
+  QueryFormData,
 } from '@superset-ui/core';
 import Loading from 'src/components/Loading';
 import { EmptyStateMedium } from 'src/components/EmptyState';
@@ -38,6 +38,7 @@ import TableView, { EmptyWrapperType } from 'src/components/TableView';
 import { useTableColumns } from 'src/explore/components/DataTableControl';
 import { getDatasourceSamples } from 'src/components/Chart/chartAction';
 import TableControls from './TableControls';
+import { getDrillPayload } from './utils';
 
 type ResultsPage = {
   total: number;
@@ -51,15 +52,17 @@ const MAX_CACHED_PAGES = 5;
 
 export default function DrillDetailPane({
   datasource,
-  initialFilters,
+  queryFormData,
+  drillFilters,
 }: {
   datasource: string;
-  initialFilters?: BinaryQueryObjectFilterClause[];
+  queryFormData?: QueryFormData;
+  drillFilters?: BinaryQueryObjectFilterClause[];
 }) {
   const theme = useTheme();
   const [pageIndex, setPageIndex] = useState(0);
   const lastPageIndex = useRef(pageIndex);
-  const [filters, setFilters] = useState(initialFilters || []);
+  const [filters, setFilters] = useState(drillFilters || []);
   const [isLoading, setIsLoading] = useState(false);
   const [responseError, setResponseError] = useState('');
   const [resultsPages, setResultsPages] = useState<Map<number, ResultsPage>>(
@@ -110,13 +113,11 @@ export default function DrillDetailPane({
   useEffect(() => {
     if (!resultsPages.has(pageIndex)) {
       setIsLoading(true);
-      getDatasourceSamples(
-        datasourceType,
-        datasourceId,
-        true,
-        filters.length ? { filters } : null,
-        { page: pageIndex + 1, perPage: PAGE_SIZE },
-      )
+      const jsonPayload = getDrillPayload(queryFormData, drillFilters);
+      getDatasourceSamples(datasourceType, datasourceId, true, jsonPayload, {
+        page: pageIndex + 1,
+        perPage: PAGE_SIZE,
+      })
         .then(response => {
           setResultsPages(
             new Map([
@@ -141,7 +142,15 @@ export default function DrillDetailPane({
           setIsLoading(false);
         });
     }
-  }, [datasourceId, datasourceType, filters, pageIndex, resultsPages]);
+  }, [
+    datasourceId,
+    datasourceType,
+    drillFilters,
+    filters,
+    pageIndex,
+    queryFormData,
+    resultsPages,
+  ]);
 
   // this is to preserve the order of the columns, even if there are integer values,
   // while also only grabbing the first column's keys
diff --git a/superset-frontend/src/dashboard/components/DrillDetailPane/utils.ts b/superset-frontend/src/dashboard/components/DrillDetailPane/utils.ts
new file mode 100644
index 0000000000..e59af45b0d
--- /dev/null
+++ b/superset-frontend/src/dashboard/components/DrillDetailPane/utils.ts
@@ -0,0 +1,46 @@
+/**
+ * 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 { omit } from 'lodash';
+import {
+  ensureIsArray,
+  QueryFormData,
+  BinaryQueryObjectFilterClause,
+  buildQueryObject,
+} from '@superset-ui/core';
+
+export function getDrillPayload(
+  queryFormData?: QueryFormData,
+  drillFilters?: BinaryQueryObjectFilterClause[],
+) {
+  if (!queryFormData) {
+    return undefined;
+  }
+  const queryObject = buildQueryObject(queryFormData);
+  const extras = omit(queryObject.extras, 'having');
+  const filters = [
+    ...ensureIsArray(queryObject.filters),
+    ...ensureIsArray(drillFilters),
+  ];
+  return {
+    granularity: queryObject.granularity,
+    time_range: queryObject.time_range,
+    filters,
+    extras,
+  };
+}
diff --git a/superset-frontend/src/dashboard/components/SliceHeaderControls/index.tsx b/superset-frontend/src/dashboard/components/SliceHeaderControls/index.tsx
index bb41c82d3e..77d80199c1 100644
--- a/superset-frontend/src/dashboard/components/SliceHeaderControls/index.tsx
+++ b/superset-frontend/src/dashboard/components/SliceHeaderControls/index.tsx
@@ -448,7 +448,10 @@ class SliceHeaderControls extends React.PureComponent<
               }
               modalTitle={t('Drill to detail: %s', slice.slice_name)}
               modalBody={
-                <DrillDetailPane datasource={this.props.slice.datasource} />
+                <DrillDetailPane
+                  datasource={this.props.slice.datasource}
+                  queryFormData={this.props.formData}
+                />
               }
             />
           </Menu.Item>