choo121600 commented on code in PR #67242:
URL: https://github.com/apache/airflow/pull/67242#discussion_r3295168362


##########
airflow-core/src/airflow/api_fastapi/core_api/routes/ui/dags.py:
##########
@@ -308,3 +311,47 @@ def get_latest_run_info(dag_id: str, session: SessionDep) 
-> DAGRunLightResponse
     latest_run_info = session.execute(latest_run_info_select).one_or_none()
 
     return DAGRunLightResponse(**latest_run_info._mapping) if latest_run_info 
else None
+
+
+@dags_router.get(
+    "/run_state_counts",
+    dependencies=[
+        Depends(requires_access_dag(method="GET")),
+        Depends(requires_access_dag(method="GET", 
access_entity=DagAccessEntity.RUN)),
+    ],
+    operation_id="get_dag_run_state_counts_ui",
+)
+def get_dag_run_state_counts(
+    session: SessionDep,
+    readable_dags_filter: ReadableDagsFilterDep,
+    dag_ids: Annotated[list[str], Query(min_length=1)],
+    run_after_gte: datetime | None = None,
+) -> DAGsRunStateCountsCollectionResponse:
+    """Return per-DAG DagRun state counts (zero-filled) for the DAG list 
page."""
+    permitted_dag_ids = readable_dags_filter.value or set()
+    requested_dag_ids = [dag_id for dag_id in dict.fromkeys(dag_ids) if dag_id 
in permitted_dag_ids]
+    counts_by_dag: dict[str, dict[DagRunState, int]] = {
+        dag_id: {state: 0 for state in DagRunState} for dag_id in 
requested_dag_ids
+    }
+
+    if requested_dag_ids:
+        # Single GROUP BY query — composite Index("dag_id_state", dag_id, 
_state)
+        # makes this an index-only scan, sub-100ms even with many DAGs/states.

Review Comment:
   This performance guarantee comment feels a bit too strong.
   When run_after_gte is included, it may no longer be an index-only scan, so 
the current wording does not seem entirely accurate.



##########
airflow-core/src/airflow/api_fastapi/core_api/datamodels/ui/dags.py:
##########
@@ -37,3 +38,16 @@ class DAGWithLatestDagRunsCollectionResponse(BaseModel):
 
     total_entries: int
     dags: list[DAGWithLatestDagRunsResponse]
+
+
+class DAGRunStateCountsResponse(BaseModel):
+    """Per-DAG counts of DagRuns grouped by state."""

Review Comment:
   We use Dag instead of DAG.
   Could you update this occurrence to Dag?
   It looks like the same change may be needed in a few other places as well.



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