amoghrajesh commented on code in PR #45960:
URL: https://github.com/apache/airflow/pull/45960#discussion_r1967155582


##########
tests/api_fastapi/execution_api/routes/test_asset_events.py:
##########
@@ -0,0 +1,99 @@
+# 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 pytest
+
+from airflow.models.asset import AssetActive, AssetEvent, AssetModel
+from airflow.utils import timezone
+
+DEFAULT_DATE = timezone.parse("2021-01-01T00:00:00")
+
+pytestmark = pytest.mark.db_test
+
+
+class TestGetAssetEventByAsset:
+    def test_get_by_name_uri(self, client, session):

Review Comment:
   Lets paramerterise this one for various scenarios of success and failure.



##########
task_sdk/src/airflow/sdk/definitions/asset/__init__.py:
##########
@@ -694,6 +695,39 @@ def as_expression(self) -> Any:
         return {"all": [o.as_expression() for o in self.objects]}
 
 
[email protected](kw_only=True)

Review Comment:
   Do we really need to have kw_only?



##########
airflow/api_fastapi/execution_api/routes/asset_events.py:
##########
@@ -0,0 +1,81 @@
+# 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_event 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")
+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:
+    if name and uri:
+        where_clause = and_(AssetModel.name == name, AssetModel.uri == uri)

Review Comment:
   Dont we need the `.active.has()` here?



##########
task_sdk/src/airflow/sdk/execution_time/comms.py:
##########
@@ -104,6 +105,28 @@ def from_asset_response(cls, asset_response: 
AssetResponse) -> AssetResult:
         return cls(**asset_response.model_dump(exclude_defaults=True), 
type="AssetResult")
 
 
+class AssetEventsResult(AssetEventsResponse):
+    """Response to GetAssetEvent request."""
+
+    type: Literal["AssetEventsResult"] = "AssetEventsResult"
+
+    @classmethod
+    def from_asset_events_response(cls, asset_events_response: 
AssetEventsResponse) -> AssetEventsResult:
+        """
+        Get AssetEventsResult from AssetEventsResponse.
+
+        AssetEventsResponse is autogenerated from the API schema, so we need 
to convert it to AssetEventsResponse
+        for communication between the Supervisor and the task process.
+        """
+        # Exclude defaults to avoid sending unnecessary data
+        # Pass the type as AssetResult explicitly so we can then call 
model_dump_json with exclude_unset=True

Review Comment:
   ```suggestion
           # Pass the type as AssetEventResult explicitly so we can then call 
model_dump_json with exclude_unset=True
   ```



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

Reply via email to