2020-11-25 03:50:30 -05:00
|
|
|
# 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.
|
|
|
|
|
2022-07-26 20:01:56 -04:00
|
|
|
import json
|
|
|
|
from contextlib import contextmanager
|
2023-06-01 15:01:10 -04:00
|
|
|
from typing import Any, Optional
|
2022-07-26 20:01:56 -04:00
|
|
|
from uuid import uuid4
|
2020-11-25 03:50:30 -05:00
|
|
|
|
|
|
|
from flask_appbuilder.security.sqla.models import User
|
|
|
|
|
2022-07-26 20:01:56 -04:00
|
|
|
from superset import db, security_manager
|
2020-11-25 03:50:30 -05:00
|
|
|
from superset.models.core import Database
|
|
|
|
from superset.models.dashboard import Dashboard
|
2022-07-26 20:01:56 -04:00
|
|
|
from superset.models.slice import Slice
|
|
|
|
from superset.reports.models import (
|
2021-04-15 17:07:49 -04:00
|
|
|
ReportDataFormat,
|
2020-12-21 14:07:30 -05:00
|
|
|
ReportExecutionLog,
|
|
|
|
ReportRecipients,
|
2022-07-26 20:01:56 -04:00
|
|
|
ReportRecipientType,
|
2020-12-21 14:07:30 -05:00
|
|
|
ReportSchedule,
|
2022-07-26 20:01:56 -04:00
|
|
|
ReportScheduleType,
|
2020-12-21 14:07:30 -05:00
|
|
|
ReportState,
|
|
|
|
)
|
2022-10-31 08:32:49 -04:00
|
|
|
from superset.utils.core import override_user
|
2022-07-26 20:01:56 -04:00
|
|
|
from tests.integration_tests.test_app import app
|
|
|
|
from tests.integration_tests.utils import read_fixture
|
|
|
|
|
|
|
|
TEST_ID = str(uuid4())
|
|
|
|
CSV_FILE = read_fixture("trends.csv")
|
|
|
|
SCREENSHOT_FILE = read_fixture("sample.png")
|
2022-10-31 08:32:49 -04:00
|
|
|
DEFAULT_OWNER_EMAIL = "admin@fab.org"
|
2020-11-25 03:50:30 -05:00
|
|
|
|
|
|
|
|
|
|
|
def insert_report_schedule(
|
|
|
|
type: str,
|
|
|
|
name: str,
|
|
|
|
crontab: str,
|
2023-06-01 15:01:10 -04:00
|
|
|
owners: list[User],
|
2021-07-27 21:28:24 -04:00
|
|
|
timezone: Optional[str] = None,
|
2020-11-25 03:50:30 -05:00
|
|
|
sql: Optional[str] = None,
|
|
|
|
description: Optional[str] = None,
|
|
|
|
chart: Optional[Slice] = None,
|
|
|
|
dashboard: Optional[Dashboard] = None,
|
|
|
|
database: Optional[Database] = None,
|
|
|
|
validator_type: Optional[str] = None,
|
|
|
|
validator_config_json: Optional[str] = None,
|
|
|
|
log_retention: Optional[int] = None,
|
2020-12-21 14:07:30 -05:00
|
|
|
last_state: Optional[ReportState] = None,
|
2020-11-25 03:50:30 -05:00
|
|
|
grace_period: Optional[int] = None,
|
2023-06-01 15:01:10 -04:00
|
|
|
recipients: Optional[list[ReportRecipients]] = None,
|
2021-04-15 17:07:49 -04:00
|
|
|
report_format: Optional[ReportDataFormat] = None,
|
2023-06-01 15:01:10 -04:00
|
|
|
logs: Optional[list[ReportExecutionLog]] = None,
|
|
|
|
extra: Optional[dict[Any, Any]] = None,
|
2021-12-22 15:16:04 -05:00
|
|
|
force_screenshot: bool = False,
|
2020-11-25 03:50:30 -05:00
|
|
|
) -> ReportSchedule:
|
|
|
|
owners = owners or []
|
|
|
|
recipients = recipients or []
|
|
|
|
logs = logs or []
|
2020-12-21 14:07:30 -05:00
|
|
|
last_state = last_state or ReportState.NOOP
|
2022-10-31 08:32:49 -04:00
|
|
|
|
|
|
|
with override_user(owners[0]):
|
|
|
|
report_schedule = ReportSchedule(
|
|
|
|
type=type,
|
|
|
|
name=name,
|
|
|
|
crontab=crontab,
|
|
|
|
timezone=timezone,
|
|
|
|
sql=sql,
|
|
|
|
description=description,
|
|
|
|
chart=chart,
|
|
|
|
dashboard=dashboard,
|
|
|
|
database=database,
|
|
|
|
owners=owners,
|
|
|
|
validator_type=validator_type,
|
|
|
|
validator_config_json=validator_config_json,
|
|
|
|
log_retention=log_retention,
|
|
|
|
grace_period=grace_period,
|
|
|
|
recipients=recipients,
|
|
|
|
logs=logs,
|
|
|
|
last_state=last_state,
|
|
|
|
report_format=report_format,
|
|
|
|
extra=extra,
|
|
|
|
force_screenshot=force_screenshot,
|
|
|
|
)
|
2020-11-25 03:50:30 -05:00
|
|
|
db.session.add(report_schedule)
|
|
|
|
db.session.commit()
|
|
|
|
return report_schedule
|
2022-07-26 20:01:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
def create_report_notification(
|
|
|
|
email_target: Optional[str] = None,
|
|
|
|
slack_channel: Optional[str] = None,
|
|
|
|
chart: Optional[Slice] = None,
|
|
|
|
dashboard: Optional[Dashboard] = None,
|
|
|
|
database: Optional[Database] = None,
|
|
|
|
sql: Optional[str] = None,
|
|
|
|
report_type: ReportScheduleType = ReportScheduleType.REPORT,
|
|
|
|
validator_type: Optional[str] = None,
|
|
|
|
validator_config_json: Optional[str] = None,
|
|
|
|
grace_period: Optional[int] = None,
|
|
|
|
report_format: Optional[ReportDataFormat] = None,
|
|
|
|
name: Optional[str] = None,
|
2023-06-01 15:01:10 -04:00
|
|
|
extra: Optional[dict[str, Any]] = None,
|
2022-07-26 20:01:56 -04:00
|
|
|
force_screenshot: bool = False,
|
2023-06-01 15:01:10 -04:00
|
|
|
owners: Optional[list[User]] = None,
|
2022-07-26 20:01:56 -04:00
|
|
|
) -> ReportSchedule:
|
2022-10-31 08:32:49 -04:00
|
|
|
if not owners:
|
|
|
|
owners = [
|
|
|
|
(
|
|
|
|
db.session.query(security_manager.user_model)
|
|
|
|
.filter_by(email=DEFAULT_OWNER_EMAIL)
|
|
|
|
.one_or_none()
|
|
|
|
)
|
|
|
|
]
|
2022-07-26 20:01:56 -04:00
|
|
|
|
|
|
|
if slack_channel:
|
|
|
|
recipient = ReportRecipients(
|
|
|
|
type=ReportRecipientType.SLACK,
|
|
|
|
recipient_config_json=json.dumps(
|
|
|
|
{
|
|
|
|
"target": slack_channel,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
recipient = ReportRecipients(
|
|
|
|
type=ReportRecipientType.EMAIL,
|
|
|
|
recipient_config_json=json.dumps({"target": email_target}),
|
|
|
|
)
|
|
|
|
|
|
|
|
if name is None:
|
|
|
|
name = "report_with_csv" if report_format else "report"
|
|
|
|
|
|
|
|
report_schedule = insert_report_schedule(
|
|
|
|
report_type,
|
|
|
|
name=name,
|
|
|
|
crontab="0 9 * * *",
|
|
|
|
description="Daily report",
|
|
|
|
sql=sql,
|
|
|
|
chart=chart,
|
|
|
|
dashboard=dashboard,
|
|
|
|
database=database,
|
|
|
|
recipients=[recipient],
|
2022-10-31 08:32:49 -04:00
|
|
|
owners=owners,
|
2022-07-26 20:01:56 -04:00
|
|
|
validator_type=validator_type,
|
|
|
|
validator_config_json=validator_config_json,
|
|
|
|
grace_period=grace_period,
|
2024-03-22 12:54:30 -04:00
|
|
|
report_format=report_format or ReportDataFormat.PNG,
|
2022-07-26 20:01:56 -04:00
|
|
|
extra=extra,
|
|
|
|
force_screenshot=force_screenshot,
|
|
|
|
)
|
|
|
|
return report_schedule
|
|
|
|
|
|
|
|
|
|
|
|
def cleanup_report_schedule(report_schedule: ReportSchedule) -> None:
|
|
|
|
db.session.query(ReportExecutionLog).filter(
|
|
|
|
ReportExecutionLog.report_schedule == report_schedule
|
|
|
|
).delete()
|
|
|
|
db.session.query(ReportRecipients).filter(
|
|
|
|
ReportRecipients.report_schedule == report_schedule
|
|
|
|
).delete()
|
|
|
|
|
|
|
|
db.session.delete(report_schedule)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def create_dashboard_report(dashboard, extra, **kwargs):
|
|
|
|
report_schedule = create_report_notification(
|
|
|
|
email_target="target@example.com",
|
|
|
|
dashboard=dashboard,
|
|
|
|
extra={
|
|
|
|
"dashboard": extra,
|
|
|
|
},
|
2024-04-15 17:44:47 -04:00
|
|
|
**kwargs,
|
2022-07-26 20:01:56 -04:00
|
|
|
)
|
|
|
|
error = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
yield report_schedule
|
|
|
|
except Exception as ex: # pylint: disable=broad-except
|
|
|
|
error = ex
|
|
|
|
|
|
|
|
# make sure to clean up in case of yield exceptions
|
|
|
|
cleanup_report_schedule(report_schedule)
|
|
|
|
|
|
|
|
if error:
|
|
|
|
raise error
|