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

[fix] new dashboard state #5213

Merged
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
6 changes: 2 additions & 4 deletions superset/assets/src/dashboard/reducers/getInitialState.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default function(bootstrapData) {
// dashboard layout
const { position_json: positionJson } = dashboard;
const shouldConvertToV2 =
!positionJson || positionJson[DASHBOARD_VERSION_KEY] !== 'v2';
positionJson && positionJson[DASHBOARD_VERSION_KEY] !== 'v2';

const layout = shouldConvertToV2
? layoutConverter(dashboard)
Expand All @@ -69,7 +69,6 @@ export default function(bootstrapData) {

// find root level chart container node for newly-added slices
const parentId = findFirstParentContainerId(layout);
let hasUnsavedChanges = false;
const chartQueries = {};
const slices = {};
const sliceIds = new Set();
Expand Down Expand Up @@ -112,7 +111,6 @@ export default function(bootstrapData) {
layout[chartHolder.id] = chartHolder;
rowContainer.children.push(chartHolder.id);
chartIdToLayoutId[chartHolder.meta.chartId] = chartHolder.id;
hasUnsavedChanges = true;
}
}

Expand Down Expand Up @@ -173,7 +171,7 @@ export default function(bootstrapData) {
css: dashboard.css || '',
editMode: dashboard.dash_edit_perm && editMode,
showBuilderPane: dashboard.dash_edit_perm && editMode,
hasUnsavedChanges,
hasUnsavedChanges: false,
maxUndoHistoryExceeded: false,
isV2Preview: shouldConvertToV2,
},
Expand Down
2 changes: 1 addition & 1 deletion superset/assets/src/explore/components/SaveModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class SaveModal extends React.Component {
.then((data) => {
// Go to new slice url or dashboard url
if (gotodash) {
window.location = supersetURL(data.dashboard, { edit: 'true' });
window.location = supersetURL(data.dashboard);
} else {
window.location = data.slice.slice_url;
}
Expand Down
37 changes: 25 additions & 12 deletions superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1553,6 +1553,8 @@ def copy_dash(self, dashboard_id):

dash.owners = [g.user] if g.user else []
dash.dashboard_title = data['dashboard_title']

is_v2_dash = Superset._is_v2_dash(data['positions'])
if data['duplicate_slices']:
# Duplicating slices as well, mapping old ids to new ones
old_to_new_sliceids = {}
Expand All @@ -1562,17 +1564,24 @@ def copy_dash(self, dashboard_id):
session.add(new_slice)
session.flush()
new_slice.dashboards.append(dash)
old_to_new_sliceids[slc.id] = new_slice.id
old_to_new_sliceids['{}'.format(slc.id)] = \
'{}'.format(new_slice.id)

# update chartId of layout entities
for value in data['positions'].values():
if (
isinstance(value, dict) and value.get('meta') and
value.get('meta').get('chartId')
):
old_id = value.get('meta').get('chartId')
new_id = old_to_new_sliceids[old_id]
value['meta']['chartId'] = new_id
# in v2_dash positions json data, chartId should be integer,
# while in older version slice_id is string type
if is_v2_dash:
for value in data['positions'].values():
if (
isinstance(value, dict) and value.get('meta') and
value.get('meta').get('chartId')
):
old_id = '{}'.format(value.get('meta').get('chartId'))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@graceguo-supercat can you add a comment about strings vs ints here so the logic is documented somewhere?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in v2 dashboard position json, chartId is integer type:

"DASHBOARD_CHART_TYPE-H10M97Xk-7": {
        "children": [],
        "id": "DASHBOARD_CHART_TYPE-H10M97Xk-7",
        "meta": {
            "chartId": 136,
            "height": 50,
            "sliceName": "Treemap",
            "width": 8
        },
        "type": "DASHBOARD_CHART_TYPE"
    },

in older dashboard position json data, slice_id is string type:

   {
        "slice_id": "49", 
        "size_x": 8, 
        "size_y": 16, 
        "v": 1, 
        "col": 33, 
        "row": 24
    }, 

new_id = int(old_to_new_sliceids[old_id])
value['meta']['chartId'] = new_id
else:
for d in data['positions']:
d['slice_id'] = old_to_new_sliceids[d['slice_id']]
else:
dash.slices = original_dash.slices
dash.params = original_dash.params
Expand Down Expand Up @@ -1602,13 +1611,17 @@ def save_dash(self, dashboard_id):
return 'SUCCESS'

@staticmethod
def _set_dash_metadata(dashboard, data):
positions = data['positions']
is_v2_dash = (
def _is_v2_dash(positions):
return (
isinstance(positions, dict) and
positions.get('DASHBOARD_VERSION_KEY') == 'v2'
)

@staticmethod
def _set_dash_metadata(dashboard, data):
positions = data['positions']
is_v2_dash = Superset._is_v2_dash(positions)

# @TODO remove upon v1 deprecation
if not is_v2_dash:
positions = data['positions']
Expand Down