dsuhinin commented on code in PR #65722:
URL: https://github.com/apache/airflow/pull/65722#discussion_r3194021879


##########
airflow-core/src/airflow/api_fastapi/core_api/routes/public/dag_run.py:
##########
@@ -130,10 +131,36 @@ def get_dag_run(dag_id: str, dag_run_id: str, session: 
SessionDep) -> DAGRunResp
             status.HTTP_404_NOT_FOUND,
             f"The DagRun with dag_id: `{dag_id}` and run_id: `{dag_run_id}` 
was not found",
         )
-
     return dag_run
 
 
+@dag_run_router.get(
+    "/{dag_run_id}/stats",
+    responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND]),
+    dependencies=[Depends(requires_access_dag(method="GET", 
access_entity=DagAccessEntity.RUN))],
+)
+def get_dag_run_stats(dag_id: str, dag_run_id: str, session: SessionDep) -> 
DagRunStatsResponse:
+    """Get duration statistics for a DAG based on its historical completed 
runs."""
+    if not session.scalar(select(DagRun.id).filter_by(dag_id=dag_id, 
run_id=dag_run_id)):
+        raise HTTPException(
+            status.HTTP_404_NOT_FOUND,
+            f"The DagRun with dag_id: `{dag_id}` and run_id: `{dag_run_id}` 
was not found",
+        )
+
+    durations = [
+        d
+        for d in session.scalars(
+            select(DagRun.duration.expression).where(  # type: 
ignore[attr-defined]
+                DagRun.dag_id == dag_id,
+                DagRun.state.in_([DagRunState.SUCCESS, DagRunState.FAILED]),
+            )
+        )
+        if d is not None
+    ]

Review Comment:
   ok, applied limit to fetch last 100 runs.



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