Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AIP-84: Migrating GET Dataset events for DAG runs api to fastAPI #43874

Merged
merged 29 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
cf5218c
AIP-84: Migrating GET Assets to fastAPI
amoghrajesh Nov 6, 2024
5a49280
matching response to legacy
amoghrajesh Nov 6, 2024
962572b
Adding unit tests - part 1
amoghrajesh Nov 8, 2024
428cb6c
Update airflow/api_fastapi/common/parameters.py
amoghrajesh Nov 8, 2024
a78d3cb
fixing the dag_ids filter
amoghrajesh Nov 8, 2024
882d20c
fixing the dag_ids filter
amoghrajesh Nov 8, 2024
25bb08e
Adding unit tests - part 2
amoghrajesh Nov 8, 2024
658479d
Merge branch 'main' into AIP84-get-asset-to-fastapi
amoghrajesh Nov 8, 2024
fa0cd23
fixing unit tests & updating parameter type
amoghrajesh Nov 8, 2024
dd791c2
review comments pierre
amoghrajesh Nov 8, 2024
06fa0a7
fixing last commit
amoghrajesh Nov 8, 2024
3bd803b
Merge branch 'main' into AIP84-get-asset-to-fastapi
amoghrajesh Nov 8, 2024
fc29d7d
Merge branch 'main' into AIP84-get-asset-to-fastapi
amoghrajesh Nov 8, 2024
7a97220
fixing unit tests
amoghrajesh Nov 9, 2024
df1ff8e
AIP-84: Migrating GET Dataset events for DAG runs to fastAPI
amoghrajesh Nov 11, 2024
6c80ced
adding test cases
amoghrajesh Nov 11, 2024
e344263
adding test cases
amoghrajesh Nov 11, 2024
0df9ebd
Merge branch 'main' into AIP84-get-asset-to-fastapi
amoghrajesh Nov 12, 2024
5fb8bc1
Merge branch 'main' into AIP84-get-asset-to-fastapi
amoghrajesh Nov 12, 2024
6426b0b
Merge branch 'AIP84-get-asset-to-fastapi' into AIP84-get-dataset-even…
amoghrajesh Nov 12, 2024
80c4da0
Merge branch 'main' into AIP84-get-dataset-events-dagrun
amoghrajesh Nov 13, 2024
e7b96aa
review comments pierre
amoghrajesh Nov 13, 2024
c4d7dfc
fixing unit tests
amoghrajesh Nov 13, 2024
e269dd3
Merge branch 'main' into AIP84-get-dataset-events-dagrun
amoghrajesh Nov 14, 2024
0da44d0
review comments pierre
amoghrajesh Nov 14, 2024
75a387e
Merge branch 'main' into AIP84-get-dataset-events-dagrun
amoghrajesh Nov 14, 2024
f93032c
review comments and fixing a test
amoghrajesh Nov 14, 2024
13dff1c
Merge branch 'main' into AIP84-get-dataset-events-dagrun
amoghrajesh Nov 15, 2024
6ff2fdd
review comments on ut
amoghrajesh Nov 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions airflow/api_connexion/endpoints/dag_run_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def get_dag_run(
raise BadRequest("DAGRunSchema error", detail=str(e))


@mark_fastapi_migration_done
@security.requires_access_dag("GET", DagAccessEntity.RUN)
@security.requires_access_asset("GET")
@provide_session
Expand Down
44 changes: 43 additions & 1 deletion airflow/api_fastapi/core_api/datamodels/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from datetime import datetime

from pydantic import BaseModel
from pydantic import BaseModel, Field, model_validator


class DagScheduleAssetReference(BaseModel):
Expand Down Expand Up @@ -64,3 +64,45 @@ class AssetCollectionResponse(BaseModel):

assets: list[AssetResponse]
total_entries: int


class DagRunAssetReference(BaseModel):
"""Serializable version of the DagRunAssetReference ORM SqlAlchemyModel."""
amoghrajesh marked this conversation as resolved.
Show resolved Hide resolved

run_id: str
dag_id: str
execution_date: datetime = Field(alias="logical_date")
start_date: datetime
end_date: datetime | None = None
state: str
data_interval_start: datetime
data_interval_end: datetime


class AssetEventResponse(BaseModel):
"""Asset event serializer for responses."""

id: int
asset_id: int
asset_uri: str
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

@model_validator(mode="before")
def rename_uri_to_asset_uri(cls, values):
"""Rename 'uri' to 'asset_uri' during serialization to match legacy response."""
if hasattr(values, "uri") and values.uri:
values.asset_uri = values.uri
return values
amoghrajesh marked this conversation as resolved.
Show resolved Hide resolved


class AssetEventCollectionResponse(BaseModel):
"""Asset collection response."""
amoghrajesh marked this conversation as resolved.
Show resolved Hide resolved

asset_events: list[AssetEventResponse]
total_entries: int
165 changes: 165 additions & 0 deletions airflow/api_fastapi/core_api/openapi/v1-generated.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1434,6 +1434,58 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/HTTPValidationError'
/public/dags/{dag_id}/dagRuns/{dag_run_id}/upstreamAssetEvents:
get:
tags:
- DagRun
summary: Get Upstream Asset Events
description: If dag run is asset-triggered, return the asset events that triggered
it.
operationId: get_upstream_asset_events
parameters:
- name: dag_id
in: path
required: true
schema:
type: string
title: Dag Id
- name: dag_run_id
in: path
required: true
schema:
type: string
title: Dag Run Id
responses:
'200':
description: Successful Response
content:
application/json:
schema:
$ref: '#/components/schemas/AssetEventCollectionResponse'
'401':
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPExceptionResponse'
description: Unauthorized
'403':
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPExceptionResponse'
description: Forbidden
'404':
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPExceptionResponse'
description: Not Found
'422':
description: Validation Error
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPValidationError'
/public/dagSources/{file_token}:
get:
tags:
Expand Down Expand Up @@ -3400,6 +3452,75 @@ components:
- total_entries
title: AssetCollectionResponse
description: Asset collection response.
AssetEventCollectionResponse:
properties:
asset_events:
items:
$ref: '#/components/schemas/AssetEventResponse'
type: array
title: Asset Events
total_entries:
type: integer
title: Total Entries
type: object
required:
- asset_events
- total_entries
title: AssetEventCollectionResponse
description: Asset collection response.
AssetEventResponse:
properties:
id:
type: integer
title: Id
asset_id:
type: integer
title: Asset Id
asset_uri:
type: string
title: Asset Uri
extra:
anyOf:
- type: object
- type: 'null'
title: Extra
source_task_id:
anyOf:
- type: string
- type: 'null'
title: Source Task Id
source_dag_id:
anyOf:
- type: string
- type: 'null'
title: Source Dag Id
source_run_id:
anyOf:
- type: string
- type: 'null'
title: Source Run Id
source_map_index:
type: integer
title: Source Map Index
created_dagruns:
items:
$ref: '#/components/schemas/DagRunAssetReference'
type: array
title: Created Dagruns
timestamp:
type: string
format: date-time
title: Timestamp
type: object
required:
- id
- asset_id
- asset_uri
- source_map_index
- created_dagruns
- timestamp
title: AssetEventResponse
description: Asset event serializer for responses.
AssetResponse:
properties:
id:
Expand Down Expand Up @@ -4385,6 +4506,50 @@ components:
- latest_dag_processor_heartbeat
title: DagProcessorInfoSchema
description: Schema for DagProcessor info.
DagRunAssetReference:
properties:
run_id:
type: string
title: Run Id
dag_id:
type: string
title: Dag Id
logical_date:
type: string
format: date-time
title: Logical Date
start_date:
type: string
format: date-time
title: Start Date
end_date:
anyOf:
- type: string
format: date-time
- type: 'null'
title: End Date
state:
type: string
title: State
data_interval_start:
type: string
format: date-time
title: Data Interval Start
data_interval_end:
type: string
format: date-time
title: Data Interval End
type: object
required:
- run_id
- dag_id
- logical_date
- start_date
- state
- data_interval_start
- data_interval_end
title: DagRunAssetReference
description: Serializable version of the DagRunAssetReference ORM SqlAlchemyModel.
DagRunState:
type: string
enum:
Expand Down
35 changes: 35 additions & 0 deletions airflow/api_fastapi/core_api/routes/public/dag_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
)
from airflow.api_fastapi.common.db.common import get_session
from airflow.api_fastapi.common.router import AirflowRouter
from airflow.api_fastapi.core_api.datamodels.assets import AssetEventCollectionResponse, AssetEventResponse
from airflow.api_fastapi.core_api.datamodels.dag_run import (
DAGRunPatchBody,
DAGRunPatchStates,
Expand Down Expand Up @@ -148,3 +149,37 @@ def patch_dag_run(
dag_run = session.get(DagRun, dag_run.id)

return DAGRunResponse.model_validate(dag_run, from_attributes=True)


@dag_run_router.get(
"/{dag_run_id}/upstreamAssetEvents",
responses=create_openapi_http_exception_doc(
[
status.HTTP_401_UNAUTHORIZED,
status.HTTP_403_FORBIDDEN,
amoghrajesh marked this conversation as resolved.
Show resolved Hide resolved
status.HTTP_404_NOT_FOUND,
]
),
)
def get_upstream_asset_events(
dag_id: str, dag_run_id: str, session: Annotated[Session, Depends(get_session)]
) -> AssetEventCollectionResponse:
"""If dag run is asset-triggered, return the asset events that triggered it."""
dag_run: DagRun | None = session.scalar(
select(DagRun).where(
DagRun.dag_id == dag_id,
DagRun.run_id == dag_run_id,
)
)
if dag_run is None:
raise HTTPException(
status.HTTP_404_NOT_FOUND,
f"The DagRun with dag_id: `{dag_id}` and run_id: `{dag_run_id}` was not found",
)
events = dag_run.consumed_asset_events
return AssetEventCollectionResponse(
asset_events=[
AssetEventResponse.model_validate(asset_event, from_attributes=True) for asset_event in events
],
total_entries=len(events),
)
22 changes: 22 additions & 0 deletions airflow/ui/openapi-gen/queries/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,28 @@ export const UseDagRunServiceGetDagRunKeyFn = (
},
queryKey?: Array<unknown>,
) => [useDagRunServiceGetDagRunKey, ...(queryKey ?? [{ dagId, dagRunId }])];
export type DagRunServiceGetUpstreamAssetEventsDefaultResponse = Awaited<
ReturnType<typeof DagRunService.getUpstreamAssetEvents>
>;
export type DagRunServiceGetUpstreamAssetEventsQueryResult<
TData = DagRunServiceGetUpstreamAssetEventsDefaultResponse,
TError = unknown,
> = UseQueryResult<TData, TError>;
export const useDagRunServiceGetUpstreamAssetEventsKey =
"DagRunServiceGetUpstreamAssetEvents";
export const UseDagRunServiceGetUpstreamAssetEventsKeyFn = (
{
dagId,
dagRunId,
}: {
dagId: string;
dagRunId: string;
},
queryKey?: Array<unknown>,
) => [
useDagRunServiceGetUpstreamAssetEventsKey,
...(queryKey ?? [{ dagId, dagRunId }]),
];
export type DagSourceServiceGetDagSourceDefaultResponse = Awaited<
ReturnType<typeof DagSourceService.getDagSource>
>;
Expand Down
26 changes: 26 additions & 0 deletions airflow/ui/openapi-gen/queries/prefetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,32 @@ export const prefetchUseDagRunServiceGetDagRun = (
queryKey: Common.UseDagRunServiceGetDagRunKeyFn({ dagId, dagRunId }),
queryFn: () => DagRunService.getDagRun({ dagId, dagRunId }),
});
/**
* Get Upstream Asset Events
* If dag run is asset-triggered, return the asset events that triggered it.
* @param data The data for the request.
* @param data.dagId
* @param data.dagRunId
* @returns AssetEventCollectionResponse Successful Response
* @throws ApiError
*/
export const prefetchUseDagRunServiceGetUpstreamAssetEvents = (
queryClient: QueryClient,
{
dagId,
dagRunId,
}: {
dagId: string;
dagRunId: string;
},
) =>
queryClient.prefetchQuery({
queryKey: Common.UseDagRunServiceGetUpstreamAssetEventsKeyFn({
dagId,
dagRunId,
}),
queryFn: () => DagRunService.getUpstreamAssetEvents({ dagId, dagRunId }),
});
/**
* Get Dag Source
* Get source code using file token.
Expand Down
33 changes: 33 additions & 0 deletions airflow/ui/openapi-gen/queries/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,39 @@ export const useDagRunServiceGetDagRun = <
queryFn: () => DagRunService.getDagRun({ dagId, dagRunId }) as TData,
...options,
});
/**
* Get Upstream Asset Events
* If dag run is asset-triggered, return the asset events that triggered it.
* @param data The data for the request.
* @param data.dagId
* @param data.dagRunId
* @returns AssetEventCollectionResponse Successful Response
* @throws ApiError
*/
export const useDagRunServiceGetUpstreamAssetEvents = <
TData = Common.DagRunServiceGetUpstreamAssetEventsDefaultResponse,
TError = unknown,
TQueryKey extends Array<unknown> = unknown[],
>(
{
dagId,
dagRunId,
}: {
dagId: string;
dagRunId: string;
},
queryKey?: TQueryKey,
options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">,
) =>
useQuery<TData, TError>({
queryKey: Common.UseDagRunServiceGetUpstreamAssetEventsKeyFn(
{ dagId, dagRunId },
queryKey,
),
queryFn: () =>
DagRunService.getUpstreamAssetEvents({ dagId, dagRunId }) as TData,
...options,
});
/**
* Get Dag Source
* Get source code using file token.
Expand Down
Loading