Skip to content

Commit

Permalink
disabling an alert that is working will turn it to not triggered
Browse files Browse the repository at this point in the history
  • Loading branch information
dpgaspar committed Dec 21, 2020
1 parent 2793f69 commit 556a338
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
12 changes: 11 additions & 1 deletion superset/reports/commands/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from superset.dao.exceptions import DAOUpdateFailedError
from superset.databases.dao import DatabaseDAO
from superset.exceptions import SupersetSecurityException
from superset.models.reports import ReportSchedule, ReportScheduleType
from superset.models.reports import ReportSchedule, ReportScheduleType, ReportState
from superset.reports.commands.base import BaseReportScheduleCommand
from superset.reports.commands.exceptions import (
DatabaseNotFoundValidationError,
Expand Down Expand Up @@ -70,6 +70,16 @@ def validate(self) -> None:
if not self._model:
raise ReportScheduleNotFoundError()

# Change the state to not triggered when the user deactivates
# A report that is currently in a working state. This prevents
# an alert/report from being kept in a working state if activated back
if (
self._model.last_state == ReportState.WORKING
and "active" in self._properties
and not self._properties["active"]
):
self._properties["last_state"] = ReportState.NOOP

# Validate name uniqueness
if not ReportScheduleDAO.validate_update_uniqueness(
name, report_schedule_id=self._model_id
Expand Down
51 changes: 49 additions & 2 deletions tests/reports/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
# isort:skip_file
"""Unit tests for Superset"""
from datetime import datetime
from typing import List, Optional
import json

from flask_appbuilder.security.sqla.models import User
import pytest
import prison
from sqlalchemy.sql import func
Expand Down Expand Up @@ -48,6 +46,32 @@


class TestReportSchedulesApi(SupersetTestCase):
@pytest.fixture()
def create_working_report_schedule(self):
with self.create_app().app_context():

admin_user = self.get_user("admin")
alpha_user = self.get_user("alpha")
chart = db.session.query(Slice).first()
example_db = get_example_database()

report_schedule = insert_report_schedule(
type=ReportScheduleType.ALERT,
name=f"name_working",
crontab=f"* * * * *",
sql=f"SELECT value from table",
description=f"Report working",
chart=chart,
database=example_db,
owners=[admin_user, alpha_user],
last_state=ReportState.WORKING,
)

yield

db.session.delete(report_schedule)
db.session.commit()

@pytest.fixture()
def create_report_schedules(self):
with self.create_app().app_context():
Expand Down Expand Up @@ -581,6 +605,29 @@ def test_update_report_schedule(self):
assert updated_model.chart_id == report_schedule_data["chart"]
assert updated_model.database_id == report_schedule_data["database"]

@pytest.mark.usefixtures("create_working_report_schedule")
def test_update_report_schedule_state_working(self):
"""
ReportSchedule Api: Test update state in a working report
"""
report_schedule = (
db.session.query(ReportSchedule)
.filter(ReportSchedule.name == "name_working")
.one_or_none()
)

self.login(username="admin")
report_schedule_data = {"active": False}
uri = f"api/v1/report/{report_schedule.id}"
rv = self.client.put(uri, json=report_schedule_data)
assert rv.status_code == 200
report_schedule = (
db.session.query(ReportSchedule)
.filter(ReportSchedule.name == "name_working")
.one_or_none()
)
assert report_schedule.last_state == ReportState.NOOP

@pytest.mark.usefixtures("create_report_schedules")
def test_update_report_schedule_uniqueness(self):
"""
Expand Down
10 changes: 9 additions & 1 deletion tests/reports/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
from superset import db
from superset.models.core import Database
from superset.models.dashboard import Dashboard
from superset.models.reports import ReportExecutionLog, ReportRecipients, ReportSchedule
from superset.models.reports import (
ReportExecutionLog,
ReportRecipients,
ReportSchedule,
ReportState,
)
from superset.models.slice import Slice


Expand All @@ -39,13 +44,15 @@ def insert_report_schedule(
validator_type: Optional[str] = None,
validator_config_json: Optional[str] = None,
log_retention: Optional[int] = None,
last_state: Optional[ReportState] = None,
grace_period: Optional[int] = None,
recipients: Optional[List[ReportRecipients]] = None,
logs: Optional[List[ReportExecutionLog]] = None,
) -> ReportSchedule:
owners = owners or []
recipients = recipients or []
logs = logs or []
last_state = last_state or ReportState.NOOP
report_schedule = ReportSchedule(
type=type,
name=name,
Expand All @@ -62,6 +69,7 @@ def insert_report_schedule(
grace_period=grace_period,
recipients=recipients,
logs=logs,
last_state=last_state,
)
db.session.add(report_schedule)
db.session.commit()
Expand Down

0 comments on commit 556a338

Please sign in to comment.