Skip to content

Commit

Permalink
Use nested function for resolve grouping/sorting key
Browse files Browse the repository at this point in the history
  • Loading branch information
Taragolis committed Aug 21, 2023
1 parent c42a112 commit 69c3193
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions airflow/www/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,6 @@ def sanitize_args(args: dict[str, str]) -> dict[str, str]:
)


def _sorted_groupby(it, key: Callable | None = None, reverse: bool = False):
"""Helper around `itertools.groupby`, which pre-sort iterable by the same key."""
yield from itertools.groupby(sorted(it, key=key, reverse=reverse), key=key)


def get_safe_url(url):
"""Given a user-supplied URL, ensure it points to our web server."""
if not url:
Expand Down Expand Up @@ -3241,9 +3236,12 @@ def duration(self, dag_id: str, session: Session = NEW_SESSION):
if failed_task_instance.duration:
fails_totals[dict_key] += failed_task_instance.duration

# we must group any mapped TIs by dag_id, task_id, run_id
# We must group any mapped TIs by dag_id, task_id, run_id
def grouping_key(ti: TaskInstance):
return ti.dag_id, ti.task_id, ti.run_id

mapped_tis = set()
for _, group in _sorted_groupby(task_instances, key=lambda ti: (ti.dag_id, ti.task_id, ti.run_id)):
for _, group in itertools.groupby(sorted(task_instances, key=grouping_key), key=grouping_key):
tis = list(group)
duration = sum(x.duration for x in tis if x.duration)
if duration:
Expand Down

0 comments on commit 69c3193

Please sign in to comment.