Example #1
0
def add_timeseries(request):
    """Add timeseries data for a meter.

    Payload is expected to look like the following:
    {
        'organization_id': 435,
        'meter_id': 34,
        'timeseries': [
            {
                'begin_time': 2342342232,
                'end_time': 23423433433,
                'cost': 232.23,
            }...
        ]
    }
    """
    body = json.loads(request.body)
    meter_id = body.get('meter_id', '')
    ts_data = body.get('timeseries', [])
    try:
        meter = Meter.objects.get(pk=meter_id)
    except Meter.DoesNotExist:
        return {'status': 'error', 'message': 'Meter ID does not match'}

    for ts_item in ts_data:
        TimeSeries.objects.create(
            begin_time=convert_datestr(ts_item.get('begin_time', None)),
            end_time=convert_datestr(ts_item.get('end_time', None)),
            reading=ts_item.get('reading', None),
            cost=ts_item.get('cost', None),
            meter=meter
        )

    return {'status': 'success'}
Example #2
0
    def test_date_conversion(self):
        dt = datetime(2016, 0o7, 15, 12, 30)
        self.assertEqual(convert_datestr(dt.strftime("%Y-%m-%d %H:%M")), dt)

        # with TZ info
        dt = make_aware(datetime(2016, 0o7, 15, 12, 30), pytz.UTC)
        self.assertEqual(convert_datestr(dt.strftime("%Y-%m-%d %H:%M"), True), dt)
    def test_handle_ts_data(self):
        """Create timeseries data entries and assoc. with a meter."""
        ts_data = [
            {
                'cost': 1212,
                'id': None,
                'begin_time': '2014-06-09',
                'occupancy': 100,
                'reading': 2324,
                'end_time': '2014-07-08',
            },
            {
                'cost': 122,
                'id': None,
                'begin_time': '2014-07-09',
                'occupancy': 88,
                'reading': 12,
                'end_time': '2014-08-08',
            },
        ]
        seed_models._handle_ts_data(self.meter, ts_data)
        qs = seed_models.TimeSeries.objects.all()
        self.assertEqual(qs.count(), 2)

        for index, ts in enumerate(qs):
            self.assertEqual(ts.begin_time,
                             convert_datestr(ts_data[index]['begin_time']))
            self.assertEqual(ts.end_time,
                             convert_datestr(ts_data[index]['end_time']))
            self.assertEqual(ts.cost, ts_data[index]['cost'])
            self.assertEqual(ts.occupancy, float(ts_data[index]['occupancy']))
            self.assertEqual(ts.reading, float(ts_data[index]['reading']))
Example #4
0
def add_timeseries(request):
    """Add timeseries data for a meter.

    Payload is expected to look like the following:
    {
        'organization_id': 435,
        'meter_id': 34,
        'timeseries': [
            {
                'begin_time': 2342342232,
                'end_time': 23423433433,
                'cost': 232.23,
            }...
        ]
    }
    """
    body = json.loads(request.body)
    meter_id = body.get('meter_id', '')
    ts_data = body.get('timeseries', [])
    try:
        meter = Meter.objects.get(pk=meter_id)
    except Meter.DoesNotExist:
        return {'status': 'error', 'message': 'Meter ID does not match'}

    for ts_item in ts_data:
        TimeSeries.objects.create(
            begin_time=convert_datestr(ts_item.get('begin_time', None)),
            end_time=convert_datestr(ts_item.get('end_time', None)),
            reading=ts_item.get('reading', None),
            cost=ts_item.get('cost', None),
            meter=meter)

    return {'status': 'success'}
Example #5
0
 def clean(self):
     date_field_names = ('year_ending', 'generation_date', 'release_date',
                         'recent_sale_date')
     for field in date_field_names:
         value = getattr(self, field)
         if value and isinstance(value, (str, unicode)):
             setattr(self, field, convert_datestr(value))
Example #6
0
 def clean(self):
     date_field_names = ('year_ending', 'generation_date', 'release_date',
                         'recent_sale_date')
     for field in date_field_names:
         value = getattr(self, field)
         if value and isinstance(value, basestring):
             _log.info("Saving %s which is a date time" % field)
             _log.info(convert_datestr(value))
             _log.info(date_cleaner(value))
    def test_handle_ts_data(self):
        """Create timeseries data entries and assoc. with a meter."""
        ts_data = [
            {
                'cost': 1212,
                'id': None,
                'begin_time': '2014-06-09',
                'occupancy': 100,
                'reading': 2324,
                'end_time': '2014-07-08',
            },
            {
                'cost': 122,
                'id': None,
                'begin_time': '2014-07-09',
                'occupancy': 88,
                'reading': 12,
                'end_time': '2014-08-08',
            },

        ]
        seed_models._handle_ts_data(self.meter, ts_data)
        qs = seed_models.TimeSeries.objects.all()
        self.assertEqual(qs.count(), 2)

        for index, ts in enumerate(qs):
            self.assertEqual(
                ts.begin_time, convert_datestr(ts_data[index]['begin_time'])
            )
            self.assertEqual(
                ts.end_time, convert_datestr(ts_data[index]['end_time'])
            )
            self.assertEqual(
                ts.cost, ts_data[index]['cost']
            )
            self.assertEqual(
                ts.occupancy, float(ts_data[index]['occupancy'])
            )
            self.assertEqual(
                ts.reading, float(ts_data[index]['reading'])
            )
Example #8
0
    def str_to_data_type(self, value):
        """
        If the check is coming from a field in the database then it will be typed correctly;
        however, for extra_data, the values are typically strings or unicode. Therefore, the
        values are typed before they are checked using the rule's data type definition.

        :param value: variant, value to type
        :return: typed value
        """

        if isinstance(value, basestring):
            # check if we can type cast the value
            try:
                # strip the string of any leading/trailing spaces
                value = value.strip()
                if self.data_type == self.TYPE_NUMBER:
                    if value == '':
                        return None
                    else:
                        return float(value)
                elif self.data_type == self.TYPE_STRING:
                    return str(value)
                elif self.data_type == self.TYPE_DATE:
                    if value == '':
                        return None
                    else:
                        return convert_datestr(value, True)
                elif self.data_type == self.TYPE_YEAR:
                    if value == '':
                        return None
                    else:
                        dt = convert_datestr(value, True)
                        if dt is not None:
                            return dt.date()
            except ValueError as e:
                raise DataQualityTypeCastError(
                    "Error converting {} with {}".format(value, e))
        else:
            return value
Example #9
0
    def str_to_data_type(self, value):
        """
        If the check is coming from a field in the database then it will be typed correctly;
        however, for extra_data, the values are typically strings or unicode. Therefore, the
        values are typed before they are checked using the rule's data type definition.

        :param value: variant, value to type
        :return: typed value
        """

        if isinstance(value, (str, unicode)):
            # check if we can type cast the value
            try:
                # since we already checked for data type, does this mean it isn't None, ever?
                if value is not None:
                    if self.data_type == TYPE_NUMBER:
                        if value == '':
                            return None
                        else:
                            return float(value)
                    elif self.data_type == TYPE_STRING:
                        return str(value)
                    elif self.data_type == TYPE_DATE:
                        if value == '':
                            return None
                        else:
                            return convert_datestr(value, True)
                    elif self.data_type == TYPE_YEAR:
                        if value == '':
                            return None
                        else:
                            dt = convert_datestr(value, True)
                            if dt is not None:
                                return dt.date()
            except ValueError as e:
                raise TypeError("Error converting {} with {}".format(value, e))
        else:
            return value
Example #10
0
 def clean(self, *args, **kwargs):
     super(BuildingSnapshot, self).clean(*args, **kwargs)
     date_field_names = (
         'year_ending',
         'generation_date',
         'release_date',
         'recent_sale_date'
     )
     if self.custom_id_1 and len(self.custom_id_1) > 128:
         self.custom_id_1 = self.custom_id_1[:128]
     for field in date_field_names:
         value = getattr(self, field)
         if value and isinstance(value, basestring):
             setattr(self, field, convert_datestr(value))
Example #11
0
    def clean(self, *args, **kwargs):
        super(BuildingSnapshot, self).clean(*args, **kwargs)

        # if self.owner:
        #     self.owner = self.owner[:128]

        date_field_names = ('year_ending', 'generation_date', 'release_date',
                            'recent_sale_date')
        custom_id_1 = getattr(self, 'custom_id_1')
        if isinstance(custom_id_1, unicode):
            custom_id_1 = unicodedata.normalize('NFKD', custom_id_1).encode(
                'ascii', 'ignore')
        if custom_id_1 and len(str(custom_id_1)) > 128:
            self.custom_id_1 = custom_id_1[:128]
        for field in date_field_names:
            value = getattr(self, field)
            if value and isinstance(value, basestring):
                setattr(self, field, convert_datestr(value))
Example #12
0
 def clean(self, *args, **kwargs):
     super(BuildingSnapshot, self).clean(*args, **kwargs)
     date_field_names = (
         'year_ending',
         'generation_date',
         'release_date',
         'recent_sale_date'
     )
     custom_id_1 = getattr(self, 'custom_id_1')
     if isinstance(custom_id_1, unicode):
         custom_id_1 = unicodedata.normalize('NFKD', custom_id_1).encode(
             'ascii', 'ignore'
         )
     if custom_id_1 and len(str(custom_id_1)) > 128:
         self.custom_id_1 = custom_id_1[:128]
     for field in date_field_names:
         value = getattr(self, field)
         if value and isinstance(value, basestring):
             setattr(self, field, convert_datestr(value))
Example #13
0
    def clean(self, *args, **kwargs):
        date_field_names = (
            'year_ending',
            'generation_date',
            'release_date',
            'recent_sale_date'
        )

        # TODO: Where to put in the custom_id_1
        # custom_id_1 = getattr(self, 'custom_id_1')
        # if isinstance(custom_id_1, unicode):
        #     custom_id_1 = unicodedata.normalize('NFKD', custom_id_1).encode(
        #         'ascii', 'ignore'
        #     )
        # if custom_id_1 and len(str(custom_id_1)) > 128:
        #     self.custom_id_1 = custom_id_1[:128]
        for field in date_field_names:
            value = getattr(self, field)
            if value and isinstance(value, basestring):
                setattr(self, field, convert_datestr(value))