-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate public endpoint Get DAG Stats to FastAPI, with main resynced
- Loading branch information
1 parent
b5b00ef
commit 440180c
Showing
12 changed files
with
877 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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,80 @@ | ||
# 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, Query | ||
from sqlalchemy import func, select | ||
from sqlalchemy.orm import Session | ||
from typing_extensions import Annotated | ||
|
||
from airflow.api_fastapi.common.db.common import ( | ||
get_session, | ||
) | ||
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, | ||
) | ||
from airflow.models.dagrun import DagRun | ||
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, 422]), | ||
) | ||
async def get_dag_stats( | ||
session: Annotated[Session, Depends(get_session)], | ||
dag_ids: list[str] = Query(None), | ||
) -> DagStatsCollectionResponse: | ||
"""Get Dag statistics.""" | ||
query = select( | ||
DagRun.dag_id, | ||
DagRun.state, | ||
func.count(DagRun.state), | ||
) | ||
if dag_ids: | ||
query = query.where(DagRun.dag_id.in_(dag_ids)) | ||
else: | ||
dag_ids = [] | ||
query = query.group_by(DagRun.dag_id, DagRun.state).order_by(DagRun.dag_id) | ||
query_result = session.execute(query) | ||
|
||
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 = [ | ||
{ | ||
"dag_id": dag_id, | ||
"stats": [ | ||
{ | ||
"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 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 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 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 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 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.