feat: Support a dynamic minimum interval for alerts and reports (#29241)

This commit is contained in:
Vitor Avila 2024-06-13 19:12:45 -03:00 committed by GitHub
parent 0dc9215c89
commit 3dadefcfb0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 58 additions and 2 deletions

View File

@ -204,6 +204,17 @@ ALERT_MINIMUM_INTERVAL = int(timedelta(minutes=10).total_seconds())
REPORT_MINIMUM_INTERVAL = int(timedelta(minutes=5).total_seconds())
```
Alternatively, you can assign a function to `ALERT_MINIMUM_INTERVAL` and/or `REPORT_MINIMUM_INTERVAL`. This is useful to dynamically retrieve a value as needed:
``` python
def alert_dynamic_minimal_interval(**kwargs) -> int:
"""
Define logic here to retrieve the value dynamically
"""
ALERT_MINIMUM_INTERVAL = alert_dynamic_minimal_interval
```
## Custom Dockerfile
If you're running the dev version of a released Superset image, like `apache/superset:3.1.0-dev`, you should be set with the above.

View File

@ -98,6 +98,8 @@ class BaseReportScheduleCommand(BaseCommand):
else "REPORT_MINIMUM_INTERVAL"
)
minimum_interval = current_app.config.get(config_key, 0)
if callable(minimum_interval):
minimum_interval = minimum_interval()
if not isinstance(minimum_interval, int):
logger.error(

View File

@ -1382,6 +1382,7 @@ ALERT_REPORTS_MIN_CUSTOM_SCREENSHOT_WIDTH = 600
ALERT_REPORTS_MAX_CUSTOM_SCREENSHOT_WIDTH = 2400
# Set a minimum interval threshold between executions (for each Alert/Report)
# Value should be an integer i.e. int(timedelta(minutes=5).total_seconds())
# You can also assign a function to the config that returns the expected integer
ALERT_MINIMUM_INTERVAL = int(timedelta(minutes=0).total_seconds())
REPORT_MINIMUM_INTERVAL = int(timedelta(minutes=0).total_seconds())

View File

@ -52,9 +52,17 @@ TEST_SCHEDULES_SINGLE_MINUTES = {
TEST_SCHEDULES = TEST_SCHEDULES_EVERY_MINUTE.union(TEST_SCHEDULES_SINGLE_MINUTES)
def dynamic_alert_minimum_interval(**kwargs) -> int:
return int(timedelta(minutes=10).total_seconds())
def dynamic_report_minimum_interval(**kwargs) -> int:
return int(timedelta(minutes=5).total_seconds())
def app_custom_config(
alert_minimum_interval: int | str = 0,
report_minimum_interval: int | str = 0,
alert_minimum_interval: int | str | Callable[[], int] = 0,
report_minimum_interval: int | str | Callable[[], int] = 0,
) -> Callable[[Callable[..., Any]], Callable[..., Any]]:
"""
Decorator to mock the current_app.config values dynamically for each test.
@ -253,3 +261,37 @@ def test_validate_report_frequency_invalid_config(
f"invalid value for {report_type}_MINIMUM_INTERVAL: 10 minutes"
)
assert expected_error_message.lower() in caplog.text.lower()
@app_custom_config(
alert_minimum_interval=dynamic_alert_minimum_interval,
report_minimum_interval=dynamic_report_minimum_interval,
)
def test_validate_report_frequency_using_callable() -> None:
"""
Test the ``validate_report_frequency`` method when the config
values are set to a function.
"""
# Should fail with a 9 minutes interval, and work with 10
with pytest.raises(ReportScheduleFrequencyNotAllowed):
BaseReportScheduleCommand().validate_report_frequency(
"1,10 * * * *",
ReportScheduleType.ALERT,
)
BaseReportScheduleCommand().validate_report_frequency(
"1,11 * * * *",
ReportScheduleType.ALERT,
)
# Should fail with a 4 minutes interval, and work with 5
with pytest.raises(ReportScheduleFrequencyNotAllowed):
BaseReportScheduleCommand().validate_report_frequency(
"1,5 * * * *",
ReportScheduleType.REPORT,
)
BaseReportScheduleCommand().validate_report_frequency(
"1,6 * * * *",
ReportScheduleType.REPORT,
)