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 one queued asset events for DAG to fastAPI #44013

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
e95d08b
AIP-84: Migrating GET queued asset events for DAG to fastAPI
amoghrajesh Nov 12, 2024
8243b5a
Merge branch 'main' into AIP84-get-all-queued-events-for-dag
amoghrajesh Nov 13, 2024
7943076
adding test cases
amoghrajesh Nov 13, 2024
2cd5dc1
Merge branch 'main' into AIP84-get-all-queued-events-for-dag
amoghrajesh Nov 13, 2024
f9de8e8
Merge branch 'main' into AIP84-get-all-queued-events-for-dag
amoghrajesh Nov 14, 2024
9f16613
AIP-84: Migrating GET one queued asset events for DAG to fastAPI
amoghrajesh Nov 14, 2024
2d1b0ae
adding unit tests
amoghrajesh Nov 14, 2024
14e187a
Merge branch 'main' into AIP84-get-all-queued-events-for-dag
amoghrajesh Nov 14, 2024
7606b78
Merge branch 'AIP84-get-all-queued-events-for-dag' into AIP84-get-one…
amoghrajesh Nov 14, 2024
123bb65
adding setup and teardown
amoghrajesh Nov 14, 2024
342a4af
Merge branch 'AIP84-get-all-queued-events-for-dag' into AIP84-get-one…
amoghrajesh Nov 14, 2024
01898bf
review comments part 1
amoghrajesh Nov 15, 2024
2a684f6
introducing _generate_queued_event_where_clause
amoghrajesh Nov 15, 2024
1c48a6f
changing to bad request
amoghrajesh Nov 15, 2024
9a7574c
adding paginated_select
amoghrajesh Nov 15, 2024
c1a7eed
moving _generate_queued_event_where_clause to assets.py
amoghrajesh Nov 15, 2024
8301a1b
moving datamodels to assets
amoghrajesh Nov 15, 2024
c593a3f
moving tests to assets
amoghrajesh Nov 15, 2024
4ee4dff
reuse fixtures from TestGetAssets and inherit classes to simplify
amoghrajesh Nov 15, 2024
0b4991e
Merge branch 'AIP84-get-all-queued-events-for-dag' into AIP84-get-one…
amoghrajesh Nov 15, 2024
c9dc30d
using _generate_queued_event_where_clause and paginated_select
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
2 changes: 2 additions & 0 deletions airflow/api_connexion/endpoints/asset_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ def _generate_queued_event_where_clause(
return where_clause


@mark_fastapi_migration_done
@security.requires_access_asset("GET")
@security.requires_access_dag("GET")
@provide_session
Expand Down Expand Up @@ -222,6 +223,7 @@ def delete_dag_asset_queued_event(
)


@mark_fastapi_migration_done
@security.requires_access_asset("GET")
@security.requires_access_dag("GET")
@provide_session
Expand Down
36 changes: 36 additions & 0 deletions airflow/api_fastapi/common/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 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 datetime import datetime

from fastapi import HTTPException, status
from pendulum.parsing import ParserError

from airflow.utils import timezone


def format_datetime(value: str) -> datetime:
"""
Format datetime objects.

If it can't be parsed, it returns an HTTP 400 exception.
"""
try:
return timezone.parse(value)
except (ParserError, TypeError) as err:
raise HTTPException(status.HTTP_400_BAD_REQUEST, detail=f"Incorrect datetime argument: {err}")
15 changes: 15 additions & 0 deletions airflow/api_fastapi/core_api/datamodels/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,18 @@ class AssetEventCollectionResponse(BaseModel):

asset_events: list[AssetEventResponse]
total_entries: int


class QueuedEventResponse(BaseModel):
"""Queued Event serializer for responses.."""

uri: str
dag_id: str
created_at: datetime


class QueuedEventCollectionResponse(BaseModel):
"""Queued Event Collection serializer for responses."""

queued_events: list[QueuedEventResponse]
total_entries: int
143 changes: 143 additions & 0 deletions airflow/api_fastapi/core_api/openapi/v1-generated.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1864,6 +1864,114 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/HTTPValidationError'
/public/dags/{dag_id}/assets/queuedEvent:
get:
tags:
- DAG
summary: Get Dag Asset Queued Events
description: Get queued asset events for a DAG.
operationId: get_dag_asset_queued_events
parameters:
- name: dag_id
in: path
required: true
schema:
type: string
title: Dag Id
- name: before
in: query
required: false
schema:
type: string
title: Before
responses:
'200':
description: Successful Response
content:
application/json:
schema:
$ref: '#/components/schemas/QueuedEventCollectionResponse'
'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/dags/{dag_id}/assets/queuedEvent/{uri}:
get:
tags:
- DAG
summary: Get Dag Asset Queued Event
description: Get a queued asset event for a DAG.
operationId: get_dag_asset_queued_event
parameters:
- name: dag_id
in: path
required: true
schema:
type: string
title: Dag Id
- name: uri
in: path
required: true
schema:
type: string
title: Uri
- name: before
in: query
required: false
schema:
type: string
title: Before
responses:
'200':
description: Successful Response
content:
application/json:
schema:
$ref: '#/components/schemas/QueuedEventResponse'
'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/eventLogs/{event_log_id}:
get:
tags:
Expand Down Expand Up @@ -5473,6 +5581,41 @@ components:
- version
title: ProviderResponse
description: Provider serializer for responses.
QueuedEventCollectionResponse:
properties:
queued_events:
items:
$ref: '#/components/schemas/QueuedEventResponse'
type: array
title: Queued Events
total_entries:
type: integer
title: Total Entries
type: object
required:
- queued_events
- total_entries
title: QueuedEventCollectionResponse
description: Queued Event Collection serializer for responses.
QueuedEventResponse:
properties:
uri:
type: string
title: Uri
dag_id:
type: string
title: Dag Id
created_at:
type: string
format: date-time
title: Created At
type: object
required:
- uri
- dag_id
- created_at
title: QueuedEventResponse
description: Queued Event serializer for responses..
ReprocessBehavior:
type: string
enum:
Expand Down
24 changes: 23 additions & 1 deletion airflow/api_fastapi/core_api/routes/public/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,40 @@
SortParam,
)
from airflow.api_fastapi.common.router import AirflowRouter
from airflow.api_fastapi.common.utils import format_datetime
from airflow.api_fastapi.core_api.datamodels.assets import (
AssetCollectionResponse,
AssetEventCollectionResponse,
AssetEventResponse,
AssetResponse,
)
from airflow.api_fastapi.core_api.openapi.exceptions import create_openapi_http_exception_doc
from airflow.models.asset import AssetEvent, AssetModel
from airflow.models.asset import AssetDagRunQueue, AssetEvent, AssetModel

assets_router = AirflowRouter(tags=["Asset"], prefix="/assets")


def _generate_queued_event_where_clause(
*,
dag_id: str | None = None,
uri: str | None = None,
before: str | None = None,
) -> list:
"""Get AssetDagRunQueue where clause."""
where_clause = []
if dag_id is not None:
where_clause.append(AssetDagRunQueue.target_dag_id == dag_id)
if uri is not None:
where_clause.append(
AssetDagRunQueue.asset_id.in_(
select(AssetModel.id).where(AssetModel.uri == uri),
),
)
if before is not None:
where_clause.append(AssetDagRunQueue.created_at < format_datetime(before))
return where_clause


@assets_router.get(
"/",
responses=create_openapi_http_exception_doc([401, 403, 404]),
Expand Down
82 changes: 82 additions & 0 deletions airflow/api_fastapi/core_api/routes/public/dags.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
SortParam,
)
from airflow.api_fastapi.common.router import AirflowRouter
from airflow.api_fastapi.core_api.datamodels.assets import QueuedEventCollectionResponse, QueuedEventResponse
from airflow.api_fastapi.core_api.datamodels.dags import (
DAGCollectionResponse,
DAGDetailsResponse,
Expand All @@ -52,8 +53,10 @@
DAGTagCollectionResponse,
)
from airflow.api_fastapi.core_api.openapi.exceptions import create_openapi_http_exception_doc
from airflow.api_fastapi.core_api.routes.public.assets import _generate_queued_event_where_clause
from airflow.exceptions import AirflowException, DagNotFound
from airflow.models import DAG, DagModel, DagTag
from airflow.models.asset import AssetDagRunQueue, AssetModel

dags_router = AirflowRouter(tags=["DAG"], prefix="/dags")

Expand Down Expand Up @@ -303,3 +306,82 @@ def delete_dag(
status.HTTP_409_CONFLICT, f"Task instances of dag with id: '{dag_id}' are still running"
)
return Response(status_code=status.HTTP_204_NO_CONTENT)


@dags_router.get(
"/{dag_id}/assets/queuedEvent",
responses=create_openapi_http_exception_doc(
[
status.HTTP_404_NOT_FOUND,
]
),
)
def get_dag_asset_queued_events(
dag_id: str,
session: Annotated[Session, Depends(get_session)],
before: str = Query(None),
) -> QueuedEventCollectionResponse:
"""Get queued asset events for a DAG."""
where_clause = _generate_queued_event_where_clause(dag_id=dag_id, before=before)
query = (
select(AssetDagRunQueue, AssetModel.uri)
.join(AssetModel, AssetDagRunQueue.asset_id == AssetModel.id)
.where(*where_clause)
)

dag_asset_queued_events_select, total_entries = paginated_select(
query,
[],
)
adrqs = session.execute(dag_asset_queued_events_select).all()

if not adrqs:
raise HTTPException(status.HTTP_404_NOT_FOUND, f"Queue event with dag_id: `{dag_id}` was not found")

queued_events = [
QueuedEventResponse(created_at=adrq.created_at, dag_id=adrq.target_dag_id, uri=uri)
for adrq, uri in adrqs
]

return QueuedEventCollectionResponse(
queued_events=[
QueuedEventResponse.model_validate(queued_event, from_attributes=True)
for queued_event in queued_events
],
total_entries=total_entries,
)


@dags_router.get(
"/{dag_id}/assets/queuedEvent/{uri:path}",
responses=create_openapi_http_exception_doc(
[
status.HTTP_401_UNAUTHORIZED,
status.HTTP_403_FORBIDDEN,
status.HTTP_404_NOT_FOUND,
]
),
)
def get_dag_asset_queued_event(
dag_id: str,
uri: str,
session: Annotated[Session, Depends(get_session)],
before: str = Query(None),
) -> QueuedEventResponse:
"""Get a queued asset event for a DAG."""
where_clause = _generate_queued_event_where_clause(dag_id=dag_id, uri=uri, before=before)
query = (
select(AssetDagRunQueue)
.join(AssetModel, AssetDagRunQueue.asset_id == AssetModel.id)
.where(*where_clause)
)
adrq = session.scalar(query)
if not adrq:
raise HTTPException(
status.HTTP_404_NOT_FOUND,
f"Queue event with dag_id: `{dag_id}` and asset uri: `{uri}` was not found",
)

queued_event = QueuedEventResponse(created_at=adrq.created_at, dag_id=adrq.target_dag_id, uri=uri)

return QueuedEventResponse.model_validate(queued_event, from_attributes=True)
Loading