pierrejeambrun commented on code in PR #44624: URL: https://github.com/apache/airflow/pull/44624#discussion_r1871111707
########## airflow/api_fastapi/core_api/routes/ui/backfills.py: ########## @@ -0,0 +1,66 @@ +# 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 typing import Annotated + +from fastapi import Depends, status +from sqlalchemy import select +from sqlalchemy.ext.asyncio import AsyncSession + +from airflow.api_fastapi.common.db.common import get_async_session, paginated_select_async +from airflow.api_fastapi.common.parameters import QueryLimit, QueryOffset, SortParam +from airflow.api_fastapi.common.router import AirflowRouter +from airflow.api_fastapi.core_api.datamodels.backfills import BackfillCollectionResponse +from airflow.api_fastapi.core_api.openapi.exceptions import ( + create_openapi_http_exception_doc, +) +from airflow.models.backfill import Backfill + +backfills_router = AirflowRouter(tags=["Backfill"], prefix="/backfills") + + +@backfills_router.get( + path="", + responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND]), +) +async def list_backfills( + limit: QueryLimit, + offset: QueryOffset, + order_by: Annotated[ + SortParam, + Depends(SortParam(["id"], Backfill).dynamic_depends()), + ], + session: Annotated[AsyncSession, Depends(get_async_session)], + dag_id: str | None = None, +) -> BackfillCollectionResponse: Review Comment: Do we need a private UI endpoint for that, or should we complete / improve the public one ? I am asking because this endpoint is really similar to the public one, by adding `dag_id` and `active` filter we should pretty much get all that we want. I know that the idea is that if it's private we can do whatever we want without worrying about backward comp, but this feels like very generic / public feature. ########## airflow/api_fastapi/core_api/routes/ui/backfills.py: ########## @@ -0,0 +1,66 @@ +# 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 typing import Annotated + +from fastapi import Depends, status +from sqlalchemy import select +from sqlalchemy.ext.asyncio import AsyncSession + +from airflow.api_fastapi.common.db.common import get_async_session, paginated_select_async +from airflow.api_fastapi.common.parameters import QueryLimit, QueryOffset, SortParam +from airflow.api_fastapi.common.router import AirflowRouter +from airflow.api_fastapi.core_api.datamodels.backfills import BackfillCollectionResponse +from airflow.api_fastapi.core_api.openapi.exceptions import ( + create_openapi_http_exception_doc, +) +from airflow.models.backfill import Backfill + +backfills_router = AirflowRouter(tags=["Backfill"], prefix="/backfills") + + +@backfills_router.get( + path="", + responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND]), +) +async def list_backfills( + limit: QueryLimit, + offset: QueryOffset, + order_by: Annotated[ + SortParam, + Depends(SortParam(["id"], Backfill).dynamic_depends()), + ], + session: Annotated[AsyncSession, Depends(get_async_session)], + dag_id: str | None = None, +) -> BackfillCollectionResponse: + conditions = [Backfill.completed_at.is_(None)] # Active dag + if dag_id: + conditions.append(Backfill.dag_id == dag_id) + + select_stmt, total_entries = await paginated_select_async( + statement=select(Backfill).where(*conditions), Review Comment: This would be removed. ########## tests/api_fastapi/core_api/routes/ui/test_backfills.py: ########## @@ -0,0 +1,126 @@ +# 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 unittest import mock + +import pendulum +import pytest + +from airflow.models import DagModel +from airflow.models.backfill import Backfill +from airflow.utils import timezone +from airflow.utils.session import provide_session + +from tests_common.test_utils.db import ( + clear_db_backfills, + clear_db_dags, + clear_db_runs, + clear_db_serialized_dags, +) + +pytestmark = pytest.mark.db_test + + +DAG_ID = "test_dag" +TASK_ID = "op1" +DAG2_ID = "test_dag2" +DAG3_ID = "test_dag3" + + +def _clean_db(): + clear_db_backfills() + clear_db_runs() + clear_db_dags() + clear_db_serialized_dags() + + [email protected](autouse=True) +def clean_db(): + _clean_db() + yield + _clean_db() + + +def to_iso(val): + return pendulum.instance(val).to_iso8601_string() + + +class TestBackfillEndpoint: + @provide_session + def _create_dag_models(self, *, count=3, dag_id_prefix="TEST_DAG", is_paused=False, session=None): + dags = [] + for num in range(1, count + 1): + dag_model = DagModel( + dag_id=f"{dag_id_prefix}_{num}", + fileloc=f"/tmp/dag_{num}.py", + is_active=True, + timetable_summary="0 0 * * *", + is_paused=is_paused, + ) + session.add(dag_model) + dags.append(dag_model) + return dags + + +class TestListBackfills(TestBackfillEndpoint): + def test_list_backfill(self, test_client, session): + dags = self._create_dag_models() + from_date = timezone.utcnow() + to_date = timezone.utcnow() + backfill0 = Backfill( + dag_id=dags[0].dag_id, from_date=from_date, to_date=to_date, completed_at=timezone.utcnow() + ) + backfill1 = Backfill(dag_id=dags[1].dag_id, from_date=from_date, to_date=to_date) + backfill2 = Backfill(dag_id=dags[2].dag_id, from_date=from_date, to_date=to_date, is_paused=True) + backfills = [backfill0, backfill1, backfill2] + for backfill in backfills: + session.add(backfill) Review Comment: `session.add_all([...])` ########## tests/api_fastapi/core_api/routes/ui/test_backfills.py: ########## @@ -0,0 +1,126 @@ +# 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 unittest import mock + +import pendulum +import pytest + +from airflow.models import DagModel +from airflow.models.backfill import Backfill +from airflow.utils import timezone +from airflow.utils.session import provide_session + +from tests_common.test_utils.db import ( + clear_db_backfills, + clear_db_dags, + clear_db_runs, + clear_db_serialized_dags, +) + +pytestmark = pytest.mark.db_test + + +DAG_ID = "test_dag" +TASK_ID = "op1" +DAG2_ID = "test_dag2" +DAG3_ID = "test_dag3" + + +def _clean_db(): + clear_db_backfills() + clear_db_runs() + clear_db_dags() + clear_db_serialized_dags() + + [email protected](autouse=True) +def clean_db(): + _clean_db() + yield + _clean_db() + + +def to_iso(val): + return pendulum.instance(val).to_iso8601_string() Review Comment: We have now `from_datetime_to_zulu` and `from_datetime_to_zulu_without_ms` to achieve the same, with is shared test code. ########## tests/api_fastapi/core_api/routes/ui/test_backfills.py: ########## @@ -0,0 +1,126 @@ +# 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 unittest import mock + +import pendulum +import pytest + +from airflow.models import DagModel +from airflow.models.backfill import Backfill +from airflow.utils import timezone +from airflow.utils.session import provide_session + +from tests_common.test_utils.db import ( + clear_db_backfills, + clear_db_dags, + clear_db_runs, + clear_db_serialized_dags, +) + +pytestmark = pytest.mark.db_test + + +DAG_ID = "test_dag" +TASK_ID = "op1" +DAG2_ID = "test_dag2" +DAG3_ID = "test_dag3" + + +def _clean_db(): + clear_db_backfills() + clear_db_runs() + clear_db_dags() + clear_db_serialized_dags() + + [email protected](autouse=True) +def clean_db(): + _clean_db() + yield + _clean_db() + + +def to_iso(val): + return pendulum.instance(val).to_iso8601_string() + + +class TestBackfillEndpoint: + @provide_session + def _create_dag_models(self, *, count=3, dag_id_prefix="TEST_DAG", is_paused=False, session=None): + dags = [] + for num in range(1, count + 1): + dag_model = DagModel( + dag_id=f"{dag_id_prefix}_{num}", + fileloc=f"/tmp/dag_{num}.py", + is_active=True, + timetable_summary="0 0 * * *", + is_paused=is_paused, + ) + session.add(dag_model) + dags.append(dag_model) + return dags + + +class TestListBackfills(TestBackfillEndpoint): + def test_list_backfill(self, test_client, session): + dags = self._create_dag_models() + from_date = timezone.utcnow() + to_date = timezone.utcnow() + backfill0 = Backfill( + dag_id=dags[0].dag_id, from_date=from_date, to_date=to_date, completed_at=timezone.utcnow() + ) + backfill1 = Backfill(dag_id=dags[1].dag_id, from_date=from_date, to_date=to_date) + backfill2 = Backfill(dag_id=dags[2].dag_id, from_date=from_date, to_date=to_date, is_paused=True) + backfills = [backfill0, backfill1, backfill2] + for backfill in backfills: + session.add(backfill) + session.commit() + response = test_client.get("/ui/backfills?dag_id=") Review Comment: We should both test that when `dag_id` is provided, or without `dag_id` provided we get what we want. i.e parametrize the test on the `params` and use `test_client.get("/ui/backfills", params=params)` (leave url / query param encoding to starlette test client, it's more readable on less prone to encoding error) ########## airflow/api_fastapi/core_api/routes/ui/backfills.py: ########## @@ -0,0 +1,66 @@ +# 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 typing import Annotated + +from fastapi import Depends, status +from sqlalchemy import select +from sqlalchemy.ext.asyncio import AsyncSession + +from airflow.api_fastapi.common.db.common import get_async_session, paginated_select_async +from airflow.api_fastapi.common.parameters import QueryLimit, QueryOffset, SortParam +from airflow.api_fastapi.common.router import AirflowRouter +from airflow.api_fastapi.core_api.datamodels.backfills import BackfillCollectionResponse +from airflow.api_fastapi.core_api.openapi.exceptions import ( + create_openapi_http_exception_doc, +) +from airflow.models.backfill import Backfill + +backfills_router = AirflowRouter(tags=["Backfill"], prefix="/backfills") + + +@backfills_router.get( + path="", + responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND]), +) +async def list_backfills( + limit: QueryLimit, + offset: QueryOffset, + order_by: Annotated[ + SortParam, + Depends(SortParam(["id"], Backfill).dynamic_depends()), + ], + session: Annotated[AsyncSession, Depends(get_async_session)], + dag_id: str | None = None, +) -> BackfillCollectionResponse: + conditions = [Backfill.completed_at.is_(None)] # Active dag + if dag_id: + conditions.append(Backfill.dag_id == dag_id) Review Comment: You should probably reuse the `parameters.py` system that we have, this way you do not have to do any filtering by hands. ########## airflow/api_fastapi/core_api/routes/ui/backfills.py: ########## @@ -0,0 +1,69 @@ +# 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 typing import Annotated + +from fastapi import Depends, status +from sqlalchemy import and_, select +from sqlalchemy.ext.asyncio import AsyncSession + +from airflow.api_fastapi.common.db.common import get_async_session, paginated_select_async +from airflow.api_fastapi.common.parameters import QueryLimit, QueryOffset, SortParam +from airflow.api_fastapi.common.router import AirflowRouter +from airflow.api_fastapi.core_api.datamodels.backfills import BackfillCollectionResponse +from airflow.api_fastapi.core_api.openapi.exceptions import ( + create_openapi_http_exception_doc, +) +from airflow.models.backfill import Backfill + +backfills_router = AirflowRouter(tags=["Backfill"], prefix="/backfills") + + +@backfills_router.get( + path="", + responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND]), +) +async def list_backfills( + limit: QueryLimit, + offset: QueryOffset, + order_by: Annotated[ + SortParam, + Depends(SortParam(["id"], Backfill).dynamic_depends()), + ], + session: Annotated[AsyncSession, Depends(get_async_session)], + dag_id: str | None = None, +) -> BackfillCollectionResponse: + print("dag_id") + conditions = [Backfill.completed_at.is_(None)] # Active dag Review Comment: Yes I think we can simply add a filter to retrieve all backfills or only `active` ones. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
