Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* Revert "fix: Error when saving datasource from Explore (apache#20749)"

This reverts commit 92f3621.

* Revert "refactor: Unify shared datasources reducers and actions (apache#20645)"

This reverts commit 2a70540.
  • Loading branch information
michael-s-molina committed Jul 20, 2022
1 parent 84b852c commit 3311128
Show file tree
Hide file tree
Showing 21 changed files with 259 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import type {
QueryFormData,
QueryFormMetric,
QueryResponse,
GenericDataType,
} from '@superset-ui/core';
import { sharedControls } from './shared-controls';
import sharedControlComponents from './shared-controls/components';
Expand Down Expand Up @@ -67,7 +66,6 @@ export interface Dataset {
id: number;
type: DatasourceType;
columns: ColumnMeta[];
column_types?: GenericDataType[];
metrics: Metric[];
column_format: Record<string, string>;
verbose_map: Record<string, string>;
Expand All @@ -81,7 +79,6 @@ export interface Dataset {
description: string | null;
uid?: string;
owners?: Owner[];
table_name?: string;
}

export interface ControlPanelState {
Expand Down
2 changes: 1 addition & 1 deletion superset-frontend/spec/helpers/reducerIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import dashboardInfo from 'src/dashboard/reducers/dashboardInfo';
import dashboardState from 'src/dashboard/reducers/dashboardState';
import dashboardFilters from 'src/dashboard/reducers/dashboardFilters';
import nativeFilters from 'src/dashboard/reducers/nativeFilters';
import datasources from 'src/datasource/reducer';
import datasources from 'src/dashboard/reducers/datasources';
import sliceEntities from 'src/dashboard/reducers/sliceEntities';
import dashboardLayout from 'src/dashboard/reducers/undoableDashboardLayout';
import messageToasts from 'src/components/MessageToasts/reducers';
Expand Down
4 changes: 2 additions & 2 deletions superset-frontend/src/components/Chart/chartReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/* eslint camelcase: 0 */
import { t } from '@superset-ui/core';
import { HYDRATE_DASHBOARD } from 'src/dashboard/actions/hydrate';
import { DatasourcesActionType } from 'src/datasource/actions';
import { DatasourcesAction } from 'src/dashboard/actions/datasources';
import { ChartState } from 'src/explore/types';
import { getFormDataFromControls } from 'src/explore/controlUtils';
import { HYDRATE_EXPLORE } from 'src/explore/actions/hydrateExplore';
Expand Down Expand Up @@ -198,7 +198,7 @@ export default function chartReducer(
if (action.type === HYDRATE_DASHBOARD || action.type === HYDRATE_EXPLORE) {
return { ...action.data.charts };
}
if (action.type === DatasourcesActionType.SET_DATASOURCES) {
if (action.type === DatasourcesAction.SET_DATASOURCES) {
return Object.fromEntries(
Object.entries(charts).map(([chartId, chart]) => [
chartId,
Expand Down
2 changes: 1 addition & 1 deletion superset-frontend/src/dashboard/actions/dashboardState.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ import serializeFilterScopes from 'src/dashboard/util/serializeFilterScopes';
import { getActiveFilters } from 'src/dashboard/util/activeDashboardFilters';
import { safeStringify } from 'src/utils/safeStringify';
import { FeatureFlag, isFeatureEnabled } from 'src/featureFlags';
import { fetchDatasourceMetadata } from 'src/dashboard/util/fetchDatasourceMetadata';
import { UPDATE_COMPONENTS_PARENTS_LIST } from './dashboardLayout';
import {
setChartConfiguration,
dashboardInfoChanged,
SET_CHART_CONFIG_COMPLETE,
} from './dashboardInfo';
import { fetchDatasourceMetadata } from './datasources';
import {
addFilter,
removeFilter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,51 @@
*/
import { Dispatch } from 'redux';
import { SupersetClient } from '@superset-ui/core';
import { Dataset } from '@superset-ui/chart-controls';
import { RootState } from 'src/dashboard/types';
import { setDatasource } from 'src/datasource/actions';
import { Datasource, RootState } from 'src/dashboard/types';

// update datasources index for Dashboard
export enum DatasourcesAction {
SET_DATASOURCES = 'SET_DATASOURCES',
SET_DATASOURCE = 'SET_DATASOURCE',
}

export type DatasourcesActionPayload =
| {
type: DatasourcesAction.SET_DATASOURCES;
datasources: Datasource[] | null;
}
| {
type: DatasourcesAction.SET_DATASOURCE;
key: Datasource['uid'];
datasource: Datasource;
};

export function setDatasources(datasources: Datasource[] | null) {
return {
type: DatasourcesAction.SET_DATASOURCES,
datasources,
};
}

export function setDatasource(datasource: Datasource, key: string) {
return {
type: DatasourcesAction.SET_DATASOURCE,
key,
datasource,
};
}

export function fetchDatasourceMetadata(key: string) {
return (dispatch: Dispatch, getState: () => RootState) => {
const { datasources } = getState();
const datasource = datasources[key];

if (datasource) {
return dispatch(setDatasource(datasource));
return dispatch(setDatasource(datasource, key));
}

return SupersetClient.get({
endpoint: `/superset/fetch_datasource_metadata?datasourceKey=${key}`,
}).then(({ json }) => dispatch(setDatasource(json as Dataset)));
}).then(({ json }) => dispatch(setDatasource(json as Datasource, key)));
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ export const datasetToSelectOption = (

// TODO: add column_types field to Dataset
// We return true if column_types is undefined or empty as a precaution against backend failing to return column_types
export const hasTemporalColumns = (dataset: Dataset) => {
export const hasTemporalColumns = (
dataset: Dataset & { column_types: GenericDataType[] },
) => {
const columnTypes = ensureIsArray(dataset?.column_types);
return (
columnTypes.length === 0 || columnTypes.includes(GenericDataType.TEMPORAL)
Expand Down
4 changes: 2 additions & 2 deletions superset-frontend/src/dashboard/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable import/prefer-default-export */
import { DatasourceType } from '@superset-ui/core';
import { Dataset } from '@superset-ui/chart-controls';
import { Datasource } from 'src/dashboard/types';
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
Expand All @@ -19,7 +19,7 @@ import { Dataset } from '@superset-ui/chart-controls';
* specific language governing permissions and limitations
* under the License.
*/
export const PLACEHOLDER_DATASOURCE: Dataset = {
export const PLACEHOLDER_DATASOURCE: Datasource = {
id: 0,
type: DatasourceType.Table,
uid: '_placeholder_',
Expand Down
2 changes: 1 addition & 1 deletion superset-frontend/src/dashboard/containers/Dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
addSliceToDashboard,
removeSliceFromDashboard,
} from 'src/dashboard/actions/dashboardState';
import { setDatasources } from 'src/datasource/actions';
import { setDatasources } from 'src/dashboard/actions/datasources';

import { triggerQuery } from 'src/components/Chart/chartAction';
import { logEvent } from 'src/logger/actions';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
useDashboardDatasets,
} from 'src/hooks/apiResources';
import { hydrateDashboard } from 'src/dashboard/actions/hydrate';
import { setDatasources } from 'src/datasource/actions';
import { setDatasources } from 'src/dashboard/actions/datasources';
import injectCustomCss from 'src/dashboard/util/injectCustomCss';
import setupPlugins from 'src/setup/setupPlugins';
import { UserWithPermissionsAndRoles } from 'src/types/bootstrapTypes';
Expand Down
43 changes: 43 additions & 0 deletions superset-frontend/src/dashboard/reducers/datasources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { keyBy } from 'lodash';
import { DatasourcesState } from 'src/dashboard/types';
import {
DatasourcesActionPayload,
DatasourcesAction,
} from '../actions/datasources';

export default function datasourcesReducer(
datasources: DatasourcesState | undefined,
action: DatasourcesActionPayload,
) {
if (action.type === DatasourcesAction.SET_DATASOURCES) {
return {
...datasources,
...keyBy(action.datasources, 'uid'),
};
}
if (action.type === DatasourcesAction.SET_DATASOURCE) {
return {
...datasources,
[action.key]: action.datasource,
};
}
return datasources || {};
}
8 changes: 7 additions & 1 deletion superset-frontend/src/dashboard/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
ChartProps,
DataMaskStateWithId,
ExtraFormData,
GenericDataType,
JsonObject,
NativeFiltersState,
} from '@superset-ui/core';
Expand Down Expand Up @@ -83,8 +84,13 @@ export type DashboardInfo = {

export type ChartsState = { [key: string]: Chart };

export type Datasource = Dataset & {
uid: string;
column_types: GenericDataType[];
table_name: string;
};
export type DatasourcesState = {
[key: string]: Dataset;
[key: string]: Datasource;
};

/** Root state of redux */
Expand Down
85 changes: 85 additions & 0 deletions superset-frontend/src/explore/actions/datasourcesActions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { DatasourceType } from '@superset-ui/core';
import {
setDatasource,
changeDatasource,
} from 'src/explore/actions/datasourcesActions';
import datasourcesReducer from '../reducers/datasourcesReducer';
import { updateFormDataByDatasource } from './exploreActions';

const CURRENT_DATASOURCE = {
id: 1,
uid: '1__table',
type: DatasourceType.Table,
columns: [],
metrics: [],
column_format: {},
verbose_map: {},
main_dttm_col: '__timestamp',
// eg. ['["ds", true]', 'ds [asc]']
datasource_name: 'test datasource',
description: null,
};

const NEW_DATASOURCE = {
id: 2,
type: DatasourceType.Table,
columns: [],
metrics: [],
column_format: {},
verbose_map: {},
main_dttm_col: '__timestamp',
// eg. ['["ds", true]', 'ds [asc]']
datasource_name: 'test datasource',
description: null,
};

const defaultDatasourcesReducerState = {
[CURRENT_DATASOURCE.uid]: CURRENT_DATASOURCE,
};

test('sets new datasource', () => {
const newState = datasourcesReducer(
defaultDatasourcesReducerState,
setDatasource(NEW_DATASOURCE),
);
expect(newState).toEqual({
...defaultDatasourcesReducerState,
'2__table': NEW_DATASOURCE,
});
});

test('change datasource action', () => {
const dispatch = jest.fn();
const getState = jest.fn(() => ({
explore: {
datasource: CURRENT_DATASOURCE,
},
}));
// ignore getState type check - we dont need explore.datasource field for this test
// @ts-ignore
changeDatasource(NEW_DATASOURCE)(dispatch, getState);
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenNthCalledWith(1, setDatasource(NEW_DATASOURCE));
expect(dispatch).toHaveBeenNthCalledWith(
2,
updateFormDataByDatasource(CURRENT_DATASOURCE, NEW_DATASOURCE),
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,34 @@
* specific language governing permissions and limitations
* under the License.
*/
import { Dataset } from '@superset-ui/chart-controls';

export enum DatasourcesActionType {
INIT_DATASOURCES = 'INIT_DATASOURCES',
SET_DATASOURCE = 'SET_DATASOURCE',
SET_DATASOURCES = 'SET_DATASOURCES',
}

export type DatasourcesAction =
| {
type: DatasourcesActionType.INIT_DATASOURCES;
datasources: { [key: string]: Dataset };
}
| {
type: DatasourcesActionType.SET_DATASOURCES;
datasources: Dataset[] | null;
}
| {
type: DatasourcesActionType.SET_DATASOURCE;
datasource: Dataset;
};
import { Dispatch } from 'redux';
import { Dataset } from '@superset-ui/chart-controls';
import { updateFormDataByDatasource } from './exploreActions';
import { ExplorePageState } from '../types';

export function initDatasources(datasources: { [key: string]: Dataset }) {
return { type: DatasourcesActionType.INIT_DATASOURCES, datasources };
export const SET_DATASOURCE = 'SET_DATASOURCE';
export interface SetDatasource {
type: string;
datasource: Dataset;
}

export function setDatasource(datasource: Dataset) {
return { type: DatasourcesActionType.SET_DATASOURCE, datasource };
return { type: SET_DATASOURCE, datasource };
}

export function setDatasources(datasources: Dataset[] | null) {
return {
type: DatasourcesActionType.SET_DATASOURCES,
datasources,
export function changeDatasource(newDatasource: Dataset) {
return function (dispatch: Dispatch, getState: () => ExplorePageState) {
const {
explore: { datasource: prevDatasource },
} = getState();
dispatch(setDatasource(newDatasource));
dispatch(updateFormDataByDatasource(prevDatasource, newDatasource));
};
}

export const datasourcesActions = {
setDatasource,
changeDatasource,
};

export type AnyDatasourcesAction = SetDatasource;
Loading

0 comments on commit 3311128

Please sign in to comment.