pierrejeambrun commented on code in PR #45420:
URL: https://github.com/apache/airflow/pull/45420#discussion_r1922627955


##########
airflow/api_fastapi/common/db/dags.py:
##########
@@ -17,31 +17,64 @@
 
 from __future__ import annotations
 
+from typing import TYPE_CHECKING
+
 from sqlalchemy import func, select
 
+if TYPE_CHECKING:
+    from sqlalchemy.sql import Select
+
 from airflow.models.dag import DagModel
 from airflow.models.dagrun import DagRun
 
-latest_dag_run_per_dag_id_cte = (
-    select(DagRun.dag_id, func.max(DagRun.start_date).label("start_date"))
-    .where()
-    .group_by(DagRun.dag_id)
-    .cte()
-)
 
+def generate_dag_with_latest_run_query(dag_runs_cte: Select | None = None) -> 
Select:
+    use_outer_join = False
 
-dags_select_with_latest_dag_run = (
-    select(DagModel)
-    .join(
-        latest_dag_run_per_dag_id_cte,
-        DagModel.dag_id == latest_dag_run_per_dag_id_cte.c.dag_id,
-        isouter=True,
+    if dag_runs_cte is None:
+        dag_runs_cte = select(DagRun).where().cte()

Review Comment:
   Ideally if `dag_runs_cte` is `None` I think we can even skip completely the 
different joins ? That  actually need to happen only when we have sub filtering 
on dag runs ?



##########
airflow/api_fastapi/core_api/routes/public/dags.py:
##########
@@ -69,6 +75,25 @@ def get_dags(
     only_active: QueryOnlyActiveFilter,
     paused: QueryPausedFilter,
     last_dag_run_state: QueryLastDagRunStateFilter,
+    dag_run_start_date_range: Annotated[
+        RangeFilter, Depends(datetime_range_filter_factory("start_date", 
DagRun))
+    ],
+    dag_run_end_date_range: Annotated[
+        RangeFilter, Depends(datetime_range_filter_factory("end_date", DagRun))
+    ],

Review Comment:
   ```suggestion
           RangeFilter, Depends(datetime_range_filter_factory("start_date", 
DagRun))
       ],
       dag_run_end_date_range: Annotated[
           RangeFilter, 
Depends(datetime_range_filter_factory("dag_run_end_date", DagRun))
       ],
   ```
   ```suggestion
           RangeFilter, Depends(datetime_range_filter_factory("start_date", 
DagRun))
       ],
       dag_run_end_date_range: Annotated[
           RangeFilter, Depends(datetime_range_filter_factory("end_date", 
DagRun))
       ],
   ```
   Same for start date.



##########
tests/api_fastapi/core_api/routes/public/test_dags.py:
##########
@@ -154,6 +157,47 @@ class TestGetDags(TestDagEndpoint):
             ({"owners": ["test_owner"], "only_active": False}, 1, [DAG3_ID]),
             ({"last_dag_run_state": "success", "only_active": False}, 1, 
[DAG3_ID]),
             ({"last_dag_run_state": "failed", "only_active": False}, 1, 
[DAG1_ID]),
+            ({"dag_run_state": "failed"}, 1, [DAG1_ID]),
+            ({"dag_run_state": "failed", "only_active": False}, 2, [DAG1_ID, 
DAG3_ID]),
+            (
+                {"start_date_gte": DAG3_START_DATE_2.isoformat(), 
"only_active": False},
+                1,
+                [DAG3_ID],
+            ),
+            (
+                {
+                    "start_date_gte": DAG1_START_DATE.isoformat(),
+                    "start_date_lte": DAG2_START_DATE.isoformat(),
+                },
+                1,
+                [DAG1_ID],
+            ),
+            (
+                {
+                    "end_date_lte": (datetime.now(tz=timezone.utc) + 
timedelta(days=1)).isoformat(),
+                    "only_active": False,
+                },
+                2,
+                [DAG1_ID, DAG3_ID],
+            ),
+            (
+                {
+                    "end_date_gte": DAG3_START_DATE_2.isoformat(),
+                    "end_date_lte": (datetime.now(tz=timezone.utc) + 
timedelta(days=1)).isoformat(),
+                    "only_active": False,
+                    "last_dag_run_state": "success",

Review Comment:
   Can we also have an example where the dag_runs that are in the `end_date` 
range have a different state than the `last_dag_run_state` ?
   
   The user request dags with runs in the `dag3_start_date_1` range, but with 
the latest dag_run_state that is the state of the `DAG3_RUN2` (which is success 
at the moment).



-- 
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]

Reply via email to