feat(explore): add config for default time filter (#21879)

Co-authored-by: Usiel Riedl <usiel.riedl@automattic.com>
This commit is contained in:
Usiel Riedl 2022-10-20 20:09:51 +08:00 committed by GitHub
parent f4da74ce8d
commit 9a063abb3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 59 additions and 17 deletions

View File

@ -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)[] =

View File

@ -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',
}),
}),
}),
}),
);
});

View File

@ -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;
}

View File

@ -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,

View File

@ -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',
},
},

View File

@ -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 (

View File

@ -59,7 +59,7 @@ from superset.constants import CHANGE_ME_SECRET_KEY
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
@ -153,6 +153,9 @@ ROW_LIMIT = 50000
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"

View File

@ -110,6 +110,7 @@ FRONTEND_CONF_KEYS = (
"COLUMNAR_EXTENSIONS",
"ALLOWED_EXTENSIONS",
"SAMPLES_ROW_LIMIT",
"DEFAULT_TIME_FILTER",
)
logger = logging.getLogger(__name__)