Skip to content

Commit

Permalink
feat: add config for default explore time filter
Browse files Browse the repository at this point in the history
Related to issue #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.
  • Loading branch information
Usiel committed Oct 20, 2022
1 parent 28c7636 commit b9bc98d
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)[] =
Expand Down
46 changes: 46 additions & 0 deletions superset-frontend/src/explore/actions/hydrateExplore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}),
}),
}),
}),
);
});
5 changes: 5 additions & 0 deletions superset-frontend/src/explore/actions/hydrateExplore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
ensureIsArray,
getCategoricalSchemeRegistry,
getSequentialSchemeRegistry,
NO_TIME_RANGE,
} from '@superset-ui/core';
import {
getFormDataFromControls,
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 0 additions & 2 deletions superset-frontend/src/explore/fixtures.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ export const exploreInitialData: ExplorePageInitialData = {
datasource: '8__table',
metric: 'count',
slice_id: 371,
time_range: 'No filter',
viz_type: 'table',
},
slice: {
Expand All @@ -128,7 +127,6 @@ export const exploreInitialData: ExplorePageInitialData = {
datasource: '8__table',
metric: 'count',
slice_id: 371,
time_range: 'No filter',
viz_type: 'table',
},
},
Expand Down
5 changes: 1 addition & 4 deletions superset-frontend/src/explore/reducers/exploreReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 (
Expand Down
3 changes: 3 additions & 0 deletions superset/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", "<ISO date> : now", etc.
DEFAULT_TIME_FILTER = None

SUPERSET_WEBSERVER_PROTOCOL = "http"
SUPERSET_WEBSERVER_ADDRESS = "0.0.0.0"
Expand Down
1 change: 1 addition & 0 deletions superset/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
"COLUMNAR_EXTENSIONS",
"ALLOWED_EXTENSIONS",
"SAMPLES_ROW_LIMIT",
"DEFAULT_TIME_FILTER",
)

logger = logging.getLogger(__name__)
Expand Down

0 comments on commit b9bc98d

Please sign in to comment.