You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by jo...@apache.org on 2023/08/09 17:42:37 UTC

[superset] branch master updated: chore: Refine native dashboard cleanup logic (#24864)

This is an automated email from the ASF dual-hosted git repository.

johnbodley pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/superset.git


The following commit(s) were added to refs/heads/master by this push:
     new 34586648a5 chore: Refine native dashboard cleanup logic (#24864)
34586648a5 is described below

commit 34586648a50ee5400fcc08dd3accc6d9fe03b142
Author: John Bodley <45...@users.noreply.github.com>
AuthorDate: Wed Aug 9 10:42:29 2023 -0700

    chore: Refine native dashboard cleanup logic (#24864)
---
 superset/cli/native_filters.py                     | 39 +++++++++++-----------
 .../utils/dashboard_filter_scopes_converter.py     | 17 ++++++----
 2 files changed, 30 insertions(+), 26 deletions(-)

diff --git a/superset/cli/native_filters.py b/superset/cli/native_filters.py
index a25724d38d..75df428e38 100644
--- a/superset/cli/native_filters.py
+++ b/superset/cli/native_filters.py
@@ -172,15 +172,16 @@ def upgrade(
                 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"]]
                     mapping[key] = key.replace("CHART-", "MARKDOWN-")
 
                     value["id"] = mapping[key]
                     value["type"] = "MARKDOWN"
 
-                    value["meta"]["code"] = dedent(
+                    meta["code"] = dedent(
                         f"""
                         &#9888; The <a href="/superset/slice/{slc.id}/">{slc.slice_name}
                         </a> filter-box chart has been migrated to a native filter.
@@ -192,14 +193,14 @@ def upgrade(
                     )
 
                     # Save the filter-box info for recovery purposes.
-                    value["meta"]["native_filter_migration"] = {
-                        key: value["meta"].pop(key)
+                    meta["native_filter_migration"] = {
+                        key: meta.pop(key)
                         for key in (
                             "chartId",
                             "sliceName",
                             "sliceNameOverride",
                         )
-                        if key in value["meta"]
+                        if key in meta
                     }
 
                     position_json[mapping[key]] = value
@@ -291,13 +292,14 @@ def downgrade(
                 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"])
                     mapping[key] = key.replace("MARKDOWN-", "CHART-")
                     value["id"] = mapping[key]
-                    del value["meta"]["code"]
+                    del meta["code"]
                     value["type"] = "CHART"
                     position_json[mapping[key]] = value
                     del position_json[key]
@@ -368,21 +370,20 @@ def cleanup(
             json_metadata = json.loads(dashboard.json_metadata or "{}")
             position_json = json.loads(dashboard.position_json or "{}")
 
-            if "native_filter_migration" not in json_metadata:
-                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)
 
             for value in position_json.values():
                 if (
                     isinstance(value, dict)
-                    and "native_filter_migration" in value["meta"]
+                    and value["type"] == "MARKDOWN"
+                    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"]
 
             dashboard.json_metadata = json.dumps(json_metadata)
             dashboard.position_json = json.dumps(position_json)
diff --git a/superset/utils/dashboard_filter_scopes_converter.py b/superset/utils/dashboard_filter_scopes_converter.py
index ce89b2a255..38090e22b6 100644
--- a/superset/utils/dashboard_filter_scopes_converter.py
+++ b/superset/utils/dashboard_filter_scopes_converter.py
@@ -298,13 +298,16 @@ def convert_filter_scopes_to_native_filters(  # pylint: disable=invalid-name,too
 
     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 (
+                    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
 
     # Wire up the hierarchical filters.
     for this in filter_boxes: