Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(reports): removing index column #32366

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions superset-frontend/src/features/alerts/AlertReportModal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,31 @@ test('does not show screenshot width when csv is selected', async () => {
expect(screen.queryByRole('spinbutton')).not.toBeInTheDocument();
});

test('properly renders remove index checkbox for CSV reports', async () => {
render(<AlertReportModal {...generateMockedProps(true, true, true)} />, {
useRedux: true,
});
userEvent.click(screen.getByTestId('contents-panel'));
await screen.findByText(/test dashboard/i);
const contentTypeSelector = screen.getByRole('combobox', {
name: /select content type/i,
});
await comboboxSelect(contentTypeSelector, 'Chart', () =>
screen.getByRole('combobox', { name: /chart/i }),
);
const reportFormatSelector = screen.getByRole('combobox', {
name: /select format/i,
});
await comboboxSelect(
reportFormatSelector,
'CSV',
() => screen.getAllByText(/Send as CSV/i)[0],
);
expect(
screen.getByRole('checkbox', { name: /remove index column/i }),
).toBeInTheDocument();
});

// Schedule Section
test('opens Schedule Section on click', async () => {
render(<AlertReportModal {...generateMockedProps(false, true, false)} />, {
Expand Down
20 changes: 20 additions & 0 deletions superset-frontend/src/features/alerts/AlertReportModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
DEFAULT_NOTIFICATION_FORMAT,
);
const [forceScreenshot, setForceScreenshot] = useState<boolean>(false);
const [removeIndex, setRemoveIndex] = useState<boolean>(false);

const [isScreenshot, setIsScreenshot] = useState<boolean>(false);
useEffect(() => {
Expand Down Expand Up @@ -669,6 +670,7 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
...currentAlert,
type: isReport ? 'Report' : 'Alert',
force_screenshot: shouldEnableForceScreenshot || forceScreenshot,
remove_index: removeIndex,
validator_type: conditionNotNull ? 'not null' : 'operator',
validator_config_json: conditionNotNull
? {}
Expand Down Expand Up @@ -1119,6 +1121,10 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
setForceScreenshot(event.target.checked);
};

const onRemoveIndexChange = (event: any) => {
setRemoveIndex(event.target.checked);
};

// Make sure notification settings has the required info
const checkNotificationSettings = () => {
if (!notificationSettings.length) {
Expand Down Expand Up @@ -1810,6 +1816,20 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
</StyledCheckbox>
</div>
)}
{isReport &&
contentType === ContentType.Chart &&
reportFormat === 'CSV' && (
<div className="inline-container">
<StyledCheckbox
data-test="include-index"
className="checkbox"
checked={removeIndex}
onChange={onRemoveIndexChange}
>
{t('Remove index column')}
</StyledCheckbox>
</div>
)}
</StyledPanel>
<StyledPanel
header={
Expand Down
1 change: 1 addition & 0 deletions superset-frontend/src/features/alerts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export type AlertObject = {
error?: string;
extra?: Extra;
force_screenshot: boolean;
remove_index?: boolean;
grace_period?: number;
id: number;
last_eval_dttm?: number;
Expand Down
3 changes: 3 additions & 0 deletions superset/commands/report/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
ReportScheduleUnexpectedError,
ReportScheduleWorkingTimeoutError,
)
from superset.commands.report.utils import remove_post_processed
from superset.common.chart_data import ChartDataResultFormat, ChartDataResultType
from superset.daos.report import (
REPORT_SCHEDULE_ERROR_NOTIFICATION_MARKER,
Expand Down Expand Up @@ -363,6 +364,8 @@

def _get_csv_data(self) -> bytes:
url = self._get_url(result_format=ChartDataResultFormat.CSV)
if self._report_schedule.remove_index:
url = remove_post_processed(url)

Check warning on line 368 in superset/commands/report/execute.py

View check run for this annotation

Codecov / codecov/patch

superset/commands/report/execute.py#L368

Added line #L368 was not covered by tests
_, username = get_executor(
executors=app.config["ALERT_REPORTS_EXECUTORS"],
model=self._report_schedule,
Expand Down
32 changes: 32 additions & 0 deletions superset/commands/report/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 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.


def remove_post_processed(url: str) -> str:
"""Remove the type=post_processed parameter from the URL query string.
Args:
url (str): The URL to process.
Returns:
str: The URL with the type=post_processed parameter removed."""
if "?" not in url:
return url
base_url, query_string = url.split("?", 1)
params = query_string.split("&")
filtered_params = [param for param in params if param != "type=post_processed"]
filtered_query_string = "&".join(filtered_params)
filtered_url = f"{base_url}?{filtered_query_string}"
return filtered_url
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 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.
"""add_remove_index_to_report_schedule

Revision ID: 3b86f563edbc
Revises: 74ad1125881c
Create Date: 2025-02-24 17:52:02.369467

"""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "3b86f563edbc"
down_revision = "74ad1125881c"


def upgrade():
op.add_column(
"report_schedule",
sa.Column("remove_index", sa.Boolean(), default=False),
)


def downgrade():
op.drop_column("report_schedule", "remove_index")
2 changes: 2 additions & 0 deletions superset/reports/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def ensure_alert_reports_enabled(self) -> Optional[Response]:
"recipients.id",
"recipients.recipient_config_json",
"recipients.type",
"remove_index",
"report_format",
"sql",
"timezone",
Expand Down Expand Up @@ -175,6 +176,7 @@ def ensure_alert_reports_enabled(self) -> Optional[Response]:
"owners",
"recipients",
"report_format",
"remove_index",
"sql",
"timezone",
"type",
Expand Down
3 changes: 3 additions & 0 deletions superset/reports/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ class ReportSchedule(AuditMixinNullable, ExtraJSONMixin, Model):
# (Reports) When generating a screenshot, bypass the cache?
force_screenshot = Column(Boolean, default=False)

# (Reports) Remove index column in report
remove_index = Column(Boolean, default=False)

custom_width = Column(Integer, nullable=True)
custom_height = Column(Integer, nullable=True)

Expand Down
16 changes: 15 additions & 1 deletion superset/reports/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,13 @@ class ReportSchedulePostSchema(Schema):
dump_default=None,
)
force_screenshot = fields.Boolean(dump_default=False)
remove_index = fields.Boolean(
metadata={
"description": _("Remove index column in report"),
"example": False,
},
dump_default=False,
)
custom_width = fields.Integer(
metadata={
"description": _("Custom width of the screenshot in pixels"),
Expand Down Expand Up @@ -370,7 +377,14 @@ class ReportSchedulePutSchema(Schema):
)
extra = fields.Dict(dump_default=None)
force_screenshot = fields.Boolean(dump_default=False)

remove_index = fields.Boolean(
metadata={
"description": _("Remove index column in report"),
"example": False,
},
dump_default=False,
required=False,
)
custom_width = fields.Integer(
metadata={
"description": _("Custom width of the screenshot in pixels"),
Expand Down
48 changes: 48 additions & 0 deletions tests/unit_tests/commands/report/test_report_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# 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 superset.commands.report.utils import remove_post_processed


def test_remove_post_processed():
url = "https://superset.com/?param1=value1&type=post_processed&param2=value2"
expected = "https://superset.com/?param1=value1&param2=value2"
assert remove_post_processed(url) == expected


def test_retain_other_parameters():
url = "https://superset.com/?param1=value1&param2=value2"
expected = "https://superset.com/?param1=value1&param2=value2"
assert remove_post_processed(url) == expected


def test_no_post_processed_present():
url = "https://superset.com/?param1=value1&param2=value2"
expected = "https://superset.com/?param1=value1&param2=value2"
assert remove_post_processed(url) == expected


def test_empty_query_string():
url = "https://superset.com/?"
expected = "https://superset.com/?"
assert remove_post_processed(url) == expected


def test_no_query_string():
url = "https://superset.com"
expected = "https://superset.com"
assert remove_post_processed(url) == expected
Loading