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

chore: Refine native dashboard cleanup logic #24864

Merged
merged 1 commit into from
Aug 9, 2023
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
39 changes: 20 additions & 19 deletions superset/cli/native_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,16 @@
if (
isinstance(value, dict)
and value["type"] == "CHART"
and value["meta"]["chartId"] in filter_boxes_by_id
and (meta := value.get("meta"))
and meta["chartId"] in filter_boxes_by_id
):
slc = filter_boxes_by_id[value["meta"]["chartId"]]
slc = filter_boxes_by_id[meta["chartId"]]

Check warning on line 178 in superset/cli/native_filters.py

View check run for this annotation

Codecov / codecov/patch

superset/cli/native_filters.py#L178

Added line #L178 was not covered by tests
mapping[key] = key.replace("CHART-", "MARKDOWN-")

value["id"] = mapping[key]
value["type"] = "MARKDOWN"

value["meta"]["code"] = dedent(
meta["code"] = dedent(

Check warning on line 184 in superset/cli/native_filters.py

View check run for this annotation

Codecov / codecov/patch

superset/cli/native_filters.py#L184

Added line #L184 was not covered by tests
f"""
&#9888; The <a href="/superset/slice/{slc.id}/">{slc.slice_name}
</a> filter-box chart has been migrated to a native filter.
Expand All @@ -192,14 +193,14 @@
)

# Save the filter-box info for recovery purposes.
value["meta"]["native_filter_migration"] = {
key: value["meta"].pop(key)
meta["native_filter_migration"] = {

Check warning on line 196 in superset/cli/native_filters.py

View check run for this annotation

Codecov / codecov/patch

superset/cli/native_filters.py#L196

Added line #L196 was not covered by tests
key: meta.pop(key)
for key in (
"chartId",
"sliceName",
"sliceNameOverride",
)
if key in value["meta"]
if key in meta
}

position_json[mapping[key]] = value
Expand Down Expand Up @@ -291,13 +292,14 @@
if (
isinstance(value, dict)
and value["type"] == "MARKDOWN"
and "native_filter_migration" in value["meta"]
and (meta := value.get("meta"))
and "native_filter_migration" in meta
):
value["meta"].update(value["meta"].pop("native_filter_migration"))
slice_ids.add(value["meta"]["chartId"])
meta.update(meta.pop("native_filter_migration"))
slice_ids.add(meta["chartId"])

Check warning on line 299 in superset/cli/native_filters.py

View check run for this annotation

Codecov / codecov/patch

superset/cli/native_filters.py#L298-L299

Added lines #L298 - L299 were not covered by tests
mapping[key] = key.replace("MARKDOWN-", "CHART-")
value["id"] = mapping[key]
del value["meta"]["code"]
del meta["code"]

Check warning on line 302 in superset/cli/native_filters.py

View check run for this annotation

Codecov / codecov/patch

superset/cli/native_filters.py#L302

Added line #L302 was not covered by tests
value["type"] = "CHART"
position_json[mapping[key]] = value
del position_json[key]
Expand Down Expand Up @@ -368,21 +370,20 @@
json_metadata = json.loads(dashboard.json_metadata or "{}")
position_json = json.loads(dashboard.position_json or "{}")

if "native_filter_migration" not in json_metadata:
Copy link
Member Author

Choose a reason for hiding this comment

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

If a dashboard has no filters then this key wouldn't exit and thus it's a misnomer to say that the dashboard hadn't been upgraded.

click.echo(f"{str(dashboard)} has not been upgraded")
continue

# Remove the saved filter configurations.
del json_metadata["native_filter_migration"]
dashboard.json_metadata = json.dumps(json_metadata)
if "native_filter_migration" in json_metadata:
del json_metadata["native_filter_migration"]
dashboard.json_metadata = json.dumps(json_metadata)

Check warning on line 376 in superset/cli/native_filters.py

View check run for this annotation

Codecov / codecov/patch

superset/cli/native_filters.py#L374-L376

Added lines #L374 - L376 were not covered by tests

for value in position_json.values():
if (
isinstance(value, dict)
and "native_filter_migration" in value["meta"]
and value["type"] == "MARKDOWN"
Copy link
Member Author

Choose a reason for hiding this comment

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

Not all values in the the position_json dictionary contain the meta key.

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
and value["type"] == "MARKDOWN"
and "meta" in value

Copy link
Member Author

Choose a reason for hiding this comment

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

@michael-s-molina I've updated the PR to use a variant of this (in the form of the walrus operator) throughout for consistency.

and (meta := value.get("meta"))
and "native_filter_migration" in meta
):
slice_ids.add(value["meta"]["native_filter_migration"]["chartId"])
del value["meta"]["native_filter_migration"]
slice_ids.add(meta["native_filter_migration"]["chartId"])
del meta["native_filter_migration"]

Check warning on line 386 in superset/cli/native_filters.py

View check run for this annotation

Codecov / codecov/patch

superset/cli/native_filters.py#L385-L386

Added lines #L385 - L386 were not covered by tests

dashboard.json_metadata = json.dumps(json_metadata)
dashboard.position_json = json.dumps(position_json)
Expand Down
17 changes: 10 additions & 7 deletions superset/utils/dashboard_filter_scopes_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,16 @@

for filter_box in filter_boxes:
for value in position_json.values():
if (
isinstance(value, dict)
and value["type"] == "CHART"
and value["meta"]["chartId"] == filter_box.id
and value["parents"] # Misnomer as this the the complete ancestry.
):
ancestors_by_id[filter_box.id] = set(value["parents"])
try:
if (

Check warning on line 302 in superset/utils/dashboard_filter_scopes_converter.py

View check run for this annotation

Codecov / codecov/patch

superset/utils/dashboard_filter_scopes_converter.py#L301-L302

Added lines #L301 - L302 were not covered by tests
isinstance(value, dict)
and value["type"] == "CHART"
and value["meta"]["chartId"] == filter_box.id
and value["parents"] # Misnomer as this the the complete ancestry.
):
ancestors_by_id[filter_box.id] = set(value["parents"])
except KeyError:
pass

Check warning on line 310 in superset/utils/dashboard_filter_scopes_converter.py

View check run for this annotation

Codecov / codecov/patch

superset/utils/dashboard_filter_scopes_converter.py#L308-L310

Added lines #L308 - L310 were not covered by tests

# Wire up the hierarchical filters.
for this in filter_boxes:
Expand Down
Loading