-
Notifications
You must be signed in to change notification settings - Fork 17.3k
Migrate the public endpoint Get DAG Stats to FastAPI #43255
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
Merged
pierrejeambrun
merged 3 commits into
apache:main
from
omkar-foss:aip-84/dags/get-dag-stats
Nov 5, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # 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 sqlalchemy import func, select | ||
|
|
||
| from airflow.models.dagrun import DagRun | ||
|
|
||
| dagruns_select_with_state_count = ( | ||
| select( | ||
| DagRun.dag_id, | ||
| DagRun.state, | ||
| func.count(DagRun.state), | ||
| ) | ||
| .group_by(DagRun.dag_id, DagRun.state) | ||
| .order_by(DagRun.dag_id) | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| # 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 fastapi import Depends | ||
| from sqlalchemy.orm import Session | ||
| from typing_extensions import Annotated | ||
|
|
||
| from airflow.api_fastapi.common.db.common import ( | ||
| get_session, | ||
| paginated_select, | ||
| ) | ||
| from airflow.api_fastapi.common.db.dag_runs import dagruns_select_with_state_count | ||
| from airflow.api_fastapi.common.parameters import QueryDagIdsFilter | ||
| from airflow.api_fastapi.common.router import AirflowRouter | ||
| from airflow.api_fastapi.core_api.openapi.exceptions import create_openapi_http_exception_doc | ||
| from airflow.api_fastapi.core_api.serializers.dag_stats import ( | ||
| DagStatsCollectionResponse, | ||
| DagStatsResponse, | ||
| DagStatsStateResponse, | ||
| ) | ||
| from airflow.utils.state import DagRunState | ||
|
|
||
| dag_stats_router = AirflowRouter(tags=["DagStats"], prefix="/dagStats") | ||
|
|
||
|
|
||
| @dag_stats_router.get( | ||
| "/", | ||
| responses=create_openapi_http_exception_doc([400, 401, 403, 404]), | ||
| ) | ||
| async def get_dag_stats( | ||
| session: Annotated[Session, Depends(get_session)], | ||
| dag_ids: QueryDagIdsFilter, | ||
| ) -> DagStatsCollectionResponse: | ||
| """Get Dag statistics.""" | ||
| dagruns_select, _ = paginated_select( | ||
| base_select=dagruns_select_with_state_count, | ||
| filters=[dag_ids], | ||
| session=session, | ||
| return_total_entries=False, | ||
| ) | ||
| query_result = session.execute(dagruns_select) | ||
|
|
||
| result_dag_ids = [] | ||
| dag_state_data = {} | ||
| for dag_id, state, count in query_result: | ||
| dag_state_data[(dag_id, state)] = count | ||
| if dag_id not in result_dag_ids: | ||
| result_dag_ids.append(dag_id) | ||
|
|
||
| dags = [ | ||
| DagStatsResponse( | ||
| dag_id=dag_id, | ||
| stats=[ | ||
| DagStatsStateResponse( | ||
| state=state, | ||
| count=dag_state_data.get((dag_id, state), 0), | ||
| ) | ||
| for state in DagRunState | ||
| ], | ||
| ) | ||
| for dag_id in result_dag_ids | ||
| ] | ||
| return DagStatsCollectionResponse(dags=dags, total_entries=len(dags)) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # 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 pydantic import BaseModel | ||
|
|
||
| from airflow.utils.state import DagRunState | ||
|
|
||
|
|
||
| class DagStatsStateResponse(BaseModel): | ||
| """DagStatsState serializer for responses.""" | ||
|
|
||
| state: DagRunState | ||
| count: int | ||
|
|
||
|
|
||
| class DagStatsResponse(BaseModel): | ||
| """DAG Stats serializer for responses.""" | ||
|
|
||
| dag_id: str | ||
| stats: list[DagStatsStateResponse] | ||
|
|
||
|
|
||
| class DagStatsCollectionResponse(BaseModel): | ||
| """DAG Stats Collection serializer for responses.""" | ||
|
|
||
| dags: list[DagStatsResponse] | ||
| total_entries: int |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.