|
22 | 22 | from fastapi import Depends, HTTPException, Query, Response, status
|
23 | 23 | from fastapi.exceptions import RequestValidationError
|
24 | 24 | from pydantic import ValidationError
|
25 |
| -from sqlalchemy import select, update |
| 25 | +from sqlalchemy import func, null, select, update |
26 | 26 |
|
27 | 27 | from airflow.api.common import delete_dag as delete_dag_module
|
28 | 28 | from airflow.api_fastapi.common.dagbag import DagBagDep
|
29 | 29 | from airflow.api_fastapi.common.db.common import (
|
30 | 30 | SessionDep,
|
| 31 | + apply_filters_to_select, |
31 | 32 | paginated_select,
|
32 | 33 | )
|
33 | 34 | from airflow.api_fastapi.common.db.dags import generate_dag_with_latest_run_query
|
@@ -115,30 +116,54 @@ def get_dags(
|
115 | 116 | session: SessionDep,
|
116 | 117 | ) -> DAGCollectionResponse:
|
117 | 118 | """Get all DAGs."""
|
118 |
| - dag_runs_select = None |
| 119 | + query = select(DagModel) |
119 | 120 |
|
120 |
| - if dag_run_state.value or dag_run_start_date_range.is_active() or dag_run_end_date_range.is_active(): |
121 |
| - dag_runs_select, _ = paginated_select( |
122 |
| - statement=select(DagRun), |
| 121 | + max_run_id_query = ( # ordering by id will not always be "latest run", but it's a simplifying assumption |
| 122 | + select(DagRun.dag_id, func.max(DagRun.id).label("max_dag_run_id")) |
| 123 | + .where(DagRun.start_date.is_not(null())) |
| 124 | + .group_by(DagRun.dag_id) |
| 125 | + .subquery(name="mrq") |
| 126 | + ) |
| 127 | + |
| 128 | + has_max_run_filter = ( |
| 129 | + dag_run_state.value |
| 130 | + or last_dag_run_state.value |
| 131 | + or dag_run_start_date_range.is_active() |
| 132 | + or dag_run_end_date_range.is_active() |
| 133 | + ) |
| 134 | + |
| 135 | + if has_max_run_filter or order_by.value in ( |
| 136 | + "last_run_state", |
| 137 | + "last_run_start_date", |
| 138 | + "-last_run_state", |
| 139 | + "-last_run_start_date", |
| 140 | + ): |
| 141 | + query = query.join( |
| 142 | + max_run_id_query, |
| 143 | + DagModel.dag_id == max_run_id_query.c.dag_id, |
| 144 | + isouter=True, |
| 145 | + ).join(DagRun, DagRun.id == max_run_id_query.c.max_dag_run_id, isouter=True) |
| 146 | + |
| 147 | + if has_max_run_filter: |
| 148 | + query = apply_filters_to_select( |
| 149 | + statement=query, |
123 | 150 | filters=[
|
124 | 151 | dag_run_start_date_range,
|
125 | 152 | dag_run_end_date_range,
|
126 | 153 | dag_run_state,
|
| 154 | + last_dag_run_state, |
127 | 155 | ],
|
128 |
| - session=session, |
129 | 156 | )
|
130 |
| - dag_runs_select = dag_runs_select.cte() |
131 | 157 |
|
132 | 158 | dags_select, total_entries = paginated_select(
|
133 |
| - statement=generate_dag_with_latest_run_query(dag_runs_select), |
| 159 | + statement=query, |
134 | 160 | filters=[
|
135 | 161 | exclude_stale,
|
136 | 162 | paused,
|
137 | 163 | dag_id_pattern,
|
138 | 164 | dag_display_name_pattern,
|
139 | 165 | tags,
|
140 | 166 | owners,
|
141 |
| - last_dag_run_state, |
142 | 167 | readable_dags_filter,
|
143 | 168 | ],
|
144 | 169 | order_by=order_by,
|
|
0 commit comments