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__)