seanghaeli commented on code in PR #76965:
URL: https://github.com/apache/airflow/pull/76965#discussion_r4651313383
##########
airflow-core/src/airflow/api_fastapi/core_api/routes/public/triggers.py:
##########
@@ -222,23 +222,75 @@ def get_triggers(
if asset_id.value is not None and asset_id.value not in
readable_triggers_filter.asset_ids:
# Which triggers watch an asset is the asset's to show.
return TriggerCollectionResponse(triggers=[], total_entries=0)
- triggers_select, total_entries = paginated_select(
- statement=_build_triggers_select(),
- filters=[
- kind,
- trigger_status,
- triggerer_hostname,
- dag_id,
- asset_id,
- teams,
- readable_triggers_filter,
- ],
- order_by=order_by,
- offset=offset,
- limit=limit,
- session=session,
+ filters = [kind, trigger_status, triggerer_hostname, dag_id, asset_id,
teams]
+
+ candidates = apply_filters_to_select(
+ statement=select(
+ Trigger.id,
+ Trigger.dag_id,
+ Trigger.owner_ref,
+ AssetWatcherModel.asset_id,
+ )
+ .outerjoin(Trigger.triggerer_job)
+ .outerjoin(AssetWatcherModel, AssetWatcherModel.trigger_id ==
Trigger.id)
+ .where(Trigger.owner_still_refers_expression()),
+ filters=filters,
)
- triggers = session.scalars(triggers_select).unique().all()
+ candidates = order_by.to_orm(candidates)
+
+ candidate_rows = session.execute(candidates)
+
+ visible_ids = []
+ total_entries = 0
+ page_offset = offset.value or 0
+ page_limit = limit.value
+
+ current_id = None
+ current_dag_id = None
+ current_owner_ref = None
+ current_assets = set()
+
+ def include_candidate():
Review Comment:
Rows of one trigger come back next to each other, because the sort always
ends with the id, so `itertools.groupby(candidate_rows, key=itemgetter(0))`
could replace `include_candidate` and the `current_*` state.
##########
airflow-core/src/airflow/api_fastapi/core_api/security.py:
##########
@@ -1236,22 +1230,14 @@ def __init__(
self.asset_ids = asset_ids
self.can_read_an_owner = can_read_an_owner
- def to_orm(self, select: Select) -> Select:
- # The kind by the owner recorded on the row, as
Trigger.kind_expression decides it, but without its CASE:
- # compared in each branch, Postgres evaluates the CASE and its watcher
subquery once per branch and row.
- ti_id = Trigger.owner_ref["ti_id"].as_string()
- callback_id = Trigger.owner_ref["callback_id"].as_string()
- return select.where(
- or_(
- and_(ti_id.is_not(None),
Trigger.dag_id.in_(self.task_dag_ids)),
- and_(ti_id.is_(None), callback_id.is_not(None),
Trigger.dag_id.in_(self.callback_dag_ids)),
- and_(
- ti_id.is_(None),
- callback_id.is_(None),
- Trigger.id.in_(_select_triggers_watching(self.asset_ids)),
- ),
- )
- )
+ def allows(self, *, dag_id: str | None, owner_ref: dict | None,
watched_asset_ids: set[int]) -> bool:
Review Comment:
#76560 adds the same check on a loaded trigger as
`PermittedTriggerFilter.permits(trigger)`, so whichever of the two lands second
should use the other's.
##########
airflow-core/tests/unit/api_fastapi/core_api/routes/public/test_triggers.py:
##########
@@ -365,6 +365,66 @@ def test_filters(self, test_client, triggers, params,
expected):
assert response.status_code == 200
assert sorted(_name(t) for t in response.json()["triggers"]) ==
expected
+ def test_pagination_counts_multi_watcher_trigger_once(self, test_client,
session, triggers):
+ _watcher(session, "second_failing", triggers["failing"])
+ session.commit()
+
+ response = test_client.get("/triggers", params={"order_by": "id"})
+ assert response.status_code == 200
+ expected_ids = [trigger["id"] for trigger in
response.json()["triggers"]]
+ assert response.json()["total_entries"] == 5
+ assert len(expected_ids) == len(set(expected_ids)) == 5
+
+ paged_ids = []
+ for offset in range(5):
+ page = test_client.get(
+ "/triggers",
+ params={"order_by": "id", "limit": 1, "offset": offset},
+ )
+ assert page.status_code == 200
+ assert page.json()["total_entries"] == 5
+ assert len(page.json()["triggers"]) == 1
+ paged_ids.append(page.json()["triggers"][0]["id"])
+
+ assert paged_ids == expected_ids
+
+ @mock.patch(f"{BASE_AUTH}.get_authorized_dag_ids", autospec=True)
+ def test_pagination_excludes_unauthorized_triggers(self, mock_dag_ids,
test_client, session, triggers):
Review Comment:
This hides only task triggers. A callback and a watcher the caller can't
read in the same walk would page all three branches of `allows`.
--
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]