From 69c3193e525e56871a5bc0402423a2ce8ee359ed Mon Sep 17 00:00:00 2001 From: Andrey Anshin Date: Mon, 21 Aug 2023 14:51:04 +0400 Subject: [PATCH] Use nested function for resolve grouping/sorting key --- airflow/www/views.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/airflow/www/views.py b/airflow/www/views.py index a63baafd626e6..ed20bc16572f3 100644 --- a/airflow/www/views.py +++ b/airflow/www/views.py @@ -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: @@ -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: