etr2460 commented on a change in pull request #9311: [cache warm_up] warm_up
slice with dashboard default_filters
URL:
https://github.com/apache/incubator-superset/pull/9311#discussion_r393810831
##########
File path: superset/views/utils.py
##########
@@ -262,3 +263,89 @@ def get_time_range_endpoints(
return (TimeRangeEndpoint(start), TimeRangeEndpoint(end))
return (TimeRangeEndpoint.INCLUSIVE, TimeRangeEndpoint.EXCLUSIVE)
+
+
+CONTAINER_TYPES = ["COLUMN", "GRID", "TABS", "TAB", "ROW"]
+
+
+def get_dashboard_extra_filters(
+ slice_id: int, dashboard_id: int
+) -> List[Dict[str, Any]]:
+ session = db.session()
+ slc = session.query(Slice).filter_by(id=slice_id).one_or_none()
+ dashboard =
session.query(Dashboard).filter_by(id=dashboard_id).one_or_none()
+
+ # is chart in this dashboard?
+ if (
+ dashboard is None
+ or not dashboard.json_metadata
+ or not dashboard.slices
+ or slc not in dashboard.slices
+ ):
+ return []
+
+ # is this dashboard has default filters?
+ json_metadata = json.loads(dashboard.json_metadata)
+ default_filters = json.loads(json_metadata.get("default_filters", "null"))
+ if not default_filters:
+ return []
+
+ # is default filters applicable to the given slice?
+ filter_scopes = json_metadata.get("filter_scopes", {})
+ layout = json.loads(dashboard.position_json or "{}")
+
+ return build_extra_filters(layout, filter_scopes, default_filters,
slice_id)
+
+
+def build_extra_filters(
+ layout: Dict,
+ filter_scopes: Dict,
+ default_filters: Dict[str, Dict[str, List]],
+ slice_id: int,
+) -> List[Dict[str, Any]]:
+ extra_filters = []
+
+ # do not apply filters if chart if chart not in filter's scope or
+ # chart is immune to the filter
+ for filter_id, columns in default_filters.items():
+ scopes_by_filter_field = filter_scopes.get(filter_id, {})
+ for col, val in columns.items():
+ current_field_scopes = scopes_by_filter_field.get(col, {})
+ # scope is list of container ids
+ scope = current_field_scopes.get("scope", ["ROOT_ID"])
+ # immune is list of slice ids
+ immune = current_field_scopes.get("immune", [])
Review comment:
instead of using comments to describe these variable names, let's name them
to something like `scoped_container_ids` and `immune_slice_ids`
----------------------------------------------------------------
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]