You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by mi...@apache.org on 2026/08/20 20:46:15 UTC
(superset) branch master updated: fix(reports): humanize day-of-month + day-of-week crontabs as OR (#43307)
This is an automated email from the ASF dual-hosted git repository.
aminghadersohi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/superset.git
The following commit(s) were added to refs/heads/master by this push:
new 42ba2a4433f fix(reports): humanize day-of-month + day-of-week crontabs as OR (#43307)
42ba2a4433f is described below
commit 42ba2a4433f2d5dbc2216fa69b4f8b0195a5c3eb
Author: Amin Ghadersohi <am...@gmail.com>
AuthorDate: Thu Aug 20 14:46:08 2026 -0600
fix(reports): humanize day-of-month + day-of-week crontabs as OR (#43307)
---
superset/reports/models.py | 4 +--
superset/tasks/cron_util.py | 45 +++++++++++++++++++++++++
tests/unit_tests/tasks/test_cron_util.py | 56 +++++++++++++++++++++++++++++++-
3 files changed, 102 insertions(+), 3 deletions(-)
diff --git a/superset/reports/models.py b/superset/reports/models.py
index 1510ea63782..886f89bc52a 100644
--- a/superset/reports/models.py
+++ b/superset/reports/models.py
@@ -20,7 +20,6 @@ import logging
from typing import Any, Optional
import rison
-from cron_descriptor import get_description
from flask_appbuilder import Model
from flask_appbuilder.models.decorators import renders
from sqlalchemy import (
@@ -44,6 +43,7 @@ from superset.models.helpers import AuditMixinNullable, ExtraJSONMixin
from superset.models.slice import Slice
from superset.reports.types import ReportScheduleExtra
from superset.subjects.models import report_schedule_editors, Subject
+from superset.tasks.cron_util import get_cron_description
from superset.utils.backports import StrEnum
from superset.utils.core import MediumText
@@ -197,7 +197,7 @@ class ReportSchedule(AuditMixinNullable, ExtraJSONMixin, Model):
@renders("crontab")
def crontab_humanized(self) -> str:
- return get_description(self.crontab)
+ return get_cron_description(self.crontab)
def get_native_filters_params(self) -> tuple[str, list[str]]:
"""
diff --git a/superset/tasks/cron_util.py b/superset/tasks/cron_util.py
index 904371d5883..2cfaf7f5c89 100644
--- a/superset/tasks/cron_util.py
+++ b/superset/tasks/cron_util.py
@@ -19,12 +19,57 @@ import logging
from collections.abc import Iterator
from datetime import datetime, timedelta
+from cron_descriptor import ExpressionDescriptor, get_description
from croniter import croniter, CroniterBadDateError
from flask import current_app
from pytz import timezone as pytz_timezone, UnknownTimeZoneError
logger = logging.getLogger(__name__)
+# Field values that place no restriction on a cron field. ``?`` is the Quartz
+# spelling of "no specific value" and is accepted by ``cron_descriptor``.
+UNRESTRICTED_CRON_FIELDS = {"*", "?"}
+
+
+def get_cron_description(cron: str) -> str:
+ """
+ Build a human readable description of a cron expression.
+
+ ``cron_descriptor`` renders a restricted day-of-month next to a restricted
+ day-of-week as a single comma separated clause -- ``0 9 7-11 * 2`` becomes
+ "At 09:00 AM, on day 7 through 11 of the month, only on Tuesday" -- which
+ reads as an intersection. POSIX cron, and therefore ``croniter`` (which
+ picks the fire times in :func:`cron_schedule_window`), takes the *union* of
+ the two fields when both are restricted: the schedule fires on every
+ matching day of the month as well as on every matching day of the week.
+ Join the two clauses with "or" so the description matches when the job
+ really runs.
+ """
+ description = get_description(cron)
+
+ fields = cron.split()
+ if len(fields) != 5:
+ return description
+
+ day_of_month, day_of_week = fields[2], fields[4]
+ if (
+ day_of_month in UNRESTRICTED_CRON_FIELDS
+ or day_of_week in UNRESTRICTED_CRON_FIELDS
+ ):
+ # Only one of the two fields narrows the days, so the description is
+ # already unambiguous.
+ return description
+
+ day_of_week_clause = ExpressionDescriptor(cron).get_day_of_week_description()
+ if not day_of_week_clause.startswith(", ") or day_of_week_clause not in description:
+ return description
+
+ clause = day_of_week_clause[len(", ") :]
+ # "only on Tuesday" claims the day-of-week narrows the day-of-month clause;
+ # it is an alternative to it, not an extra filter.
+ clause = clause.removeprefix("only ")
+ return description.replace(day_of_week_clause, f", or {clause}", 1)
+
def cron_schedule_window(
triggered_at: datetime, cron: str, timezone: str
diff --git a/tests/unit_tests/tasks/test_cron_util.py b/tests/unit_tests/tasks/test_cron_util.py
index 03a608a978e..cf4b1d6c90a 100644
--- a/tests/unit_tests/tasks/test_cron_util.py
+++ b/tests/unit_tests/tasks/test_cron_util.py
@@ -17,9 +17,10 @@
from datetime import datetime
import pytest
+from croniter import croniter
from freezegun.api import FakeDatetime
-from superset.tasks.cron_util import cron_schedule_window
+from superset.tasks.cron_util import cron_schedule_window, get_cron_description
@pytest.mark.parametrize(
@@ -256,3 +257,56 @@ def test_cron_schedule_window_invalid_cron_date(
assert (
list(cron.strftime("%A, %d %B %Y, %H:%M:%S") for cron in datetimes) == expected # noqa: C400
)
+
+
+@pytest.mark.parametrize(
+ "cron, expected",
+ [
+ # only the day-of-month is restricted
+ (
+ "0 9 7-11,19-23 * *",
+ "At 09:00 AM, on day 7 through 11 and 19 through 23 of the month",
+ ),
+ # only the day-of-week is restricted
+ ("0 9 * * 2", "At 09:00 AM, only on Tuesday"),
+ ("0 9 ? * 2", "At 09:00 AM, only on Tuesday"),
+ # neither is restricted
+ ("0 9 * * *", "At 09:00 AM"),
+ # both are restricted: cron unions them, so the description must too
+ (
+ "0 9 7-11,19-23 * 2",
+ "At 09:00 AM, on day 7 through 11 and 19 through 23 of the month, "
+ "or on Tuesday",
+ ),
+ (
+ "0 9 1,15 * 1-5",
+ "At 09:00 AM, on day 1 and 15 of the month, or Monday through Friday",
+ ),
+ (
+ "0 9 15 3 2#1",
+ "At 09:00 AM, on day 15 of the month, or on the first Tuesday of the "
+ "month, only in March",
+ ),
+ ("0 9 L * 5", "At 09:00 AM, on the last day of the month, or on Friday"),
+ ],
+)
+def test_get_cron_description(cron: str, expected: str) -> None:
+ """
+ Test that the humanized cron matches the days the schedule actually fires on.
+ """
+
+ assert get_cron_description(cron) == expected
+
+
+def test_get_cron_description_matches_fire_times() -> None:
+ """
+ A restricted day-of-month and day-of-week are OR'ed, never AND'ed.
+ """
+
+ cron = "0 9 7-11,19-23 * 2"
+ # 2026-09-01 is a Tuesday that falls outside both day-of-month ranges, so
+ # an AND reading of the description would have skipped it
+ first_fire_time = croniter(cron, datetime(2026, 9, 1)).get_next(datetime)
+
+ assert first_fire_time == datetime(2026, 9, 1, 9, 0)
+ assert "or on Tuesday" in get_cron_description(cron)