rawwar commented on code in PR #50060:
URL: https://github.com/apache/airflow/pull/50060#discussion_r2074503359
##########
airflow-core/src/airflow/api_fastapi/core_api/routes/public/assets.py:
##########
@@ -135,20 +135,71 @@ def get_assets(
session: SessionDep,
) -> AssetCollectionResponse:
"""Get assets."""
+ # Build a query that will be used to retrieve the ID and timestamp of the
latest AssetEvent
+ last_asset_events = (
+ select(AssetEvent.asset_id,
func.max(AssetEvent.timestamp).label("last_timestamp"))
+ .group_by(AssetEvent.asset_id)
+ .subquery()
+ )
+
+ # First, we're pulling the Asset ID, AssetEvent ID, and AssetEvent
timestamp for the latest (last)
+ # AssetEvent. We'll eventually OUTER JOIN this to the AssetModel
+ asset_event_query = (
+ select(
+ AssetEvent.asset_id, # The ID of the Asset, which we'll need to
JOIN to the AssetModel
+ func.max(AssetEvent.id).label("last_asset_event_id"), # The ID of
the last AssetEvent
+ func.max(AssetEvent.timestamp).label("last_asset_event_timestamp"),
+ )
+ .join(
+ last_asset_events,
+ and_(
+ AssetEvent.asset_id == last_asset_events.c.asset_id,
+ AssetEvent.timestamp == last_asset_events.c.last_timestamp,
+ ),
+ )
+ .group_by(AssetEvent.asset_id)
+ .subquery()
+ )
+
+ assets_select_statement = select(
+ AssetModel,
+ asset_event_query.c.last_asset_event_id, # This should be the
AssetEvent.id
+ asset_event_query.c.last_asset_event_timestamp,
+ ).outerjoin(asset_event_query, AssetModel.id ==
asset_event_query.c.asset_id)
+
assets_select, total_entries = paginated_select(
- statement=select(AssetModel),
+ statement=assets_select_statement,
filters=[only_active, name_pattern, uri_pattern, dag_ids],
order_by=order_by,
offset=offset,
limit=limit,
session=session,
)
- assets = session.scalars(
+ assets_rows = session.execute(
assets_select.options(
- subqueryload(AssetModel.consuming_dags),
subqueryload(AssetModel.producing_tasks)
+ subqueryload(AssetModel.consuming_dags),
+ subqueryload(AssetModel.producing_tasks),
)
)
+
+ # Create an empty list to be returned
+ assets = []
+
+ for row in assets_rows:
+ asset, last_asset_event_id, last_asset_event_timestamp = row
Review Comment:
```suggestion
for asset, last_asset_event_id, last_asset_event_timestamp in
assets_rows:
```
--
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]