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

feat: mechanism for re-saving charts #25196

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions superset-frontend/src/dashboard/actions/hydrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export const hydrateDashboard =
owners: slice.owners,
modified: slice.modified,
changed_on: new Date(slice.changed_on).getTime(),
force_save: slice.force_save,
};

sliceIds.add(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import cx from 'classnames';
import React from 'react';
import PropTypes from 'prop-types';
import { styled, t, logging } from '@superset-ui/core';
import { SupersetClient, styled, t, logging } from '@superset-ui/core';
import { debounce, isEqual } from 'lodash';
import { withRouter } from 'react-router-dom';

Expand Down Expand Up @@ -86,6 +86,7 @@ const propTypes = {
datasetsStatus: PropTypes.oneOf(['loading', 'error', 'complete']),
isInView: PropTypes.bool,
emitCrossFilters: PropTypes.bool,
updateSlices: PropTypes.func.isRequired,
};

const defaultProps = {
Expand Down Expand Up @@ -219,6 +220,23 @@ class Chart extends React.Component {
const descriptionHeight = this.getDescriptionHeight();
this.setState({ descriptionHeight });
}

if (this.props.slice.force_save) {
const { slice, updateSlices } = this.props;
SupersetClient.put({
endpoint: `/api/v1/chart/${slice.slice_id}`,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
query_context: slice.query_context,
params: JSON.stringify(slice.form_data),
force_save: false,
}),
}).then(response =>
updateSlices({
[slice.slice_id]: { ...slice, ...response.json.result },
}),
);
}
}

componentWillUnmount() {
Expand Down
2 changes: 2 additions & 0 deletions superset-frontend/src/dashboard/containers/Chart.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
unsetFocusedFilterField,
} from 'src/dashboard/actions/dashboardState';
import { updateComponents } from 'src/dashboard/actions/dashboardLayout';
import { updateSlices } from 'src/dashboard/actions/sliceEntities';
import { changeFilter } from 'src/dashboard/actions/dashboardFilters';
import {
addSuccessToast,
Expand Down Expand Up @@ -116,6 +117,7 @@ function mapDispatchToProps(dispatch) {
unsetFocusedFilterField,
refreshChart,
logEvent,
updateSlices,
},
dispatch,
);
Expand Down
1 change: 1 addition & 0 deletions superset-frontend/src/explore/actions/saveModalActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export const getSlicePayload = (
ownState: null,
}),
),
force_save: false,
};
return payload;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,28 @@ const ExploreChartPanel = ({
chart.chartStatus !== 'failed' &&
ensureIsArray(chart.queriesResponse).length > 0;

/* When feature flags are toggled we might need to resave the chart to update all
* required parameters. This can be done by setting `Slice.force_save` to false in a
* migration or manually. */
const updateChart = useCallback(
async function overwriteChart() {
if (slice?.force_save) {
await actions.updateSlice(slice, slice.slice_name);
// TODO (betodealmeida): better refresh logic
window.location.reload();
}
},
[slice],
);

useEffect(() => {
updateChart();
}, [updateChart]);

/* Orignally charts could be saved without `Slice.query_context`, but the field is
* needed for alerts & reports and can only be computed by the visualization Javascript
* code. Because of this, when we encounter a chart where the field is null we compute
* and store it. */
const updateQueryContext = useCallback(
async function fetchChartData() {
if (slice && slice.query_context === null) {
Expand Down
1 change: 1 addition & 0 deletions superset-frontend/src/types/Chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export type Slice = {
query_context?: object;
is_managed_externally: boolean;
owners?: number[];
force_save?: boolean;
};

export default Chart;
2 changes: 2 additions & 0 deletions superset/charts/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ def ensure_thumbnails_enabled(self) -> Optional[Response]:
"viz_type",
"query_context",
"is_managed_externally",
"force_save",
"tags.id",
"tags.name",
"tags.type",
Expand Down Expand Up @@ -194,6 +195,7 @@ def ensure_thumbnails_enabled(self) -> Optional[Response]:
"thumbnail_url",
"url",
"viz_type",
"force_save",
"tags.id",
"tags.name",
"tags.type",
Expand Down
15 changes: 15 additions & 0 deletions superset/charts/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ class ChartEntityResponseSchema(Schema):
description_markeddown = fields.String(
metadata={"description": description_markeddown_description}
)
force_save = fields.Boolean(
metadata={
"description": "Does the chart need to be re-saved to update metadata?"
}
)
form_data = fields.Dict(metadata={"description": form_data_description})
slice_url = fields.String(metadata={"description": slice_url_description})
certified_by = fields.String(metadata={"description": certified_by_description})
Expand Down Expand Up @@ -229,6 +234,11 @@ class ChartPostSchema(Schema):
)
is_managed_externally = fields.Boolean(allow_none=True, dump_default=False)
external_url = fields.String(allow_none=True)
force_save = fields.Boolean(
metadata={
"description": "Does the chart need to be re-saved to update metadata?"
}
)


class ChartPutSchema(Schema):
Expand Down Expand Up @@ -285,6 +295,11 @@ class ChartPutSchema(Schema):
is_managed_externally = fields.Boolean(allow_none=True, dump_default=False)
external_url = fields.String(allow_none=True)
tags = fields.Nested(TagSchema, many=True)
force_save = fields.Boolean(
metadata={
"description": "Does the chart need to be re-saved to update metadata?"
}
)


class ChartGetDatasourceObjectDataResponseSchema(Schema):
Expand Down
5 changes: 5 additions & 0 deletions superset/explore/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ class SliceSchema(Schema):
slice_id = fields.Integer(metadata={"description": "The slice ID."})
slice_name = fields.String(metadata={"description": "The slice name."})
slice_url = fields.String(metadata={"description": "The slice URL."})
force_save = fields.Boolean(
metadata={
"description": "Does the chart need to be re-saved to update metadata?"
}
)


class ExploreContextSchema(Schema):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 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.
"""Add force_save column to charts

Revision ID: 122965576ebd
Revises: ec54aca4c8a2
Create Date: 2023-09-05 16:44:58.627402

"""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "122965576ebd"
down_revision = "ec54aca4c8a2"


def upgrade():
op.add_column(
"slices",
sa.Column("force_save", sa.Boolean(), nullable=True, default=False),
)


def downgrade():
op.drop_column("slices", "force_save")
5 changes: 5 additions & 0 deletions superset/models/slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ class Slice( # pylint: disable=too-many-public-methods
lazy="subquery",
)

# force the chart to re-save itself; this is useful when the backend has missing
# information that can only be computed by the frontend
force_save = Column(Boolean, nullable=True, default=False)

token = ""

export_fields = [
Expand Down Expand Up @@ -247,6 +251,7 @@ def data(self) -> dict[str, Any]:
"certified_by": self.certified_by,
"certification_details": self.certification_details,
"is_managed_externally": self.is_managed_externally,
"force_save": self.force_save,
}

@property
Expand Down
1 change: 1 addition & 0 deletions tests/integration_tests/charts/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,7 @@ def test_get_chart(self):
"viz_type": None,
"query_context": None,
"is_managed_externally": False,
"force_save": False,
}
data = json.loads(rv.data.decode("utf-8"))
self.assertIn("changed_on_delta_humanized", data["result"])
Expand Down