From b9bc98d130fd061ec665215d60d19d25502bba18 Mon Sep 17 00:00:00 2001 From: Usiel Riedl Date: Thu, 20 Oct 2022 10:45:43 +0800 Subject: [PATCH 1/2] feat: add config for default explore time filter Related to issue https://github.com/apache/superset/issues/13125 First change to complete #13125: We add a new configuration `DEFAULT_TIME_FILTER` to allow admins to set a global deafult time filter for any explore charts. By default this is set to `None`, which replicates the previous behavior. To get a default filter of -1w the configuration can be set to `'Last week'`. There is a minor change in behavior `UPDATE_FORM_DATA_BY_DATASOURCE`, the time filter is not reset anymore (the time dimension does still). This is similar to the behavior of other globally configured filters such as the row limit for explore. --- .../superset-ui-core/src/query/constants.ts | 1 - .../explore/actions/hydrateExplore.test.ts | 46 +++++++++++++++++++ .../src/explore/actions/hydrateExplore.ts | 5 ++ .../DateFilterControl/DateFilterLabel.tsx | 11 +---- superset-frontend/src/explore/fixtures.tsx | 2 - .../src/explore/reducers/exploreReducer.js | 5 +- superset/config.py | 3 ++ superset/views/base.py | 1 + 8 files changed, 58 insertions(+), 16 deletions(-) diff --git a/superset-frontend/packages/superset-ui-core/src/query/constants.ts b/superset-frontend/packages/superset-ui-core/src/query/constants.ts index 812ad0990c1bc..7976e87a4a281 100644 --- a/superset-frontend/packages/superset-ui-core/src/query/constants.ts +++ b/superset-frontend/packages/superset-ui-core/src/query/constants.ts @@ -25,7 +25,6 @@ import { } from './types'; export const DTTM_ALIAS = '__timestamp'; -export const DEFAULT_TIME_RANGE = 'No filter'; // TODO: make this configurable per Superset installation export const NO_TIME_RANGE = 'No filter'; export const EXTRA_FORM_DATA_OVERRIDE_EXTRA_KEYS: (keyof ExtraFormDataOverrideExtras)[] = diff --git a/superset-frontend/src/explore/actions/hydrateExplore.test.ts b/superset-frontend/src/explore/actions/hydrateExplore.test.ts index c74dc1f2847c4..e7858b167086d 100644 --- a/superset-frontend/src/explore/actions/hydrateExplore.test.ts +++ b/superset-frontend/src/explore/actions/hydrateExplore.test.ts @@ -160,3 +160,49 @@ test('creates hydrate action with existing state', () => { }), ); }); + +test('uses configured default time range if not set', () => { + const dispatch = jest.fn(); + const getState = jest.fn(() => ({ + user: {}, + charts: {}, + datasources: {}, + common: { + conf: { + DEFAULT_TIME_FILTER: 'Last year', + }, + }, + explore: {}, + })); + // @ts-ignore + hydrateExplore({ form_data: {}, slice: {}, dataset: {} })(dispatch, getState); + expect(dispatch).toHaveBeenCalledWith( + expect.objectContaining({ + data: expect.objectContaining({ + explore: expect.objectContaining({ + form_data: expect.objectContaining({ + time_range: 'Last year', + }), + }), + }), + }), + ); + const withTimeRangeSet = { + form_data: { time_range: 'Last day' }, + slice: {}, + dataset: {}, + }; + // @ts-ignore + hydrateExplore(withTimeRangeSet)(dispatch, getState); + expect(dispatch).toHaveBeenCalledWith( + expect.objectContaining({ + data: expect.objectContaining({ + explore: expect.objectContaining({ + form_data: expect.objectContaining({ + time_range: 'Last day', + }), + }), + }), + }), + ); +}); diff --git a/superset-frontend/src/explore/actions/hydrateExplore.ts b/superset-frontend/src/explore/actions/hydrateExplore.ts index dd97e73822993..e4bd1bb82f210 100644 --- a/superset-frontend/src/explore/actions/hydrateExplore.ts +++ b/superset-frontend/src/explore/actions/hydrateExplore.ts @@ -30,6 +30,7 @@ import { ensureIsArray, getCategoricalSchemeRegistry, getSequentialSchemeRegistry, + NO_TIME_RANGE, } from '@superset-ui/core'; import { getFormDataFromControls, @@ -62,6 +63,10 @@ export const hydrateExplore = initialFormData.viz_type = getUrlParam(URL_PARAMS.vizType) || defaultVizType; } + if (!initialFormData.time_range) { + initialFormData.time_range = + common?.conf?.DEFAULT_TIME_FILTER || NO_TIME_RANGE; + } if (dashboardId) { initialFormData.dashboardId = dashboardId; } diff --git a/superset-frontend/src/explore/components/controls/DateFilterControl/DateFilterLabel.tsx b/superset-frontend/src/explore/components/controls/DateFilterControl/DateFilterLabel.tsx index b4cd377caf3fa..4b84ccf20423b 100644 --- a/superset-frontend/src/explore/components/controls/DateFilterControl/DateFilterLabel.tsx +++ b/superset-frontend/src/explore/components/controls/DateFilterControl/DateFilterLabel.tsx @@ -17,14 +17,7 @@ * under the License. */ import React, { useState, useEffect, useMemo } from 'react'; -import { - css, - styled, - t, - useTheme, - DEFAULT_TIME_RANGE, - NO_TIME_RANGE, -} from '@superset-ui/core'; +import { css, styled, t, useTheme, NO_TIME_RANGE } from '@superset-ui/core'; import Button from 'src/components/Button'; import ControlHeader from 'src/explore/components/ControlHeader'; import Label, { Type } from 'src/components/Label'; @@ -128,7 +121,7 @@ const IconWrapper = styled.span` export default function DateFilterLabel(props: DateFilterControlProps) { const { - value = DEFAULT_TIME_RANGE, + value = NO_TIME_RANGE, onChange, type, onOpenPopover = noOp, diff --git a/superset-frontend/src/explore/fixtures.tsx b/superset-frontend/src/explore/fixtures.tsx index 755985be2bda9..d7c1276247d5d 100644 --- a/superset-frontend/src/explore/fixtures.tsx +++ b/superset-frontend/src/explore/fixtures.tsx @@ -115,7 +115,6 @@ export const exploreInitialData: ExplorePageInitialData = { datasource: '8__table', metric: 'count', slice_id: 371, - time_range: 'No filter', viz_type: 'table', }, slice: { @@ -128,7 +127,6 @@ export const exploreInitialData: ExplorePageInitialData = { datasource: '8__table', metric: 'count', slice_id: 371, - time_range: 'No filter', viz_type: 'table', }, }, diff --git a/superset-frontend/src/explore/reducers/exploreReducer.js b/superset-frontend/src/explore/reducers/exploreReducer.js index e282d88f04bec..7b630f992717d 100644 --- a/superset-frontend/src/explore/reducers/exploreReducer.js +++ b/superset-frontend/src/explore/reducers/exploreReducer.js @@ -17,7 +17,7 @@ * under the License. */ /* eslint camelcase: 0 */ -import { ensureIsArray, DEFAULT_TIME_RANGE } from '@superset-ui/core'; +import { ensureIsArray } from '@superset-ui/core'; import { DYNAMIC_PLUGIN_CONTROLS_READY } from 'src/components/Chart/chartAction'; import { getControlsState } from 'src/explore/store'; import { @@ -59,11 +59,8 @@ export default function exploreReducer(state = {}, action) { prevDatasource.id !== newDatasource.id || prevDatasource.type !== newDatasource.type ) { - // reset time range filter to default - newFormData.time_range = DEFAULT_TIME_RANGE; newFormData.datasource = newDatasource.uid; } - // reset control values for column/metric related controls Object.entries(controls).forEach(([controlName, controlState]) => { if ( diff --git a/superset/config.py b/superset/config.py index 64dc3baa125b5..a2ddf5d88371c 100644 --- a/superset/config.py +++ b/superset/config.py @@ -153,6 +153,9 @@ def _try_json_readsha(filepath: str, length: int) -> Optional[str]: SAMPLES_ROW_LIMIT = 1000 # max rows retrieved by filter select auto complete FILTER_SELECT_ROW_LIMIT = 10000 +# default time filter in explore +# values may be "Last day", "Last week", " : now", etc. +DEFAULT_TIME_FILTER = None SUPERSET_WEBSERVER_PROTOCOL = "http" SUPERSET_WEBSERVER_ADDRESS = "0.0.0.0" diff --git a/superset/views/base.py b/superset/views/base.py index cf7868eaf1c04..28511535f4145 100644 --- a/superset/views/base.py +++ b/superset/views/base.py @@ -110,6 +110,7 @@ "COLUMNAR_EXTENSIONS", "ALLOWED_EXTENSIONS", "SAMPLES_ROW_LIMIT", + "DEFAULT_TIME_FILTER", ) logger = logging.getLogger(__name__) From 809b36561875147eb55f9d093489b1e9dfc692a7 Mon Sep 17 00:00:00 2001 From: Usiel Riedl Date: Thu, 20 Oct 2022 16:17:13 +0800 Subject: [PATCH 2/2] Set explicit default for DEFAULT_TIME_FILTER Using "No filter" string instead of None to avoid issues downstream if None is not properly handled. --- superset/config.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/superset/config.py b/superset/config.py index a2ddf5d88371c..d340ead61a736 100644 --- a/superset/config.py +++ b/superset/config.py @@ -59,7 +59,7 @@ from superset.jinja_context import BaseTemplateProcessor from superset.stats_logger import DummyStatsLogger from superset.superset_typing import CacheConfig -from superset.utils.core import is_test, parse_boolean_string +from superset.utils.core import is_test, NO_TIME_RANGE, parse_boolean_string from superset.utils.encrypt import SQLAlchemyUtilsAdapter from superset.utils.log import DBEventLogger from superset.utils.logging_configurator import DefaultLoggingConfigurator @@ -155,7 +155,7 @@ def _try_json_readsha(filepath: str, length: int) -> Optional[str]: FILTER_SELECT_ROW_LIMIT = 10000 # default time filter in explore # values may be "Last day", "Last week", " : now", etc. -DEFAULT_TIME_FILTER = None +DEFAULT_TIME_FILTER = NO_TIME_RANGE SUPERSET_WEBSERVER_PROTOCOL = "http" SUPERSET_WEBSERVER_ADDRESS = "0.0.0.0"