You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by GitBox <gi...@apache.org> on 2021/09/03 00:00:30 UTC

[GitHub] [superset] john-bodley commented on a change in pull request #16549: feat(dashboard): Native filters - add type to native filter configuration

john-bodley commented on a change in pull request #16549:
URL: https://github.com/apache/superset/pull/16549#discussion_r701494484



##########
File path: superset/migrations/versions/021b81fe4fbb_add_type_to_native_filter_configuration.py
##########
@@ -0,0 +1,94 @@
+# 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 type to native filter configuration
+
+Revision ID: 021b81fe4fbb
+Revises: 07071313dd52
+Create Date: 2021-08-31 11:37:40.604081
+
+"""
+
+# revision identifiers, used by Alembic.
+revision = "021b81fe4fbb"
+down_revision = "07071313dd52"
+
+import json
+import logging
+
+import sqlalchemy as sa
+from alembic import op
+from sqlalchemy.ext.declarative.api import declarative_base
+
+from superset import db
+
+Base = declarative_base()
+
+logger = logging.getLogger("alembic")
+
+
+class Dashboard(Base):
+    __tablename__ = "dashboards"
+    id = sa.Column(sa.Integer, primary_key=True)
+    json_metadata = sa.Column(sa.Text)
+
+
+def upgrade():
+    logger.info("[AddTypeToNativeFilter] Starting upgrade")
+    bind = op.get_bind()
+    session = db.Session(bind=bind)
+
+    for dashboard in session.query(Dashboard).all():
+        logger.info("[AddTypeToNativeFilter] Updating Dashboard<pk:%s> ", dashboard.id)
+
+        if not dashboard.json_metadata:
+            logger.info(
+                "[AddTypeToNativeFilter] Skipping Dashboard<pk:%s> ", dashboard.id
+            )
+            continue
+
+        json_meta = json.loads(dashboard.json_metadata)

Review comment:
       You might want to wrap this in a try/except since some charts could store invalid JSON. You can see how other migrations handle this.

##########
File path: superset/migrations/versions/021b81fe4fbb_add_type_to_native_filter_configuration.py
##########
@@ -0,0 +1,94 @@
+# 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 type to native filter configuration
+
+Revision ID: 021b81fe4fbb
+Revises: 07071313dd52
+Create Date: 2021-08-31 11:37:40.604081
+
+"""
+
+# revision identifiers, used by Alembic.
+revision = "021b81fe4fbb"
+down_revision = "07071313dd52"
+
+import json
+import logging
+
+import sqlalchemy as sa
+from alembic import op
+from sqlalchemy.ext.declarative.api import declarative_base
+
+from superset import db
+
+Base = declarative_base()
+
+logger = logging.getLogger("alembic")
+
+
+class Dashboard(Base):
+    __tablename__ = "dashboards"
+    id = sa.Column(sa.Integer, primary_key=True)
+    json_metadata = sa.Column(sa.Text)
+
+
+def upgrade():
+    logger.info("[AddTypeToNativeFilter] Starting upgrade")
+    bind = op.get_bind()
+    session = db.Session(bind=bind)
+
+    for dashboard in session.query(Dashboard).all():
+        logger.info("[AddTypeToNativeFilter] Updating Dashboard<pk:%s> ", dashboard.id)
+
+        if not dashboard.json_metadata:
+            logger.info(
+                "[AddTypeToNativeFilter] Skipping Dashboard<pk:%s> ", dashboard.id
+            )
+            continue
+
+        json_meta = json.loads(dashboard.json_metadata)
+
+        for native_filter in json_meta.get("native_filter_configuration", []):
+            native_filter.setdefault("type", "NATIVE_FILTER")
+        dashboard.json_metadata = json.dumps(json_meta)

Review comment:
       For efficiency we likely should only be updating `dashboard.json_metadata` for those dashboards which were modified, so maybe something of the form, 
   
   ```python
   if "native_filter_configuration" in json_meta:
       for native_filter in json_meta["native_filter_configuration"]:
           native_filter["type"] = "NATIVE_FILTER"
       
       dashboard.json_metadata = json.dumps(json_meta)
   ```

##########
File path: superset/migrations/versions/021b81fe4fbb_add_type_to_native_filter_configuration.py
##########
@@ -0,0 +1,94 @@
+# 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 type to native filter configuration
+
+Revision ID: 021b81fe4fbb
+Revises: 07071313dd52
+Create Date: 2021-08-31 11:37:40.604081
+
+"""
+
+# revision identifiers, used by Alembic.
+revision = "021b81fe4fbb"
+down_revision = "07071313dd52"
+
+import json
+import logging
+
+import sqlalchemy as sa
+from alembic import op
+from sqlalchemy.ext.declarative.api import declarative_base
+
+from superset import db
+
+Base = declarative_base()
+
+logger = logging.getLogger("alembic")
+
+
+class Dashboard(Base):
+    __tablename__ = "dashboards"
+    id = sa.Column(sa.Integer, primary_key=True)
+    json_metadata = sa.Column(sa.Text)
+
+
+def upgrade():
+    logger.info("[AddTypeToNativeFilter] Starting upgrade")
+    bind = op.get_bind()
+    session = db.Session(bind=bind)
+
+    for dashboard in session.query(Dashboard).all():
+        logger.info("[AddTypeToNativeFilter] Updating Dashboard<pk:%s> ", dashboard.id)
+
+        if not dashboard.json_metadata:
+            logger.info(
+                "[AddTypeToNativeFilter] Skipping Dashboard<pk:%s> ", dashboard.id
+            )
+            continue
+
+        json_meta = json.loads(dashboard.json_metadata)
+
+        for native_filter in json_meta.get("native_filter_configuration", []):
+            native_filter.setdefault("type", "NATIVE_FILTER")

Review comment:
       Why not just `native_filter["type"] = "NATIVE_FILTER"` if `type` is a new key (which per line 90 it would indicate that it is)? I always forget how `dict.setdefault` works as it's rarely used.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org