1919
2020from typing import Annotated
2121
22- from fastapi import HTTPException , Query , status
22+ from fastapi import Query , status
2323from sqlalchemy import and_ , select
2424
2525from airflow .api_fastapi .common .db .common import SessionDep
2626from airflow .api_fastapi .common .router import AirflowRouter
27- from airflow .api_fastapi .execution_api .datamodels .asset import AssetEventResponse
27+ from airflow .api_fastapi .execution_api .datamodels .asset import (
28+ AssetEventCollectionResponse ,
29+ )
2830from airflow .models .asset import AssetAliasModel , AssetEvent , AssetModel
2931
3032# TODO: Add dependency on JWT token
3638)
3739
3840
39- class LazyAssetEventSelectSequence (LazySelectSequence [AssetEvent ]):
40- """
41- List-like interface to lazily access AssetEvent rows.
42-
43- :meta private:
44- """
45-
46- @staticmethod
47- def _rebuild_select (stmt : TextClause ) -> Select :
48- return select (AssetEvent ).from_statement (stmt )
49-
50- @staticmethod
51- def _process_row (row : Row ) -> AssetEvent :
52- return row [0 ]
53-
54-
55- def _get_asset_event_through_sql_clause (
41+ def _get_asset_events_through_sql_clauses (
5642 * , join_clause , where_clause , session : SessionDep
57- ) -> AssetEventResponse :
58- asset_event = LazyAssetEventSelectSequence .from_select (
59- select (AssetEvent ).join (join_clause ).where (where_clause ),
60- order_by = [AssetEvent .timestamp ],
61- session = session ,
43+ ) -> AssetEventCollectionResponse :
44+ asset_events = session .scalars (
45+ select (AssetEvent ).join (join_clause ).where (where_clause ).order_by (AssetEvent .timestamp )
6246 )
63- _raise_if_not_found (asset_event = asset_event , msg = "Not found" )
64- return AssetEventResponse .model_validate (asset_event )
47+ return AssetEventCollectionResponse .model_validate ({"asset_events" : asset_events or []})
6548
6649
6750@router .get ("/by-asset-name-uri" )
6851def get_asset_event_by_asset_name_uri (
6952 name : Annotated [str , Query (description = "The name of the Asset" )],
7053 uri : Annotated [str , Query (description = "The URI of the Asset" )],
7154 session : SessionDep ,
72- ) -> AssetEventResponse :
73- return _get_asset_event_through_sql_clause (
55+ ) -> AssetEventCollectionResponse :
56+ return _get_asset_events_through_sql_clauses (
7457 join_clause = AssetEvent .asset ,
7558 where_clause = and_ (AssetModel .name == name , AssetModel .uri == uri ),
7659 session = session ,
@@ -81,8 +64,8 @@ def get_asset_event_by_asset_name_uri(
8164def get_asset_event_by_uri (
8265 uri : Annotated [str , Query (description = "The URI of the Asset" )],
8366 session : SessionDep ,
84- ) -> AssetEventResponse :
85- return _get_asset_event_through_sql_clause (
67+ ) -> AssetEventCollectionResponse :
68+ return _get_asset_events_through_sql_clauses (
8669 join_clause = AssetEvent .asset ,
8770 where_clause = and_ (AssetModel .uri == uri , AssetModel .active .has ()),
8871 session = session ,
@@ -93,8 +76,8 @@ def get_asset_event_by_uri(
9376def get_asset_event_by_name (
9477 name : Annotated [str , Query (description = "The name of the Asset" )],
9578 session : SessionDep ,
96- ) -> AssetEventResponse :
97- return _get_asset_event_through_sql_clause (
79+ ) -> AssetEventCollectionResponse :
80+ return _get_asset_events_through_sql_clauses (
9881 join_clause = AssetEvent .asset ,
9982 where_clause = and_ (AssetModel .uri == name , AssetModel .active .has ()),
10083 session = session ,
@@ -105,20 +88,9 @@ def get_asset_event_by_name(
10588def get_asset_event_by_alias_name (
10689 name : Annotated [str , Query (description = "The name of the Asset Alias" )],
10790 session : SessionDep ,
108- ) -> AssetEventResponse :
109- return _get_asset_event_through_sql_clause (
91+ ) -> AssetEventCollectionResponse :
92+ return _get_asset_events_through_sql_clauses (
11093 join_clause = AssetEvent .source_aliases ,
11194 where_clause = (AssetAliasModel .name == name ),
11295 session = session ,
11396 )
114-
115-
116- def _raise_if_not_found (asset_event , msg ):
117- if asset_event is None :
118- raise HTTPException (
119- status .HTTP_404_NOT_FOUND ,
120- detail = {
121- "reason" : "not_found" ,
122- "message" : msg ,
123- },
124- )
0 commit comments