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(Filters): Apply native & cross filters on common columns #30438

Merged
merged 21 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift click to select a range
bc85d17
fix(Filters): Apply native & cross filters on common columns
geido Sep 30, 2024
0f661c6
chore(Dashboard): Implement affected logic to highlight
geido Oct 3, 2024
8091a09
Merge branch 'master' of https://github.com/apache/superset into geid…
geido Oct 3, 2024
08ea4be
Merge branch 'master' of https://github.com/apache/superset into geid…
geido Oct 4, 2024
d2314e3
fix(Scope): Only include charts in scope
geido Oct 4, 2024
7fbb688
fix(CrossFilters): Check for deleted filter
geido Oct 4, 2024
2f180ae
fix(Scopes): Check for scopes array
geido Oct 7, 2024
2b6dd47
chore(Tests): Align tests with best practices
geido Oct 7, 2024
8fffbec
fix(Filters): Apply filters on underlying dataset columns
geido Oct 8, 2024
2106416
fix(Columns): Include all column names
geido Oct 8, 2024
bdef07f
fix(Dataset): Combine all columns
geido Oct 8, 2024
433ae8e
fix(Dataset): Remove unnecessary ref to column names
geido Oct 8, 2024
b5a69dd
Merge branch 'master' of https://github.com/apache/superset into geid…
geido Oct 8, 2024
03ebe9c
fix(CrossFilters): Check for actual cross filter target
geido Oct 8, 2024
b2e0b38
fix(CrossFilter): Relaunch query for removed cross-filter
geido Oct 8, 2024
f293ae1
Merge branch 'master' of https://github.com/apache/superset into geid…
geido Oct 10, 2024
e45da52
fix(Filters): Let backend handle jinja and adhoc cols
geido Oct 10, 2024
8de9cd6
chore(Todo): Remove todo comment
geido Oct 10, 2024
a18cddb
chore(Tests): Enhance tests
geido Oct 11, 2024
0d750b5
fix(Expression): Defult to array for non-array expression
geido Oct 14, 2024
1e471fc
chore(Type): Improve AppliedNativeFilterType
geido Oct 15, 2024
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
Prev Previous commit
Next Next commit
chore(Dashboard): Implement affected logic to highlight
  • Loading branch information
geido committed Oct 3, 2024
commit 0f661c6b291ab17a46f3a1acf8e1debd11328b82
1 change: 1 addition & 0 deletions superset-frontend/spec/fixtures/mockNativeFilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 133,7 @@ export const singleNativeFiltersState = {
id: [NATIVE_FILTER_ID],
name: 'eth',
type: 'text',
filterType: 'filter_select',
targets: [{ datasetId: 13, column: { name: 'ethnic_minority' } }],
defaultDataMask: {
filterState: {
Expand Down
22 changes: 21 additions & 1 deletion superset-frontend/src/dashboard/components/Dashboard.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 37,9 @@ import { dashboardLayout } from 'spec/fixtures/mockDashboardLayout';
import dashboardState from 'spec/fixtures/mockDashboardState';
import { sliceEntitiesForChart as sliceEntities } from 'spec/fixtures/mockSliceEntities';
import { getAllActiveFilters } from 'src/dashboard/util/activeAllDashboardFilters';
import { getRelatedCharts } from 'src/dashboard/util/getRelatedCharts';

jest.mock('src/dashboard/util/getRelatedCharts');

describe('Dashboard', () => {
const props = {
Expand Down Expand Up @@ -130,6 133,7 @@ describe('Dashboard', () => {

afterEach(() => {
refreshSpy.restore();
jest.clearAllMocks();
});

it('should not call refresh when is editMode', () => {
Expand All @@ -153,6 157,9 @@ describe('Dashboard', () => {
});

it('should call refresh when native filters changed', () => {
getRelatedCharts.mockReturnValue({
[NATIVE_FILTER_ID]: [230],
});
wrapper.setProps({
activeFilters: {
...OVERRIDE_FILTERS,
Expand All @@ -170,7 177,7 @@ describe('Dashboard', () => {
[NATIVE_FILTER_ID]: {
scope: [230],
values: extraFormData,
filterType: null,
filterType: 'filter_select',
targets: [
{
datasetId: 13,
Expand All @@ -184,6 191,13 @@ describe('Dashboard', () => {
});

it('should call refresh if a filter is added', () => {
getRelatedCharts.mockReturnValue({
'1_region': [1],
'2_country_name': [1, 2],
'3_region': [1],
'3_country_name': [],
gender: [1],
});
const newFilter = {
gender: { values: ['boy', 'girl'], scope: [1] },
};
Expand All @@ -195,6 209,12 @@ describe('Dashboard', () => {
});

it('should call refresh if a filter is removed', () => {
getRelatedCharts.mockReturnValue({
'1_region': [1],
'2_country_name': [1, 2],
'3_region': [1],
'3_country_name': [],
});
wrapper.setProps({
activeFilters: {},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 54,7 @@ export const getAllActiveFilters = ({
chartConfiguration?.[filterId]?.crossFilters?.chartsInScope ??
allSliceIds ??
[];
const filterType = nativeFilters?.[filterId]?.filterType ?? null;
const filterType = nativeFilters?.[filterId]?.filterType;
const targets = nativeFilters?.[filterId]?.targets ?? scope;
// Iterate over all roots to find all affected charts
activeFilters[filterId] = {
Expand Down
224 changes: 111 additions & 113 deletions superset-frontend/src/dashboard/util/getRelatedCharts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,134 21,132 @@ import { DatasourceType } from '@superset-ui/core';
import { DatasourcesState } from '../types';
import { getRelatedCharts } from './getRelatedCharts';

describe('getRelatedCharts', () => {
const slices = [
{ datasource: 'ds1', slice_id: 1 },
{ datasource: 'ds2', slice_id: 2 },
];
const slices = {
'1': { datasource: 'ds1', slice_id: 1 },
'2': { datasource: 'ds2', slice_id: 2 },
};

const datasources: DatasourcesState = {
ds1: {
uid: 'ds1',
datasource_name: 'ds1',
table_name: 'table1',
description: '',
id: 100,
columns: [{ column_name: 'column1' }, { column_name: 'column2' }],
column_types: [],
type: DatasourceType.Table,
metrics: [],
column_formats: {},
currency_formats: {},
verbose_map: {},
main_dttm_col: '',
filter_select_enabled: true,
},
ds2: {
uid: 'ds2',
datasource_name: 'ds2',
table_name: 'table2',
description: '',
id: 200,
columns: [{ column_name: 'column3' }, { column_name: 'column4' }],
column_types: [],
type: DatasourceType.Table,
metrics: [],
column_formats: {},
currency_formats: {},
verbose_map: {},
main_dttm_col: '',
filter_select_enabled: true,
const datasources: DatasourcesState = {
ds1: {
uid: 'ds1',
datasource_name: 'ds1',
table_name: 'table1',
description: '',
id: 100,
columns: [{ column_name: 'column1' }, { column_name: 'column2' }],
column_types: [],
type: DatasourceType.Table,
metrics: [],
column_formats: {},
currency_formats: {},
verbose_map: {},
main_dttm_col: '',
filter_select_enabled: true,
},
ds2: {
uid: 'ds2',
datasource_name: 'ds2',
table_name: 'table2',
description: '',
id: 200,
columns: [{ column_name: 'column3' }, { column_name: 'column4' }],
column_types: [],
type: DatasourceType.Table,
metrics: [],
column_formats: {},
currency_formats: {},
verbose_map: {},
main_dttm_col: '',
filter_select_enabled: true,
},
};

test('Return chart ids matching the dataset id', () => {
const filters = {
filterKey1: {
filterType: 'filter_select',
targets: [
{
column: { name: 'column1' },
datasetId: 100,
},
],
},
};

it('should return chart ids matching the dataset id', () => {
const filters = {
filterKey1: {
filterType: 'filter_select',
targets: [
{
column: { name: 'column1' },
datasetId: 100,
},
],
},
};

const result = getRelatedCharts(filters, slices, datasources);
expect(result).toEqual({
filterKey1: [1],
});
const result = getRelatedCharts(filters, slices, datasources);
expect(result).toEqual({
filterKey1: [1],
});
});

it('should return chart ids matching the column name', () => {
const filters = {
filterKey1: {
filterType: 'filter_select',
targets: [
{
column: { name: 'column3' },
datasetId: 999,
},
],
},
};
test('Return chart ids matching the column name', () => {
const filters = {
filterKey1: {
filterType: 'filter_select',
targets: [
{
column: { name: 'column3' },
datasetId: 999,
},
],
},
};

const result = getRelatedCharts(filters, slices, datasources);
expect(result).toEqual({
filterKey1: [2],
});
const result = getRelatedCharts(filters, slices, datasources);
expect(result).toEqual({
filterKey1: [2],
});
});

it('should return chart ids when column display name matches', () => {
const filters = {
filterKey1: {
filterType: 'filter_select',
targets: [
{
column: { displayName: 'column4' },
datasetId: 999,
},
],
},
};
test('Return chart ids when column display name matches', () => {
const filters = {
filterKey1: {
filterType: 'filter_select',
targets: [
{
column: { displayName: 'column4' },
datasetId: 999,
},
],
},
};

const result = getRelatedCharts(filters, slices, datasources);
expect(result).toEqual({
filterKey1: [2],
});
const result = getRelatedCharts(filters, slices, datasources);
expect(result).toEqual({
filterKey1: [2],
});
});

it('should return scope when filterType is not filter_select', () => {
const filters = {
filterKey1: {
filterType: 'filter_time',
scope: [3, 4],
},
};
test('Return scope when filterType is not filter_select', () => {
const filters = {
filterKey1: {
filterType: 'filter_time',
scope: [3, 4],
},
};

const result = getRelatedCharts(filters, slices, datasources);
expect(result).toEqual({
filterKey1: [3, 4],
});
const result = getRelatedCharts(filters, slices, datasources);
expect(result).toEqual({
filterKey1: [3, 4],
});
});

it('should return an empty array if no matching charts found', () => {
const filters = {
filterKey1: {
filterType: 'filter_select',
targets: [
{
column: { name: 'nonexistent_column' },
datasetId: 300,
},
],
},
};
test('Return an empty array if no matching charts found', () => {
const filters = {
filterKey1: {
filterType: 'filter_select',
targets: [
{
column: { name: 'nonexistent_column' },
datasetId: 300,
},
],
},
};

const result = getRelatedCharts(filters, slices, datasources);
expect(result).toEqual({
filterKey1: [],
});
const result = getRelatedCharts(filters, slices, datasources);
expect(result).toEqual({
filterKey1: [],
});
});
18 changes: 11 additions & 7 deletions superset-frontend/src/dashboard/util/getRelatedCharts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 27,14 @@ type Slice = {

function getRelatedChartsForSelectFilter(
filter: Filter,
slices: Slice[],
slices: Record<string, Slice>,
datasources: DatasourcesState,
) {
return Object.values(slices)
.filter(({ datasource }) => {
const chartDatasource = datasources[datasource];
if (!chartDatasource) return false;
const { column, datasetId } = filter.targets[0];
const { column, datasetId } = filter.targets[0] ?? {};

return (
chartDatasource.id === datasetId ||
Expand All @@ -49,7 49,7 @@ function getRelatedChartsForSelectFilter(
}
function getRelatedChartsForCrossFilter(
filterKey: string,
slices: Slice[],
slices: Record<string, Slice>,
datasources: DatasourcesState,
): number[] {
const sourceSlice = slices[filterKey];
Expand All @@ -60,7 60,7 @@ function getRelatedChartsForCrossFilter(

return Object.values(slices)
.filter(slice => {
if (slice.slice_id === Number[filterKey]) return false;
if (slice.slice_id === Number(filterKey)) return false;
const targetDatasource = datasources[slice.datasource];
if (!targetDatasource) return false;
if (targetDatasource === sourceDatasource) return true;
Expand All @@ -76,7 76,7 @@ function getRelatedChartsForCrossFilter(

export function getRelatedCharts(
filters: Object,
slices: Slice[],
slices: Record<string, Slice>,
datasources: DatasourcesState,
) {
return Object.entries(filters).reduce((acc, [filterKey, filter]) => {
Expand All @@ -90,7 90,7 @@ export function getRelatedCharts(
),
};
}
if (filter.filterType === null) {
if (!filter.filterType) {
return {
...acc,
[filterKey]: getRelatedChartsForCrossFilter(
Expand All @@ -102,7 102,11 @@ export function getRelatedCharts(
}
return {
...acc,
[filterKey]: filter.scope,
[filterKey]: filter.chartsInScope
? filter.chartsInScope.filter(
(chartId: number) => !filter.scope.excluded.includes(chartId),
)
: filter.scope,
};
}, {});
}
Loading