def get_value(self, authorization=None, method=None, testcase=None): 'Retrieve the most recent value for a given UUID' assert authorization != None assert method != None # Look up the index of the data in the sorted-set. This will be # something returned by value_name() data = yield UserData.find( where=self.args_to_where(without=['testcase'], uuid=authorization.uuid, method=method), limit=1, orderby='timestamp DESC' ) if not data: defer.returnValue([]) try: val = int(data.data) except: val = json.loads(data.data) if authorization.expired_on_timestamp and authorization.expired_on_timestamp > data.timestamp: ret = [val, authorization.expired_on_timestamp] else: ret = [val] defer.returnValue(ret)
def is_duplicate_value(self, **kwargs): """ Determine if kwargs['data'] is a duplicate of the most recent value for kwargs['uuid']/kwargs['method']. """ old_data = yield UserData.find( where=self.args_to_where(without=['authorization', 'feed_type', 'timestamp'], **kwargs), limit=1 ) if old_data and str(old_data.data) == str(kwargs['data']): defer.returnValue(True) defer.returnValue(False)
def get_value_range(self, **kwargs): """Retrieve the list of values between 'start' and 'end' for an 'authorization'""" assert kwargs.get('authorization') != None assert kwargs.get('method') != None direction = 'ASC' if kwargs.get('reverse'): direction = 'DESC' start_time = int(kwargs.get('start', 1)) end_time = int(kwargs.get('end', int(time.time()))) authorization = kwargs['authorization'] data = yield UserData.find( where=[ 'uuid = ? and method = ? and timestamp between ? and ?', authorization.uuid, kwargs['method'], start_time, end_time, ], orderby='timestamp %s' % direction ) extended_start_time = start_time if start_time > 1 and start_time not in [x.timestamp for x in data]: addl_row = yield UserData.find( where=[ 'uuid = ? and method = ? and timestamp < ?', authorization.uuid, kwargs['method'], start_time, ], orderby='timestamp DESC', limit=1, ) if addl_row: extended_start_time = addl_row.timestamp if direction == 'DESC': data.append(addl_row) else: data.insert(0, addl_row) ret = [] for d in data: try: val = int(d.data) except: val = json.loads(d.data) ret.append(['%s' % d.timestamp, val]) if kwargs.get('num'): ret = ret[0:kwargs['num']] ret_idx = len(ret) - 1 is_expired_in_range = extended_start_time <= authorization.expired_on_timestamp <= end_time while ret_idx >= 0: if authorization.expired_on_timestamp >= int(ret[ret_idx][0]) and is_expired_in_range: ret[ret_idx].append(authorization.expired_on_timestamp) break ret_idx = ret_idx - 1 for item in ret: if int(item[0]) < start_time: item[0] = str(start_time) defer.returnValue(ret)