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


##########
airflow/api_fastapi/core_api/routes/public/dag_stats.py:
##########
@@ -0,0 +1,77 @@
+# 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
+
+from fastapi import Depends
+from sqlalchemy import func, select
+from sqlalchemy.orm import Query, Session
+from typing_extensions import Annotated
+
+from airflow.api_fastapi.common.db.common import (
+    get_session,
+)
+from airflow.api_fastapi.common.router import AirflowRouter
+from airflow.api_fastapi.core_api.openapi.exceptions import 
create_openapi_http_exception_doc
+from airflow.api_fastapi.core_api.serializers.dag_stats import (
+    DagStatsCollectionResponse,
+)
+from airflow.models.dagrun import DagRun
+from airflow.utils.state import DagRunState
+
+dag_stats_router = AirflowRouter(tags=["DagStats"], prefix="/dagStats")
+
+
+@dag_stats_router.get(
+    "/",
+    responses=create_openapi_http_exception_doc([400, 401, 403, 404, 422]),
+)
+async def get_dag_stats(
+    session: Annotated[Session, Depends(get_session)],
+    dag_ids: str | None = None,
+) -> DagStatsCollectionResponse:
+    """Get Dag statistics."""
+    query: Query = select(
+        DagRun.dag_id,
+        DagRun.state,
+        func.count(DagRun.state),
+    )
+    if dag_ids:
+        query_dag_ids = sorted(set(dag_ids.split(",")))
+        query = query.where(DagRun.dag_id.in_(query_dag_ids))
+    query = query.group_by(DagRun.dag_id, DagRun.state)
+    query_result = session.execute(query)
+
+    result_dag_ids = set()

Review Comment:
   instead of using a set here, you can use a `list` and only `append` to it if 
it does not contain the `id`.
   
   This way you have `unique` `dag_ids` inside it, but the ordering is still 
maintained, and you response will have the appropriate order because we iterate 
the result set that will itself be in the correct order.



-- 
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: commits-unsubscr...@airflow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to