ktmud commented on a change in pull request #9284: Reduce dashboard bootstrap 
payload
URL: 
https://github.com/apache/incubator-superset/pull/9284#discussion_r391320599
 
 

 ##########
 File path: superset/connectors/base/models.py
 ##########
 @@ -213,6 +235,82 @@ def data(self) -> Dict[str, Any]:
             "select_star": self.select_star,
         }
 
+    def data_for_slices(self, slices: List[Slice]) -> Dict[str, Any]:
+        """
+        The representation of the datasource containing only the required data
+        to render the provided slices.
+
+        Used to reduce the payload when loading a dashboard.
+        """
+        data = self.data
+        metric_names = set()
+        column_names = set()
+        for slc in slices:
+            form_data = slc.form_data
+
+            # pull out all required metrics from the form_data
+            for param in METRICS_FORM_DATA_PARAMS:
+                if form_data.get(param):
+                    for metric in form_data.get(param, []):
+                        metric_names.add(utils.get_metric_name(metric))
+
+                        # extra logic to handle adhoc metrics with column 
references
+                        if isinstance(metric, dict) and metric.get("column"):
+                            column_names.add(
+                                metric.get("column", {}).get("column_name")
+                            )
+
+            for param in METRIC_FORM_DATA_PARAMS:
+                metric = form_data.get(param)
+                metric_names.add(utils.get_metric_name(metric))
+
+            # pull out all required columns from the form_data
+            if form_data.get("adhoc_filters"):
+                for filter_ in form_data.get("adhoc_filters", []):
+                    if filter_["clause"] == "WHERE" and filter_.get("subject"):
+                        column_names.add(filter_.get("subject"))
+
+            for param in COLUMNS_FORM_DATA_PARAMS:
+                if form_data.get(param):
+                    for column in form_data.get(param, []):
+                        column_names.add(column)
+
+            for param in COLUMN_FORM_DATA_PARAMS:
+                if form_data.get(param):
+                    column_names.add(form_data.get(param))
 
 Review comment:
   Maybe you can loop throw `form_data` keys and check whether it's in any of 
the consts instead.
   
   ```python
           metric_names = []
           column_names = []
   
           for slc in slices:
               # pull out all required columns from the form_data
               for param, value in slc.form_data.items():
                   if param == 'adhoc_filters':
                       column_names += [
                           filter_.get("subject") for filter_ in value
                           if filter_["clause"] == "WHERE" and 
filter_.get("subject")
                       ]
                   elif param in METRICS_FORM_DATA_PARAMS:
                       metric_names += map(utils.get_metric_name, value)
                       # extra logic to handle adhoc metrics with column 
references
                       column_names += [
                           metric["column"].get("column_name")
                           for metric in value
                           if isinstance(metric, dict) and 
isinstance(metric["column"])
                       ]
                   elif param in METRIC_FORM_DATA_PARAMS:
                       metric_names.append(utils.get_metric_name(value))
                   elif param in COLUMNS_FORM_DATA_PARAMS:
                       column_names += value
                   elif param in COLUMN_FORM_DATA_PARAMS:
                       column_names.append(value)
   
           metric_names = set(metric_names)
           column_names = set(column_names)
   ```
   
   Btw the consts should probably be `set()`.

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to