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 2020/10/30 08:02:39 UTC

[GitHub] [incubator-superset] nytai opened a new pull request #11502: refactor: reduce number of api calls needed to fetch favorite status for charts and dashboards

nytai opened a new pull request #11502:
URL: https://github.com/apache/incubator-superset/pull/11502


   ### SUMMARY
   <!--- Describe the change below, including rationale and design decisions -->
   See attached issue
   ### BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
   <!--- Skip this if not applicable -->
   Nothin should have changed
   
   ### TEST PLAN
   <!--- What steps should be taken to verify the changes -->
   - WIP
   ### ADDITIONAL INFORMATION
   <!--- Check any relevant boxes with "x" -->
   <!--- HINT: Include "Fixes #nnn" if you are fixing an existing issue -->
   - [ ] Has associated issue:
   - [x] Changes UI fixes #11384 
   - [ ] Requires DB Migration.
   - [ ] Confirm DB Migration upgrade and downgrade tested.
   - [ ] Introduces new feature or API
   - [ ] Removes existing feature or API
   


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


[GitHub] [incubator-superset] ktmud commented on a change in pull request #11502: refactor: reduce number of api calls needed to fetch favorite status for charts and dashboards

Posted by GitBox <gi...@apache.org>.
ktmud commented on a change in pull request #11502:
URL: https://github.com/apache/incubator-superset/pull/11502#discussion_r516417810



##########
File path: superset-frontend/src/views/CRUD/hooks.ts
##########
@@ -299,64 +301,85 @@ export function useSingleViewResource<D extends object = any>(
   };
 }
 
-// the hooks api has some known limitations around stale state in closures.
-// See https://github.com/reactjs/rfcs/blob/master/text/0068-react-hooks.md#drawbacks
-// the useRef hook is a way of getting around these limitations by having a consistent ref
-// that points to the most recent value.
+enum FavStarClassName {
+  CHART = 'slice',
+  DASHBOARD = 'Dashboard',
+}
+
+type FavoriteStatusResponse = {
+  result: Array<{
+    id: string;
+    value: boolean;
+  }>;
+};
+
+const favoriteApis = {
+  chart: makeApi<string, FavoriteStatusResponse>({

Review comment:
       You can also pass a json object to the request payload, which would be better suited for large search params and strong typing.
   
   In the future, we may even add built-in rison support so that rison requests can have typing, too.

##########
File path: superset-frontend/src/views/CRUD/hooks.ts
##########
@@ -299,64 +301,85 @@ export function useSingleViewResource<D extends object = any>(
   };
 }
 
-// the hooks api has some known limitations around stale state in closures.
-// See https://github.com/reactjs/rfcs/blob/master/text/0068-react-hooks.md#drawbacks
-// the useRef hook is a way of getting around these limitations by having a consistent ref
-// that points to the most recent value.
+enum FavStarClassName {
+  CHART = 'slice',
+  DASHBOARD = 'Dashboard',
+}
+
+type FavoriteStatusResponse = {
+  result: Array<{
+    id: string;
+    value: boolean;
+  }>;
+};
+
+const favoriteApis = {
+  chart: makeApi<string, FavoriteStatusResponse>({

Review comment:
       I'd vote for `requestType: "rison"` so to not add a new config option.




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


[GitHub] [incubator-superset] codecov-io commented on pull request #11502: refactor: reduce number of api calls needed to fetch favorite status for charts and dashboards

Posted by GitBox <gi...@apache.org>.
codecov-io commented on pull request #11502:
URL: https://github.com/apache/incubator-superset/pull/11502#issuecomment-719427449


   # [Codecov](https://codecov.io/gh/apache/incubator-superset/pull/11502?src=pr&el=h1) Report
   > Merging [#11502](https://codecov.io/gh/apache/incubator-superset/pull/11502?src=pr&el=desc) into [master](https://codecov.io/gh/apache/incubator-superset/commit/f7051eaaded4e371f8a04f82915969bbaeb3763e?el=desc) will **decrease** coverage by `4.03%`.
   > The diff coverage is `65.90%`.
   
   [![Impacted file tree graph](https://codecov.io/gh/apache/incubator-superset/pull/11502/graphs/tree.svg?width=650&height=150&src=pr&token=KsB0fHcx6l)](https://codecov.io/gh/apache/incubator-superset/pull/11502?src=pr&el=tree)
   
   ```diff
   @@            Coverage Diff             @@
   ##           master   #11502      +/-   ##
   ==========================================
   - Coverage   66.56%   62.52%   -4.04%     
   ==========================================
     Files         869      451     -418     
     Lines       41587    15507   -26080     
     Branches     3797     3798       +1     
   ==========================================
   - Hits        27681     9696   -17985     
   + Misses      13804     5632    -8172     
   - Partials      102      179      +77     
   ```
   
   | Flag | Coverage Δ | |
   |---|---|---|
   | #cypress | `?` | |
   | #javascript | `62.52% <65.90%> (-0.05%)` | :arrow_down: |
   | #python | `?` | |
   
   Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags#carryforward-flags-in-the-pull-request-comment) to find out more.
   
   | [Impacted Files](https://codecov.io/gh/apache/incubator-superset/pull/11502?src=pr&el=tree) | Coverage Δ | |
   |---|---|---|
   | [...perset-frontend/src/views/CRUD/chart/ChartCard.tsx](https://codecov.io/gh/apache/incubator-superset/pull/11502/diff?src=pr&el=tree#diff-c3VwZXJzZXQtZnJvbnRlbmQvc3JjL3ZpZXdzL0NSVUQvY2hhcnQvQ2hhcnRDYXJkLnRzeA==) | `88.46% <ø> (-7.97%)` | :arrow_down: |
   | [...rontend/src/views/CRUD/dashboard/DashboardCard.tsx](https://codecov.io/gh/apache/incubator-superset/pull/11502/diff?src=pr&el=tree#diff-c3VwZXJzZXQtZnJvbnRlbmQvc3JjL3ZpZXdzL0NSVUQvZGFzaGJvYXJkL0Rhc2hib2FyZENhcmQudHN4) | `84.37% <ø> (-9.75%)` | :arrow_down: |
   | [...-frontend/src/views/CRUD/welcome/ActivityTable.tsx](https://codecov.io/gh/apache/incubator-superset/pull/11502/diff?src=pr&el=tree#diff-c3VwZXJzZXQtZnJvbnRlbmQvc3JjL3ZpZXdzL0NSVUQvd2VsY29tZS9BY3Rpdml0eVRhYmxlLnRzeA==) | `40.84% <ø> (-5.64%)` | :arrow_down: |
   | [...t-frontend/src/views/CRUD/welcome/SavedQueries.tsx](https://codecov.io/gh/apache/incubator-superset/pull/11502/diff?src=pr&el=tree#diff-c3VwZXJzZXQtZnJvbnRlbmQvc3JjL3ZpZXdzL0NSVUQvd2VsY29tZS9TYXZlZFF1ZXJpZXMudHN4) | `61.33% <ø> (-1.83%)` | :arrow_down: |
   | [superset-frontend/src/views/CRUD/hooks.ts](https://codecov.io/gh/apache/incubator-superset/pull/11502/diff?src=pr&el=tree#diff-c3VwZXJzZXQtZnJvbnRlbmQvc3JjL3ZpZXdzL0NSVUQvaG9va3MudHM=) | `51.51% <26.31%> (-12.27%)` | :arrow_down: |
   | [...frontend/src/views/CRUD/welcome/DashboardTable.tsx](https://codecov.io/gh/apache/incubator-superset/pull/11502/diff?src=pr&el=tree#diff-c3VwZXJzZXQtZnJvbnRlbmQvc3JjL3ZpZXdzL0NSVUQvd2VsY29tZS9EYXNoYm9hcmRUYWJsZS50c3g=) | `61.81% <80.00%> (+1.44%)` | :arrow_up: |
   | [superset-frontend/src/components/FaveStar.tsx](https://codecov.io/gh/apache/incubator-superset/pull/11502/diff?src=pr&el=tree#diff-c3VwZXJzZXQtZnJvbnRlbmQvc3JjL2NvbXBvbmVudHMvRmF2ZVN0YXIudHN4) | `89.47% <100.00%> (-10.53%)` | :arrow_down: |
   | [...set-frontend/src/components/ListViewCard/index.tsx](https://codecov.io/gh/apache/incubator-superset/pull/11502/diff?src=pr&el=tree#diff-c3VwZXJzZXQtZnJvbnRlbmQvc3JjL2NvbXBvbmVudHMvTGlzdFZpZXdDYXJkL2luZGV4LnRzeA==) | `94.23% <100.00%> (-3.89%)` | :arrow_down: |
   | [...perset-frontend/src/views/CRUD/chart/ChartList.tsx](https://codecov.io/gh/apache/incubator-superset/pull/11502/diff?src=pr&el=tree#diff-c3VwZXJzZXQtZnJvbnRlbmQvc3JjL3ZpZXdzL0NSVUQvY2hhcnQvQ2hhcnRMaXN0LnRzeA==) | `79.78% <100.00%> (-8.76%)` | :arrow_down: |
   | [...rontend/src/views/CRUD/dashboard/DashboardList.tsx](https://codecov.io/gh/apache/incubator-superset/pull/11502/diff?src=pr&el=tree#diff-c3VwZXJzZXQtZnJvbnRlbmQvc3JjL3ZpZXdzL0NSVUQvZGFzaGJvYXJkL0Rhc2hib2FyZExpc3QudHN4) | `73.68% <100.00%> (-5.70%)` | :arrow_down: |
   | ... and [599 more](https://codecov.io/gh/apache/incubator-superset/pull/11502/diff?src=pr&el=tree-more) | |
   
   ------
   
   [Continue to review full report at Codecov](https://codecov.io/gh/apache/incubator-superset/pull/11502?src=pr&el=continue).
   > **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
   > `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
   > Powered by [Codecov](https://codecov.io/gh/apache/incubator-superset/pull/11502?src=pr&el=footer). Last update [f7051ea...1275a01](https://codecov.io/gh/apache/incubator-superset/pull/11502?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
   


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


[GitHub] [incubator-superset] codecov-io edited a comment on pull request #11502: refactor: reduce number of api calls needed to fetch favorite status for charts and dashboards

Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #11502:
URL: https://github.com/apache/incubator-superset/pull/11502#issuecomment-719427449






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


[GitHub] [incubator-superset] nytai merged pull request #11502: refactor: reduce number of api calls needed to fetch favorite status for charts and dashboards

Posted by GitBox <gi...@apache.org>.
nytai merged pull request #11502:
URL: https://github.com/apache/incubator-superset/pull/11502


   


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


[GitHub] [incubator-superset] nytai commented on a change in pull request #11502: refactor: reduce number of api calls needed to fetch favorite status for charts and dashboards

Posted by GitBox <gi...@apache.org>.
nytai commented on a change in pull request #11502:
URL: https://github.com/apache/incubator-superset/pull/11502#discussion_r516410236



##########
File path: superset-frontend/src/views/CRUD/hooks.ts
##########
@@ -299,64 +299,66 @@ export function useSingleViewResource<D extends object = any>(
   };
 }
 
-// the hooks api has some known limitations around stale state in closures.
-// See https://github.com/reactjs/rfcs/blob/master/text/0068-react-hooks.md#drawbacks
-// the useRef hook is a way of getting around these limitations by having a consistent ref
-// that points to the most recent value.
 export function useFavoriteStatus(
-  initialState: FavoriteStatus,
-  baseURL: string,
+  type: 'chart' | 'dashboard',
+  ids: Array<string | number>,
   handleErrorMsg: (message: string) => void,
 ) {
-  const [favoriteStatus, setFavoriteStatus] = useState<FavoriteStatus>(
-    initialState,
-  );
-  const favoriteStatusRef = useRef<FavoriteStatus>(favoriteStatus);
-  useEffect(() => {
-    favoriteStatusRef.current = favoriteStatus;
-  });
+  const [favoriteStatus, setFavoriteStatus] = useState<FavoriteStatus>({});
 
   const updateFavoriteStatus = (update: FavoriteStatus) =>
     setFavoriteStatus(currentState => ({ ...currentState, ...update }));
 
-  const fetchFaveStar = (id: number) => {
+  useEffect(() => {
+    if (!ids.length) {
+      return;
+    }
     SupersetClient.get({
-      endpoint: `${baseURL}/${id}/count/`,
+      endpoint: `/api/v1/${type}/favorite_status/?q=${rison.encode(ids)}`,
     }).then(
       ({ json }) => {
-        updateFavoriteStatus({ [id]: json.count > 0 });
+        const update = (json?.result as {
+          id: string;
+          value: boolean;
+        }[])?.reduce((acc, element) => {
+          acc[element.id] = element.value;
+          return acc;
+        }, {});
+        updateFavoriteStatus(update);
       },
       createErrorHandler(errMsg =>
         handleErrorMsg(
           t('There was an error fetching the favorite status: %s', errMsg),
         ),
       ),
     );
-  };
-
-  const saveFaveStar = (id: number, isStarred: boolean) => {
-    const urlSuffix = isStarred ? 'unselect' : 'select';
-
-    SupersetClient.get({
-      endpoint: `${baseURL}/${id}/${urlSuffix}/`,
-    }).then(
-      () => {
-        updateFavoriteStatus({ [id]: !isStarred });
-      },
-      createErrorHandler(errMsg =>
-        handleErrorMsg(
-          t('There was an error saving the favorite status: %s', errMsg),
+  }, [ids]);
+
+  const saveFaveStar = useCallback(
+    (id: number, isStarred: boolean) => {
+      const urlSuffix = isStarred ? 'unselect' : 'select';
+      console.log('fetching', id, isStarred);
+      SupersetClient.get({
+        endpoint: `/superset/favstar/${
+          type === 'chart' ? 'slice' : 'Dashboard'
+        }/${id}/${urlSuffix}/`,
+      }).then(
+        ({ json }) => {
+          updateFavoriteStatus({
+            [id]: (json as { count: number })?.count > 0,
+          });
+        },
+        createErrorHandler(errMsg =>
+          handleErrorMsg(
+            t('There was an error saving the favorite status: %s', errMsg),
+          ),

Review comment:
       I really like the `makeApi` util! 

##########
File path: superset/dashboards/api.py
##########
@@ -589,3 +596,48 @@ def thumbnail(
         return Response(
             FileWrapper(screenshot), mimetype="image/png", direct_passthrough=True
         )
+
+    @expose("/favorite_status/", methods=["GET"])
+    @protect()
+    @safe
+    @statsd_metrics
+    @rison(get_fav_star_ids_schema)
+    def favorite_status(self, **kwargs: Any) -> Response:
+        """Favorite Stars for Dashboards
+        ---
+        get:
+          description: >-
+            Check favorited dashboards for current user
+          parameters:
+          - in: query
+            name: q
+            content:
+              application/json:
+                schema:
+                  $ref: '#/components/schemas/get_fav_star_ids_schema'
+          responses:
+            200:
+              description:
+              content:
+                application/json:
+                  schema:
+                    $ref: "#/components/schemas/GetFavStarIdsSchema"
+            400:
+              $ref: '#/components/responses/400'
+            401:
+              $ref: '#/components/responses/401'
+            404:
+              $ref: '#/components/responses/404'
+            500:
+              $ref: '#/components/responses/500'
+        """
+        requested_ids = kwargs["rison"]
+        dashboards = DashboardDAO.find_by_ids(requested_ids)

Review comment:
       Unfortunately we need to run this additional query. The find_by_ids method has business logic to ensure the user has access to the requested dashboard by applying the dashboard filter (https://github.com/preset-io/incubator-superset/blob/cb866b30c0a7029ff558297b1f15c7328f64c500/superset/dashboards/filters.py#L59). We could try to fetch the favorite_ids with the dashboard filter in a subquery, but the performance gains wouldn't be significant. We're also doing something similar for bulk delete already (https://github.com/preset-io/incubator-superset/blob/cb866b30c0a7029ff558297b1f15c7328f64c500/superset/dashboards/commands/bulk_delete.py#L54). 

##########
File path: superset-frontend/src/views/CRUD/hooks.ts
##########
@@ -299,64 +301,85 @@ export function useSingleViewResource<D extends object = any>(
   };
 }
 
-// the hooks api has some known limitations around stale state in closures.
-// See https://github.com/reactjs/rfcs/blob/master/text/0068-react-hooks.md#drawbacks
-// the useRef hook is a way of getting around these limitations by having a consistent ref
-// that points to the most recent value.
+enum FavStarClassName {
+  CHART = 'slice',
+  DASHBOARD = 'Dashboard',
+}
+
+type FavoriteStatusResponse = {
+  result: Array<{
+    id: string;
+    value: boolean;
+  }>;
+};
+
+const favoriteApis = {
+  chart: makeApi<string, FavoriteStatusResponse>({

Review comment:
       Yup, just a simple `encodeRison` flag would be enough to pass a properly typed object yet still have it issue a rison request. 




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