codeant-ai-for-open-source[bot] commented on code in PR #41133: URL: https://github.com/apache/superset/pull/41133#discussion_r3573280683
########## superset/tasks/export_dashboard_excel.py: ########## @@ -0,0 +1,361 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +""" +Celery task that exports every chart on a dashboard to a single multi-sheet +``.xlsx`` file, uploads it to S3, and emails the requesting user a pre-signed +download link. + +In ``"data"`` mode the task re-runs each chart's saved query context under the +requesting user, applies the live dashboard filter state, and streams the results +row-by-row into a constant-memory workbook so large dashboards never load all +data at once. In ``"images"`` mode non-table charts are instead rendered to +images (through the same headless path as scheduled reports, reflecting the live +filters) and embedded, while table-like charts stay tabular. +""" + +from __future__ import annotations + +import logging +import os +import tempfile +from datetime import datetime, timedelta, timezone +from typing import Any + +from celery.exceptions import SoftTimeLimitExceeded +from flask import current_app, g + +from superset import db, security_manager +from superset.charts.data.dashboard_filter_context import ( + apply_dashboard_filter_context, + get_dashboard_filter_context, +) +from superset.charts.schemas import ChartDataQueryContextSchema +from superset.commands.chart.data.get_data_command import ChartDataCommand +from superset.commands.distributed_lock.release import ReleaseDistributedLock +from superset.common.chart_data import ChartDataResultFormat, ChartDataResultType +from superset.dashboards.excel_export import email +from superset.dashboards.excel_export.layout import get_charts_in_layout_order +from superset.dashboards.excel_export.screenshot import render_chart_image +from superset.extensions import celery_app +from superset.utils import json, s3 +from superset.utils.core import override_user +from superset.utils.excel_streaming import StreamingXlsxWriter + +logger = logging.getLogger(__name__) + +# Export modes: "data" streams every chart's tabular result (the default, +# unchanged behavior); "images" embeds non-table charts as rendered images and +# keeps only table-like charts tabular. +EXPORT_MODE_DATA = "data" +EXPORT_MODE_IMAGES = "images" + +# Viz types kept as tabular data in image mode; everything else is rendered as an +# image. Operators can override the set via ``EXCEL_EXPORT_TABLE_VIZ_TYPES``. +TABLE_VIZ_TYPES = {"table", "pivot_table_v2", "pivot_table"} + +EXPORT_SOFT_TIME_LIMIT = 600 +EXPORT_HARD_TIME_LIMIT = 660 + +# Namespace + TTL for the per-user+dashboard in-flight lock the API acquires +# before enqueue and this task releases when it settles. The lock uses the +# shared, atomic DistributedLock backend (Redis when configured, the metadata +# DB otherwise) so it actually synchronizes across the web server and workers β +# unlike a plain cache, which is a no-op under the default ``NullCache``. +# The TTL outlives the hard time limit so a worker killed at that limit (which +# skips the ``finally`` release) cannot hold the lock forever; the release in +# ``finally`` is the fast path that frees it as soon as the task settles. +EXPORT_LOCK_NAMESPACE = "excel_export" +EXPORT_LOCK_TTL_SECONDS = EXPORT_HARD_TIME_LIMIT + 60 + + +def export_lock_params(user_id: int, dashboard_id: int) -> dict[str, int]: + """Key parameters identifying the per-user+dashboard in-flight lock.""" + return {"user_id": user_id, "dashboard_id": dashboard_id} + + +class _ChartSkippedError(Exception): + """Signals a chart that could not be exported and should be listed as skipped.""" + + +def _chart_label(chart: Any) -> str: + """Human-readable label for a chart in the skipped-charts list.""" + return f"{chart.id} - {chart.slice_name or ''}".strip() + + +def _record_to_row(record: dict[str, Any], colnames: list[str]) -> list[Any]: + return [record.get(col) for col in colnames] + + +def _table_viz_types() -> set[str]: + """Viz types kept tabular in image mode (config override or built-in default).""" + return current_app.config.get("EXCEL_EXPORT_TABLE_VIZ_TYPES") or TABLE_VIZ_TYPES + + +def _renders_as_image(chart: Any, mode: str) -> bool: + """Whether this chart is embedded as an image rather than streamed as data.""" + return mode == EXPORT_MODE_IMAGES and chart.viz_type not in _table_viz_types() + + +def _write_chart_image_sheet( + writer: StreamingXlsxWriter, + chart: Any, + dashboard_id: int, + active_data_mask: dict[str, Any], + user: Any, +) -> None: + """ + Render a single chart to an image and embed it as its own sheet. + + :raises _ChartSkippedError: if the chart could not be rendered + """ + image = render_chart_image(chart, dashboard_id, active_data_mask, user) + if image is None: + raise _ChartSkippedError + writer.add_image_sheet(_chart_label(chart), image) + + +def _write_chart_sheets( + writer: StreamingXlsxWriter, + chart: Any, + dashboard_id: int, + active_data_mask: dict[str, Any], +) -> None: + """ + Run a single chart's query and stream its result(s) into the workbook. + + Charts may yield more than one query (e.g. mixed-series charts); each becomes + its own sheet. Raises if the chart cannot be exported, so the caller can skip + it and note it in the email. + """ + json_body = json.loads(chart.query_context) + # Override any stale saved values: we always want full JSON results. + json_body["result_format"] = ChartDataResultFormat.JSON + json_body["result_type"] = ChartDataResultType.FULL + json_body.pop("force", None) + + filter_context = get_dashboard_filter_context( + dashboard_id=dashboard_id, + chart_id=chart.id, + active_data_mask=active_data_mask, + ) + if filter_context.extra_form_data: + apply_dashboard_filter_context(json_body, filter_context.extra_form_data) + + # Jinja macros resolve form data from g.form_data; expose the saved context. + g.form_data = json_body + + query_context = ChartDataQueryContextSchema().load(json_body) + command = ChartDataCommand(query_context) + command.validate() + result = command.run() + + for index, query in enumerate(result["queries"]): + colnames = query.get("colnames") or [] + data = query.get("data") or [] + if index == 0: + name = f"{chart.id} - {chart.slice_name or ''}" + else: + name = f"{chart.id}.{index} - {chart.slice_name or ''}" + writer.add_sheet( + name, + colnames, + (_record_to_row(record, colnames) for record in data), + ) + + +def _build_workbook( + path: str, + dashboard: Any, + active_data_mask: dict[str, Any], + job_id: str, + mode: str, + user: Any, +) -> dict[str, list[str]]: + """Build the workbook on disk. + + Return the charts that could not be exported, grouped by the reason they + were omitted (see the ``email.ERROR_*`` reason keys), so the notification + can explain each group separately. + """ + errored: dict[str, list[str]] = {} + writer = StreamingXlsxWriter(path) + try: + for chart in get_charts_in_layout_order(dashboard): + label = _chart_label(chart) + as_image = _renders_as_image(chart, mode) + # Image charts render from their saved params and don't need a query + # context; data (and table) charts still do. + if not as_image and not chart.query_context: + errored.setdefault(email.ERROR_NO_QUERY_CONTEXT, []).append(label) + continue + try: + if as_image: + _write_chart_image_sheet( + writer, chart, dashboard.id, active_data_mask, user + ) + else: + _write_chart_sheets(writer, chart, dashboard.id, active_data_mask) + except SoftTimeLimitExceeded: + # A soft timeout is a task-level signal, not a per-chart failure: + # let it propagate so the outer handler emails a failure and runs + # cleanup, rather than continuing until the hard limit kills the + # worker (which would skip cleanup, leak temp files, and hold the + # in-flight lock until its TTL). ``except Exception`` below would + # otherwise swallow it, since it subclasses ``Exception``. + raise + except _ChartSkippedError: + logger.warning( + "Skipping chart %s in dashboard export %s (could not render)", + chart.id, + job_id, + ) + errored.setdefault(email.ERROR_GENERAL, []).append(label) + except Exception: # pylint: disable=broad-except + logger.exception( + "Skipping chart %s in dashboard export %s", chart.id, job_id + ) + errored.setdefault(email.ERROR_GENERAL, []).append(label) + + if writer.sheet_count == 0: + flat = [label for labels in errored.values() for label in labels] + writer.add_summary_sheet( + "Export Summary", + ["No chart data could be exported.", *flat], + ) + finally: + writer.close() + return errored + + +def _send_failure_email( + user: Any, dashboard_title: str, requested_at: datetime +) -> None: + if not (user and getattr(user, "email", None)): + return + try: + email.send_export_email( + user.email, + email.build_subject(dashboard_title, success=False), + email.build_failure_email(dashboard_title, requested_at), + ) + except Exception: # pylint: disable=broad-except + logger.exception("Failed to send export failure email") + + +@celery_app.task( + name="export_dashboard_excel", + bind=True, + soft_time_limit=EXPORT_SOFT_TIME_LIMIT, + time_limit=EXPORT_HARD_TIME_LIMIT, + max_retries=0, +) +def export_dashboard_excel( + self: Any, # pylint: disable=unused-argument + dashboard_id: int, + user_id: int, + active_data_mask: dict[str, Any], + job_id: str, + mode: str = EXPORT_MODE_DATA, +) -> None: + """ + Export a dashboard's charts to an ``.xlsx`` and email a download link. + + :param dashboard_id: The dashboard to export + :param user_id: The requesting user (the task runs with their permissions) + :param active_data_mask: Live dashboard filter state keyed by native filter id + :param job_id: Correlation id, also the Celery task id and S3 object name + :param mode: ``"data"`` streams every chart's tabular result; ``"images"`` + embeds non-table charts as rendered images and keeps tables tabular + """ + # pylint: disable=import-outside-toplevel + from superset.models.dashboard import Dashboard + + requested_at = datetime.now(tz=timezone.utc) + user = security_manager.get_user_by_id(user_id) + dashboard_title = "" Review Comment: **Suggestion:** Declare an explicit type for this local string variable to satisfy the type-hint requirement for relevant variables. [custom_rule] **Severity Level:** Minor π§Ή <details> <summary><b>Why it matters? β </b></summary> This local string variable is assigned without a type hint in newly added Python code, and it can be annotated as a string. That is a valid instance of the missing type-hint rule. </details> <details> <summary><b>Rule source π </b></summary> .cursor/rules/dev-standard.mdc (line 28) </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=876f48e903b54391ad5b2ab15efa6d0f&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=876f48e903b54391ad5b2ab15efa6d0f&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent π€ </b></summary> ```mdx This is a comment left during a code review. **Path:** superset/tasks/export_dashboard_excel.py **Line:** 289:289 **Comment:** *Custom Rule: Declare an explicit type for this local string variable to satisfy the type-hint requirement for relevant variables. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41133&comment_hash=ba8da4c7773db0e5539853e69d1ffa427a1394be7c11638fd98597a1e4a6a44d&reaction=like'>π</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41133&comment_hash=ba8da4c7773db0e5539853e69d1ffa427a1394be7c11638fd98597a1e4a6a44d&reaction=dislike'>π</a> ########## superset/tasks/export_dashboard_excel.py: ########## @@ -0,0 +1,361 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +""" +Celery task that exports every chart on a dashboard to a single multi-sheet +``.xlsx`` file, uploads it to S3, and emails the requesting user a pre-signed +download link. + +In ``"data"`` mode the task re-runs each chart's saved query context under the +requesting user, applies the live dashboard filter state, and streams the results +row-by-row into a constant-memory workbook so large dashboards never load all +data at once. In ``"images"`` mode non-table charts are instead rendered to +images (through the same headless path as scheduled reports, reflecting the live +filters) and embedded, while table-like charts stay tabular. +""" + +from __future__ import annotations + +import logging +import os +import tempfile +from datetime import datetime, timedelta, timezone +from typing import Any + +from celery.exceptions import SoftTimeLimitExceeded +from flask import current_app, g + +from superset import db, security_manager +from superset.charts.data.dashboard_filter_context import ( + apply_dashboard_filter_context, + get_dashboard_filter_context, +) +from superset.charts.schemas import ChartDataQueryContextSchema +from superset.commands.chart.data.get_data_command import ChartDataCommand +from superset.commands.distributed_lock.release import ReleaseDistributedLock +from superset.common.chart_data import ChartDataResultFormat, ChartDataResultType +from superset.dashboards.excel_export import email +from superset.dashboards.excel_export.layout import get_charts_in_layout_order +from superset.dashboards.excel_export.screenshot import render_chart_image +from superset.extensions import celery_app +from superset.utils import json, s3 +from superset.utils.core import override_user +from superset.utils.excel_streaming import StreamingXlsxWriter + +logger = logging.getLogger(__name__) + +# Export modes: "data" streams every chart's tabular result (the default, +# unchanged behavior); "images" embeds non-table charts as rendered images and +# keeps only table-like charts tabular. +EXPORT_MODE_DATA = "data" +EXPORT_MODE_IMAGES = "images" + +# Viz types kept as tabular data in image mode; everything else is rendered as an +# image. Operators can override the set via ``EXCEL_EXPORT_TABLE_VIZ_TYPES``. +TABLE_VIZ_TYPES = {"table", "pivot_table_v2", "pivot_table"} + +EXPORT_SOFT_TIME_LIMIT = 600 +EXPORT_HARD_TIME_LIMIT = 660 + +# Namespace + TTL for the per-user+dashboard in-flight lock the API acquires +# before enqueue and this task releases when it settles. The lock uses the +# shared, atomic DistributedLock backend (Redis when configured, the metadata +# DB otherwise) so it actually synchronizes across the web server and workers β +# unlike a plain cache, which is a no-op under the default ``NullCache``. +# The TTL outlives the hard time limit so a worker killed at that limit (which +# skips the ``finally`` release) cannot hold the lock forever; the release in +# ``finally`` is the fast path that frees it as soon as the task settles. +EXPORT_LOCK_NAMESPACE = "excel_export" +EXPORT_LOCK_TTL_SECONDS = EXPORT_HARD_TIME_LIMIT + 60 + + +def export_lock_params(user_id: int, dashboard_id: int) -> dict[str, int]: + """Key parameters identifying the per-user+dashboard in-flight lock.""" + return {"user_id": user_id, "dashboard_id": dashboard_id} + + +class _ChartSkippedError(Exception): + """Signals a chart that could not be exported and should be listed as skipped.""" + + +def _chart_label(chart: Any) -> str: + """Human-readable label for a chart in the skipped-charts list.""" + return f"{chart.id} - {chart.slice_name or ''}".strip() + + +def _record_to_row(record: dict[str, Any], colnames: list[str]) -> list[Any]: + return [record.get(col) for col in colnames] + + +def _table_viz_types() -> set[str]: + """Viz types kept tabular in image mode (config override or built-in default).""" + return current_app.config.get("EXCEL_EXPORT_TABLE_VIZ_TYPES") or TABLE_VIZ_TYPES + + +def _renders_as_image(chart: Any, mode: str) -> bool: + """Whether this chart is embedded as an image rather than streamed as data.""" + return mode == EXPORT_MODE_IMAGES and chart.viz_type not in _table_viz_types() + + +def _write_chart_image_sheet( + writer: StreamingXlsxWriter, + chart: Any, + dashboard_id: int, + active_data_mask: dict[str, Any], + user: Any, +) -> None: + """ + Render a single chart to an image and embed it as its own sheet. + + :raises _ChartSkippedError: if the chart could not be rendered + """ + image = render_chart_image(chart, dashboard_id, active_data_mask, user) + if image is None: + raise _ChartSkippedError + writer.add_image_sheet(_chart_label(chart), image) + + +def _write_chart_sheets( + writer: StreamingXlsxWriter, + chart: Any, + dashboard_id: int, + active_data_mask: dict[str, Any], +) -> None: + """ + Run a single chart's query and stream its result(s) into the workbook. + + Charts may yield more than one query (e.g. mixed-series charts); each becomes + its own sheet. Raises if the chart cannot be exported, so the caller can skip + it and note it in the email. + """ + json_body = json.loads(chart.query_context) + # Override any stale saved values: we always want full JSON results. + json_body["result_format"] = ChartDataResultFormat.JSON + json_body["result_type"] = ChartDataResultType.FULL + json_body.pop("force", None) + + filter_context = get_dashboard_filter_context( + dashboard_id=dashboard_id, + chart_id=chart.id, + active_data_mask=active_data_mask, + ) + if filter_context.extra_form_data: + apply_dashboard_filter_context(json_body, filter_context.extra_form_data) + + # Jinja macros resolve form data from g.form_data; expose the saved context. + g.form_data = json_body + + query_context = ChartDataQueryContextSchema().load(json_body) + command = ChartDataCommand(query_context) + command.validate() + result = command.run() + + for index, query in enumerate(result["queries"]): + colnames = query.get("colnames") or [] + data = query.get("data") or [] + if index == 0: + name = f"{chart.id} - {chart.slice_name or ''}" + else: + name = f"{chart.id}.{index} - {chart.slice_name or ''}" + writer.add_sheet( + name, + colnames, + (_record_to_row(record, colnames) for record in data), + ) + + +def _build_workbook( + path: str, + dashboard: Any, + active_data_mask: dict[str, Any], + job_id: str, + mode: str, + user: Any, +) -> dict[str, list[str]]: + """Build the workbook on disk. + + Return the charts that could not be exported, grouped by the reason they + were omitted (see the ``email.ERROR_*`` reason keys), so the notification + can explain each group separately. + """ + errored: dict[str, list[str]] = {} + writer = StreamingXlsxWriter(path) + try: + for chart in get_charts_in_layout_order(dashboard): + label = _chart_label(chart) + as_image = _renders_as_image(chart, mode) + # Image charts render from their saved params and don't need a query + # context; data (and table) charts still do. + if not as_image and not chart.query_context: + errored.setdefault(email.ERROR_NO_QUERY_CONTEXT, []).append(label) + continue + try: + if as_image: + _write_chart_image_sheet( + writer, chart, dashboard.id, active_data_mask, user + ) + else: + _write_chart_sheets(writer, chart, dashboard.id, active_data_mask) + except SoftTimeLimitExceeded: + # A soft timeout is a task-level signal, not a per-chart failure: + # let it propagate so the outer handler emails a failure and runs + # cleanup, rather than continuing until the hard limit kills the + # worker (which would skip cleanup, leak temp files, and hold the + # in-flight lock until its TTL). ``except Exception`` below would + # otherwise swallow it, since it subclasses ``Exception``. + raise + except _ChartSkippedError: + logger.warning( + "Skipping chart %s in dashboard export %s (could not render)", + chart.id, + job_id, + ) + errored.setdefault(email.ERROR_GENERAL, []).append(label) + except Exception: # pylint: disable=broad-except + logger.exception( + "Skipping chart %s in dashboard export %s", chart.id, job_id + ) + errored.setdefault(email.ERROR_GENERAL, []).append(label) + + if writer.sheet_count == 0: + flat = [label for labels in errored.values() for label in labels] + writer.add_summary_sheet( + "Export Summary", + ["No chart data could be exported.", *flat], + ) + finally: + writer.close() + return errored + + +def _send_failure_email( + user: Any, dashboard_title: str, requested_at: datetime +) -> None: + if not (user and getattr(user, "email", None)): + return + try: + email.send_export_email( + user.email, + email.build_subject(dashboard_title, success=False), + email.build_failure_email(dashboard_title, requested_at), + ) + except Exception: # pylint: disable=broad-except + logger.exception("Failed to send export failure email") + + +@celery_app.task( + name="export_dashboard_excel", + bind=True, + soft_time_limit=EXPORT_SOFT_TIME_LIMIT, + time_limit=EXPORT_HARD_TIME_LIMIT, + max_retries=0, +) +def export_dashboard_excel( + self: Any, # pylint: disable=unused-argument + dashboard_id: int, + user_id: int, + active_data_mask: dict[str, Any], + job_id: str, + mode: str = EXPORT_MODE_DATA, +) -> None: + """ + Export a dashboard's charts to an ``.xlsx`` and email a download link. + + :param dashboard_id: The dashboard to export + :param user_id: The requesting user (the task runs with their permissions) + :param active_data_mask: Live dashboard filter state keyed by native filter id + :param job_id: Correlation id, also the Celery task id and S3 object name + :param mode: ``"data"`` streams every chart's tabular result; ``"images"`` + embeds non-table charts as rendered images and keeps tables tabular + """ + # pylint: disable=import-outside-toplevel + from superset.models.dashboard import Dashboard + + requested_at = datetime.now(tz=timezone.utc) Review Comment: **Suggestion:** Add an explicit type annotation for this local variable so its expected datetime value is clear to static type checkers. [custom_rule] **Severity Level:** Minor π§Ή <details> <summary><b>Why it matters? β </b></summary> This new Python code declares a local variable without a type hint, and it is clearly annotatable as a datetime value. That matches the rule requiring type hints on relevant variables. </details> <details> <summary><b>Rule source π </b></summary> .cursor/rules/dev-standard.mdc (line 28) </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=2279fc591fda472a95824c0d92b8182d&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=2279fc591fda472a95824c0d92b8182d&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent π€ </b></summary> ```mdx This is a comment left during a code review. **Path:** superset/tasks/export_dashboard_excel.py **Line:** 287:287 **Comment:** *Custom Rule: Add an explicit type annotation for this local variable so its expected datetime value is clear to static type checkers. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41133&comment_hash=2c4e7c2b17bef41d8999fb789a9d0da95303d0626bf5225b7521a115e9552bd5&reaction=like'>π</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41133&comment_hash=2c4e7c2b17bef41d8999fb789a9d0da95303d0626bf5225b7521a115e9552bd5&reaction=dislike'>π</a> ########## tests/integration_tests/dashboards/api_tests.py: ########## @@ -3262,6 +3265,102 @@ def test_get_all_related_viewers_with_extra_filters(self): response = json.loads(rv.data.decode("utf-8")) assert response["count"] > 0 + def test_export_xlsx_501_when_bucket_unset(self): Review Comment: **Suggestion:** Add explicit type hints to this new test method signature, including a return type annotation. [custom_rule] **Severity Level:** Minor π§Ή <details> <summary><b>Why it matters? β </b></summary> This is a newly added Python test method with no argument or return type annotations, which violates the requirement to add type hints to new or modified Python code. </details> <details> <summary><b>Rule source π </b></summary> .cursor/rules/dev-standard.mdc (line 28) </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=22fd63f3d7a247ff8db46d5eed848b99&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=22fd63f3d7a247ff8db46d5eed848b99&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent π€ </b></summary> ```mdx This is a comment left during a code review. **Path:** tests/integration_tests/dashboards/api_tests.py **Line:** 3268:3268 **Comment:** *Custom Rule: Add explicit type hints to this new test method signature, including a return type annotation. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41133&comment_hash=0131206b9d29b64f2ac210054334d37c22c7a30c784e55b227f555eef9f52939&reaction=like'>π</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41133&comment_hash=0131206b9d29b64f2ac210054334d37c22c7a30c784e55b227f555eef9f52939&reaction=dislike'>π</a> ########## tests/integration_tests/dashboards/api_tests.py: ########## @@ -3262,6 +3265,102 @@ def test_get_all_related_viewers_with_extra_filters(self): response = json.loads(rv.data.decode("utf-8")) assert response["count"] > 0 + def test_export_xlsx_501_when_bucket_unset(self): + """Dashboard API: export_xlsx returns 501 when the S3 bucket is unset.""" + admin = self.get_user("admin") + dashboard = self.insert_dashboard("xlsx-501", None, [admin.id]) + self.login(ADMIN_USERNAME) + try: + rv = self.client.post(f"api/v1/dashboard/{dashboard.id}/export_xlsx/") + assert rv.status_code == 501 + finally: + db.session.delete(dashboard) + db.session.commit() + + @with_config({"EXCEL_EXPORT_S3_BUCKET": "exports"}) + @patch("superset.dashboards.api.export_dashboard_excel") + def test_export_xlsx_404_for_missing_dashboard(self, mock_task): Review Comment: **Suggestion:** Add type annotations for the mock argument and the return type on this new test method. [custom_rule] **Severity Level:** Minor π§Ή <details> <summary><b>Why it matters? β </b></summary> This new test method is unannotated: both the mock parameter and the return type lack type hints, so it matches the custom typing rule violation. </details> <details> <summary><b>Rule source π </b></summary> .cursor/rules/dev-standard.mdc (line 28) </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=91a35a70832d4bb88d800832e96604d2&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=91a35a70832d4bb88d800832e96604d2&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent π€ </b></summary> ```mdx This is a comment left during a code review. **Path:** tests/integration_tests/dashboards/api_tests.py **Line:** 3282:3282 **Comment:** *Custom Rule: Add type annotations for the mock argument and the return type on this new test method. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41133&comment_hash=f3941ba0f263d549accffc11c1bbe601785d7eadfe69669bbf1a2b8385d68be4&reaction=like'>π</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41133&comment_hash=f3941ba0f263d549accffc11c1bbe601785d7eadfe69669bbf1a2b8385d68be4&reaction=dislike'>π</a> ########## tests/integration_tests/dashboards/api_tests.py: ########## @@ -3262,6 +3265,102 @@ def test_get_all_related_viewers_with_extra_filters(self): response = json.loads(rv.data.decode("utf-8")) assert response["count"] > 0 + def test_export_xlsx_501_when_bucket_unset(self): + """Dashboard API: export_xlsx returns 501 when the S3 bucket is unset.""" + admin = self.get_user("admin") + dashboard = self.insert_dashboard("xlsx-501", None, [admin.id]) + self.login(ADMIN_USERNAME) + try: + rv = self.client.post(f"api/v1/dashboard/{dashboard.id}/export_xlsx/") + assert rv.status_code == 501 + finally: + db.session.delete(dashboard) + db.session.commit() + + @with_config({"EXCEL_EXPORT_S3_BUCKET": "exports"}) + @patch("superset.dashboards.api.export_dashboard_excel") + def test_export_xlsx_404_for_missing_dashboard(self, mock_task): + """Dashboard API: export_xlsx returns 404 for an unknown dashboard.""" + self.login(ADMIN_USERNAME) + rv = self.client.post("api/v1/dashboard/99999999/export_xlsx/") + assert rv.status_code == 404 + mock_task.apply_async.assert_not_called() + + @with_config({"EXCEL_EXPORT_S3_BUCKET": "exports"}) + @patch("superset.dashboards.api.export_dashboard_excel") + def test_export_xlsx_400_for_empty_dashboard(self, mock_task): + """Dashboard API: export_xlsx returns 400 for a dashboard with no charts.""" + admin = self.get_user("admin") + dashboard = self.insert_dashboard("xlsx-empty", None, [admin.id]) + self.login(ADMIN_USERNAME) + try: + rv = self.client.post(f"api/v1/dashboard/{dashboard.id}/export_xlsx/") + assert rv.status_code == 400 + mock_task.apply_async.assert_not_called() + finally: + db.session.delete(dashboard) + db.session.commit() + + @pytest.mark.usefixtures("load_world_bank_dashboard_with_slices") + @with_config({"EXCEL_EXPORT_S3_BUCKET": "exports"}) + @patch("superset.dashboards.api.AcquireDistributedLock") + @patch("superset.dashboards.api.export_dashboard_excel") + def test_export_xlsx_202_enqueues_task(self, mock_task, mock_acquire): + """Dashboard API: export_xlsx enqueues the task and returns 202 + job_id.""" + self.login(ADMIN_USERNAME) + dashboard = db.session.query(Dashboard).filter_by(slug="world_health").first() + rv = self.client.post( + f"api/v1/dashboard/{dashboard.id}/export_xlsx/", + json={"active_data_mask": {}}, + ) + assert rv.status_code == 202 + body = json.loads(rv.data.decode("utf-8")) + job_id = body["job_id"] + assert job_id + # The in-flight lock is acquired before the task is enqueued. + mock_acquire.return_value.run.assert_called_once() Review Comment: **Suggestion:** Add full type hints to this newly added multiline method signature, including argument and return annotations. [custom_rule] **Severity Level:** Minor π§Ή <details> <summary><b>Why it matters? β </b></summary> This new multiline test method is also unannotated on both parameters and return type, so it violates the Python type-hint requirement. </details> <details> <summary><b>Rule source π </b></summary> .cursor/rules/dev-standard.mdc (line 28) </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=a6c1558f62be4e428f8afd11f33dd219&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=a6c1558f62be4e428f8afd11f33dd219&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent π€ </b></summary> ```mdx This is a comment left during a code review. **Path:** tests/integration_tests/dashboards/api_tests.py **Line:** 3319:3321 **Comment:** *Custom Rule: Add full type hints to this newly added multiline method signature, including argument and return annotations. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41133&comment_hash=8ec555cc33ff4cd9850a4ee63574ef6f5067efee1663065c7bd10da813dc4007&reaction=like'>π</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41133&comment_hash=8ec555cc33ff4cd9850a4ee63574ef6f5067efee1663065c7bd10da813dc4007&reaction=dislike'>π</a> ########## tests/unit_tests/dashboards/test_excel_export_screenshot.py: ########## @@ -0,0 +1,166 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +from typing import Any +from unittest.mock import MagicMock, patch + +import pytest +from celery.exceptions import SoftTimeLimitExceeded + +from superset.charts.data.dashboard_filter_context import DashboardFilterContext +from superset.dashboards.excel_export import screenshot as screenshot_module +from superset.dashboards.excel_export.screenshot import render_chart_image +from superset.utils import json + +MODULE = "superset.dashboards.excel_export.screenshot" Review Comment: **Suggestion:** Add an explicit type annotation to this module-level constant to satisfy the type-hint requirement for relevant variables. [custom_rule] **Severity Level:** Minor π§Ή <details> <summary><b>Why it matters? β </b></summary> The file is newly added Python code and this module-level constant is unannotated even though it can clearly be typed as a string, so it matches the type-hint requirement. </details> <details> <summary><b>Rule source π </b></summary> .cursor/rules/dev-standard.mdc (line 28) </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=b933975cf2c149d785a6a1e330657453&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=b933975cf2c149d785a6a1e330657453&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent π€ </b></summary> ```mdx This is a comment left during a code review. **Path:** tests/unit_tests/dashboards/test_excel_export_screenshot.py **Line:** 30:30 **Comment:** *Custom Rule: Add an explicit type annotation to this module-level constant to satisfy the type-hint requirement for relevant variables. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41133&comment_hash=671b084f9056f59f3d49b70a99dee469656dd953c1fb341891978d8d56ba7ca1&reaction=like'>π</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41133&comment_hash=671b084f9056f59f3d49b70a99dee469656dd953c1fb341891978d8d56ba7ca1&reaction=dislike'>π</a> ########## tests/integration_tests/dashboards/api_tests.py: ########## @@ -3262,6 +3265,102 @@ def test_get_all_related_viewers_with_extra_filters(self): response = json.loads(rv.data.decode("utf-8")) assert response["count"] > 0 + def test_export_xlsx_501_when_bucket_unset(self): + """Dashboard API: export_xlsx returns 501 when the S3 bucket is unset.""" + admin = self.get_user("admin") + dashboard = self.insert_dashboard("xlsx-501", None, [admin.id]) + self.login(ADMIN_USERNAME) + try: + rv = self.client.post(f"api/v1/dashboard/{dashboard.id}/export_xlsx/") + assert rv.status_code == 501 + finally: + db.session.delete(dashboard) + db.session.commit() + + @with_config({"EXCEL_EXPORT_S3_BUCKET": "exports"}) + @patch("superset.dashboards.api.export_dashboard_excel") + def test_export_xlsx_404_for_missing_dashboard(self, mock_task): + """Dashboard API: export_xlsx returns 404 for an unknown dashboard.""" + self.login(ADMIN_USERNAME) + rv = self.client.post("api/v1/dashboard/99999999/export_xlsx/") + assert rv.status_code == 404 + mock_task.apply_async.assert_not_called() + + @with_config({"EXCEL_EXPORT_S3_BUCKET": "exports"}) + @patch("superset.dashboards.api.export_dashboard_excel") + def test_export_xlsx_400_for_empty_dashboard(self, mock_task): Review Comment: **Suggestion:** Add type hints for this methodβs mock parameter and return type. [custom_rule] **Severity Level:** Minor π§Ή <details> <summary><b>Why it matters? β </b></summary> This added Python method has no type annotations on its parameter or return type, so the suggestion correctly identifies a real violation of the type-hint rule. </details> <details> <summary><b>Rule source π </b></summary> .cursor/rules/dev-standard.mdc (line 28) </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=aec7e184a8f743719551ad53312bd406&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=aec7e184a8f743719551ad53312bd406&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent π€ </b></summary> ```mdx This is a comment left during a code review. **Path:** tests/integration_tests/dashboards/api_tests.py **Line:** 3291:3291 **Comment:** *Custom Rule: Add type hints for this methodβs mock parameter and return type. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41133&comment_hash=aa7e91cc2601428a21ed6263c1b7485f6aabf88a2ad96f97c08504b0a3aaea6f&reaction=like'>π</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41133&comment_hash=aa7e91cc2601428a21ed6263c1b7485f6aabf88a2ad96f97c08504b0a3aaea6f&reaction=dislike'>π</a> ########## tests/unit_tests/tasks/test_export_dashboard_excel.py: ########## @@ -0,0 +1,401 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +import glob +import os +import tempfile +from collections.abc import Iterator +from contextlib import ExitStack +from typing import Any +from unittest import mock + +import pytest +from celery.exceptions import SoftTimeLimitExceeded + +from superset.utils import json + +MODULE = "superset.tasks.export_dashboard_excel" + + +# A minimal valid 1x1 transparent PNG for image-mode tests. +_PNG_1x1 = ( + b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06" + b"\x00\x00\x00\x1f\x15\xc4\x89\x00\x00\x00\rIDATx\x9cc\x00\x01\x00\x00\x05\x00" + b"\x01\r\n-\xb4\x00\x00\x00\x00IEND\xaeB`\x82" +) Review Comment: **Suggestion:** Add an explicit bytes type annotation for this module-level binary constant to comply with required variable typing. [custom_rule] **Severity Level:** Minor π§Ή <details> <summary><b>Why it matters? β </b></summary> This is a module-level constant storing bytes and can be type-annotated, but the existing code omits the annotation. That is a real violation of the type-hint requirement. </details> <details> <summary><b>Rule source π </b></summary> .cursor/rules/dev-standard.mdc (line 28) </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=4ebd66d1b0fa4eddb4f8b995ad6b2223&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=4ebd66d1b0fa4eddb4f8b995ad6b2223&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent π€ </b></summary> ```mdx This is a comment left during a code review. **Path:** tests/unit_tests/tasks/test_export_dashboard_excel.py **Line:** 36:40 **Comment:** *Custom Rule: Add an explicit bytes type annotation for this module-level binary constant to comply with required variable typing. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41133&comment_hash=ba925e5aed5ed4d589d7766cb85f52ad4e05f9019f5f81565e31da633dab1808&reaction=like'>π</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41133&comment_hash=ba925e5aed5ed4d589d7766cb85f52ad4e05f9019f5f81565e31da633dab1808&reaction=dislike'>π</a> -- 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]
