aminghadersohi commented on code in PR #46045:
URL: https://github.com/apache/superset/pull/46045#discussion_r4488648466
##########
superset/daos/dataset.py:
##########
@@ -192,8 +191,64 @@ def get_related_objects_for_datasets(dataset_ids:
list[int]) -> dict[str, Any]:
.distinct()
.all()
)
+ seen_ids = {dashboard.id for dashboard in dashboards}
+ extra_ids = [
+ dashboard_id
+ for dashboard_id in DatasetDAO._dashboard_ids_targeting_datasets(
+ dataset_ids
+ )
+ if dashboard_id not in seen_ids
+ ]
+ if extra_ids:
+ dashboards.extend(
+
db.session.query(Dashboard).filter(Dashboard.id.in_(extra_ids)).all()
+ )
return {"charts": charts, "dashboards": dashboards}
+ @staticmethod
+ def get_charts_for_datasets(dataset_ids: list[int]) -> list[Slice]:
+ """
+ Return the charts built on any of the datasets. Callers that only need
+ charts should use this rather than ``get_related_objects``, which also
+ scans dashboard metadata.
+ """
+ if not dataset_ids:
+ return []
+ return (
+ db.session.query(Slice)
+ .filter(
+ Slice.datasource_id.in_(dataset_ids),
+ Slice.datasource_type == DatasourceType.TABLE,
+ )
+ .all()
+ )
+
+ @staticmethod
+ def _dashboard_ids_targeting_datasets(dataset_ids: list[int]) -> list[int]:
+ """
+ Ids of the dashboards whose native filters or display controls target
+ any of the datasets.
+
+ Targets live in ``json_metadata`` text, so a cheap ``LIKE`` prefilter
+ narrows the rows and the parsed metadata is then checked exactly.
+ Dashboards with missing or malformed metadata never match.
+ """
+ wanted = set(dataset_ids)
+ rows = (
+ db.session.query(Dashboard.id, Dashboard.json_metadata)
+ .filter(
+ Dashboard.json_metadata.like('%"datasetId"%'),
+ Dashboard.deleted_at.is_(None),
+ )
+ .order_by(Dashboard.id)
+ .all()
+ )
Review Comment:
Acceptable as is: filter targets exist only in json_metadata, so no index
can serve any correct lookup. It runs only on delete/usage lookups, never on
list pages; I measured 0.15 s at 1k and 2.7 s at 20k dashboards (14 KB
metadata), against the indexed chart query's ~2 ms.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]