|
58 | 58 | from airflow.api_fastapi.core_api.datamodels.dags import DAG_ALIAS_MAPPING, DAGResponse |
59 | 59 | from airflow.api_fastapi.core_api.datamodels.ui.dag_runs import DAGRunLightResponse |
60 | 60 | from airflow.api_fastapi.core_api.datamodels.ui.dags import ( |
| 61 | + DAGLatestRunTaskInstanceStateCountsResponse, |
61 | 62 | DAGRunStateCountsResponse, |
| 63 | + DAGsLatestRunTaskInstanceStateCountsCollectionResponse, |
62 | 64 | DAGsRunStateCountsCollectionResponse, |
63 | 65 | DAGWithLatestDagRunsCollectionResponse, |
64 | 66 | DAGWithLatestDagRunsResponse, |
@@ -343,3 +345,75 @@ def get_dag_run_state_counts( |
343 | 345 | ], |
344 | 346 | state_count_limit=STATE_COUNT_CAP, |
345 | 347 | ) |
| 348 | + |
| 349 | + |
| 350 | +@dags_router.get( |
| 351 | + "/latest_run_task_instance_state_counts", |
| 352 | + dependencies=[ |
| 353 | + Depends(requires_access_dag(method="GET")), |
| 354 | + Depends(requires_access_dag(method="GET", access_entity=DagAccessEntity.TASK_INSTANCE)), |
| 355 | + ], |
| 356 | + operation_id="get_latest_run_task_instance_state_counts_ui", |
| 357 | +) |
| 358 | +def get_latest_run_task_instance_state_counts( |
| 359 | + session: SessionDep, |
| 360 | + readable_dags_filter: ReadableDagsFilterDep, |
| 361 | + dag_ids: Annotated[list[str], Query(min_length=1, max_length=conf.getint("api", "maximum_page_limit"))], |
| 362 | +) -> DAGsLatestRunTaskInstanceStateCountsCollectionResponse: |
| 363 | + """ |
| 364 | + Return task-instance state counts for each Dag's latest run, for the Dag list page. |
| 365 | +
|
| 366 | + Dags without any run are omitted from the response. |
| 367 | + """ |
| 368 | + permitted_dag_ids = readable_dags_filter.value or set() |
| 369 | + requested_dag_ids = sorted(set(dag_ids) & permitted_dag_ids) |
| 370 | + |
| 371 | + dags: list[DAGLatestRunTaskInstanceStateCountsResponse] = [] |
| 372 | + if not requested_dag_ids: |
| 373 | + return DAGsLatestRunTaskInstanceStateCountsCollectionResponse(dags=dags) |
| 374 | + |
| 375 | + latest_run_branches = [ |
| 376 | + select(DagRun.dag_id, DagRun.run_id) |
| 377 | + .where(DagRun.dag_id == dag_id) |
| 378 | + .order_by(DagRun.run_after.desc()) |
| 379 | + .limit(1) |
| 380 | + .subquery() |
| 381 | + for dag_id in requested_dag_ids |
| 382 | + ] |
| 383 | + latest_runs_union = union_all(*(select(branch) for branch in latest_run_branches)).subquery() |
| 384 | + latest_run_id_by_dag: dict[str, str] = { |
| 385 | + row.dag_id: row.run_id for row in session.execute(select(latest_runs_union)) |
| 386 | + } |
| 387 | + |
| 388 | + if latest_run_id_by_dag: |
| 389 | + # Each branch filters on (dag_id, run_id) equality, which the ti_dag_run index |
| 390 | + # covers. A run's task instances are bounded by the Dag's task structure, so the |
| 391 | + # per-state counts are exact (no cap needed here, unlike the cross-run counts in |
| 392 | + # get_dag_run_state_counts). |
| 393 | + ti_branches = [ |
| 394 | + select(literal(dag_id).label("dag_id"), TaskInstance.state.label("state")) |
| 395 | + .where(TaskInstance.dag_id == dag_id, TaskInstance.run_id == run_id) |
| 396 | + .subquery() |
| 397 | + for dag_id, run_id in latest_run_id_by_dag.items() |
| 398 | + ] |
| 399 | + tis_union = union_all(*(select(branch) for branch in ti_branches)).subquery() |
| 400 | + counts_by_dag: dict[str, dict[str, int]] = {dag_id: {} for dag_id in latest_run_id_by_dag} |
| 401 | + for row in session.execute( |
| 402 | + select(tis_union.c.dag_id, tis_union.c.state, func.count().label("cnt")).group_by( |
| 403 | + tis_union.c.dag_id, tis_union.c.state |
| 404 | + ) |
| 405 | + ): |
| 406 | + state_key = row.state if row.state is not None else "no_status" |
| 407 | + counts_by_dag[row.dag_id][state_key] = row.cnt |
| 408 | + |
| 409 | + dags = [ |
| 410 | + DAGLatestRunTaskInstanceStateCountsResponse( |
| 411 | + dag_id=dag_id, |
| 412 | + run_id=latest_run_id_by_dag[dag_id], |
| 413 | + state_counts=counts_by_dag[dag_id], |
| 414 | + ) |
| 415 | + for dag_id in requested_dag_ids |
| 416 | + if dag_id in latest_run_id_by_dag |
| 417 | + ] |
| 418 | + |
| 419 | + return DAGsLatestRunTaskInstanceStateCountsCollectionResponse(dags=dags) |
0 commit comments