amoghrajesh commented on code in PR #45960:
URL: https://github.com/apache/airflow/pull/45960#discussion_r1960970174
##########
task_sdk/src/airflow/sdk/execution_time/comms.py:
##########
@@ -104,6 +105,30 @@ def from_asset_response(cls, asset_response:
AssetResponse) -> AssetResult:
return cls(**asset_response.model_dump(exclude_defaults=True),
type="AssetResult")
+class AssetEventCollectionResult(AssetEventCollectionResponse):
Review Comment:
Just `AssetEventResult`?
##########
task_sdk/src/airflow/sdk/api/datamodels/_generated.py:
##########
@@ -1,7 +1,3 @@
-# generated by datamodel-codegen:
Review Comment:
Lets keep only the changes that you need for this file, yes the codegen
makes some smaller changes that arent needed at times.
##########
airflow/api_fastapi/execution_api/routes/asset_events.py:
##########
@@ -0,0 +1,96 @@
+# 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 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 (
+ AssetEventCollectionResponse,
+)
+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
+) -> AssetEventCollectionResponse:
+ asset_events = session.scalars(
+
select(AssetEvent).join(join_clause).where(where_clause).order_by(AssetEvent.timestamp)
+ )
+ return AssetEventCollectionResponse.model_validate({"asset_events":
asset_events or []})
+
+
[email protected]("/by-asset-name-uri")
+def get_asset_event_by_asset_name_uri(
+ name: Annotated[str, Query(description="The name of the Asset")],
+ uri: Annotated[str, Query(description="The URI of the Asset")],
+ session: SessionDep,
+) -> AssetEventCollectionResponse:
+ return _get_asset_events_through_sql_clauses(
+ join_clause=AssetEvent.asset,
+ where_clause=and_(AssetModel.name == name, AssetModel.uri == uri),
+ session=session,
+ )
+
+
[email protected]("/by-asset-uri")
+def get_asset_event_by_uri(
+ uri: Annotated[str, Query(description="The URI of the Asset")],
+ session: SessionDep,
+) -> AssetEventCollectionResponse:
+ return _get_asset_events_through_sql_clauses(
+ join_clause=AssetEvent.asset,
+ where_clause=and_(AssetModel.uri == uri, AssetModel.active.has()),
+ session=session,
+ )
+
+
[email protected]("/by-asset-name")
+def get_asset_event_by_name(
+ name: Annotated[str, Query(description="The name of the Asset")],
+ session: SessionDep,
+) -> AssetEventCollectionResponse:
+ return _get_asset_events_through_sql_clauses(
+ join_clause=AssetEvent.asset,
+ where_clause=and_(AssetModel.uri == name, AssetModel.active.has()),
+ session=session,
+ )
Review Comment:
Can we coagulate these 3 into 1: `by-asset-name-or-uri`? Lesser apis to
manage :)
##########
airflow/api_fastapi/execution_api/datamodels/asset.py:
##########
@@ -36,7 +41,48 @@ class AssetAliasResponse(BaseModel):
group: str
-class AssetProfile(StrictBaseModel):
+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
+ asset_id: int
+ uri: str | None = Field(alias="uri", default=None)
+ name: str | None = Field(alias="name", default=None)
+ group: str | None = Field(alias="group", default=None)
+ extra: dict | None = None
+ source_task_id: str | None = None
+ source_dag_id: str | None = None
+ source_run_id: str | None = None
+ source_map_index: int
+ created_dagruns: list[DagRunAssetReference]
+ timestamp: datetime
+
+ @field_validator("extra", mode="after")
+ @classmethod
+ def redact_extra(cls, v: dict):
+ return redact(v)
+
+
+class AssetEventCollectionResponse(BaseModel):
+ """Collection of AssetEventResponse."""
+
+ asset_events: list[AssetEventResponse]
Review Comment:
Lets create a seperate asset_event.py datamodel for asset events? It will
clutter this one
##########
task_sdk/src/airflow/sdk/api/client.py:
##########
@@ -331,6 +332,30 @@ def get(self, name: str | None = None, uri: str | None =
None) -> AssetResponse:
return AssetResponse.model_validate_json(resp.read())
+class AssetEventOperations:
+ __slots__ = ("client",)
+
+ def __init__(self, client: Client):
+ self.client = client
+
+ def get(
+ self, name: str | None = None, uri: str | None = None, alias_name: str
| None = None
+ ) -> AssetEventCollectionResponse:
+ """Get Asset value from the API server."""
Review Comment:
```suggestion
"""Get Asset event from the API server."""
```
##########
airflow/api_fastapi/execution_api/datamodels/asset.py:
##########
@@ -36,7 +41,48 @@ class AssetAliasResponse(BaseModel):
group: str
-class AssetProfile(StrictBaseModel):
+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
+ asset_id: int
+ uri: str | None = Field(alias="uri", default=None)
+ name: str | None = Field(alias="name", default=None)
+ group: str | None = Field(alias="group", default=None)
+ extra: dict | None = None
+ source_task_id: str | None = None
+ source_dag_id: str | None = None
+ source_run_id: str | None = None
+ source_map_index: int
+ created_dagruns: list[DagRunAssetReference]
+ timestamp: datetime
+
+ @field_validator("extra", mode="after")
+ @classmethod
+ def redact_extra(cls, v: dict):
+ return redact(v)
Review Comment:
Nice :)
##########
task_sdk/src/airflow/sdk/api/datamodels/_generated.py:
##########
@@ -18,14 +14,19 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
+
+# generated by datamodel-codegen:
+# filename: http://0.0.0.0:9091/execution/openapi.json
+# version: 0.27.3
Review Comment:
Why this change?
--
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]