Example #1
0
 def infer_data_interval(self, run_after: DateTime) -> DataInterval:
     weekday = run_after.weekday()
     if weekday in (0, 6):  # Monday and Sunday -- interval is last Friday.
         days_since_friday = (run_after.weekday() - 4) % 7
         delta = timedelta(days=days_since_friday)
     else:  # Otherwise the interval is yesterday.
         delta = timedelta(days=1)
     start = DateTime.combine((run_after - delta).date(), Time.min).replace(tzinfo=UTC)
     return DataInterval(start=start, end=(start + timedelta(days=1)))
Example #2
0
def find_object_of_same_weekday_and_time(indict: Dict,
                                         time_slot: DateTime,
                                         ignore_not_found: bool = False):
    """
    Based on a profile with datetimes that span in one week as keys and some values, finds the
    corresponding value of the same weekday and time. This method is mainly useful for Canary
    Network, during which profiles get recycled every week. Therefore in order to find the
    value of a profile that corresponds to any requested time, the requested time slot is
    mapped on the week that the profile includes, and returns the value of the profile on the
    same weekday and on the same time
    @param indict: profile dict (keys are datetimes, values are arbitrary objects, but usually
                   floats) that contains data for one week that will be used as reference for
                   the requested time_slots
    @param time_slot: DateTime value that represents the requested time slot
    @param ignore_not_found: Boolean parameter that controls whether an error log will be reported
                             if the time_slot cannot be found in the original dict
    @return: Profile value for the requested time slot
    """
    if GlobalConfig.IS_CANARY_NETWORK:
        start_time = list(indict.keys())[0]
        add_days = time_slot.weekday() - start_time.weekday()
        if add_days < 0:
            add_days += 7
        timestamp_key = datetime(year=start_time.year,
                                 month=start_time.month,
                                 day=start_time.day,
                                 hour=time_slot.hour,
                                 minute=time_slot.minute,
                                 tz=TIME_ZONE).add(days=add_days)

        if timestamp_key in indict:
            return indict[timestamp_key]
        else:
            if not ignore_not_found:
                logging.error(
                    f"Weekday and time not found in dict for {time_slot}")
            return

    else:
        return indict[time_slot]