SavingFrame commented on code in PR #52748:
URL: https://github.com/apache/airflow/pull/52748#discussion_r2183426443


##########
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:
   Okay, it was a point i was doubt about. Now I always return datetime in UTC 
format. My assumption that we always have logical_date in the  UTC in any 
database, so we can use `date()` function and then convert it into UTC. The do 
the same with hourly granularity.
   
   Thank you a lot for reviewing. All comments are fixed



-- 
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]

Reply via email to