rawwar commented on code in PR #44303:
URL: https://github.com/apache/airflow/pull/44303#discussion_r1855200663


##########
airflow/api_fastapi/core_api/routes/public/task_instances.py:
##########
@@ -234,6 +236,70 @@ def get_task_instance_dependencies(
     return TaskDependencyCollectionResponse.model_validate({"dependencies": 
deps})
 
 
+@task_instances_router.get(
+    task_instances_prefix + "/{task_id}/tries",
+    responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND]),
+)
+def get_task_instance_tries(
+    dag_id: str,
+    dag_run_id: str,
+    task_id: str,
+    session: Annotated[Session, Depends(get_session)],
+    map_index: int = -1,
+) -> TaskInstanceHistoryCollectionResponse:
+    """Get list of task instances history."""
+
+    def _query(orm_object: Base) -> Select:
+        query = select(orm_object).where(
+            orm_object.dag_id == dag_id,
+            orm_object.run_id == dag_run_id,
+            orm_object.task_id == task_id,
+            orm_object.map_index == map_index,
+        )
+        print(type(query))
+        return query
+
+    # Exclude TaskInstance with state UP_FOR_RETRY since they have been 
recorded in TaskInstanceHistory
+    tis = session.scalars(
+        _query(TI).where(or_(TI.state != TaskInstanceState.UP_FOR_RETRY, 
TI.state.is_(None)))
+    ).all()
+    task_instance_select = session.scalars(_query(TIH)).all() + tis
+
+    if not task_instance_select:
+        raise HTTPException(
+            status.HTTP_404_NOT_FOUND,
+            f"The Task Instance with dag_id: `{dag_id}`, run_id: 
`{dag_run_id}`, task_id: `{task_id}` and map_index: `{map_index}` was not 
found",
+        )
+    task_instances = [
+        TaskInstanceHistoryResponse.model_validate(task_instance, 
from_attributes=True)
+        for task_instance in task_instance_select
+    ]
+    return TaskInstanceHistoryCollectionResponse(
+        task_instances=task_instances,
+        total_entries=len(task_instances),
+    )

Review Comment:
   can you use `cast` like here: 
https://github.com/apache/airflow/blob/2390504d2424aa664a752433c6a868d7b645f72a/airflow/api_fastapi/core_api/routes/public/dag_run.py#L224



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