Copilot commented on code in PR #76598:
URL: https://github.com/apache/airflow/pull/76598#discussion_r4717301237
##########
airflow-core/src/airflow/api_fastapi/core_api/services/ui/calendar.py:
##########
@@ -74,19 +73,17 @@ def get_calendar_data(
List of calendar time range results
"""
date_filter = partition_date if partition_date.is_active() else
logical_date
- historical_data, raw_dag_states = self._get_historical_dag_runs(
- dag_id,
- session,
- date_filter,
- granularity,
- )
+ historical_dag_runs = self._get_historical_dag_runs(dag_id, session,
date_filter, granularity)
- planned_data = self._get_planned_dag_runs(dag, raw_dag_states,
date_filter, granularity)
+ # Planned runs extend from the latest run of the Dag, regardless of
the displayed date range,
+ # so that ranges without any historical runs (e.g. a future year)
still show planned runs.
+ latest_run = self._get_latest_dag_run(dag_id, session)
Review Comment:
This adds a database query to partitioned calendars too, but both
`TestPartitionedCalendar.test_daily_calendar_partitioned` and
`test_hourly_calendar_partitioned` still use `assert_queries_count(4)`
(test_calendar.py:601 and :658). Their past-date fixtures execute the
historical lookup plus this new lookup, raising the count to five and failing
all five parametrized cases. Please update those assertions as well, or avoid
the additional query for that path.
---
Drafted-by: GitHub Copilot (no human review before posting)
##########
airflow-core/src/airflow/api_fastapi/core_api/services/ui/calendar.py:
##########
@@ -95,8 +92,26 @@ def _get_historical_dag_runs(
session: Session,
date_filter: RangeFilter,
granularity: Literal["hourly", "daily"],
- ) -> tuple[list[CalendarTimeRangeResponse], Sequence[Row]]:
- """Get historical Dag runs from the database."""
+ ) -> list[CalendarTimeRangeResponse]:
+ """
+ Fetch historical Dag runs.
+
+ :param dag_id: The Dag ID
+ :param session: Database session
+ :param date_filter: Filter for specific dates
+ :param granularity: hourly or daily
+ :return:
+ """
+ if date_filter.value:
+ lower_bounds = [
+ bound
+ for bound in (date_filter.value.lower_bound_gte,
date_filter.value.lower_bound_gt)
+ if bound is not None
+ ]
+ if lower_bounds and max(lower_bounds) > timezone.utcnow():
+ # The lower boundary is in the future, therefore skip
requesting historical dag runs.
+ return []
Review Comment:
Future-dated runs can already exist, including manual runs and partitioned
runs with a positive `run_offset`, so this early return hides their actual
states. Keep the historical lookup for future ranges. Replace the new
query-skipping test with a regression case that verifies an existing
future-dated run is returned.
---
Drafted-by: GitHub Copilot (no human review before posting)
##########
airflow-core/src/airflow/api_fastapi/core_api/services/ui/calendar.py:
##########
@@ -137,42 +152,64 @@ def _get_historical_dag_runs(
for ds in dag_states
]
- return calendar_results, dag_states
+ return calendar_results
+
+ def _get_latest_dag_run(self, dag_id: str, session: Session) -> Row | None:
+ """
+ Get the latest Dag run of the Dag (by partition date or logical date),
ignoring any date filter.
+
+ :param dag_id: The Dag ID
+ :param session: Database session
+ :return: The latest run, or None if the Dag has no runs
+ """
+ return session.execute(
+ sa.select(
+ DagRun.data_interval_start,
+ DagRun.data_interval_end,
+ DagRun.run_after,
+ DagRun.partition_date,
+ )
+ .where(DagRun.dag_id == dag_id)
Review Comment:
On PostgreSQL, descending ordering puts NULL values first, so a manual run
with neither `logical_date` nor `partition_date` is selected ahead of every
dated run. Since that run also has no data interval, planned runs disappear
from otherwise valid calendar ranges. Exclude runs without either date before
applying the ordering and limit.
---
Drafted-by: GitHub Copilot (no human review before posting)
##########
airflow-core/src/airflow/api_fastapi/core_api/services/ui/calendar.py:
##########
@@ -186,23 +223,34 @@ def _get_planned_dag_runs(
False,
)
- return self._calculate_timetable_planned_runs(
- dag, last_info, year, restriction, date_filter, granularity
- )
+ return self._calculate_timetable_planned_runs(dag, last_info,
restriction, date_filter, granularity)
Review Comment:
Only the non-partitioned cron path fast-forwards; delta and partitioned
timetables still spend their 2,000-iteration budget before reaching distant
requested ranges. For example, an hourly `CronPartitionTimetable` with its
latest run in the past returns no planned entries for a range 100 days ahead of
the current time. Seek to the lower bound while preserving schedule alignment
and partition offsets, keep a bounded iteration safeguard, and add future-range
regression tests for both paths.
---
Drafted-by: GitHub Copilot (no human review before posting)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]