Repository navigation
fix(dashboard): copy a layout that omits the source's current charts - #45472
Conversation
Opening a dashboard version as a new dashboard sends that version's layout to the copy endpoint. When the layout omitted a chart the source dashboard has today, the copy failed with a generic 400 and nothing was logged. copy_dashboard linked the source's current charts to the new dashboard before set_dash_metadata rebuilt membership from the layout. Dropping a chart then left a pending removal on that chart's unloaded dashboards collection, so the flush deleted a dashboard_slices row that was never inserted and raised StaleDataError. Link the source's charts only when no layout is sent, and log the cause when a copy fails.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #45472 +/- ##
=======================================
Coverage 83.59% 83.60%
=======================================
Files 3050 3050
Lines 200206 200206
Branches 46682 46682
=======================================
+ Hits 167368 167373 +5
+ Misses 29595 29589 -6
- Partials 3243 3244 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
CodeAnt PR Risk: Low Risk
Assessed commit: |
Restoring a dashboard version whose layout lacks a chart the dashboard gained since, or has one it lost since, sets the dashboard's charts once from the version. It doesn't add and then drop charts the way the copy did. Pin that for both directions, for both at once and for the empty baseline, checking the live membership, the restored layout and the membership history.
fitzee
left a comment
There was a problem hiding this comment.
LGTM, no blocking issues. The root cause is right: linking original_dash.slices and then letting set_dash_metadata replace the collection queues an append and a remove on the unloaded Slice.dashboards backref, and the flush then issues a DELETE for a dashboard_slices row that was never inserted. Membership ends up the same as before for every input that used to succeed, because set_dash_metadata already rebuilt it wholesale from positions. The fix just removes the redundant first link. Reverting only superset/daos/dashboard.py to master makes test_copy_without_charts_dropped_from_layout fail with StaleDataError: DELETE statement on table 'dashboard_slices' expected to delete 2 row(s); Only 0 were matched. With the fix, the whole unit file passes (12/12). CI on 5481b9c is green: test-postgres, test-mysql, test-sqlite and the unit tests pass, and the skipped jobs are frontend/docs only.
Three non-blocking follow-ups are inline. All of them predate this PR, but the version-fork flow makes them easier to hit.
| ) | ||
| value["meta"]["chartId"] = new_id | ||
| else: | ||
| elif metadata.get("positions") is None: |
There was a problem hiding this comment.
Non-blocking, pre-existing. With this change the posted layout is the only thing that decides the copy's membership. Nothing on the server checks that the layout's chart IDs belong to the source or that the caller can access them. A non-duplicate copy whose layout references a chart from an unrelated dashboard links that chart to the new dashboard (confirmed in a SQLite DAO harness). createDashboardFromSnapshot filters unreachable charts through fetchReachableChartIds, but that runs in the browser and isn't access control. PUT on a dashboard you own has the same gap, so this PR doesn't widen it. That matters because dashboard membership feeds the dashboardId datasource grant in raise_for_access (viewer-promiscuous / embedded paths). It's worth a follow-up that limits layout chart IDs to the source's members, plus charts from version history the caller can access.
There was a problem hiding this comment.
Agreed. This predates the PR (PUT on a dashboard you own has the same gap), so I've kept it out of this fix and it's tracked as a follow-up: limit the layout's chart IDs to the source's members, plus charts from version history the caller can access, on the server.
| # flush delete dashboard_slices rows that were never inserted. | ||
| dash.slices = original_dash.slices | ||
|
|
||
| dash.params = original_dash.params |
There was a problem hiding this comment.
Non-blocking, pre-existing. set_dash_metadata starts from dash.params, which is the source's current metadata here, and only overlays the keys in the payload. For a version fork, any key the old snapshot lacks comes from today's dashboard. Keys that are carried through also aren't pruned to the layout's charts. With a layout that keeps only chart A, chart_configuration, timed_refresh_immune_slices and expanded_slices entries for the omitted chart B survive in the copy's json_metadata. Only filter_scopes and default_filters are filtered by slice_ids. The frontend seems to tolerate dangling cross-filter entries, but the fork isn't a faithful copy of the version.
There was a problem hiding this comment.
Right, the copy starts from the source's current params and only overlays the posted keys, so a version fork inherits today's keys and per-chart entries for charts the layout dropped. Pre-existing, so not changing it here; tracked as a follow-up to build the fork's metadata from the snapshot and prune per-chart keys to the layout's charts.
| if copied_id is not None: | ||
| db.session.delete(db.session.get(Dashboard, copied_id)) | ||
| db.session.delete(dashboard) | ||
| # Clones carry the source names, including ones the layout left out. |
There was a problem hiding this comment.
Non-blocking. This cleanup shows the side effect of the duplicate_slices=True cases. The clone loop clones every non-archived member of the source, whether or not the layout references it. Charts the layout leaves out become orphan clones: owned by the caller, on no dashboard, and recorded as added-then-removed in the new dashboard's association history. The test only checks copied.slices, so it passes while leaving those orphans behind. Skipping clones whose original ID isn't in the layout's chart IDs would fix this. The remap and filter_scopes mapping already only use IDs from the layout. At minimum, assert the orphan count so a later change to this behavior is deliberate.
There was a problem hiding this comment.
Good catch. The clone loop has always cloned every non-archived member regardless of the layout, so that's pre-existing too. Tracked as a follow-up to skip clones whose original isn't in the layout and assert the orphan count in this test.
SUMMARY
TL;DR: Copying a dashboard with a layout that leaves out some of the source dashboard's current charts failed with a generic
400 Arguments are not correct, and nothing was logged. This PR fixes the copy and logs the cause when a copy fails.What users saw. In a dashboard's version history, choosing an older version and clicking "Open as new dashboard" showed "Failed to create new dashboard from this version: arguments are not correct", and no new dashboard was created. It happened whenever the dashboard had gained a chart since that older version, which is true of most dashboards that are still being worked on, so most older versions couldn't be opened as a new dashboard. The message was misleading (nothing was wrong with the request), and the server logged nothing, so the failure was invisible to operators too.
Why, in plain terms. Making the copy took two steps: first the new dashboard was given all of the original dashboard's charts as they are today, then it was trimmed down to the charts in the old version's layout. A chart that was added after the old version was therefore added and immediately removed again. The database layer skipped the add but still tried to carry out the remove, found nothing to delete, and aborted the whole copy. That internal error was turned into the generic message above and not logged.
What this changes. When a layout is supplied, the copy now takes its charts from that layout only, so there's no add-then-remove. And if a copy does fail for a database reason, the real error is now logged.
Background. In version history, "Open as new dashboard" forks an older version by posting that version's layout to
POST /api/v1/dashboard/<id>/copy/(withoutduplicate_slices). The copy endpoint rebuilds the new dashboard's charts fromjson_metadata.positions.Problem.
DashboardDAO.copy_dashboardfirst linked all of the source's current charts to the new dashboard (dash.slices = original_dash.slices). Thenset_dash_metadatareplaced that list with the charts in the layout. For each chart the layout dropped, the chart's unloadedSlice.dashboardsbackref collection was left holding a queued append followed by a remove. On flush, SQLAlchemy issuedDELETE FROM dashboard_slicesfor a row that was never inserted. That raisedStaleDataError, which the command's transaction handler turned intoDashboardCopyError, and the API returned the generic 400.on_errorlogs only exceptions that carry anexceptionattribute, so nothing was logged.Any older version that doesn't contain every chart the dashboard has today hits this. It also hits an empty layout. It happens on PostgreSQL and SQLite alike.
Changes.
DashboardDAO.copy_dashboardlinks the source's charts only when no layout is sent. With a layout,set_dash_metadataremains the only place that sets membership. Theduplicate_slicespath is unchanged, and the integration test covers it with partial and empty layouts too.copy_dashlogsDashboardCopyErrorwithexc_info, so the underlying database error appears in the logs. The response is unchanged. The log sits in the REST route so the sharedon_errorhelper, which every command's transaction uses, keeps its behavior. The MCP duplicate tool still reports the error without logging its cause.BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
N/A (API behavior).
Before:
POST /api/v1/dashboard/<id>/copy/with a layout that omits a current chart returns400 {"message": "Arguments are not correct"}. After: it returns200, and the new dashboard has exactly the charts in the layout. The source dashboard is unchanged.TESTING INSTRUCTIONS
pytest tests/unit_tests/dao/test_copy_dashboard_legacy_ids.py -k dropped_from_layout. It fails withStaleDataErrorbefore the fix.pytest tests/integration_tests/dashboards/api_tests.py -k "copy_dashboard_layout_without_current_charts or copy_dashboard_logs_database_error". Both fail before the fix.pytest tests/integration_tests/dashboards/version_restore_tests.py -k TestDashboardRestoreLayoutMembership.ADDITIONAL INFORMATION