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


##########
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:
   Some users have millions of runs over the course of years. Querying that 
every time the user opens the Details tab is a heavy lift.
   
   I don't think even putting limits is the right approach. We probably need to 
be calculating these over time. but we also need to make sure we don't blow up 
migrations if we do a db approach.



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