From 9bcf5b342e44ff5b749f9ff4318d61546df4428e Mon Sep 17 00:00:00 2001 From: blag Date: Wed, 5 Oct 2022 23:04:34 -0700 Subject: [PATCH] Move user-facing string to template (#26815) Co-authored-by: Jed Cunningham <66968678+jedcunningham@users.noreply.github.com> (cherry picked from commit bab6dbec3883084e5872123b515c2a8491c32380) --- airflow/models/dag.py | 9 ++++++--- airflow/www/templates/airflow/dags.html | 4 +++- tests/models/test_dag.py | 15 ++++++++++++--- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/airflow/models/dag.py b/airflow/models/dag.py index 1a6b9906b5225..6b5bdde6de6e0 100644 --- a/airflow/models/dag.py +++ b/airflow/models/dag.py @@ -198,13 +198,16 @@ def get_last_dagrun(dag_id, session, include_externally_triggered=False): return query.first() -def get_dataset_triggered_next_run_info(dag_ids: list[str], *, session: Session) -> dict[str, str]: +def get_dataset_triggered_next_run_info(dag_ids: list[str], *, session: Session) -> dict[str, dict[str, int]]: """ Given a list of dag_ids, get string representing how close any that are dataset triggered are their next run, e.g. "1 of 2 datasets updated" """ return { - x.dag_id: f"{x.ready} of {x.total} datasets updated" + x.dag_id: { + "ready": x.ready, + "total": x.total, + } for x in session.query( DagScheduleDatasetReference.dag_id, func.count().label("total"), @@ -3338,7 +3341,7 @@ def calculate_dagrun_date_fields( ) @provide_session - def get_dataset_triggered_next_run_info(self, *, session=NEW_SESSION) -> str | None: + def get_dataset_triggered_next_run_info(self, *, session=NEW_SESSION) -> dict[str, int] | None: if self.schedule_interval != "Dataset": return None return get_dataset_triggered_next_run_info([self.dag_id], session=session)[self.dag_id] diff --git a/airflow/www/templates/airflow/dags.html b/airflow/www/templates/airflow/dags.html index 0884a06c179fb..c675b4a05aa0a 100644 --- a/airflow/www/templates/airflow/dags.html +++ b/airflow/www/templates/airflow/dags.html @@ -308,7 +308,9 @@

{{ page_title }}

data-dag-id="{{ dag.dag_id }}" data-summary="{{ dataset_triggered_next_run_info[dag.dag_id] }}" > - {{ dataset_triggered_next_run_info[dag.dag_id] }} + {% with ds_info = dataset_triggered_next_run_info[dag.dag_id] %} + {{ ds_info.ready }} of {{ ds_info.total }} datasets updated + {% endwith %} {% endif %} diff --git a/tests/models/test_dag.py b/tests/models/test_dag.py index 54634e463f96b..775f557094a56 100644 --- a/tests/models/test_dag.py +++ b/tests/models/test_dag.py @@ -2953,12 +2953,21 @@ def test_get_dataset_triggered_next_run_info(dag_maker): session.flush() info = get_dataset_triggered_next_run_info([dag1.dag_id], session=session) - assert "0 of 1 datasets updated" == info[dag1.dag_id] + assert info[dag1.dag_id] == { + "ready": 0, + "total": 1, + } # This time, check both dag2 and dag3 at the same time (tests filtering) info = get_dataset_triggered_next_run_info([dag2.dag_id, dag3.dag_id], session=session) - assert "1 of 2 datasets updated" == info[dag2.dag_id] - assert "1 of 3 datasets updated" == info[dag3.dag_id] + assert info[dag2.dag_id] == { + "ready": 1, + "total": 2, + } + assert info[dag3.dag_id] == { + "ready": 1, + "total": 3, + } def test_dag_uses_timetable_for_run_id(session):