Skip to content

Commit

Permalink
Optimize calendar view for cron scheduled DAGs (#24262)
Browse files Browse the repository at this point in the history
(cherry picked from commit 23fb663)
  • Loading branch information
jedcunningham authored and ephraimbuddy committed Jun 29, 2022
1 parent 72a36ef commit 24da785
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions airflow/www/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import warnings
from bisect import insort_left
from collections import defaultdict
from datetime import timedelta
from datetime import datetime, timedelta
from functools import wraps
from json import JSONDecodeError
from operator import itemgetter
Expand All @@ -38,6 +38,7 @@
import markupsafe
import nvd3
import sqlalchemy as sqla
from croniter import croniter
from flask import (
Response,
abort,
Expand Down Expand Up @@ -115,6 +116,7 @@
from airflow.ti_deps.dep_context import DepContext
from airflow.ti_deps.dependencies_deps import RUNNING_DEPS, SCHEDULER_QUEUED_DEPS
from airflow.timetables.base import DataInterval, TimeRestriction
from airflow.timetables.interval import CronDataIntervalTimetable
from airflow.utils import json as utils_json, timezone, yaml
from airflow.utils.airflow_flask_app import get_airflow_app
from airflow.utils.dates import infer_time_unit, scale_time_units
Expand Down Expand Up @@ -2741,18 +2743,28 @@ def _convert_to_date(session, column):
restriction = TimeRestriction(dag.start_date, dag.end_date, False)
dates = collections.Counter()

while True:
info = dag.timetable.next_dagrun_info(
last_automated_data_interval=last_automated_data_interval, restriction=restriction
)
if info is None:
break

if info.logical_date.year != year:
break

last_automated_data_interval = info.data_interval
dates[info.logical_date] += 1
if isinstance(dag.timetable, CronDataIntervalTimetable):
for next in croniter(
dag.timetable.summary, start_time=last_automated_data_interval.end, ret_type=datetime
):
if next is None:
break
if next.year != year:
break
if dag.end_date and next > dag.end_date:
break
dates[next.date()] += 1
else:
while True:
info = dag.timetable.next_dagrun_info(
last_automated_data_interval=last_automated_data_interval, restriction=restriction
)
if info is None:
break
if info.logical_date.year != year:
break
last_automated_data_interval = info.data_interval
dates[info.logical_date] += 1

data_dag_states.extend(
{'date': date.isoformat(), 'state': 'planned', 'count': count}
Expand Down

0 comments on commit 24da785

Please sign in to comment.