bugraoz93 commented on code in PR #44332:
URL: https://github.com/apache/airflow/pull/44332#discussion_r1894482701


##########
airflow/api_fastapi/core_api/routes/ui/grid.py:
##########
@@ -0,0 +1,229 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+from __future__ import annotations
+
+import collections
+import itertools
+from typing import Annotated
+
+from fastapi import Depends, HTTPException, Request, status
+from sqlalchemy import select
+
+from airflow import DAG
+from airflow.api_fastapi.common.db.common import SessionDep, paginated_select
+from airflow.api_fastapi.common.parameters import (
+    OptionalDateTimeQuery,
+    QueryDagRunRunTypesFilter,
+    QueryDagRunStateFilter,
+    QueryIncludeDownstream,
+    QueryIncludeUpstream,
+    QueryLimit,
+    QueryOffset,
+    Range,
+    RangeFilter,
+    SortParam,
+)
+from airflow.api_fastapi.common.router import AirflowRouter
+from airflow.api_fastapi.core_api.datamodels.ui.grid import (
+    GridDAGRunwithTIs,
+    GridResponse,
+)
+from airflow.api_fastapi.core_api.openapi.exceptions import 
create_openapi_http_exception_doc
+from airflow.api_fastapi.core_api.services.ui.grid import (
+    fill_task_instance_summaries,
+    get_child_task_map,
+    get_dag_run_sort_param,
+    get_task_group_map,
+)
+from airflow.models import DagRun, TaskInstance
+from airflow.models.dagrun import DagRunNote
+from airflow.models.taskinstance import TaskInstanceNote
+
+grid_router = AirflowRouter(prefix="/grid", tags=["Grid"])
+
+
+@grid_router.get(
+    "/{dag_id}",
+    include_in_schema=False,
+    responses=create_openapi_http_exception_doc([status.HTTP_400_BAD_REQUEST, 
status.HTTP_404_NOT_FOUND]),
+)
+def grid_data(
+    dag_id: str,
+    run_types: QueryDagRunRunTypesFilter,
+    run_states: QueryDagRunStateFilter,
+    session: SessionDep,
+    offset: QueryOffset,
+    request: Request,
+    limit: QueryLimit,
+    order_by: Annotated[
+        SortParam,
+        Depends(
+            SortParam(
+                ["logical_date", "data_interval_start", "data_interval_end", 
"start_date", "end_date"], DagRun
+            ).dynamic_depends()
+        ),
+    ],
+    include_upstream: QueryIncludeUpstream = False,
+    include_downstream: QueryIncludeDownstream = False,
+    logical_date_gte: OptionalDateTimeQuery = None,
+    logical_date_lte: OptionalDateTimeQuery = None,
+    root: str | None = None,
+) -> GridResponse:
+    """Return grid data."""
+    dag: DAG = request.app.state.dag_bag.get_dag(dag_id)
+    if not dag:
+        raise HTTPException(status.HTTP_404_NOT_FOUND, f"Dag with id {dag_id} 
was not found")
+
+    date_filter = RangeFilter(
+        Range(lower_bound=logical_date_gte, upper_bound=logical_date_lte),
+        attribute=DagRun.logical_date,
+    )
+    # Retrieve, sort and encode the previous DAG Runs
+    base_query = (
+        select(
+            DagRun.run_id,
+            DagRun.queued_at,
+            DagRun.start_date,
+            DagRun.end_date,
+            DagRun.state,
+            DagRun.run_type,
+            DagRun.data_interval_start,
+            DagRun.data_interval_end,
+            DagRun.dag_version_id.label("version_number"),
+            DagRunNote.content.label("note"),
+        )
+        .join(DagRun.dag_run_note, isouter=True)
+        .select_from(DagRun)
+        .where(DagRun.dag_id == dag.dag_id)
+    )
+
+    dag_runs_select_filter, _ = paginated_select(
+        statement=base_query,
+        filters=[
+            run_types,
+            run_states,
+            date_filter,
+        ],
+        order_by=get_dag_run_sort_param(dag=dag, request_order_by=order_by),
+        offset=offset,
+        limit=limit,
+    )
+
+    dag_runs = session.execute(dag_runs_select_filter)
+
+    # Check if there are any DAG Runs with given criteria to eliminate 
unnecessary queries/errors
+    if not dag_runs:
+        return GridResponse(dag_runs=[])
+
+    # Retrieve, sort and encode the Task Instances
+    tis_of_dag_runs, _ = paginated_select(
+        statement=select(
+            TaskInstance.run_id,
+            TaskInstance.task_id,
+            TaskInstance.try_number,
+            TaskInstance.state,
+            TaskInstance.start_date,
+            TaskInstance.end_date,
+            TaskInstance.queued_dttm.label("queued_dttm"),
+            TaskInstanceNote.content.label("note"),

Review Comment:
   I have updated accordingly and updated the accessing of those elements from 
`execute` to `scalars`. 



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