def serialize_list(self, embed, cache): if not embed: return super( AggregateScalarSensorDataResource, self).serialize_list( embed, cache) if 'aggtime' not in self._filters: raise BadRequestException( "Missing aggtime arguement") href = self.get_list_href(True) serialized_data = { '_links': { 'curies': CHAIN_CURIES }, 'dataType': 'float' } request_time = timezone.now() if 'timestamp__gte' in self._filters: try: page_start = datetime.utcfromtimestamp( float(self._filters['timestamp__gte'])).replace( tzinfo=timezone.utc) except ValueError: raise BadRequestException( "Invalid timestamp format for lower bound of date range.") else: page_start = request_time - self.default_timespan() if 'timestamp__lt' in self._filters: try: page_end = datetime.utcfromtimestamp( float(self._filters['timestamp__lt'])).replace( tzinfo=timezone.utc) except ValueError: raise BadRequestException( "Invalid timestamp format for upper bound of date range.") else: page_end = request_time self._filters['timestamp__gte'] = page_start self._filters['timestamp__lt'] = page_end objs = influx_client.get_sensor_data(self._filters) serialized_data = self.add_page_links(serialized_data, href, page_start, page_end) serialized_data['data'] = [{ 'max': obj['max'], 'min': obj['min'], 'mean': obj['mean'], 'count': obj['count'], 'timestamp': obj['time']} for obj in objs] return serialized_data
def serialize_list(self, embed, cache): '''a "list" of SensorData resources is actually represented as a single resource with a list of data points''' if not embed: return super(ScalarSensorDataResource, self).serialize_list(embed, cache) href = self.get_list_href() serialized_data = { '_links': { 'curies': CHAIN_CURIES, 'createForm': { 'href': self.get_create_href(), 'title': 'Add Data' } }, 'dataType': 'float' } request_time = timezone.now() # if the time filters aren't given then use the most recent timespan, # if they are given, then we need to convert them from unix time to use # in the queryset filter if 'timestamp__gte' in self._filters: try: page_start = datetime.utcfromtimestamp( float(self._filters['timestamp__gte'])).replace( tzinfo=timezone.utc) except ValueError: raise BadRequestException( "Invalid timestamp format for lower bound of date range.") else: page_start = request_time - self.default_timespan if 'timestamp__lt' in self._filters: try: page_end = datetime.utcfromtimestamp( float(self._filters['timestamp__lt'])).replace( tzinfo=timezone.utc) except ValueError: raise BadRequestException( "Invalid timestamp format for upper bound of date range.") else: page_end = request_time self._filters['timestamp__gte'] = page_start self._filters['timestamp__lt'] = page_end objs = self._queryset.filter(**self._filters).order_by('timestamp') serialized_data = self.add_page_links(serialized_data, href, page_start, page_end) serialized_data['data'] = [{ 'value': obj.value, 'timestamp': obj.timestamp.isoformat() } for obj in objs] return serialized_data
def get_sensor_data(self, filters): if 'aggtime' not in filters: measurement = self._measurement # arguements are unicode strings elif filters['aggtime'] == u'1h': measurement = self._measurement + '_1h' elif filters['aggtime'] == u'1d': measurement = self._measurement + '_1d' elif filters['aggtime'] == u'1w': measurement = self._measurement + '_1w' else: raise BadRequestException( 'Invalid argument for aggtime. Must be 1h, 1d, or 1w') # exclude the old values that don't have metrics query = "SELECT * FROM {0} WHERE sensor_id = '{1}' AND metric != ''".format( measurement, filters['sensor_id']) if 'timestamp__gte' in filters: timestamp_gte = InfluxClient.convert_timestamp( filters['timestamp__gte']) query += ' AND time >= {}'.format(timestamp_gte) if 'timestamp__lt' in filters: timestamp_lt = InfluxClient.convert_timestamp( filters['timestamp__lt']) query += ' AND time < {}'.format(timestamp_lt) result = self.get_values(self.get(query, True)) return result
def default_timespan(self): aggtime = self._filters.get('aggtime', None) if aggtime is None: return timedelta(hours=6) elif aggtime == '1h': return timedelta(hours=500) elif aggtime == '1d': return timedelta(days=500) elif aggtime == '1w': return timedelta(weeks=500) else: raise BadRequestException('Invalid argument for aggtime. Must be 1h, 1d, or 1w')
def get_sensor_data(self, filters): timestamp_gte = InfluxClient.convert_timestamp( filters['timestamp__gte']) timestamp_lt = InfluxClient.convert_timestamp(filters['timestamp__lt']) if 'aggtime' not in filters: measurement = self._measurement # arguements are unicode strings elif filters['aggtime'] == u'1h': measurement = self._measurement + '_1h' elif filters['aggtime'] == u'1d': measurement = self._measurement + '_1d' elif filters['aggtime'] == u'1w': measurement = self._measurement + '_1w' else: raise BadRequestException( 'Invalid argument for aggtime. Must be 1h, 1d, or 1w') query = "SELECT * FROM {0} WHERE sensor_id = \'{1}\' AND time >= {2} AND time < {3}".format( measurement, filters['sensor_id'], timestamp_gte, timestamp_lt) result = self.get_values(self.get(query, True)) return result
def serialize_list(self, embed, cache): '''a "list" of SensorData resources is actually represented as a single resource with a list of data points''' if not embed: return super(PresenceDataResource, self).serialize_list(embed, cache) href = self.get_list_href() items = [] serialized_data = { '_links': { 'curies': CHAIN_CURIES, 'createForm': { 'href': self.get_create_href(), 'title': 'Add Data' }, 'items': items }, 'dataType': 'presence' } request_time = timezone.now() # if the time filters aren't given then use the most recent timespan, # if they are given, then we need to convert them from unix time to use # in the queryset filter if 'timestamp__gte' in self._filters: try: page_start = datetime.utcfromtimestamp( float(self._filters['timestamp__gte'])) except ValueError: raise BadRequestException( "Invalid timestamp format for lower bound of date range.") else: page_start = request_time - self.default_timespan() if 'timestamp__lt' in self._filters: try: page_end = datetime.utcfromtimestamp( float(self._filters['timestamp__lt'])) except ValueError: raise BadRequestException( "Invalid timestamp format for upper bound of date range.") else: page_end = request_time self._filters['timestamp__gte'] = page_start self._filters['timestamp__lt'] = page_end objs = self._queryset.filter(**self._filters).order_by('timestamp') serialized_data = self.add_page_links(serialized_data, href, page_start, page_end) # Make links: for obj in objs: presence_data_resource = PresenceDataResource( obj=obj, request=self._request) items.append({ 'href': presence_data_resource.get_single_href(), 'title': "%s %s %s at time %s" % (obj.person.last_name, "at" if obj.present else "left", obj.sensor.device, obj.timestamp.isoformat()) }) return serialized_data