Skip to content

Commit 10eade8

Browse files
fix(explore): base the added time filter on the dataset, not form_data.time_range
Co-authored-by: rebenitez1802 <2151636+rebenitez1802@users.noreply.github.com>
1 parent 09a73b7 commit 10eade8

4 files changed

Lines changed: 49 additions & 24 deletions

File tree

‎superset-frontend/src/components/Chart/chartAction.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ export const dynamicPluginControlsReady =
428428
datasource: state.explore.datasource || { type: 'table' },
429429
initialTimeRange: getInitialTimeRange(
430430
!state.explore.slice,
431-
state.explore.form_data,
431+
state.explore.datasource as { extra?: string } | undefined,
432432
state.common?.conf?.DEFAULT_TIME_FILTER as string | undefined,
433433
),
434434
};

‎superset-frontend/src/explore/actions/hydrateExplore.test.ts‎

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -624,17 +624,41 @@ test.each([
624624
},
625625
);
626626

627-
test.each([VizType.Table, 'cal_heatmap'])(
628-
'a range supplied to a new %s chart wins over the dataset default',
629-
vizType => {
627+
test('a new cal_heatmap chart keeps the time range supplied in its form data', () => {
628+
const query = hydrateNewChartQuery({
629+
vizType: 'cal_heatmap',
630+
formData: { time_range: NO_TIME_RANGE },
631+
});
632+
expect(getTimeFilter(query)).toEqual(['created_at', NO_TIME_RANGE]);
633+
});
634+
635+
// SQL Lab opens new charts with `time_range: 'No filter'`
636+
// (EXPLORE_CHART_DEFAULT), which must not override either default.
637+
test.each([
638+
['the dataset default', datasetExtra, datasetRange],
639+
['the instance default', '{}', instanceRange],
640+
])(
641+
"a new Table chart opened with time_range 'No filter' starts with %s",
642+
(_, extra, range) => {
630643
const query = hydrateNewChartQuery({
631-
vizType,
644+
extra,
632645
formData: { time_range: NO_TIME_RANGE },
633646
});
634-
expect(getTimeFilter(query)).toEqual(['created_at', NO_TIME_RANGE]);
647+
expect(getTimeFilter(query)).toEqual(['created_at', range]);
635648
},
636649
);
637650

651+
test('a saved Table chart with its own time_range and no time filter opens with the instance default', () => {
652+
const query = hydrateNewChartQuery({
653+
slice: exploreInitialData.slice,
654+
formData: { time_range: 'Last week' },
655+
});
656+
expect(getTimeFilter({ ...query, time_range: undefined })).toEqual([
657+
'created_at',
658+
instanceRange,
659+
]);
660+
});
661+
638662
test('an ad hoc time filter supplied to a new chart is its only time filter', () => {
639663
const filter = {
640664
expressionType: 'SIMPLE',

‎superset-frontend/src/explore/actions/hydrateExplore.ts‎

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
ExplorePageInitialData,
2727
ExplorePageState,
2828
} from 'src/explore/types';
29-
import { getChartKey, getInitialTimeRange } from 'src/explore/exploreUtils';
29+
import { getChartKey } from 'src/explore/exploreUtils';
3030
import { getControlsState, handleDeprecatedControls } from 'src/explore/store';
3131
import { Dispatch } from 'redux';
3232
import {
@@ -133,11 +133,12 @@ export const hydrateExplore =
133133
initialFormData.viz_type =
134134
getUrlParam(URL_PARAMS.vizType) || defaultVizType;
135135
}
136+
const initialTimeRange =
137+
(isNewChart && getDatasetDefaultTimeFilter(dataset)) ||
138+
common?.conf?.DEFAULT_TIME_FILTER ||
139+
NO_TIME_RANGE;
136140
if (!initialFormData.time_range) {
137-
initialFormData.time_range =
138-
(isNewChart && getDatasetDefaultTimeFilter(dataset)) ||
139-
common?.conf?.DEFAULT_TIME_FILTER ||
140-
NO_TIME_RANGE;
141+
initialFormData.time_range = initialTimeRange;
141142
}
142143
if (
143144
initialFormData.include_time &&
@@ -253,14 +254,7 @@ export const hydrateExplore =
253254
// apply initial mapStateToProps for all controls, must execute AFTER
254255
// bootstrapState has initialized `controls`. Order of execution is not
255256
// guaranteed, so controls shouldn't rely on each other's mapped state.
256-
const controlPanelState = {
257-
...exploreState,
258-
initialTimeRange: getInitialTimeRange(
259-
isNewChart,
260-
initialFormData,
261-
common?.conf?.DEFAULT_TIME_FILTER,
262-
),
263-
};
257+
const controlPanelState = { ...exploreState, initialTimeRange };
264258
Object.entries(exploreState.controls).forEach(([key, controlState]) => {
265259
exploreState.controls[key] = applyMapStateToPropsToControl(
266260
controlState,

‎superset-frontend/src/explore/exploreUtils/index.ts‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ import {
3131
JsonObject,
3232
NO_TIME_RANGE,
3333
} from '@superset-ui/core';
34+
import {
35+
Dataset,
36+
getDatasetDefaultTimeFilter,
37+
} from '@superset-ui/chart-controls';
3438
import { availableDomains } from 'src/utils/hostNamesConfig';
3539
import { safeStringify } from 'src/utils/safeStringify';
3640
import { optionLabel } from 'src/utils/common';
@@ -533,16 +537,19 @@ export function formatSelectOptions<T extends { toString(): string }>(
533537
}
534538

535539
/**
536-
* Returns the range of the time filter Explore adds while it opens a chart: a
537-
* new chart's initial range, or the instance default for a saved chart so that
538-
* opening it doesn't change its query.
540+
* Returns the range of the time filter Explore adds while it opens a chart:
541+
* the dataset's default time filter for a new chart, otherwise the instance
542+
* default. A `time_range` carried in the form data (for example SQL Lab's
543+
* `'No filter'`) is ignored, as it was before dataset defaults existed.
539544
*/
540545
export function getInitialTimeRange(
541546
isNewChart: boolean,
542-
formData: QueryFormData,
547+
dataset: Pick<Dataset, 'extra'> | null | undefined,
543548
defaultTimeFilter?: string,
544549
): string {
545550
return (
546-
(isNewChart && formData.time_range) || defaultTimeFilter || NO_TIME_RANGE
551+
(isNewChart && getDatasetDefaultTimeFilter(dataset)) ||
552+
defaultTimeFilter ||
553+
NO_TIME_RANGE
547554
);
548555
}

0 commit comments

Comments
 (0)