Skip to content

Commit

Permalink
fix(filters): Adds a fix for saving time range adhoc_filters (apache#…
Browse files Browse the repository at this point in the history
…30581)

Co-authored-by: Muhammad Musfir <[email protected]>
  • Loading branch information
ObservabilityTeam and Muhammad Musfir authored Oct 15, 2024
1 parent 6ede327 commit 2c3ba95
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 14 deletions.
60 changes: 58 additions & 2 deletions superset-frontend/src/explore/actions/saveModalActions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ import sinon from 'sinon';
import fetchMock from 'fetch-mock';
import { Dispatch } from 'redux';
import { ADD_TOAST } from 'src/components/MessageToasts/actions';
import { DatasourceType, QueryFormData } from '@superset-ui/core';
import {
DatasourceType,
QueryFormData,
SimpleAdhocFilter,
} from '@superset-ui/core';
import {
createDashboard,
createSlice,
Expand All @@ -31,6 +35,7 @@ import {
getSlicePayload,
PayloadSlice,
} from './saveModalActions';
import { Operators } from '../constants';

// Define test constants and mock data using imported types
const sliceId = 10;
Expand Down Expand Up @@ -594,6 +599,7 @@ describe('getSlicePayload', () => {
},
],
};

const formDataWithAdhocFiltersWithExtra: QueryFormData = {
...formDataWithNativeFilters,
viz_type: 'mixed_timeseries',
Expand Down Expand Up @@ -625,11 +631,61 @@ describe('getSlicePayload', () => {
owners as [],
formDataFromSliceWithAdhocFilterB,
);

expect(JSON.parse(result.params as string).adhoc_filters).toEqual(
formDataFromSliceWithAdhocFilterB.adhoc_filters,
);
expect(JSON.parse(result.params as string).adhoc_filters).toEqual(
expect(JSON.parse(result.params as string).adhoc_filters_b).toEqual(
formDataFromSliceWithAdhocFilterB.adhoc_filters_b,
);
});

test('should return the correct payload when formDataFromSliceWithAdhocFilter has no time range filters in mixed chart', () => {
const formDataFromSliceWithAdhocFilterB: QueryFormData = {
...formDataFromSlice,
adhoc_filters: [],
adhoc_filters_b: [],
};

const formDataWithAdhocFiltersWithExtra: QueryFormData = {
...formDataWithNativeFilters,
viz_type: 'mixed_timeseries',
adhoc_filters: [
{
clause: 'WHERE',
subject: 'year',
operator: 'TEMPORAL_RANGE',
comparator: 'No filter',
expressionType: 'SIMPLE',
isExtra: true,
},
],
adhoc_filters_b: [
{
clause: 'WHERE',
subject: 'year',
operator: 'TEMPORAL_RANGE',
comparator: 'No filter',
expressionType: 'SIMPLE',
isExtra: true,
},
],
};
const result = getSlicePayload(
sliceName,
formDataWithAdhocFiltersWithExtra,
dashboards,
owners as [],
formDataFromSliceWithAdhocFilterB,
);

const hasTemporalRange = (
JSON.parse(result.params as string).adhoc_filters_b || []
).some(
(filter: SimpleAdhocFilter) =>
filter.operator === Operators.TemporalRange,
);

expect(hasTemporalRange).toBe(true);
});
});
29 changes: 17 additions & 12 deletions superset-frontend/src/explore/actions/saveModalActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,24 @@ export const getSlicePayload = (
}

if (!hasTemporalRangeFilter(adhocFilters)) {
formDataWithNativeFilters.adhoc_filters?.forEach(
(filter: SimpleAdhocFilter) => {
if (filter.operator === Operators.TemporalRange && filter.isExtra) {
if (!adhocFilters.adhoc_filters) {
adhocFilters.adhoc_filters = [];
}
adhocFilters.adhoc_filters.push({
...filter,
comparator: 'No filter',
});
}
},
const adhocFiltersKeys = Object.keys(formDataWithNativeFilters).filter(
key => ADHOC_FILTER_REGEX.test(key),
);
adhocFiltersKeys?.forEach(filtersKey => {
formDataWithNativeFilters[filtersKey]?.forEach(
(filter: SimpleAdhocFilter) => {
if (filter.operator === Operators.TemporalRange && filter.isExtra) {
if (!adhocFilters[filtersKey]) {
adhocFilters[filtersKey] = [];
}
adhocFilters[filtersKey].push({
...filter,
comparator: 'No filter',
});
}
},
);
});
}
const formData = {
...formDataWithNativeFilters,
Expand Down

0 comments on commit 2c3ba95

Please sign in to comment.