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

refactor: Unify shared datasources reducers and actions #20645

Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -28,6 +28,7 @@ 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 @@ -66,6 +67,7 @@ export interface Dataset {
id: number;
type: DatasourceType;
columns: ColumnMeta[];
column_types?: GenericDataType[];
zhaoyongjie marked this conversation as resolved.
Show resolved Hide resolved
metrics: Metric[];
column_format: Record<string, string>;
verbose_map: Record<string, string>;
Expand All @@ -79,6 +81,7 @@ export interface Dataset {
description: string | null;
uid?: string;
owners?: Owner[];
table_name?: string;
zhaoyongjie marked this conversation as resolved.
Show resolved Hide resolved
}

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/dashboard/reducers/datasources';
import datasources from 'src/datasource/reducer';
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 { DatasourcesAction } from 'src/dashboard/actions/datasources';
import { DatasourcesActionType } from 'src/datasource/actions';
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 === DatasourcesAction.SET_DATASOURCES) {
if (action.type === DatasourcesActionType.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 @@ -88,9 +88,7 @@ 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 & { column_types: GenericDataType[] },
) => {
export const hasTemporalColumns = (dataset: Dataset) => {
const columnTypes = ensureIsArray(dataset?.column_types);
return (
columnTypes.length === 0 || columnTypes.includes(GenericDataType.TEMPORAL)
michael-s-molina marked this conversation as resolved.
Show resolved Hide resolved
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 { Datasource } from 'src/dashboard/types';
import { Dataset } from '@superset-ui/chart-controls';
/**
* 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 { Datasource } from 'src/dashboard/types';
* specific language governing permissions and limitations
* under the License.
*/
export const PLACEHOLDER_DATASOURCE: Datasource = {
export const PLACEHOLDER_DATASOURCE: Dataset = {
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/dashboard/actions/datasources';
import { setDatasources } from 'src/datasource/actions';

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/dashboard/actions/datasources';
import { setDatasources } from 'src/datasource/actions';
import injectCustomCss from 'src/dashboard/util/injectCustomCss';
import setupPlugins from 'src/setup/setupPlugins';
import { UserWithPermissionsAndRoles } from 'src/types/bootstrapTypes';
Expand Down
43 changes: 0 additions & 43 deletions superset-frontend/src/dashboard/reducers/datasources.ts

This file was deleted.

8 changes: 1 addition & 7 deletions superset-frontend/src/dashboard/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
ChartProps,
DataMaskStateWithId,
ExtraFormData,
GenericDataType,
JsonObject,
NativeFiltersState,
} from '@superset-ui/core';
Expand Down Expand Up @@ -84,13 +83,8 @@ 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]: Datasource;
[key: string]: Dataset;
};

/** Root state of redux */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,51 +18,21 @@
*/
import { Dispatch } from 'redux';
import { SupersetClient } from '@superset-ui/core';
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,
};
}
import { Dataset } from '@superset-ui/chart-controls';
import { RootState } from 'src/dashboard/types';
import { setDatasource } from 'src/datasource/actions';

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

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

return SupersetClient.get({
endpoint: `/superset/fetch_datasource_metadata?datasourceKey=${key}`,
}).then(({ json }) => dispatch(setDatasource(json as Datasource, key)));
}).then(({ json }) => dispatch(setDatasource(json as Dataset)));
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,39 @@
* specific language governing permissions and limitations
* under the License.
*/

import { Dispatch } from 'redux';
import { Dataset } from '@superset-ui/chart-controls';
import { updateFormDataByDatasource } from './exploreActions';
import { ExplorePageState } from '../types';

export const SET_DATASOURCE = 'SET_DATASOURCE';
export interface SetDatasource {
type: string;
datasource: Dataset;
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;
};

export function initDatasources(datasources: { [key: string]: Dataset }) {
return { type: DatasourcesActionType.INIT_DATASOURCES, datasources };
}

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

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

export const datasourcesActions = {
setDatasource,
changeDatasource,
};

export type AnyDatasourcesAction = SetDatasource;
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,32 @@
* specific language governing permissions and limitations
* under the License.
*/
import { Dataset } from '@superset-ui/chart-controls';
import { keyBy } from 'lodash';
import { getDatasourceUid } from 'src/utils/getDatasourceUid';
import {
AnyDatasourcesAction,
SET_DATASOURCE,
} from '../actions/datasourcesActions';
import { HYDRATE_EXPLORE, HydrateExplore } from '../actions/hydrateExplore';
DatasourcesAction,
DatasourcesActionType,
} from 'src/datasource/actions';
import { Dataset } from '@superset-ui/chart-controls';

export default function datasourcesReducer(
// TODO: change type to include other datasource types
datasources: { [key: string]: Dataset },
action: AnyDatasourcesAction | HydrateExplore,
datasources: { [key: string]: Dataset } | undefined,
action: DatasourcesAction,
) {
if (action.type === SET_DATASOURCE) {
if (action.type === DatasourcesActionType.INIT_DATASOURCES) {
return { ...action.datasources };
}
if (action.type === DatasourcesActionType.SET_DATASOURCES) {
return {
...datasources,
[getDatasourceUid(action.datasource)]: action.datasource,
...keyBy(action.datasources, 'uid'),
};
}
if (action.type === HYDRATE_EXPLORE) {
return { ...(action as HydrateExplore).data.datasources };
if (action.type === DatasourcesActionType.SET_DATASOURCE) {
return {
...datasources,
[getDatasourceUid(action.datasource)]: action.datasource,
};
}
return datasources || {};
}
Loading