|
3 | 3 | from ..utils import DataikuException |
4 | 4 | from .discussion import DSSObjectDiscussions |
5 | 5 | from .utils import DSSTaggableObjectListItem |
| 6 | +from dateutil.tz import tzlocal |
6 | 7 |
|
7 | 8 | class DSSScenario(object): |
8 | 9 | """ |
@@ -338,32 +339,59 @@ def raw_reporters(self): |
338 | 339 |
|
339 | 340 | def add_periodic_trigger(self, every_minutes=5): |
340 | 341 | """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 }} |
342 | 343 | self.raw_triggers.append(trigger) |
343 | 344 |
|
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"): |
345 | 346 | """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 }} |
347 | 350 | self.raw_triggers.append(trigger) |
348 | 351 |
|
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"): |
350 | 353 | """ |
351 | 354 | Adds a trigger that runs the scenario each day on a predefined time. |
352 | 355 |
|
353 | 356 | :param day list: if not None, only runs on given days. Day must be given as english names with capitals |
354 | 357 | """ |
| 358 | + starting_date =self.__get_starting_date__(year=year, month=month, day=day) |
355 | 359 | 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 }} |
357 | 362 | 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 }} |
360 | 365 | self.raw_triggers.append(trigger) |
361 | 366 |
|
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"): |
363 | 368 | """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 }} |
365 | 373 | self.raw_triggers.append(trigger) |
366 | 374 |
|
| 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 | + |
367 | 395 | def save(self): |
368 | 396 | """Saves the settings to the scenario""" |
369 | 397 | self.client._perform_json("PUT", |
|
0 commit comments