Skip to content

Commit

Permalink
pass datasource_type and datasource_id to form_data
Browse files Browse the repository at this point in the history
  • Loading branch information
eschutho committed May 11, 2022
1 parent 3a379af commit f5aa420
Show file tree
Hide file tree
Showing 38 changed files with 441 additions and 171 deletions.
11 changes: 8 additions & 3 deletions superset-frontend/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,12 @@ export const URL_PARAMS = {
name: 'slice_id',
type: 'string',
},
datasetId: {
name: 'dataset_id',
datasourceId: {
name: 'datasource_id',
type: 'string',
},
datasourceType: {
name: 'datasource_type',
type: 'string',
},
force: {
Expand All @@ -84,7 +88,8 @@ export const URL_PARAMS = {
export const RESERVED_CHART_URL_PARAMS: string[] = [
URL_PARAMS.formDataKey.name,
URL_PARAMS.sliceId.name,
URL_PARAMS.datasetId.name,
URL_PARAMS.datasourceId.name,
URL_PARAMS.datasourceType.name,
];
export const RESERVED_DASHBOARD_URL_PARAMS: string[] = [
URL_PARAMS.nativeFilters.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ export default class Chart extends React.Component {
: undefined;
const key = await postFormData(
this.props.datasource.id,
this.props.datasource.type,
this.props.formData,
this.props.slice.slice_id,
nextTabId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,24 @@ const ExplorePanelContainer = styled.div`
`;

const updateHistory = debounce(
async (formData, datasetId, isReplace, standalone, force, title, tabId) => {
async (
formData,
datasourceId,
datasourceType,
isReplace,
standalone,
force,
title,
tabId,
) => {
const payload = { ...formData };
const chartId = formData.slice_id;
const additionalParam = {};
if (chartId) {
additionalParam[URL_PARAMS.sliceId.name] = chartId;
} else {
additionalParam[URL_PARAMS.datasetId.name] = datasetId;
additionalParam[URL_PARAMS.datasourceId.name] = datasourceId;
additionalParam[URL_PARAMS.datasourceType.name] = datasourceType;
}

const urlParams = payload?.url_params || {};
Expand All @@ -193,11 +203,24 @@ const updateHistory = debounce(
let key;
let stateModifier;
if (isReplace) {
key = await postFormData(datasetId, formData, chartId, tabId);
key = await postFormData(
datasourceId,
datasourceType,
formData,
chartId,
tabId,
);
stateModifier = 'replaceState';
} else {
key = getUrlParam(URL_PARAMS.formDataKey);
await putFormData(datasetId, key, formData, chartId, tabId);
await putFormData(
datasourceId,
datasourceType,
key,
formData,
chartId,
tabId,
);
stateModifier = 'pushState';
}
const url = mountExploreUrl(
Expand Down Expand Up @@ -249,11 +272,12 @@ function ExploreViewContainer(props) {
dashboardId: props.dashboardId,
}
: props.form_data;
const datasetId = props.datasource.id;
const { id: datasourceId, type: datasourceType } = props.datasource;

updateHistory(
formData,
datasetId,
datasourceId,
datasourceType,
isReplace,
props.standalone,
props.force,
Expand All @@ -265,6 +289,7 @@ function ExploreViewContainer(props) {
props.dashboardId,
props.form_data,
props.datasource.id,
props.datasource.type,
props.standalone,
props.force,
tabId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,10 @@ class DatasourceControl extends React.PureComponent {
const isMissingDatasource = datasource.id == null;
let isMissingParams = false;
if (isMissingDatasource) {
const datasetId = getUrlParam(URL_PARAMS.datasetId);
const datasourceId = getUrlParam(URL_PARAMS.datasourceId);
const datasourceType = getUrlParam(URL_PARAMS.datasourceType);
const sliceId = getUrlParam(URL_PARAMS.sliceId);
if (!datasetId && !sliceId) {
if (!datasourceId && !sliceId && !datasourceType) {
isMissingParams = true;
}
}
Expand Down
29 changes: 22 additions & 7 deletions superset-frontend/src/explore/exploreUtils/formData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import { omit } from 'lodash';
import { SupersetClient, JsonObject } from '@superset-ui/core';

type Payload = {
dataset_id: number;
datasource_id: number;
datasource_type: string;
form_data: string;
chart_id?: number;
};
Expand All @@ -42,12 +43,14 @@ const assembleEndpoint = (key?: string, tabId?: string) => {
};

const assemblePayload = (
datasetId: number,
datasourceId: number,
datasourceType: string,
formData: JsonObject,
chartId?: number,
) => {
const payload: Payload = {
dataset_id: datasetId,
datasource_id: datasourceId,
datasource_type: datasourceType,
form_data: JSON.stringify(sanitizeFormData(formData)),
};
if (chartId) {
Expand All @@ -57,24 +60,36 @@ const assemblePayload = (
};

export const postFormData = (
datasetId: number,
datasourceId: number,
datasourceType: string,
formData: JsonObject,
chartId?: number,
tabId?: string,
): Promise<string> =>
SupersetClient.post({
endpoint: assembleEndpoint(undefined, tabId),
jsonPayload: assemblePayload(datasetId, formData, chartId),
jsonPayload: assemblePayload(
datasourceId,
datasourceType,
formData,
chartId,
),
}).then(r => r.json.key);

export const putFormData = (
datasetId: number,
datasourceId: number,
datasourceType: string,
key: string,
formData: JsonObject,
chartId?: number,
tabId?: string,
): Promise<string> =>
SupersetClient.put({
endpoint: assembleEndpoint(key, tabId),
jsonPayload: assemblePayload(datasetId, formData, chartId),
jsonPayload: assemblePayload(
datasourceId,
datasourceType,
formData,
chartId,
),
}).then(r => r.json.message);
2 changes: 1 addition & 1 deletion superset/commands/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,4 @@ class DatasourceNotFoundValidationError(ValidationError):
status = 404

def __init__(self) -> None:
super().__init__([_("Dataset does not exist")], field_name="datasource_id")
super().__init__([_("Datasource does not exist")], field_name="datasource_id")
4 changes: 3 additions & 1 deletion superset/databases/dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from superset.models.dashboard import Dashboard
from superset.models.slice import Slice
from superset.models.sql_lab import TabState
from superset.utils.core import DatasourceType

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -75,7 +76,8 @@ def get_related_objects(cls, database_id: int) -> Dict[str, Any]:
charts = (
db.session.query(Slice)
.filter(
Slice.datasource_id.in_(dataset_ids), Slice.datasource_type == "table"
Slice.datasource_id.in_(dataset_ids),
Slice.datasource_type == DatasourceType.TABLE,
)
.all()
)
Expand Down
4 changes: 3 additions & 1 deletion superset/datasets/dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from superset.models.core import Database
from superset.models.dashboard import Dashboard
from superset.models.slice import Slice
from superset.utils.core import DatasourceType
from superset.views.base import DatasourceFilter

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -56,7 +57,8 @@ def get_related_objects(database_id: int) -> Dict[str, Any]:
charts = (
db.session.query(Slice)
.filter(
Slice.datasource_id == database_id, Slice.datasource_type == "table"
Slice.datasource_id == database_id,
Slice.datasource_type == DatasourceType.TABLE,
)
.all()
)
Expand Down
8 changes: 6 additions & 2 deletions superset/examples/birth_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from superset.models.core import Database
from superset.models.dashboard import Dashboard
from superset.models.slice import Slice
from superset.utils.core import DatasourceType

from ..utils.database import get_example_database
from .helpers import (
Expand Down Expand Up @@ -205,13 +206,16 @@ def create_slices(tbl: SqlaTable, admin_owner: bool) -> Tuple[List[Slice], List[
if admin_owner:
slice_props = dict(
datasource_id=tbl.id,
datasource_type="table",
datasource_type=DatasourceType.TABLE,
owners=[admin],
created_by=admin,
)
else:
slice_props = dict(
datasource_id=tbl.id, datasource_type="table", owners=[], created_by=admin
datasource_id=tbl.id,
datasource_type=DatasourceType.TABLE,
owners=[],
created_by=admin,
)

print("Creating some slices")
Expand Down
3 changes: 2 additions & 1 deletion superset/examples/country_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from superset import db
from superset.connectors.sqla.models import SqlMetric
from superset.models.slice import Slice
from superset.utils.core import DatasourceType

from .helpers import (
get_example_data,
Expand Down Expand Up @@ -112,7 +113,7 @@ def load_country_map_data(only_metadata: bool = False, force: bool = False) -> N
slc = Slice(
slice_name="Birth in France by department in 2016",
viz_type="country_map",
datasource_type="table",
datasource_type=DatasourceType.TABLE,
datasource_id=tbl.id,
params=get_slice_json(slice_data),
)
Expand Down
15 changes: 8 additions & 7 deletions superset/examples/deck.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from superset import db
from superset.models.dashboard import Dashboard
from superset.models.slice import Slice
from superset.utils.core import DatasourceType

from .helpers import (
get_slice_json,
Expand Down Expand Up @@ -206,7 +207,7 @@ def load_deck_dash() -> None: # pylint: disable=too-many-statements
slc = Slice(
slice_name="Scatterplot",
viz_type="deck_scatter",
datasource_type="table",
datasource_type=DatasourceType.TABLE,
datasource_id=tbl.id,
params=get_slice_json(slice_data),
)
Expand Down Expand Up @@ -241,7 +242,7 @@ def load_deck_dash() -> None: # pylint: disable=too-many-statements
slc = Slice(
slice_name="Screen grid",
viz_type="deck_screengrid",
datasource_type="table",
datasource_type=DatasourceType.TABLE,
datasource_id=tbl.id,
params=get_slice_json(slice_data),
)
Expand Down Expand Up @@ -277,7 +278,7 @@ def load_deck_dash() -> None: # pylint: disable=too-many-statements
slc = Slice(
slice_name="Hexagons",
viz_type="deck_hex",
datasource_type="table",
datasource_type=DatasourceType.TABLE,
datasource_id=tbl.id,
params=get_slice_json(slice_data),
)
Expand Down Expand Up @@ -314,7 +315,7 @@ def load_deck_dash() -> None: # pylint: disable=too-many-statements
slc = Slice(
slice_name="Grid",
viz_type="deck_grid",
datasource_type="table",
datasource_type=DatasourceType.TABLE,
datasource_id=tbl.id,
params=get_slice_json(slice_data),
)
Expand Down Expand Up @@ -403,7 +404,7 @@ def load_deck_dash() -> None: # pylint: disable=too-many-statements
slc = Slice(
slice_name="Polygons",
viz_type="deck_polygon",
datasource_type="table",
datasource_type=DatasourceType.TABLE,
datasource_id=polygon_tbl.id,
params=get_slice_json(slice_data),
)
Expand Down Expand Up @@ -453,7 +454,7 @@ def load_deck_dash() -> None: # pylint: disable=too-many-statements
slc = Slice(
slice_name="Arcs",
viz_type="deck_arc",
datasource_type="table",
datasource_type=DatasourceType.TABLE,
datasource_id=db.session.query(table)
.filter_by(table_name="flights")
.first()
Expand Down Expand Up @@ -505,7 +506,7 @@ def load_deck_dash() -> None: # pylint: disable=too-many-statements
slc = Slice(
slice_name="Path",
viz_type="deck_path",
datasource_type="table",
datasource_type=DatasourceType.TABLE,
datasource_id=db.session.query(table)
.filter_by(table_name="bart_lines")
.first()
Expand Down
7 changes: 4 additions & 3 deletions superset/examples/energy.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from superset import db
from superset.connectors.sqla.models import SqlMetric
from superset.models.slice import Slice
from superset.utils.core import DatasourceType

from .helpers import (
get_example_data,
Expand Down Expand Up @@ -81,7 +82,7 @@ def load_energy(
slc = Slice(
slice_name="Energy Sankey",
viz_type="sankey",
datasource_type="table",
datasource_type=DatasourceType.TABLE,
datasource_id=tbl.id,
params=textwrap.dedent(
"""\
Expand All @@ -105,7 +106,7 @@ def load_energy(
slc = Slice(
slice_name="Energy Force Layout",
viz_type="graph_chart",
datasource_type="table",
datasource_type=DatasourceType.TABLE,
datasource_id=tbl.id,
params=textwrap.dedent(
"""\
Expand All @@ -129,7 +130,7 @@ def load_energy(
slc = Slice(
slice_name="Heatmap",
viz_type="heatmap",
datasource_type="table",
datasource_type=DatasourceType.TABLE,
datasource_id=tbl.id,
params=textwrap.dedent(
"""\
Expand Down
3 changes: 2 additions & 1 deletion superset/examples/long_lat.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import superset.utils.database as database_utils
from superset import db
from superset.models.slice import Slice
from superset.utils.core import DatasourceType

from .helpers import (
get_example_data,
Expand Down Expand Up @@ -113,7 +114,7 @@ def load_long_lat_data(only_metadata: bool = False, force: bool = False) -> None
slc = Slice(
slice_name="Mapbox Long/Lat",
viz_type="mapbox",
datasource_type="table",
datasource_type=DatasourceType.TABLE,
datasource_id=tbl.id,
params=get_slice_json(slice_data),
)
Expand Down
Loading

0 comments on commit f5aa420

Please sign in to comment.