pierrejeambrun commented on code in PR #50060: URL: https://github.com/apache/airflow/pull/50060#discussion_r2075442768
########## airflow-core/src/airflow/api_fastapi/core_api/routes/public/assets.py: ########## @@ -385,16 +436,41 @@ def get_asset( session: SessionDep, ) -> AssetResponse: """Get an asset.""" + # Build a subquery to be used to retrieve the latest AssetEvent by matching timestamp + last_asset_event = ( + select(func.max(AssetEvent.timestamp)).where(AssetEvent.asset_id == asset_id).scalar_subquery() + ) + + # Now, find the latest AssetEvent details using the subquery from above + asset_event_rows = session.execute( + select(AssetEvent.asset_id, AssetEvent.id, AssetEvent.timestamp).where( + AssetEvent.asset_id == asset_id, AssetEvent.timestamp == last_asset_event + ) + ).one_or_none() + + # Retrieve the Asset; there should only be one for that asset_id asset = session.scalar( select(AssetModel) .where(AssetModel.id == asset_id) .options(joinedload(AssetModel.consuming_dags), joinedload(AssetModel.producing_tasks)) ) + last_asset_event_id = asset_event_rows[1] if asset_event_rows else None + last_asset_event_timestamp = asset_event_rows[2] if asset_event_rows else None + if asset is None: raise HTTPException(status.HTTP_404_NOT_FOUND, f"The Asset with ID: `{asset_id}` was not found") - return AssetResponse.model_validate(asset) + return AssetResponse.model_validate( + { + **asset.__dict__, + "aliases": asset.aliases, + "last_asset_event": { + "id": last_asset_event_id, + "timestamp": last_asset_event_timestamp, + }, + } + ) Review Comment: I'm not sure I see exactly the benefit of it but I would gladly review a follow up PR if you feel like this can be improved. -- 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: commits-unsubscr...@airflow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org