def test_ior_with_another_filter(): f = F() assert f.filters == {} f |= F('name', 'eq', 'vince') assert f.filters == {'name': [('eq', 'vince')]} f |= F('age', 'gt', 21) assert f.filters == {'name': [('eq', 'vince')], 'age': [('gt', 21)]} f |= F('name', 'eq', 'bob') assert f.filters == {'name': [('eq', 'bob')], 'age': [('gt', 21)]}
def query_aot(sensor_hrf, size_per_page=100000, page_limit=1, mins_ago=12*60): sensors = ( SENSOR_DF.loc[SENSOR_DF['sensor_measure']==sensor_hrf, 'sensor_path'] .unique() ) if len(sensors) == 0: return pd.DataFrame() client = AotClient() # TODO: combine many sensors in API call pages = [] for sensor in sensors: f = F('size', str(size_per_page)) f &= ('sensor', sensor) f &= ('timestamp', 'ge', time_x_mins_ago(mins_ago)) response = client.list_observations(filters=f) pages.extend(unpack_response(response, page_limit=page_limit)) obs_df = pd.DataFrame(pages) obs_df = process_observations(obs_df) return obs_df
def test_to_query_params(): f = F('name', 'eq', 'vince') assert f.to_query_params() == [('name', 'eq:vince')] f &= ('name', 'eq', 'bob') assert f.to_query_params() == [('name[]', 'eq:vince'), ('name[]', 'eq:bob')]
def test_iter(): client = AotClient() dataset = client.list_observations(filters=F('size', 1)) i = 0 for page in dataset: i += 1 if i >= 5: break
def test_iand_with_a_tuple(): f = F() assert f.filters == {} f &= ('name', 'eq', 'vince') assert f.filters == {'name': [('eq', 'vince')]} f &= ('age', 'gt', 21) assert f.filters == {'name': [('eq', 'vince')], 'age': [('gt', 21)]} f &= ('name', 'eq', 'bob') assert f.filters == { 'name': [('eq', 'vince'), ('eq', 'bob')], 'age': [('gt', 21)] }
def AoT_filter(endpoint, params): # for filtering the API call ''' for filtering the API call -------------------------------------- f = a dictionary of filters where the key is a string and the values are a tuple. for example f &= ('sensor', 'image.image_detector.person_total') = {'sensor': [('image.image_detector.person_total',)]} would send an api request to https://api-of-things.plenar.io/api/observations?order=desc%3Atimestamp&page=1&sensor=image.image_detector.person_total&size=200 or f &= ('sensor', 'image.image_detector.person_total', 'hello world') = {'sensor': [('image.image_detector.person_total', 'hello world')]} would not be a valid request. ''' f = F() f &= (endpoint, params) return f
def query_aot(sensor_hrf, size_per_page=100000, page_limit=1, mins_ago=12 * 60): sensor = SENSOR_DF.loc[SENSOR_DF['sensor_measure'] == sensor_hrf, 'sensor_path'].values[0] client = AotClient() f = F('size', str(size_per_page)) f &= ('sensor', sensor) f &= ('timestamp', 'ge', time_x_mins_ago(mins_ago)) response = client.list_observations(filters=f) pages = unpack_response(response, page_limit=page_limit) obs_df = pd.DataFrame(pages) obs_df = process_observations(obs_df) return obs_df
def test_tuple_init(): f = F('age', 'gt', 21) assert f.filters == {'age': [('gt', 21)]}
def test_ior_with_a_dict(): f = F() assert f.filters == {} with pytest.raises(TypeError): f |= {'name': 'vince'}
def test_empty_init(): f = F() assert f.filters == {}
def test_two_length_filter(): f = F('include_nodes', True) assert f.to_query_params() == [('include_nodes', 'True')]
# Initialize AoT client client = AotClient() # Get previous record timestamp try: fh = open("state.txt", "r") prev_record_timestamp = fh.read() t = ciso8601.parse_datetime(prev_record_timestamp) fh.close() except FileNotFoundError: t = (datetime.datetime.utcnow() - datetime.timedelta(minutes=mins_ago)) prev_record_timestamp = t.isoformat()[0:19] # Initialize filter (city- Chicago, 5000 records, timestamp, order by timestamp) f = F('project', 'chicago') f &= ('size', '5000') f &= ('timestamp', 'gt', prev_record_timestamp) f &= ('order', 'asc:timestamp') # Set counter for retrieved pages page_num = 1 # Get observations from AoT observations = client.list_observations(filters=f) # Iterate through records try: for page in observations: print(f'Page {page_num}') # data_stream = []
def test_next_link_property(): client = AotClient() res = client.list_observations(filters=F('page', '2')) assert isinstance(res.next_link, str)