Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Time range filter applied on a dashboard is not persisting to the chart explore #22920

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ const getExploreFormData = (overrides: JsonObject = {}) => ({
sqlExpression: "city = 'Warsaw'",
filterOptionName: '567',
},
{
clause: 'WHERE' as const,
expressionType: 'SIMPLE' as const,
operator: 'TEMPORAL_RANGE' as const,
subject: 'ds',
comparator: 'No filter',
filterOptionName: '678',
},
],
adhoc_filters_b: [
{
Expand Down Expand Up @@ -158,6 +166,14 @@ const getExpectedResultFormData = (overrides: JsonObject = {}) => ({
sqlExpression: "city = 'Warsaw'",
filterOptionName: '567',
},
{
clause: 'WHERE',
expressionType: 'SIMPLE',
operator: 'TEMPORAL_RANGE',
subject: 'ds',
comparator: 'Last month',
filterOptionName: expect.any(String),
},
{
clause: 'WHERE',
expressionType: 'SIMPLE',
Expand Down Expand Up @@ -279,7 +295,7 @@ const getExpectedResultFormData = (overrides: JsonObject = {}) => ({
...overrides,
});

it('merges dashboard context form data with explore form data', () => {
test('merges dashboard context form data with explore form data', () => {
const fullFormData = getFormDataWithDashboardContext(
getExploreFormData(),
getDashboardFormData(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,25 @@ const mergeNativeFiltersToFormData = (
return nativeFiltersData;
};

const applyTimeRangeFilters = (
dashboardFormData: JsonObject,
adhocFilters: AdhocFilter[],
) => {
const extraFormData = dashboardFormData.extra_form_data || {};
if ('time_range' in extraFormData) {
return adhocFilters.map((filter: SimpleAdhocFilter) => {
if (filter.operator === 'TEMPORAL_RANGE') {
return {
...filter,
comparator: extraFormData.time_range,
};
}
return filter;
});
}
return adhocFilters;
};

export const getFormDataWithDashboardContext = (
exploreFormData: QueryFormData,
dashboardContextFormData: JsonObject,
Expand All @@ -194,14 +213,18 @@ export const getFormDataWithDashboardContext = (
.reduce(
(acc, key) => ({
...acc,
[key]: removeAdhocFilterDuplicates([
...ensureIsArray(exploreFormData[key]),
...ensureIsArray(filterBoxData[key]),
...ensureIsArray(nativeFiltersData[key]),
]),
[key]: applyTimeRangeFilters(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if mergeNativeFiltersToFormData function (in the same file) isn't a better place for this transformation

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was my initial thinking but I also want to cover filter box cases. mergeNativeFiltersToFormData does not give me easy access to the filters list, as it returns an object that may contain different keys such as adhoc_filters and adhoc_filters_b

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it 👍

dashboardContextFormData,
removeAdhocFilterDuplicates([
...ensureIsArray(exploreFormData[key]),
...ensureIsArray(filterBoxData[key]),
...ensureIsArray(nativeFiltersData[key]),
]),
),
}),
{},
);

return {
...exploreFormData,
...dashboardContextFormData,
Expand Down