Taragolis commented on code in PR #36916:
URL: https://github.com/apache/airflow/pull/36916#discussion_r1459696206


##########
airflow/hooks/utils.py:
##########
@@ -0,0 +1,120 @@
+# 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 typing
+from typing import TYPE_CHECKING
+
+from sqlalchemy import func
+
+from airflow.models import DagBag, DagRun, TaskInstance
+from airflow.utils.session import NEW_SESSION, provide_session
+from airflow.utils.sqlalchemy import tuple_in_condition
+
+if TYPE_CHECKING:
+    from sqlalchemy.orm import Query, Session
+
+
+@provide_session
+def _get_count(
+    dttm_filter,
+    external_task_ids,
+    external_task_group_id,
+    external_dag_id,
+    states,
+    session: Session = NEW_SESSION,
+) -> int:
+    """
+    Get the count of records against dttm filter and states.
+
+    :param dttm_filter: date time filter for execution date
+    :param external_task_ids: The list of task_ids
+    :param external_task_group_id: The ID of the external task group
+    :param external_dag_id: The ID of the external DAG.
+    :param states: task or dag states
+    :param session: airflow session object
+    """
+    TI = TaskInstance
+    DR = DagRun
+    if not dttm_filter:
+        return 0
+
+    if external_task_ids:
+        count = (
+            _count_query(TI, states, dttm_filter, external_dag_id, session)
+            .filter(TI.task_id.in_(external_task_ids))
+            .scalar()
+        ) / len(external_task_ids)
+    elif external_task_group_id:
+        external_task_group_task_ids = _get_external_task_group_task_ids(
+            dttm_filter, external_task_group_id, external_dag_id, session
+        )
+        if not external_task_group_task_ids:
+            count = 0
+        else:
+            count = (
+                _count_query(TI, states, dttm_filter, external_dag_id, session)
+                .filter(tuple_in_condition((TI.task_id, TI.map_index), 
external_task_group_task_ids))
+                .scalar()
+            ) / len(external_task_group_task_ids)
+    else:
+        count = _count_query(DR, states, dttm_filter, external_dag_id, 
session).scalar()
+    return typing.cast(int, count)
+
+
+def _count_query(model, states, dttm_filter, external_dag_id, session: 
Session) -> Query:
+    """
+    Get the count of records against dttm filter and states.
+
+    :param model: The SQLAlchemy model representing the relevant table.
+    :param states: task or dag states
+    :param dttm_filter: date time filter for execution date
+    :param external_dag_id: The ID of the external DAG.
+    :param session: airflow session object
+    """
+    query = session.query(func.count()).filter(
+        model.dag_id == external_dag_id,
+        model.state.in_(states),
+        model.execution_date.in_(dttm_filter),
+    )

Review Comment:
   Just wondering could we get read of SQLAlchemy Query API and replace by 
statement? So it would be compatible with SA 2.0 syntax?
   
   THis might be follow up but it would be nice if we could do in one go



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