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/02/06 22:33:15 UTC

[GitHub] [superset] dpgaspar commented on a change in pull request #12978: feat(dashboard): API to get a dashboard's charts

dpgaspar commented on a change in pull request #12978:
URL: https://github.com/apache/superset/pull/12978#discussion_r571496843



##########
File path: superset/dashboards/dao.py
##########
@@ -35,6 +36,14 @@ class DashboardDAO(BaseDAO):
     model_cls = Dashboard
     base_filter = DashboardFilter
 
+    @staticmethod
+    def get_charts_for_dashboard(dashboard_id: int) -> List[Slice]:
+        query = db.session.query(Dashboard).filter(Dashboard.id == dashboard_id)
+        dashboard = query.scalar()
+        if not dashboard:
+            raise DashboardNotFoundError()
+        return [slice.data for slice in dashboard.slices]

Review comment:
       This will trigger N+1 queries on the DB. A possible solution can be `dashboard = db.session.query(Dashboard).join(Slice, Dashboard.slices).join(Slice.table).filter(Dashboard.id == 1).options(contains_eager(Dashboard.slices).contains_eager(Slice.owners)).one_or_none()`
   
   I would avoid using `slice.data` unless it's really handy on this case

##########
File path: tests/dashboards/api_tests.py
##########
@@ -146,6 +151,53 @@ def create_dashboard_with_report(self):
             db.session.delete(dashboard)
             db.session.commit()
 
+    def test_get_dashboard_charts(self):
+        """
+        Dashboard API: Test getting charts belonging to a dashboard
+        """
+        self.login(username="admin")
+        admin = self.get_user("admin")
+        slice = self.insert_chart("slice1", [admin.id], 1, params="{}")
+        dashboard = self.insert_dashboard(
+            "title", "charted", [admin.id], admin, slices=[slice]
+        )

Review comment:
       can you change this to use a pytest.fixture we already have one `create_dashboards`

##########
File path: tests/dashboards/api_tests.py
##########
@@ -1302,7 +1354,7 @@ def test_import_dashboard_overwrite(self):
 
     def test_import_dashboard_invalid(self):
         """
-        Dataset API: Test import invalid dashboard
+        Dashboard API: Test import invalid dashboard

Review comment:
       copy pasta ;)
   

##########
File path: tests/dashboards/api_tests.py
##########
@@ -98,6 +100,9 @@ def insert_dashboard(
         db.session.commit()
         return dashboard
 
+    def get_nonexistent_dashboard_id(self):
+        return (db.session.query(func.max(Dashboard.id)).scalar() or 0) + 1

Review comment:
       nice!

##########
File path: tests/dashboards/api_tests.py
##########
@@ -146,6 +151,53 @@ def create_dashboard_with_report(self):
             db.session.delete(dashboard)
             db.session.commit()
 
+    def test_get_dashboard_charts(self):
+        """
+        Dashboard API: Test getting charts belonging to a dashboard
+        """
+        self.login(username="admin")
+        admin = self.get_user("admin")
+        slice = self.insert_chart("slice1", [admin.id], 1, params="{}")
+        dashboard = self.insert_dashboard(
+            "title", "charted", [admin.id], admin, slices=[slice]
+        )
+        uri = f"api/v1/dashboard/{dashboard.id}/charts"
+        response = self.get_assert_metric(uri, "get_charts")
+        self.assertEqual(response.status_code, 200)
+        data = json.loads(response.data.decode("utf-8"))
+        self.assertEqual(data["result"], [slice.data])
+        db.session.delete(dashboard)
+        db.session.delete(slice)
+        db.session.commit()
+
+    def test_get_dashboard_charts_not_found(self):
+        """
+        Dashboard API: Test getting charts belonging to a dashboard that does not exist
+        """
+        self.login(username="admin")
+        bad_id = self.get_nonexistent_dashboard_id()
+        uri = f"api/v1/dashboard/{bad_id}/charts"
+        response = self.get_assert_metric(uri, "get_charts")
+        self.assertEqual(response.status_code, 404)
+        db.session.commit()
+
+    def test_get_dashboard_charts_empty(self):
+        """
+        Dashboard API: Test getting charts belonging to a dashboard without any charts
+        """
+        self.login(username="admin")
+        admin = self.get_user("admin")
+        dashboard = self.insert_dashboard(

Review comment:
       pytest.fixture?

##########
File path: superset/dashboards/dao.py
##########
@@ -35,6 +36,14 @@ class DashboardDAO(BaseDAO):
     model_cls = Dashboard
     base_filter = DashboardFilter
 
+    @staticmethod
+    def get_charts_for_dashboard(dashboard_id: int) -> List[Slice]:

Review comment:
       This method does not return `List[Slice]` but `List[Dict[str, Any]]`




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

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