Esempio n. 1
0
  def mget_days(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='days')
    }

    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')

    field = fields[0]
    for cursor in cursors:
      date = cursor['date']
      data = cursor.get(field, 0)
      total = 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
          total += data[hour]
      else:
        total = data

      infos['total'] += total
      infos['stats'][which_date(date)] = total
    return infos
Esempio n. 2
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