Пример #1
0
def _date_trunc(value, timeframe):
  """
  A date flooring function.

  Returns the closest datetime to the current one that aligns to timeframe.
  For example, _date_trunc('2014-08-13 05:00:00', DateTrunc.Unit.MONTH)
  will return a Kronos time representing 2014-08-01 00:00:00.
  """
  if isinstance(value, types.StringTypes):
    value = parse(value)
    return_as_str = True
  else:
    value = kronos_time_to_datetime(value)
    return_as_str = False
  timeframes = {
    DateTrunc.Unit.SECOND: (lambda dt:
                            dt - timedelta(microseconds=dt.microsecond)),
    DateTrunc.Unit.MINUTE: (lambda dt:
                            dt - timedelta(seconds=dt.second,
                                           microseconds=dt.microsecond)),
    DateTrunc.Unit.HOUR: (lambda dt:
                          dt - timedelta(minutes=dt.minute,
                                         seconds=dt.second,
                                         microseconds=dt.microsecond)),
    DateTrunc.Unit.DAY: lambda dt: dt.date(),
    DateTrunc.Unit.WEEK: lambda dt: dt.date() - timedelta(days=dt.weekday()),
    DateTrunc.Unit.MONTH: lambda dt: datetime(dt.year, dt.month, 1),
    DateTrunc.Unit.YEAR: lambda dt: datetime(dt.year, 1, 1)
  }
  value = timeframes[timeframe](value)
  if return_as_str:
    return value.isoformat()
  return datetime_to_kronos_time(value)
Пример #2
0
def _date_trunc(value, timeframe):
  """
  A date flooring function.

  Returns the closest datetime to the current one that aligns to timeframe.
  For example, _date_trunc('2014-08-13 05:00:00', DateTrunc.Unit.MONTH)
  will return a Kronos time representing 2014-08-01 00:00:00.
  """
  if isinstance(value, types.StringTypes):
    value = parse(value)
    return_as_str = True
  else:
    value = kronos_time_to_datetime(value)
    return_as_str = False
  timeframes = {
    DateTrunc.Unit.SECOND: (lambda dt:
                            dt - timedelta(microseconds=dt.microsecond)),
    DateTrunc.Unit.MINUTE: (lambda dt:
                            dt - timedelta(seconds=dt.second,
                                           microseconds=dt.microsecond)),
    DateTrunc.Unit.HOUR: (lambda dt:
                          dt - timedelta(minutes=dt.minute,
                                         seconds=dt.second,
                                         microseconds=dt.microsecond)),
    DateTrunc.Unit.DAY: lambda dt: dt.date(),
    DateTrunc.Unit.WEEK: lambda dt: dt.date() - timedelta(days=dt.weekday()),
    DateTrunc.Unit.MONTH: lambda dt: datetime(dt.year, dt.month, 1),
    DateTrunc.Unit.YEAR: lambda dt: datetime(dt.year, 1, 1)
  }
  value = timeframes[timeframe](value)
  if return_as_str:
    return value.isoformat()
  return datetime_to_kronos_time(value)
Пример #3
0
def cohort_response(plan, events):
  cohort = defaultdict(lambda: {'cohort_size': 0,
                                'action_dates': defaultdict(int)})
  for event in events:
    cohort_date = (kronos_time_to_datetime(event[TIMESTAMP_FIELD])
                   .date()
                   .isoformat())
    step = timedelta(
      seconds=kronos_time_to_epoch_time(int(event['action_step'])))
    step = getattr(step, plan['action']['unit'])
    cohort[cohort_date]['action_dates'][step] = event['cohort_actions']
  return cohort
Пример #4
0
def _date_part(value, part):
  """
  Returns a portion of a datetime.

  Returns the portion of a datetime represented by timeframe.
  For example, _date_part('2014-08-13 05:00:00', DatePart.Unit.WEEK_DAY)
  will return 2, for Wednesday.
  """
  if isinstance(value, types.StringTypes):
    value = parse(value)
  else:
    value = kronos_time_to_datetime(value)
  value = kronos_time_to_datetime(value)
  parts = {
    DatePart.Unit.SECOND: lambda dt: dt.second,
    DatePart.Unit.MINUTE: lambda dt: dt.minute,
    DatePart.Unit.HOUR: lambda dt: dt.hour,
    DatePart.Unit.DAY: lambda dt: dt.day,
    DatePart.Unit.MONTH: lambda dt: dt.month,
    DatePart.Unit.YEAR: lambda dt: dt.year,
    DatePart.Unit.WEEK_DAY: lambda dt: dt.weekday(),
    }
  result = parts[part](value)
  return result
Пример #5
0
def _date_part(value, part):
  """
  Returns a portion of a datetime.

  Returns the portion of a datetime represented by timeframe.
  For example, _date_part('2014-08-13 05:00:00', DatePart.Unit.WEEK_DAY)
  will return 2, for Wednesday.
  """
  if isinstance(value, types.StringTypes):
    value = parse(value)
  else:
    value = kronos_time_to_datetime(value)
  parts = {
    DatePart.Unit.SECOND: lambda dt: dt.second,
    DatePart.Unit.MINUTE: lambda dt: dt.minute,
    DatePart.Unit.HOUR: lambda dt: dt.hour,
    DatePart.Unit.DAY: lambda dt: dt.day,
    DatePart.Unit.MONTH: lambda dt: dt.month,
    DatePart.Unit.YEAR: lambda dt: dt.year,
    DatePart.Unit.WEEK_DAY: lambda dt: dt.weekday(),
  }
  result = parts[part](value)
  return result