This is an automated email from the ASF dual-hosted git repository. villebro pushed a commit to branch 7.0 in repository https://gitbox.apache.org/repos/asf/superset.git
commit e9684e4cf1061f1eed65092f01d039263a505e2e Author: Mike Bridge <[email protected]> AuthorDate: Tue Nov 17 17:36:31 2026 -0600 fix(dataset): scope Usage charts by datasource type (#46258) (cherry picked from commit ea58fc9669ecc169f62b044f52d9a0973cd79222) --- .../DatasourceEditor/DatasourceEditor.tsx | 12 ++++- .../tests/DatasourceEditor.test.tsx | 61 ++++++++++++++++++++++ tests/unit_tests/charts/test_filters.py | 38 ++++++++++---- 3 files changed, 101 insertions(+), 10 deletions(-) diff --git a/superset-frontend/src/components/Datasource/components/DatasourceEditor/DatasourceEditor.tsx b/superset-frontend/src/components/Datasource/components/DatasourceEditor/DatasourceEditor.tsx index 989e0ed68da..99527179d33 100644 --- a/superset-frontend/src/components/Datasource/components/DatasourceEditor/DatasourceEditor.tsx +++ b/superset-frontend/src/components/Datasource/components/DatasourceEditor/DatasourceEditor.tsx @@ -1411,6 +1411,11 @@ function DatasourceEditor({ opr: 'eq', value: datasource.id, }, + { + col: 'datasource_type', + opr: 'eq', + value: datasource.datasource_type || datasource.type, + }, ], order_column: sortColumn, order_direction: sortDirection, @@ -1467,7 +1472,12 @@ function DatasourceEditor({ abortControllers.current.fetchUsageData = null; } }, - [datasource.id, addDangerToast], + [ + datasource.id, + datasource.datasource_type, + datasource.type, + addDangerToast, + ], ); const handleTabSelect = useCallback((key: string) => { diff --git a/superset-frontend/src/components/Datasource/components/DatasourceEditor/tests/DatasourceEditor.test.tsx b/superset-frontend/src/components/Datasource/components/DatasourceEditor/tests/DatasourceEditor.test.tsx index 2c54031750e..9d2491d1033 100644 --- a/superset-frontend/src/components/Datasource/components/DatasourceEditor/tests/DatasourceEditor.test.tsx +++ b/superset-frontend/src/components/Datasource/components/DatasourceEditor/tests/DatasourceEditor.test.tsx @@ -17,6 +17,7 @@ * under the License. */ import fetchMock from 'fetch-mock'; +import rison from 'rison'; import { fireEvent, screen, @@ -71,6 +72,66 @@ test('renders Tabs', async () => { expect(screen.getByTestId('edit-dataset-tabs')).toBeInTheDocument(); }); +test('Usage excludes semantic-view charts whose datasource id matches the dataset', async () => { + const testProps = createProps(); + const charts = [ + { + id: 101, + slice_name: 'Dataset chart', + datasource_id: testProps.datasource.id, + datasource_type: DatasourceType.Table, + }, + { + id: 102, + slice_name: 'Semantic view chart', + datasource_id: testProps.datasource.id, + datasource_type: DatasourceType.SemanticView, + }, + { + id: 103, + slice_name: 'Other dataset chart', + datasource_id: testProps.datasource.id + 1, + datasource_type: DatasourceType.Table, + }, + ]; + fetchMock.removeRoutes(); + fetchMock.get(DATASOURCE_ENDPOINT, []); + fetchMock.get('glob:*/api/v1/chart/*', ({ url }) => { + const { filters } = rison.decode<{ + filters: { + col: 'datasource_id' | 'datasource_type'; + opr: string; + value: string | number; + }[]; + }>(new URL(url, 'http://localhost').searchParams.get('q') ?? ''); + const matches = charts.filter(chart => + filters.every(filter => chart[filter.col] === filter.value), + ); + return { + count: matches.length, + ids: matches.map(chart => chart.id), + result: matches.map(chart => ({ + ...chart, + url: `/explore/?slice_id=${chart.id}`, + changed_on_delta_humanized: 'a day ago', + changed_by: null, + dashboards: [], + })), + }; + }); + setupDatasourceEditorMocks(); + + await asyncRender(testProps); + await userEvent.click(screen.getByRole('tab', { name: /usage/i })); + + expect( + await screen.findByRole('link', { name: 'Dataset chart' }), + ).toBeVisible(); + expect(screen.queryByText('Semantic view chart')).not.toBeInTheDocument(); + expect(screen.queryByText('Other dataset chart')).not.toBeInTheDocument(); + expect(screen.getByRole('tab', { name: /usage/i })).toHaveTextContent('1'); +}); + test('recommends a registered client route for the default URL', async () => { await asyncRender(createProps()); diff --git a/tests/unit_tests/charts/test_filters.py b/tests/unit_tests/charts/test_filters.py index 0db9271bf83..da51d444ede 100644 --- a/tests/unit_tests/charts/test_filters.py +++ b/tests/unit_tests/charts/test_filters.py @@ -87,11 +87,33 @@ def test_chart_datasource_filter_rejects_malformed_identity(identity: object) -> @pytest.mark.parametrize( - "identity, expected", + "filters, expected", [ - (7, "table chart"), - ("7__table", "table chart"), - ("7__semantic_view", "semantic chart"), + ( + [{"col": "datasource_id", "opr": "chart_datasource", "value": 7}], + "table chart", + ), + ( + [{"col": "datasource_id", "opr": "chart_datasource", "value": "7__table"}], + "table chart", + ), + ( + [ + { + "col": "datasource_id", + "opr": "chart_datasource", + "value": "7__semantic_view", + } + ], + "semantic chart", + ), + ( + [ + {"col": "datasource_id", "opr": "eq", "value": 7}, + {"col": "datasource_type", "opr": "eq", "value": "table"}, + ], + "table chart", + ), ], ) @with_feature_flags(SEMANTIC_LAYERS=True) @@ -100,10 +122,10 @@ def test_chart_list_datasource_identity( full_api_access: None, mocker: MockerFixture, session: Session, - identity: int | str, + filters: list[dict[str, str | int]], expected: str, ) -> None: - """The registered list operator separates same-ID sources and accepts old links.""" + """Registered list operators separate same-ID sources and accept old links.""" # Avoid app-init imports: models and API registration need the app fixture. from superset.charts.api import ChartRestApi from superset.models.slice import Slice @@ -124,9 +146,7 @@ def test_chart_list_datasource_identity( query: str = prison.dumps( { "columns": ["id", "slice_name"], - "filters": [ - {"col": "datasource_id", "opr": "chart_datasource", "value": identity} - ], + "filters": filters, } ) response: TestResponse = client.get("/api/v1/chart/", query_string={"q": query})
