Esempio n. 1
0
 def which_date(datetime):
   if datetime.date == from_datetime.date:
     return datetime_to_str(from_datetime)
   elif datetime.date == to_datetime.date:
     return datetime_to_str(to_datetime)
   else:
     return datetime.strftime('%Y-%m-%d 00:00:00')
Esempio n. 2
0
def query_test(from_datetime=2, to_datetime=2):
  now = datetime.datetime.now()
  engine = QueryGetEngine()
  query = {
    'event_name': 'create_clip',
    'from_datetime': datetime_to_str(now - delta_day*from_datetime),
    'to_datetime': datetime_to_str(now + delta_day*to_datetime)
  }
  fields = [{
    'uid': '4',
    'board':'27'
  }]
  info = engine.execute(ip=ip, app_name='cayman', query=query, fields=fields)
  return info
Esempio n. 3
0
def delta_hour_dict(from_datetime, to_datetime, blank=0):
  """
  以小时为维度处理时间,默认值为0
  """
  delta_hours = 1

  hours = {
    datetime_to_str(to_datetime): blank,
    datetime_to_str(from_datetime): blank
  }

  while True:
    datetime = from_datetime + timedelta(hours=delta_hours)
    if datetime > to_datetime:
      break
    else:
      hours.update({
        datetime_to_str(datetime): blank
      })
    delta_hours += 1
  return hours
Esempio n. 4
0
def delta_day_dict(from_datetime, to_datetime, blank=0):
  """
  以天为维度处理时间,默认值为0
  """
  start_day = py_time(year=from_datetime.year, month=from_datetime.month,
                      day=from_datetime.day)
  end_day = py_time(year=to_datetime.year, month=to_datetime.month, day=to_datetime.day)

  time_deltas = end_day - start_day
  days = {}
  for day in range(time_deltas.days+1):
    if day == 0:
      datetime = from_datetime
    elif day == time_deltas.days:
      datetime = to_datetime
    else:
      datetime = start_day + timedelta(days=day)
    days.update({
      datetime_to_str(datetime): blank
    })
  return days
Esempio n. 5
0
  def mget_hours(cls, from_datetime=py_time.now(), to_datetime=py_time.now(), fields=[]):
    delta = timedelta(hours=24)
    from_yesterday = from_datetime - delta
    to_tomorrow = to_datetime + delta
    cursors = cls.get_by_query(query={'date': {'$gte': from_yesterday, '$lt': to_tomorrow}}, only=fields)
    infos = {
      'total': 0,
      'stats': time_dict(from_datetime, to_datetime, using='hours')
    }

    field = fields[0]
    for cursor in cursors:
      date = cursor['date']
      data = cursor.get(field, 0)
      if isinstance(data, dict):
        for hour in data:
          d_time = document_datetime(date, hour)
          if not (d_time >= from_datetime and d_time <= to_datetime):
            continue
          infos["total"] += data[hour]
          infos["stats"][datetime_to_str(d_time)] = data[hour]
      else:
        infos["total"] += data
    return infos
Esempio n. 6
0
def random_datetime():
  hour = random.choice(range(24))
  d = random.choice([two_days_ago, yesterday, now, tomorrow, two_days_later])
  d_time = datetime.datetime(year=d.year, month=d.month, day=d.day, hour=hour)
  return datetime_to_str(d_time)