You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by "zhaoyongjie (via GitHub)" <gi...@apache.org> on 2023/09/13 19:05:00 UTC

[GitHub] [superset] zhaoyongjie commented on pull request #25192: feat: Add slice_id to query object

zhaoyongjie commented on PR #25192:
URL: https://github.com/apache/superset/pull/25192#issuecomment-1718167112

   @Vitor-Avila Thanks for your contribution, but I have different potin for this feature, --- how to identify a dashboard or a chart in `SQL_QUERY_MUTATOR`.
   
   I don't think this PR is a good pattern in current Query Object because the "slice_id" isn't in a Query. In our fork-superset(Burda Group), we set lots of "tags" in Superset. e.g.:
   
   <img width="917" alt="image" src="https://github.com/apache/superset/assets/2016594/d5bac2c5-5f59-4520-a55a-79a4e0376823">
   
   In fact, this is a simple implement, the slice_id or any ID(dashboard_id, or others ids) passed from **url parameter**. 
   I give some pseudocode
   
   ```python
         def SQL_QUERY_MUTATOR(sql: str, **kwargs) -> str:
             import json
             import logging
             from enum import Enum
             logger = logging.getLogger(__name__)
   
             from flask import request
   
             from superset.security import SupersetSecurityManager
             from superset.models.dashboard import Dashboard
             from superset.models.slice import Slice as Chart
             from superset.datasource.dao import DatasourceDAO
             from superset import db
   
             class QueryType(str, Enum):
               DASHBOARD = "dashboard"
               CHART = "chart"
               REPORT = "report"
               SQL_LAB = "sql_lab"
               EXPLORE = "explore"
               NATIVE_FILTER = "native_filter"
   
             form_data = request.args.get("form_data", "")
             try:
               form_data = json.loads(form_data)
             except json.JSONDecodeError:
               form_data = {}
   
             slice_id = form_data.get("slice_id")
             dashboard_id = request.args.get("dashboard_id")
             query_info_dict = {}
   
             if dashboard_id is not None:
               # 1) Query from Dashboard
               dashboard = Dashboard.get(str(dashboard_id))
               query_info_dict["query_type"] = QueryType.DASHBOARD.value
               query_info_dict["dashboard_id"] = dashboard_id
               query_info_dict["dashboard_title"] = getattr(dashboard, "dashboard_title", None)
             if slice_id is not None:
               # 2) Query from Chart
               chart = Chart.get(slice_id)
               query_info_dict["query_type"] = QueryType.CHART.value
               query_info_dict["chart_id"] = slice_id
               query_info_dict["chart_title"] = getattr(chart, "slice_name", None)
   
             if slice_id is None and dashboard_id is None and "sql_json" in request.path:
               # 3) Query from SQL Lab
               query_info_dict["query_type"] = QueryType.SQL_LAB.value
   
             if (
               slice_id is None
               and dashboard_id is None
               and ({"/api/v1/chart/data", "/superset/explore_json/"}).intersection({request.path})
             ):
               # 4) Query from Explore(unsaved chart)
               query_info_dict["query_type"] = QueryType.EXPLORE.value
   
             if request.args.get("tag") == "report" and request.args.get("execution_id"):
               # 5) Query from Report
               query_info_dict["query_type"] = QueryType.REPORT.value
               query_info_dict["report_id"] = request.args.get("report_id")
               query_info_dict["execution_id"] = request.args.get("execution_id")
               query_info_dict["report_name"] = request.args.get("report_name")
   
             if request.args.get("tag") == "native_filter" and request.args.get("filter_name"):
               # 6) a query from Dashboard Native Filter
               query_info_dict["query_type"] = QueryType.NATIVE_FILTER.value
               query_info_dict["filter_name"] = request.args.get("filter_name")
             ....
             ....
   ```
   
   


-- 
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: notifications-unsubscribe@superset.apache.org

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