Skip to content

Commit

Permalink
feat(explore): add config for default time filter (#21879)
Browse files Browse the repository at this point in the history
Co-authored-by: Usiel Riedl <usiel.riedl@automattic.com>
  • Loading branch information
Usiel and Usiel committed Oct 20, 2022
1 parent f4da74c commit 9a063ab
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 17 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
5 changes: 4 additions & 1 deletion superset/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down 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 = NO_TIME_RANGE

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 9a063ab

Please sign in to comment.