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


##########
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:
   LIke this, indeed no chance this respond in a timely manner for big 
installation. 
   
   But computing this on the latest 100 runs with a limit as you mention, I 
think it's worth trying.
   
   @dsuhinin If you want to try on a bigger setup. You can use 
https://github.com/apache/airflow/issues/62020. There is a zip file with dags, 
let this run for a few hours. (just run breeze those dags are enabled by 
default), and you will end up with a bunch of runs pretty quickly.
   



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