You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by "john-bodley (via GitHub)" <gi...@apache.org> on 2023/02/03 17:53:28 UTC

[GitHub] [superset] john-bodley commented on a diff in pull request #22964: chore: Migrate /superset/user_slices and /superset/fave_slices to API v1

john-bodley commented on code in PR #22964:
URL: https://github.com/apache/superset/pull/22964#discussion_r1096083263


##########
superset/charts/commands/get_slices.py:
##########
@@ -0,0 +1,67 @@
+# 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.
+import logging
+from typing import cast, Optional
+
+from flask_appbuilder.models.sqla import Model
+
+from superset import security_manager
+from superset.charts.dao import ChartDAO
+from superset.commands.base import BaseCommand
+from superset.utils.core import get_user_id
+from superset.utils.dates import datetime_to_epoch
+
+logger = logging.getLogger(__name__)
+
+
+class GetUserSlicesCommand(BaseCommand):
+    _user_id: Optional[int]
+
+    def __init__(self, user_id: Optional[int] = None):
+        self._user_id = user_id
+
+    def run(self) -> Model:
+        self.validate()
+
+        slices = ChartDAO.user_slices(cast(int, self._user_id))
+
+        payload = []
+        for o in slices:

Review Comment:
   We have a tendency to use `slc` rather than the more opaque `o` to represent a slice. That said I’m not sure what `o` actually represents given that is presented as `o.Slice`.



##########
superset/charts/commands/get_slices.py:
##########
@@ -0,0 +1,67 @@
+# 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.
+import logging
+from typing import cast, Optional
+
+from flask_appbuilder.models.sqla import Model
+
+from superset import security_manager
+from superset.charts.dao import ChartDAO
+from superset.commands.base import BaseCommand
+from superset.utils.core import get_user_id
+from superset.utils.dates import datetime_to_epoch
+
+logger = logging.getLogger(__name__)
+
+
+class GetUserSlicesCommand(BaseCommand):
+    _user_id: Optional[int]
+
+    def __init__(self, user_id: Optional[int] = None):
+        self._user_id = user_id
+
+    def run(self) -> Model:
+        self.validate()
+
+        slices = ChartDAO.user_slices(cast(int, self._user_id))
+
+        payload = []
+        for o in slices:
+            item = {
+                "id": o.Slice.id,
+                "title": o.Slice.slice_name,
+                "url": o.Slice.slice_url,
+                "data": o.Slice.form_data,
+                "dttm": datetime_to_epoch(
+                    o.FavStar.dttm if o.FavStar else o.Slice.changed_on
+                ),
+                "viz_type": o.Slice.viz_type,
+                "is_favorite": bool(o.FavStar),
+            }
+            if o.Slice.created_by:
+                user = o.Slice.created_by
+                item["creator"] = str(user)
+                item["creator_url"] = "/superset/profile/{}/".format(user.username)
+            payload.append(item)
+
+        return payload
+
+    def validate(self) -> None:
+        if not self._user_id:

Review Comment:
   I know we do this elsewhere but mutating state inside `validate` seems wrong to me.



##########
superset/charts/dao.py:
##########
@@ -82,3 +84,34 @@ def favorited_ids(charts: List[Slice]) -> List[FavStar]:
             )
             .all()
         ]
+
+    @staticmethod
+    def user_slices(user_id: int) -> List[Any]:
+        owner_ids_query = (
+            db.session.query(Slice.id)
+            .join(Slice.owners)
+            .filter(security_manager.user_model.id == user_id)
+        )
+
+        return (
+            db.session.query(Slice, FavStar)
+            .join(
+                FavStar,
+                and_(
+                    FavStar.user_id == user_id,
+                    FavStar.class_name == "slice",
+                    Slice.id == FavStar.obj_id,
+                ),
+                isouter=True,
+            )
+            .filter(  # pylint: disable=comparison-with-callable
+                or_(
+                    Slice.id.in_(owner_ids_query),

Review Comment:
   Any reason this isn’t a join as opposed to a subquery? I suspect the subquery likely fetches significantly more slices than the user favorited.



-- 
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