ashb commented on code in PR #22332:
URL: https://github.com/apache/airflow/pull/22332#discussion_r842068258


##########
airflow/timetables/events.py:
##########
@@ -0,0 +1,97 @@
+# 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 typing import Iterable, Optional
+
+import numpy as np
+import pendulum
+from pendulum import DateTime
+
+from airflow.timetables.base import DagRunInfo, DataInterval, TimeRestriction, 
Timetable
+
+
+class EventsTimetable(Timetable):
+    """
+    Timetable that schedules DAG runs at specific listed datetimes. Suitable 
for
+    predictable but truly irregular scheduling such as sporting events.
+
+    :param event_dates: List of datetimes for the DAG to run at
+    :param restrict_to_events: Whether manual runs should use the most recent 
event or
+    the current time
+    """
+
+    description = ""
+
+    def __init__(self, event_dates: Iterable[DateTime], restrict_to_events: 
bool = False, presorted=False):
+
+        self.event_dates = np.array(event_dates)
+        if not presorted:
+            # For long lists this could take a while, so only want to do it 
once
+            self.event_dates = np.sort(self.event_dates)
+        self.restrict_to_events = restrict_to_events
+        self.description = self.summary
+
+    @property
+    def summary(self) -> str:
+        return f"{len(self.event_dates)} Events"

Review Comment:
   I suspect this won't be that useful in the UI. Could you include a 
screenshot of the tooltip/column of the homepage with a DAG using this 
timetable?



##########
airflow/timetables/events.py:
##########
@@ -0,0 +1,97 @@
+# 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 typing import Iterable, Optional
+
+import numpy as np
+import pendulum
+from pendulum import DateTime
+
+from airflow.timetables.base import DagRunInfo, DataInterval, TimeRestriction, 
Timetable
+
+
+class EventsTimetable(Timetable):
+    """
+    Timetable that schedules DAG runs at specific listed datetimes. Suitable 
for
+    predictable but truly irregular scheduling such as sporting events.
+
+    :param event_dates: List of datetimes for the DAG to run at
+    :param restrict_to_events: Whether manual runs should use the most recent 
event or
+    the current time
+    """
+
+    description = ""
+
+    def __init__(self, event_dates: Iterable[DateTime], restrict_to_events: 
bool = False, presorted=False):
+
+        self.event_dates = np.array(event_dates)
+        if not presorted:
+            # For long lists this could take a while, so only want to do it 
once
+            self.event_dates = np.sort(self.event_dates)

Review Comment:
   Using numpy here seems very heavy (and  although `np` is a core dep of 
Airflow right now we'd like to remove i -- isn't this the same as
   ```suggestion
           self.event_dates = list(event_dates)
           if not presorted:
               # For long lists this could take a while, so only want to do it 
once
               self.event_dates = sorted(self.event_dates)
   ```



##########
airflow/timetables/events.py:
##########
@@ -0,0 +1,97 @@
+# 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 typing import Iterable, Optional
+
+import numpy as np
+import pendulum
+from pendulum import DateTime
+
+from airflow.timetables.base import DagRunInfo, DataInterval, TimeRestriction, 
Timetable
+
+
+class EventsTimetable(Timetable):
+    """
+    Timetable that schedules DAG runs at specific listed datetimes. Suitable 
for
+    predictable but truly irregular scheduling such as sporting events.
+
+    :param event_dates: List of datetimes for the DAG to run at
+    :param restrict_to_events: Whether manual runs should use the most recent 
event or
+    the current time
+    """
+
+    description = ""
+
+    def __init__(self, event_dates: Iterable[DateTime], restrict_to_events: 
bool = False, presorted=False):
+
+        self.event_dates = np.array(event_dates)
+        if not presorted:
+            # For long lists this could take a while, so only want to do it 
once
+            self.event_dates = np.sort(self.event_dates)
+        self.restrict_to_events = restrict_to_events
+        self.description = self.summary
+
+    @property
+    def summary(self) -> str:
+        return f"{len(self.event_dates)} Events"
+
+    def __repr__(self):
+        return self.summary
+
+    def next_dagrun_info(
+        self,
+        *,
+        last_automated_data_interval: Optional[DataInterval],
+        restriction: TimeRestriction,
+    ) -> Optional[DagRunInfo]:
+        if last_automated_data_interval is None:
+            next_event = self.event_dates.min()
+        else:
+            future_dates = self.event_dates[self.event_dates > 
last_automated_data_interval.end]
+            if len(future_dates) > 0:
+                next_event = future_dates.min()
+            else:
+                return None

Review Comment:
   ```suggestion
               future_dates = itertools.dropwhile(lambda when: when < 
last_automated_data_interval.end, self.event_dates)
               next_event = next(future_dates, None)
               if next_event is None:
                   return None



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