def most_recent_observation(self): """ Get the most recent observation for the device """ e = iot_event.query( iot_event.mac == self.dev_id).order(-iot_event.timestamp).get() if e is None: w = iot_week.query( iot_week.mac == self.dev_id).order(-iot_week.start).get() assert w is not None assert len(w.data) > 0 obs = list(w.data[-1]) obs[0] = 0 name = w.name start = format_timestamp(w.d_end) key = w.stored_params data = tuple(obs) else: name = e.name start = format_timestamp(e.timestamp) key = iot_week.stored_params data = [0] + [ int(e.params[k]) for k in iot_week.stored_params.split(',') ] return { 'id': self.dev_id, 'name': name, 'start': start, 'keys': key, 'obs': [data] }
def __init__(self, dev_id): self.dev_id = dev_id if len( iot_event.query(iot_event.mac == dev_id).fetch( 1, keys_only=True)) == 0: if len( iot_week.query(iot_week.mac == dev_id).fetch( 1, keys_only=True)) == 0: raise AttributeError( 'No device with MAC {} found'.format(dev_id))
def name(self): recent = iot_event.query( projection=['name'], distinct=True).filter(iot_event.mac == self.dev_id).get() try: return recent.name except: older = iot_week.query( projection=['name'], distinct=True).filter(iot_week.mac == self.dev_id).get() return older.name
def get(self): my_remote_addr = self.request.remote_addr recent = iot_event.query( projection=['mac', 'name'], distinct=True).filter( iot_event.remote_addr == my_remote_addr).fetch(1000) _log('{} results fetched from projection query on iot_event'.format( len(recent))) older = iot_week.query( projection=['mac', 'name'], distinct=True).filter( iot_week.remote_addrs == my_remote_addr).fetch(1000) _log('{} older results fetched from projection query on iot_week'. format(len(older))) macs = {} for r in recent + older: macs[r.mac] = r.name return self.get_response(200, {'iplist': { 'id': 'me', 'results': macs }})
def prefetch_recent(self): setattr( self, '__recent', iot_event.query(iot_event.mac == self.dev_id).order( iot_event.timestamp).fetch_async(1000))