pierrejeambrun commented on code in PR #52748: URL: https://github.com/apache/airflow/pull/52748#discussion_r2182365615
########## airflow-core/src/airflow/api_fastapi/core_api/datamodels/ui/calendar.py: ########## @@ -0,0 +1,38 @@ +# 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 typing import Literal + +from pydantic import BaseModel + +from airflow.utils.state import DagRunState + + +class CalendarTimeRangeResult(BaseModel): + """Task Instance Summary model for the Grid UI.""" Review Comment: Wrong doc string ########## airflow-core/src/airflow/api_fastapi/core_api/routes/ui/calendar.py: ########## @@ -0,0 +1,82 @@ +# 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, Literal + +from fastapi import Depends + +from airflow.api_fastapi.auth.managers.models.resource_details import DagAccessEntity +from airflow.api_fastapi.common.dagbag import DagBagDep +from airflow.api_fastapi.common.db.common import SessionDep +from airflow.api_fastapi.common.parameters import RangeFilter, datetime_range_filter_factory +from airflow.api_fastapi.common.router import AirflowRouter +from airflow.api_fastapi.core_api.datamodels.ui.calendar import CalendarTimeRangeResult +from airflow.api_fastapi.core_api.security import requires_access_dag +from airflow.api_fastapi.core_api.services.ui.calendar import CalendarService +from airflow.models.dagrun import DagRun + +calendar_router = AirflowRouter(prefix="/calendar", tags=["Calendar"]) + + +@calendar_router.get( + "/{dag_id}", + dependencies=[ + Depends( + requires_access_dag( + method="GET", + access_entity=DagAccessEntity.TASK_INSTANCE, + ) + ), + Depends( + requires_access_dag( + method="GET", + access_entity=DagAccessEntity.RUN, + ) + ), + ], +) +def get_calendar( + dag_id: str, + session: SessionDep, + dag_bag: DagBagDep, + logical_date: Annotated[RangeFilter, Depends(datetime_range_filter_factory("logical_date", DagRun))], + granularity: Literal["hourly", "daily"] = "daily", +) -> list[CalendarTimeRangeResult]: + """ + Get calendar data for a DAG including historical and planned runs. + + Args: + dag_id: The DAG ID + session: Database session + dag_bag: DAG bag dependency + logical_date: Date range filter + granularity: Time granularity ("hourly" or "daily") + + Returns: + List of calendar time range results Review Comment: You can remove, this is already documented in swagger from the params ########## airflow-core/src/airflow/api_fastapi/core_api/datamodels/ui/calendar.py: ########## @@ -0,0 +1,38 @@ +# 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 typing import Literal + +from pydantic import BaseModel + +from airflow.utils.state import DagRunState + + +class CalendarTimeRangeResult(BaseModel): Review Comment: Can you follow the naming convention of other datamodles please. (Response vs Body) (Collection, with `total_entries`, even if this endpoint is not paginated) ########## airflow-core/src/airflow/api_fastapi/core_api/services/ui/calendar.py: ########## @@ -0,0 +1,321 @@ +# 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 + +import collections +import datetime +from collections.abc import Iterator +from typing import Literal, cast + +import sqlalchemy as sa +import structlog +from croniter.croniter import croniter +from pendulum import DateTime +from sqlalchemy.engine import Row +from sqlalchemy.orm import Session + +from airflow.api_fastapi.common.parameters import RangeFilter +from airflow.api_fastapi.core_api.datamodels.ui.calendar import CalendarTimeRangeResult +from airflow.models.dag import DAG +from airflow.models.dagrun import DagRun +from airflow.timetables._cron import CronMixin +from airflow.timetables.base import DataInterval, TimeRestriction +from airflow.timetables.simple import ContinuousTimetable +from airflow.utils import timezone + +log = structlog.get_logger(logger_name=__name__) + + +class CalendarService: + """Service class for calendar-related operations.""" + + MAX_PLANNED_RUNS: int = 2000 + + def get_calendar_data( + self, + dag_id: str, + session: Session, + dag: DAG, + logical_date: RangeFilter, + granularity: Literal["hourly", "daily"] = "daily", + ) -> list[CalendarTimeRangeResult]: + """ + Get calendar data for a DAG including historical and planned runs. + + Args: + dag_id: The DAG ID + session: Database session + dag: The DAG object + logical_date: Date range filter + granularity: Time granularity ("hourly" or "daily") + + Returns: + List of calendar time range results + """ + historical_data, raw_dag_states = self._get_historical_dag_runs( + dag_id, + session, + logical_date, + granularity, + ) + + planned_data = self._get_planned_dag_runs(dag, raw_dag_states, logical_date, granularity) + + return historical_data + planned_data + + def _get_historical_dag_runs( + self, + dag_id: str, + session: Session, + logical_date: RangeFilter, + granularity: Literal["hourly", "daily"], + ) -> tuple[list[CalendarTimeRangeResult], list[Row]]: + """Get historical DAG runs from the database.""" + dialect = session.bind.dialect.name + + time_expression = self._get_time_truncation_expression(DagRun.logical_date, granularity, dialect) + + select_stmt = ( + sa.select( + time_expression.label("datetime"), + DagRun.state, + sa.func.max(DagRun.data_interval_start).label("data_interval_start"), + sa.func.max(DagRun.data_interval_end).label("data_interval_end"), + sa.func.count("*").label("count"), + ) + .where(DagRun.dag_id == dag_id) + .group_by(time_expression, DagRun.state) + .order_by(time_expression.asc()) + ) + + select_stmt = logical_date.to_orm(select_stmt) + dag_states = session.execute(select_stmt).all() + + calendar_results = [ + CalendarTimeRangeResult( + # ds.datetime in sqlite and mysql is a string, in postgresql it is a datetime + datetime=ds.datetime.replace(tzinfo=None) Review Comment: ```suggestion datetime=ds.datetime.replace(tzinfo=None) ``` Why setting the tzinfo at None like this? (If there is a tzinfo this will actually be wrong, and the AIP should return tz aware datetime) ########## airflow-core/src/airflow/api_fastapi/core_api/datamodels/ui/calendar.py: ########## @@ -0,0 +1,38 @@ +# 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 typing import Literal + +from pydantic import BaseModel + +from airflow.utils.state import DagRunState + + +class CalendarTimeRangeResult(BaseModel): + """Task Instance Summary model for the Grid UI.""" + + datetime: datetime Review Comment: nit: I think we should keep 'date' even if its a datetime. (start_date, end_date are all called date but they are actually datetime), just for consistencey -- 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: commits-unsubscr...@airflow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org