sensor_helper.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. from __future__ import annotations
  18. from typing import TYPE_CHECKING, cast
  19. from sqlalchemy import func, select
  20. from airflow.models import DagBag, DagRun, TaskInstance
  21. from airflow.utils.session import NEW_SESSION, provide_session
  22. from airflow.utils.sqlalchemy import tuple_in_condition
  23. if TYPE_CHECKING:
  24. from sqlalchemy.orm import Query, Session
  25. @provide_session
  26. def _get_count(
  27. dttm_filter,
  28. external_task_ids,
  29. external_task_group_id,
  30. external_dag_id,
  31. states,
  32. session: Session = NEW_SESSION,
  33. ) -> int:
  34. """
  35. Get the count of records against dttm filter and states.
  36. :param dttm_filter: date time filter for execution date
  37. :param external_task_ids: The list of task_ids
  38. :param external_task_group_id: The ID of the external task group
  39. :param external_dag_id: The ID of the external DAG.
  40. :param states: task or dag states
  41. :param session: airflow session object
  42. """
  43. TI = TaskInstance
  44. DR = DagRun
  45. if not dttm_filter:
  46. return 0
  47. if external_task_ids:
  48. count = (
  49. session.scalar(
  50. _count_query(TI, states, dttm_filter, external_dag_id, session).filter(
  51. TI.task_id.in_(external_task_ids)
  52. )
  53. )
  54. ) / len(external_task_ids)
  55. elif external_task_group_id:
  56. external_task_group_task_ids = _get_external_task_group_task_ids(
  57. dttm_filter, external_task_group_id, external_dag_id, session
  58. )
  59. if not external_task_group_task_ids:
  60. count = 0
  61. else:
  62. count = (
  63. session.scalar(
  64. _count_query(TI, states, dttm_filter, external_dag_id, session).filter(
  65. tuple_in_condition((TI.task_id, TI.map_index), external_task_group_task_ids)
  66. )
  67. )
  68. ) / len(external_task_group_task_ids)
  69. else:
  70. count = session.scalar(_count_query(DR, states, dttm_filter, external_dag_id, session))
  71. return cast(int, count)
  72. def _count_query(model, states, dttm_filter, external_dag_id, session: Session) -> Query:
  73. """
  74. Get the count of records against dttm filter and states.
  75. :param model: The SQLAlchemy model representing the relevant table.
  76. :param states: task or dag states
  77. :param dttm_filter: date time filter for execution date
  78. :param external_dag_id: The ID of the external DAG.
  79. :param session: airflow session object
  80. """
  81. query = select(func.count()).filter(
  82. model.dag_id == external_dag_id, model.state.in_(states), model.execution_date.in_(dttm_filter)
  83. )
  84. return query
  85. def _get_external_task_group_task_ids(dttm_filter, external_task_group_id, external_dag_id, session):
  86. """
  87. Get the count of records against dttm filter and states.
  88. :param dttm_filter: date time filter for execution date
  89. :param external_task_group_id: The ID of the external task group
  90. :param external_dag_id: The ID of the external DAG.
  91. :param session: airflow session object
  92. """
  93. refreshed_dag_info = DagBag(read_dags_from_db=True).get_dag(external_dag_id, session)
  94. task_group = refreshed_dag_info.task_group_dict.get(external_task_group_id)
  95. if task_group:
  96. group_tasks = session.scalars(
  97. select(TaskInstance).filter(
  98. TaskInstance.dag_id == external_dag_id,
  99. TaskInstance.task_id.in_(task.task_id for task in task_group),
  100. TaskInstance.execution_date.in_(dttm_filter),
  101. )
  102. )
  103. return [(t.task_id, t.map_index) for t in group_tasks]
  104. # returning default task_id as group_id itself, this will avoid any failure in case of
  105. # 'check_existence=False' and will fail on timeout
  106. return [(external_task_group_id, -1)]