-
Notifications
You must be signed in to change notification settings - Fork 16.4k
feat(task_sdk): add support for inlet_events in Task Context #45960
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
616c28e
feat(task_sdk): add support for inlet_events in Task Context
Lee-W e6bdd8f
feat(task_sdk): add AssetEventCollectionResponse
Lee-W 99b05ba
refactor(task_sdk): combine asset event uris
Lee-W 92c8810
refactor(api_fastapi): extract asset_event datamodels from asset
Lee-W e3169ac
fix(task_sdk): revert unrelated datamodels change
Lee-W 63c1b19
fix(task_sdk/context): add _get_asset_events_from_db for fixing tests
Lee-W 51619c6
test(task_sdk): add test cases for execution_time context inlet access
Lee-W 0218165
test(task_sdk): extend test_handle_requests to include asset event calls
Lee-W ce8274d
test(execution_api): add tests to asset event apis
Lee-W 362a1b5
fix(execution_api): remove unnecessary redact
Lee-W 9bc8314
feat(task_sdk): extract asset response from asset event response
Lee-W edfdf4e
feat(task_sdk): add missing http exception
Lee-W 3f79ef8
feat(task_sdk): extract asset response from asset event response
Lee-W e96c579
feat(task_sdk): remove duplicate inlet logic
Lee-W 259ae05
feat(task_sdk): remove AssetEvent form definitions
Lee-W 1cf88c4
test(task_sdk): add test case test_run_with_asset_inlets
Lee-W 82ed494
docs(newsfragments): add description of how inlet_events access has b…
Lee-W File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
airflow/api_fastapi/execution_api/datamodels/asset_event.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # 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 datetime import datetime | ||
|
|
||
| from airflow.api_fastapi.core_api.base import BaseModel, StrictBaseModel | ||
| from airflow.api_fastapi.execution_api.datamodels.asset import AssetResponse | ||
|
|
||
|
|
||
| class DagRunAssetReference(StrictBaseModel): | ||
| """DagRun serializer for asset responses.""" | ||
|
|
||
| run_id: str | ||
| dag_id: str | ||
| logical_date: datetime | None | ||
| start_date: datetime | ||
| end_date: datetime | None | ||
| state: str | ||
| data_interval_start: datetime | None | ||
| data_interval_end: datetime | None | ||
|
|
||
|
|
||
| class AssetEventResponse(BaseModel): | ||
| """Asset event schema with fields that are needed for Runtime.""" | ||
|
|
||
| id: int | ||
| timestamp: datetime | ||
| extra: dict | None = None | ||
|
|
||
| asset: AssetResponse | ||
| created_dagruns: list[DagRunAssetReference] | ||
|
|
||
| source_task_id: str | None = None | ||
| source_dag_id: str | None = None | ||
| source_run_id: str | None = None | ||
| source_map_index: int = -1 | ||
|
|
||
|
|
||
| class AssetEventsResponse(BaseModel): | ||
| """Collection of AssetEventResponse.""" | ||
|
|
||
| asset_events: list[AssetEventResponse] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
airflow/api_fastapi/execution_api/routes/asset_events.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| # 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 typing import Annotated | ||
|
|
||
| from fastapi import HTTPException, Query, status | ||
| from sqlalchemy import and_, select | ||
|
|
||
| from airflow.api_fastapi.common.db.common import SessionDep | ||
| from airflow.api_fastapi.common.router import AirflowRouter | ||
| from airflow.api_fastapi.execution_api.datamodels.asset import AssetResponse | ||
| from airflow.api_fastapi.execution_api.datamodels.asset_event import ( | ||
| AssetEventResponse, | ||
| AssetEventsResponse, | ||
| ) | ||
| from airflow.models.asset import AssetAliasModel, AssetEvent, AssetModel | ||
|
|
||
| # TODO: Add dependency on JWT token | ||
| router = AirflowRouter( | ||
| responses={ | ||
| status.HTTP_404_NOT_FOUND: {"description": "Asset not found"}, | ||
| status.HTTP_401_UNAUTHORIZED: {"description": "Unauthorized"}, | ||
| }, | ||
| ) | ||
|
|
||
|
|
||
| def _get_asset_events_through_sql_clauses( | ||
| *, join_clause, where_clause, session: SessionDep | ||
| ) -> AssetEventsResponse: | ||
| asset_events = session.scalars( | ||
| select(AssetEvent).join(join_clause).where(where_clause).order_by(AssetEvent.timestamp) | ||
| ) | ||
| return AssetEventsResponse.model_validate( | ||
| { | ||
| "asset_events": [ | ||
| AssetEventResponse( | ||
| id=event.id, | ||
| timestamp=event.timestamp, | ||
| extra=event.extra, | ||
| asset=AssetResponse( | ||
| name=event.asset.name, | ||
| uri=event.asset.uri, | ||
| group=event.asset.group, | ||
| extra=event.asset.extra, | ||
| ), | ||
| created_dagruns=event.created_dagruns, | ||
| source_task_id=event.source_task_id, | ||
| source_dag_id=event.source_dag_id, | ||
| source_run_id=event.source_run_id, | ||
| source_map_index=event.source_map_index, | ||
| ) | ||
| for event in asset_events | ||
| ] | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| @router.get("/by-asset") | ||
| def get_asset_event_by_asset_name_uri( | ||
| name: Annotated[str | None, Query(description="The name of the Asset")], | ||
| uri: Annotated[str | None, Query(description="The URI of the Asset")], | ||
| session: SessionDep, | ||
| ) -> AssetEventsResponse: | ||
| if name and uri: | ||
| where_clause = and_(AssetModel.name == name, AssetModel.uri == uri) | ||
Lee-W marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| elif uri: | ||
| where_clause = and_(AssetModel.uri == uri, AssetModel.active.has()) | ||
| elif name: | ||
| where_clause = and_(AssetModel.name == name, AssetModel.active.has()) | ||
| else: | ||
| raise HTTPException( | ||
| status_code=status.HTTP_400_BAD_REQUEST, | ||
| detail={ | ||
| "reason": "Missing parameter", | ||
| "message": "name and uri cannot both be None", | ||
| }, | ||
| ) | ||
|
|
||
| return _get_asset_events_through_sql_clauses( | ||
| join_clause=AssetEvent.asset, | ||
| where_clause=where_clause, | ||
| session=session, | ||
| ) | ||
|
|
||
|
|
||
| @router.get("/by-asset-alias") | ||
| def get_asset_event_by_asset_alias( | ||
| name: Annotated[str, Query(description="The name of the Asset Alias")], | ||
| session: SessionDep, | ||
| ) -> AssetEventsResponse: | ||
| return _get_asset_events_through_sql_clauses( | ||
| join_clause=AssetEvent.source_aliases, | ||
| where_clause=(AssetAliasModel.name == name), | ||
| session=session, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.