Skip to content

Commit ea6bfcc

Browse files
committed
Improve time trigger Python API [ch63893]
1 parent 960270c commit ea6bfcc

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

dataikuapi/dss/scenario.py

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from ..utils import DataikuException
44
from .discussion import DSSObjectDiscussions
55
from .utils import DSSTaggableObjectListItem
6+
from dateutil.tz import tzlocal
67

78
class DSSScenario(object):
89
"""
@@ -338,32 +339,59 @@ def raw_reporters(self):
338339

339340
def add_periodic_trigger(self, every_minutes=5):
340341
"""Adds a trigger that runs the scenario every X minutes"""
341-
trigger = {"active": True, "type": "temporal", "params": { "frequency": "Minutely", "count": every_minutes}}
342+
trigger = {"active": True, "type": "temporal", "params": { "frequency": "Minutely", "repeatFrequency": every_minutes }}
342343
self.raw_triggers.append(trigger)
343344

344-
def add_hourly_trigger(self, minute_of_hour=0):
345+
def add_hourly_trigger(self, minute_of_hour=0, year=None, month=None, day=None, starting_hour=0, repeat_every=1, timezone="SERVER"):
345346
"""Adds a trigger that runs the scenario each hour on a predefined minute"""
346-
trigger = {"active": True, "type": "temporal", "params": { "frequency": "Hourly", "minute": minute_of_hour}}
347+
starting_date =self.__get_starting_date__(year=year, month=month, day=day)
348+
trigger = {"active": True, "type": "temporal", "params": { "frequency": "Hourly", "hour": starting_hour, "minute": minute_of_hour,
349+
"startingFrom": starting_date, "repeatFrequency": repeat_every, "timezone": timezone }}
347350
self.raw_triggers.append(trigger)
348351

349-
def add_daily_trigger(self, hour=2, minute=0, days=None):
352+
def add_daily_trigger(self, hour=2, minute=0, days=None, year=None, month=None, day=None, repeat_every=1, timezone="SERVER"):
350353
"""
351354
Adds a trigger that runs the scenario each day on a predefined time.
352355
353356
:param day list: if not None, only runs on given days. Day must be given as english names with capitals
354357
"""
358+
starting_date =self.__get_starting_date__(year=year, month=month, day=day)
355359
if days is None:
356-
trigger = {"active": True, "type": "temporal", "params": { "frequency": "Daily", "hour": hour, "minute": minute}}
360+
trigger = {"active": True, "type": "temporal", "params": { "frequency": "Daily", "hour": hour, "minute": minute, "startingFrom": starting_date,
361+
"repeatFrequency": repeat_every, "timezone": timezone }}
357362
else:
358-
trigger = {"active": True, "type": "temporal", "params": { "frequency": "Weekly", "hour": hour, "minute": minute,
359-
"daysOfWeek": days}}
363+
trigger = {"active": True, "type": "temporal", "params": { "frequency": "Weekly", "hour": hour, "minute": minute, "startingFrom": starting_date,
364+
"daysOfWeek": days, "repeatFrequency": repeat_every, "timezone": timezone }}
360365
self.raw_triggers.append(trigger)
361366

362-
def add_monthly_trigger(self, day=1, hour=2, minute=0):
367+
def add_monthly_trigger(self, day=1, hour=2, minute=0, year=None, month=None, run_on="ON_THE_DAY", repeat_every=1, timezone="SERVER"):
363368
"""Adds a trigger that runs the scenario once per month"""
364-
trigger = {"active": True, "type": "temporal", "params": { "frequency": "Monthly", "dayOfMonth": day, "hour": hour, "minute": minute}}
369+
# We want the default value to be the First of January is nothing was set.
370+
starting_date = self.__get_starting_date__(year=year, month=month, day=day, default_is_today=False)
371+
trigger = {"active": True, "type": "temporal", "params": { "frequency": "Monthly", "startingFrom": starting_date, "hour": hour, "minute": minute,
372+
"monthlyRunOn": run_on, "repeatFrequency": repeat_every, "timezone": timezone }}
365373
self.raw_triggers.append(trigger)
366374

375+
def __get_starting_date__(self, year, month, day, default_is_today=True):
376+
today = datetime.now(tzlocal())
377+
if day is None or not isinstance(day, int) or day < 1 or day > 31:
378+
if default_is_today:
379+
day = today.day
380+
else:
381+
# Will be use to avoid Monthly trigger on the 31 of February if the day is not set
382+
day = 1
383+
if month is None or not isinstance(month, int) or month < 1 or month > 12:
384+
if default_is_today:
385+
month = today.month
386+
else:
387+
# Will be use to avoid Monthly trigger on the 31 of February if the month is not set
388+
month = 1
389+
if year is None or not isinstance(year, int):
390+
year = today.year
391+
start_date = today.replace(year=year, month=month, day=day, hour=0, minute=0, second=0, microsecond=0)
392+
return start_date.strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + start_date.strftime('%z')
393+
394+
367395
def save(self):
368396
"""Saves the settings to the scenario"""
369397
self.client._perform_json("PUT",

0 commit comments

Comments
 (0)