def test_fix_404(): with translator_for(None) as t: assert isinstance(t, CrateTranslator) os.environ[ 'QL_CONFIG'] = 'src/translators/tests/ql-config-timescale-default.yml' with translator_for(None) as t: assert isinstance(t, PostgresTranslator)
def test_os_env(): os.environ['QL_DEFAULT_DB'] = 'timescale' with translator_for(None) as t: assert isinstance(t, PostgresTranslator) os.environ['QL_DEFAULT_DB'] = 'crate' with translator_for(None) as t: assert isinstance(t, CrateTranslator)
def delete_entity(entity_id, type_=None, from_date=None, to_date=None): try: with translator_for(fiware_s()) as trans: deleted = trans.delete_entity(eid=entity_id, etype=type_, from_date=from_date, to_date=to_date, fiware_service=fiware_s(), fiware_servicepath=fiware_sp(),) except AmbiguousNGSIIdError as e: return { "error": "AmbiguousNGSIIdError", "description": str(e) }, 409 logging.getLogger(__name__).info("deleted {} entities".format(deleted)) if deleted == 0: r = { "error": "Not Found", "description": "No records were found for such query." } return r, 404 if deleted > 0: return '{} records successfully deleted.'.format(deleted), 204
def delete_entities(entity_type, from_date=None, to_date=None, drop_table=False): with translator_for(fiware_s()) as trans: if drop_table and fiware_sp() is None: trans.drop_table(etype=entity_type, fiware_service=fiware_s()) logging.getLogger(__name__).info( "dropped entity_type {}".format(entity_type)) return 'entity table dropped', 204 elif drop_table: e = fiware_sp().split(",") if e is not None and len(e) > 0: return "request has one or more fiware service paths," \ "dropTable requires no service path", 422 deleted = trans.delete_entities(etype=entity_type, from_date=from_date, to_date=to_date, fiware_service=fiware_s(), fiware_servicepath=fiware_sp(),) logging.getLogger(__name__).info( "deleted {} entities of type {}".format(deleted, entity_type)) if deleted == 0: r = { "error": "Not Found", "description": "No records were found for such query." } return r, 404 if deleted > 0: return '{} records successfully deleted.'.format(deleted), 204
def delete_entities(entity_type, from_date=None, to_date=None, drop_table=False): with translator_for(fiware_s()) as trans: if drop_table: trans.drop_table(etype=entity_type, fiware_service=fiware_s()) logging.getLogger(__name__).info( "dropped entity_type {}".format(entity_type)) return 'entity table dropped', 204 deleted = trans.delete_entities( etype=entity_type, from_date=from_date, to_date=to_date, fiware_service=fiware_s(), fiware_servicepath=fiware_sp(), ) logging.getLogger(__name__).info( "deleted {} entities of type {}".format(deleted, entity_type)) if deleted == 0: r = { "error": "Not Found", "description": "No records were found for such query." } return r, 404 if deleted > 0: return '{} records successfully deleted.'.format(deleted), 204
def query_NTNE( limit=10000, type_=None, # In Query from_date=None, to_date=None, offset=0): """ See /entities in API Specification quantumleap.yml """ fiware_s = request.headers.get('fiware-service', None) fiware_sp = request.headers.get('fiware-servicepath', None) entities = None try: with translator_for(fiware_s) as trans: entities = trans.query_ids(limit=limit, entity_type=type_, from_date=from_date, to_date=to_date, offset=offset, fiware_service=fiware_s, fiware_servicepath=fiware_sp) except NGSIUsageError as e: msg = "Bad Request Error: {}".format(e) logging.getLogger(__name__).error(msg, exc_info=True) return msg, 400 except InvalidParameterValue as e: msg = "Bad Request Error: {}".format(e) logging.getLogger(__name__).error(msg, exc_info=True) return {"error": "{}".format(type(e)), "description": str(e)}, 422 except Exception as e: msg = "Internal server Error: {}".format(e) logging.getLogger(__name__).error(msg, exc_info=True) return msg, 500 if entities: res = [] for entity in entities: entity['entityId'] = entity['id'] entity['entityType'] = entity['type'] entity['index'] = entity['index'][0] del entity['id'] del entity['type'] res.append(entity) logging.warning( "usage of id and type rather than entityId and entityType from version 0.9" ) logging.getLogger(__name__).info("Query processed successfully") return res r = { "error": "Not Found", "description": "No records were found for such query." } logging.getLogger(__name__).info("No value found for query") return r, 404
def notify(): if request.json is None: return 'Discarding notification due to lack of request body. ' \ 'Lost in a redirect maybe?', 400 if 'data' not in request.json: return 'Discarding notification due to lack of request body ' \ 'content.', 400 payload = request.json['data'] # preprocess and validate each entity update for entity in payload: # Validate entity update error = _validate_payload(entity) if error: return error, 400 # Add TIME_INDEX attribute custom_index = request.headers.get(TIME_INDEX_HEADER_NAME, None) entity[TIME_INDEX_NAME] = \ select_time_index_value_as_iso(custom_index, entity) # Add GEO-DATE if enabled add_geodata(entity) # Always normalize location if there's one normalize_location(entity) # Define FIWARE tenant fiware_s = request.headers.get('fiware-service', None) # It seems orion always sends a 'Fiware-Servicepath' header with value '/' # But this is not correctly documented in the API, so in order not to # depend on this, QL will not treat servicepath if there's no service # specified. if fiware_s: fiware_sp = request.headers.get('fiware-servicepath', None) else: fiware_sp = None res_entity = [] e = None for entity in payload: # Validate entity update e = _filter_empty_entities(entity) if e is not None: e_new = _filter_no_type_no_value_entities(e) res_entity.append(e_new) payload = res_entity # Send valid entities to translator try: with translator_for(fiware_s) as trans: trans.insert(payload, fiware_s, fiware_sp) except: msg = "Notification not processed or not updated" log().error(msg) return msg, 500 msg = 'Notification successfully processed' log().info(msg) return msg
def run(self): data = self.task_input() svc = data.fiware_service svc_path = data.fiware_service_path try: with translator_for(svc) as trans: trans.insert(data.payload, svc, svc_path) except Exception as e: self._handle_exception(svc, e)
def count_entities(entity_type: str, service: Optional[str], entity_id: Optional[str] = None) -> int: table = full_table_name(service, entity_type) stmt = f"SELECT count(*) FROM {table}" if entity_id: stmt += f" WHERE {ENTITY_ID_COL} = '{entity_id}'" with translator_for(service) as trans: trans.cursor.execute(stmt) cnt = trans.cursor.fetchall()[0] return cnt[0]
def query_NTNE(limit=10000, type_=None, # In Query from_date=None, to_date=None, offset=0): """ See /entities in API Specification quantumleap.yml """ fiware_s = request.headers.get('fiware-service', None) fiware_sp = request.headers.get('fiware-servicepath', None) entities = None try: with translator_for(fiware_s) as trans: entities = trans.query_ids(limit=limit, entity_type=type_, from_date=from_date, to_date=to_date, offset=offset, fiware_service=fiware_s, fiware_servicepath=fiware_sp) except NGSIUsageError as e: msg = "Bad Request Error: {}".format(e) logging.getLogger().error(msg, exc_info=True) return msg, 400 except InvalidParameterValue as e: return { "error": "{}".format(type(e)), "description": str(e) }, 422 except Exception as e: msg = "Internal server Error: {}".format(e) logging.getLogger().error(msg, exc_info=True) return msg, 500 if entities: res = [] for entity in entities: res.append(entity) return res r = { "error": "Not Found", "description": "No records were found for such query." } return r, 404
def test_instanceId(service, reporter_dataset): h = {'Fiware-Service': service} r = requests.get(query_url(), headers=h) assert r.status_code == 200, r.text entities = None with translator_for(service) as trans: instanceIds = trans.query_instanceId(entity_id=entity_id, entity_type=entity_type, fiware_service=service) if instanceIds: unique_instanceIds = [] # traverse for all elements for x in instanceIds: # check if exists in unique_list or not if x not in unique_instanceIds: unique_instanceIds.append(x) assert len(unique_instanceIds) == len(r.json()['entities'][0]['index'])
def test_tenant1(): with translator_for('t1') as t: assert isinstance(t, PostgresTranslator)
def test_no_tenant(): with translator_for(None) as t: assert isinstance(t, CrateTranslator)
def test_unknown_tenant(): with translator_for('not-in-config') as t: assert isinstance(t, CrateTranslator)
def test_tenant2(): with translator_for('t2') as t: assert isinstance(t, CrateTranslator)
def query_1T1ENA(entity_id, # In Path type_=None, # In Query attrs=None, aggr_method=None, aggr_period=None, options=None, from_date=None, to_date=None, last_n=None, limit=10000, offset=0, georel=None, geometry=None, coords=None): """ See /entities/{entityId}/attrs/{attrName} in API Specification quantumleap.yml """ r, c = _validate_query_params(attrs, aggr_period, aggr_method, options=options) if c != 200: return r, c r, c, geo_query = handle_geo_query(georel, geometry, coords) if r: return r, c if attrs is not None: attrs = attrs.split(',') fiware_s = request.headers.get('fiware-service', None) fiware_sp = request.headers.get('fiware-servicepath', '/') entities = None try: with translator_for(fiware_s) as trans: entities, err = trans.query(attr_names=attrs, entity_type=type_, entity_id=entity_id, aggr_method=aggr_method, aggr_period=aggr_period, from_date=from_date, to_date=to_date, last_n=last_n, limit=limit, offset=offset, fiware_service=fiware_s, fiware_servicepath=fiware_sp, geo_query=geo_query) except NGSIUsageError as e: msg = "Bad Request Error: {}".format(e) logging.getLogger(__name__).error(msg, exc_info=True) return { "error": "{}".format(type(e)), "description": str(e) }, 400 except InvalidParameterValue as e: msg = "Bad Request Error: {}".format(e) logging.getLogger(__name__).error(msg, exc_info=True) return { "error": "{}".format(type(e)), "description": str(e) }, 422 except Exception as e: # Temp workaround to debug test_not_found msg = "Something went wrong with QL. Error: {}".format(e) logging.getLogger(__name__).error(msg, exc_info=True) return msg, 500 if err == "AggrMethod cannot be applied": r = { "error": "AggrMethod cannot be applied", "description": "AggrMethod cannot be applied on type TEXT and BOOLEAN."} logging.getLogger(__name__).info("AggrMethod cannot be applied") return r, 404 if entities: if len(entities) > 1: logging.warning("Not expecting more than one result for a 1T1ENA.") attributes = [] ignore = ('type', 'id', 'index') attrs = [at for at in sorted(entities[0].keys()) if at not in ignore] for at in attrs: attributes.append({ 'attrName': at, 'values': entities[0][at]['values'] }) index = [] if aggr_method and not aggr_period else entities[0]['index'] res = { 'entityId': entity_id, 'entityType': entities[0]['type'], 'index': index, 'attributes': attributes } logging.getLogger(__name__).info("Query processed successfully") logging.warning( "usage of id and type rather than entityId and entityType from version 0.9") return res r = { "error": "Not Found", "description": "No records were found for such query." } logging.getLogger(__name__).info("No value found for query") return r, 404
def query_NTNENA( id_=None, # In Query attrs=None, type_=None, aggr_method=None, aggr_period=None, aggr_scope=None, options=None, from_date=None, to_date=None, last_n=None, limit=10000, offset=0, georel=None, geometry=None, coords=None, id_pattern=None): """ See /v2/attrs in API Specification quantumleap.yml """ r, c = _validate_query_params(attrs, aggr_period, aggr_method, aggr_scope, options) if c != 200: return r, c r, c, geo_query = handle_geo_query(georel, geometry, coords) if r: return r, c if attrs is not None: attrs = attrs.split(',') fiware_s = request.headers.get('fiware-service', None) fiware_sp = request.headers.get('fiware-servicepath', '/') entities = None entity_ids = None if id_: entity_ids = [s.strip() for s in id_.split(',') if s] try: with translator_for(fiware_s) as trans: entities, err = trans.query(attr_names=attrs, entity_type=type_, entity_ids=entity_ids, aggr_method=aggr_method, aggr_period=aggr_period, aggr_scope=aggr_scope, from_date=from_date, to_date=to_date, last_n=last_n, limit=limit, offset=offset, idPattern=id_pattern, fiware_service=fiware_s, fiware_servicepath=fiware_sp, geo_query=geo_query) except NGSIUsageError as e: msg = "Bad Request Error: {}".format(e) logging.getLogger(__name__).error(msg, exc_info=True) return msg, 400 except InvalidParameterValue as e: msg = "Bad Request Error: {}".format(e) logging.getLogger(__name__).error(msg, exc_info=True) return {"error": "{}".format(type(e)), "description": str(e)}, 422 except Exception as e: msg = "Something went wrong with QL. Error: {}".format(e) logging.getLogger(__name__).error(msg, exc_info=True) return msg, 500 attributes = [] entries = [] attrs_names = [] attrs_values = [] ignore = ('id', 'index', 'type') if entities: for e in entities: attrs = [at for at in sorted(e.keys()) if at not in ignore] for at in attrs: if at not in attrs_names: attrs_names.append(at) for at in attrs_names: entity_type = [] entity_types = [] entity_value = [] for e in entities: matched_attr = lookup_string_match(e, at) if matched_attr is not None: try: f_date = dateutil.parser.isoparse(from_date).replace( tzinfo=timezone.utc).isoformat() except Exception as ex: f_date = '' try: t_date = dateutil.parser.isoparse(to_date).replace( tzinfo=timezone.utc).isoformat() except Exception as ex: t_date = '' index = [ f_date, t_date ] if aggr_method and not aggr_period else e['index'] entity = { 'entityId': e['id'], 'index': index, 'values': matched_attr['values'] if matched_attr else [], } if e['type'] not in entity_types: entity_value = [] entity_value.append(entity) entity_ty = { 'entityType': e['type'], 'entities': entity_value } entity_type.append(entity_ty) entity_types.append(e['type']) else: entity_value.append(entity) entity_type.pop() entity_ty = { 'entityType': e['type'], 'entities': entity_value } entity_type.append(entity_ty) attrs_value = {'attrName': at, 'types': entity_type} attrs_values.append(attrs_value) res = {'attrs': attrs_values} logging.getLogger(__name__).info("Query processed successfully") logging.warning( "usage of id and type rather than entityId and entityType from version 0.9" ) return res if err == "AggrMethod cannot be applied": r = { "error": "AggrMethod cannot be applied", "description": "AggrMethod cannot be applied on type TEXT and BOOLEAN." } logging.getLogger(__name__).info("AggrMethod cannot be applied") return r, 404 r = { "error": "Not Found", "description": "No records were found for such query." } logging.getLogger(__name__).info("No value found for query") return r, 404
def query(): """ See /op/query in API Specification quantumleap.yml """ if request.json is None: return 'Discarding query due to lack of request body. ' \ 'Lost in a redirect maybe?', 400 if 'entities' not in request.json: return 'Discarding query due to lack of request body ' \ 'content.', 400 # Validate request body error = _validate_body(request.json) if error: return error, 400 entity = request.json['entities'] attrs = request.json['attrs'] res = [] # Validate entity for et in entity: entity_type = et["type"] entity_id = et["id"] eid = entity_id.split() entities = None try: with translator_for(fiware_s()) as trans: entities = trans.query_last_value( attr_names=attrs, entity_type=entity_type, entity_ids=eid, fiware_service=fiware_s(), fiware_servicepath=fiware_sp()) except NGSIUsageError as e: msg = "Bad Request Error: {}".format(e) logging.getLogger(__name__).error(msg, exc_info=True) return { "error": "{}".format(type(e)), "description": str(e) }, 400 except InvalidParameterValue as e: msg = "Bad Request Error: {}".format(e) logging.getLogger(__name__).error(msg, exc_info=True) return { "error": "{}".format(type(e)), "description": str(e) }, 422 except Exception as e: msg = "Something went wrong with QL. Error: {}".format(e) logging.getLogger(__name__).error(msg, exc_info=True) return msg, 500 if entities: if len(entities) > 1: import warnings warnings.warn( "Not expecting more than one result for a 1T1ENA.") logging.getLogger(__name__).info("Query processed successfully") res.append(entities[0]) else: r = { "error": "Not Found", "description": "No records were found for such query." } logging.getLogger(__name__).info("No value found for query") return r, 404 return res, 200
def query_NTNE1A(attr_name, # In Path type_=None, # In Query id_=None, aggr_method=None, aggr_period=None, aggr_scope=None, options=None, from_date=None, to_date=None, last_n=None, limit=10000, offset=0, georel=None, geometry=None, coords=None): """ See /attrs/{attrName} in API Specification quantumleap.yml """ r, c = _validate_query_params([attr_name], aggr_period, aggr_method, aggr_scope, options) if c != 200: return r, c r, c, geo_query = handle_geo_query(georel, geometry, coords) if r: return r, c fiware_s = request.headers.get('fiware-service', None) fiware_sp = request.headers.get('fiware-servicepath', None) entities = None entity_ids = None if id_: entity_ids = [s.strip() for s in id_.split(',') if s] try: with translator_for(fiware_s) as trans: entities = trans.query(attr_names=[attr_name], entity_type=type_, entity_ids=entity_ids, aggr_method=aggr_method, aggr_period=aggr_period, aggr_scope=aggr_scope, from_date=from_date, to_date=to_date, last_n=last_n, limit=limit, offset=offset, fiware_service=fiware_s, fiware_servicepath=fiware_sp, geo_query=geo_query) except NGSIUsageError as e: msg = "Bad Request Error: {}".format(e) logging.getLogger().error(msg, exc_info=True) return msg, 400 except InvalidParameterValue as e: return { "error": "{}".format(type(e)), "description": str(e) }, 422 except Exception as e: msg = "Internal server Error: {}".format(e) logging.getLogger().error(msg, exc_info=True) return msg, 500 attributes = [] entries = [] entity_value = [] entity_type = [] entity_types = [] if entities: for e in entities: matched_attr = lookup_string_match(e, attr_name) try: f_date = dateutil.parser.isoparse(from_date).replace(tzinfo=timezone.utc).isoformat() except Exception as ex: f_date = '' try: t_date = dateutil.parser.isoparse(to_date).replace(tzinfo=timezone.utc).isoformat() except Exception as ex: t_date = '' index = [f_date, t_date] if aggr_method and not aggr_period else e['index'] entity = { 'entityId': e['id'], 'index': index, 'values': matched_attr['values'] if matched_attr else [] } if e['type'] not in entity_types: entity_value = [] entity_value.append(entity) entity_ty = { 'entityType': e['type'], 'entities': entity_value } entity_type.append(entity_ty) entity_types.append(e['type']) else: entity_value.append(entity) entity_type.pop() entity_ty = { 'entities': entity_value, 'entityType': e['type'] } entity_type.append(entity_ty) res = { 'attrName': attr_name, 'types': entity_type } return res r = { "error": "Not Found", "description": "No records were found for such query." } return r, 404
def query_1T1E1A( attr_name, # In Path entity_id, type_=None, # In Query aggr_method=None, aggr_period=None, options=None, from_date=None, to_date=None, last_n=None, limit=10000, offset=0, georel=None, geometry=None, coords=None): """ See /entities/{entityId}/attrs/{attrName} in API Specification quantumleap.yml """ r, c = _validate_query_params([attr_name], aggr_period, aggr_method, options=options) if c != 200: return r, c r, c, geo_query = handle_geo_query(georel, geometry, coords) if r: return r, c fiware_s = request.headers.get('fiware-service', None) fiware_sp = request.headers.get('fiware-servicepath', None) entities = None try: with translator_for(fiware_s) as trans: entities = trans.query(attr_names=[attr_name], entity_type=type_, entity_id=entity_id, aggr_method=aggr_method, aggr_period=aggr_period, from_date=from_date, to_date=to_date, last_n=last_n, limit=limit, offset=offset, fiware_service=fiware_s, fiware_servicepath=fiware_sp, geo_query=geo_query) except NGSIUsageError as e: msg = "Bad Request Error: {}".format(e) logging.getLogger(__name__).error(msg, exc_info=True) return {"error": "{}".format(type(e)), "description": str(e)}, 400 except Exception as e: # Temp workaround to debug test_not_found msg = "Something went wrong with QL. Error: {}".format(e) logging.getLogger(__name__).error(msg, exc_info=True) return msg, 500 if entities: if len(entities) > 1: import warnings warnings.warn("Not expecting more than one result for a 1T1E1A.") index = [] if aggr_method and not aggr_period else entities[0]['index'] matched_attr = lookup_string_match(entities[0], attr_name) res = { 'entityId': entities[0]['id'], 'entityType': entities[0]['type'], 'attrName': attr_name, 'index': index, 'values': matched_attr['values'] if matched_attr else [] } logging.getLogger(__name__).info("Query processed successfully") return res r = { "error": "Not Found", "description": "No records were found for such query." } logging.getLogger(__name__).info("No value found for query") return r, 404
def query_entities_array(entity_type=None, # In Path id_=None, # In Query attrs=None, aggr_method=None, aggr_period=None, aggr_scope=None, options=None, from_date=None, to_date=None, last_n=None, limit=10000, offset=0, georel=None, geometry=None, coords=None, pattern=None): """ See /entitiesArray/{entityType} in API Specification quantumleap.yml """ r, c = _validate_query_params(attrs, aggr_period, aggr_method, aggr_scope) if c != 200: return r, c r, c, geo_query = handle_geo_query(georel, geometry, coords) if r: return r, c if attrs is not None: attrs = attrs.split(',') fiware_s = request.headers.get('fiware-service', None) fiware_sp = request.headers.get('fiware-servicepath', None) entities = None entity_ids = None if id_: entity_ids = [s.strip() for s in id_.split(',') if s] try: with translator_for(fiware_s) as trans: entities = trans.query_array(attr_names=attrs, entity_type=entity_type, entity_ids=entity_ids, aggr_method=aggr_method, aggr_period=aggr_period, aggr_scope=aggr_scope, from_date=from_date, to_date=to_date, last_n=last_n, limit=limit, offset=offset, fiware_service=fiware_s, fiware_servicepath=fiware_sp, geo_query=geo_query, pattern=pattern, options=options) except NGSIUsageError as e: msg = "Bad Request Error: {}".format(e) logging.getLogger(__name__).error(msg, exc_info=True) return { "error": "{}".format(type(e)), "description": str(e) }, 400 except InvalidParameterValue as e: msg = "Bad Request Error: {}".format(e) logging.getLogger(__name__).error(msg, exc_info=True) return { "error": "{}".format(type(e)), "description": str(e) }, 422 except Exception as e: msg = "Something went wrong with QL. Error: {}".format(e) logging.getLogger(__name__).error(msg, exc_info=True) return msg, 500 if entities: return entities r = { "error": "Not Found", "description": "No records were found for such query." } logging.getLogger(__name__).info("No value found for query") return r, 404
def query_1TNE1A( attr_name, # In Path entity_type, id_=None, # In Query aggr_method=None, aggr_period=None, aggr_scope=None, options=None, from_date=None, to_date=None, last_n=None, limit=10000, offset=0, georel=None, geometry=None, coords=None): """ See /types/{entityType}/attrs/{attrName} in API Specification quantumleap.yml """ r, c = _validate_query_params([attr_name], aggr_period, aggr_method, aggr_scope, options) if c != 200: return r, c r, c, geo_query = handle_geo_query(georel, geometry, coords) if r: return r, c fiware_s = request.headers.get('fiware-service', None) fiware_sp = request.headers.get('fiware-servicepath', None) entities = None entity_ids = None if id_: entity_ids = [s.strip() for s in id_.split(',') if s] try: with translator_for(fiware_s) as trans: entities = trans.query(attr_names=[attr_name], entity_type=entity_type, entity_ids=entity_ids, aggr_method=aggr_method, aggr_period=aggr_period, aggr_scope=aggr_scope, from_date=from_date, to_date=to_date, last_n=last_n, limit=limit, offset=offset, fiware_service=fiware_s, fiware_servicepath=fiware_sp, geo_query=geo_query) except NGSIUsageError as e: return {"error": "{}".format(type(e)), "description": str(e)}, 400 except InvalidParameterValue as e: return {"error": "{}".format(type(e)), "description": str(e)}, 422 except Exception as e: # Temp workaround to debug test_not_found msg = "Something went wrong with QL. Error: {}".format(e) logging.getLogger().error(msg, exc_info=True) return msg, 500 if entities: res = _prepare_response( entities, attr_name, entity_type, entity_ids, aggr_method, aggr_period, from_date, to_date, ) return res r = { "error": "Not Found", "description": "No records were found for such query." } return r, 404
def notify(): if request.json is None: return 'Discarding notification due to lack of request body. ' \ 'Lost in a redirect maybe?', 400 if 'data' not in request.json: return 'Discarding notification due to lack of request body ' \ 'content.', 400 payload = request.json['data'] # preprocess and validate each entity update for entity in payload: # Validate entity update error = _validate_payload(entity) if error: # TODO in this way we return error for even if only one entity is wrong return error, 400 # Add TIME_INDEX attribute custom_index = request.headers.get(TIME_INDEX_HEADER_NAME, None) entity[TIME_INDEX_NAME] = \ select_time_index_value_as_iso(custom_index, entity) # Add GEO-DATE if enabled add_geodata(entity) # Always normalize location if there's one normalize_location(entity) # Define FIWARE tenant fiware_s = request.headers.get('fiware-service', None) # It seems orion always sends a 'Fiware-Servicepath' header with value '/' # But this is not correctly documented in the API, so in order not to # depend on this, QL will not treat servicepath if there's no service # specified. if fiware_s: fiware_sp = request.headers.get('fiware-servicepath', None) else: fiware_sp = None res_entity = [] e = None for entity in payload: # Validate entity update e = _filter_empty_entities(entity) if e is not None: e_new = _filter_no_type_no_value_entities(e) res_entity.append(e_new) payload = res_entity entity_id = [i["id"] for i in payload] # Send valid entities to translator try: with translator_for(fiware_s) as trans: trans.insert(payload, fiware_s, fiware_sp) except Exception as e: msg = "Notification not processed or not updated for payload: %s. " \ "%s" % (payload, str(e)) log().error(msg) error_code = 500 if e.__class__ == InvalidHeaderValue or \ e.__class__ == InvalidParameterValue or \ e.__class__ == NGSIUsageError: error_code = 400 return msg, error_code msg = "Notification successfully processed for : 'tenant' %s, " \ "'fiwareServicePath' %s, " \ "'entity_id' %s" % (fiware_s, fiware_sp, entity_id) log().info(msg) return msg
def query_1TNENA( entity_type=None, # In Path id_=None, # In Query attrs=None, aggr_method=None, aggr_period=None, aggr_scope=None, options=None, from_date=None, to_date=None, last_n=None, limit=10000, offset=0, georel=None, geometry=None, coords=None): """ See /types/{entityType} in API Specification quantumleap.yml """ r, c = _validate_query_params(attrs, aggr_period, aggr_method, aggr_scope, options) if c != 200: return r, c r, c, geo_query = handle_geo_query(georel, geometry, coords) if r: return r, c if attrs is not None: attrs = attrs.split(',') fiware_s = request.headers.get('fiware-service', None) fiware_sp = request.headers.get('fiware-servicepath', None) entities = None entity_ids = None if id_: entity_ids = [s.strip() for s in id_.split(',') if s] try: with translator_for(fiware_s) as trans: entities, err = trans.query(attr_names=attrs, entity_type=entity_type, entity_ids=entity_ids, aggr_method=aggr_method, aggr_period=aggr_period, aggr_scope=aggr_scope, from_date=from_date, to_date=to_date, last_n=last_n, limit=limit, offset=offset, fiware_service=fiware_s, fiware_servicepath=fiware_sp, geo_query=geo_query) except NGSIUsageError as e: msg = "Bad Request Error: {}".format(e) logging.getLogger(__name__).error(msg, exc_info=True) return {"error": "{}".format(type(e)), "description": str(e)}, 400 except InvalidParameterValue as e: msg = "Bad Request Error: {}".format(e) logging.getLogger(__name__).error(msg, exc_info=True) return {"error": "{}".format(type(e)), "description": str(e)}, 422 except Exception as e: msg = "Something went wrong with QL. Error: {}".format(e) logging.getLogger(__name__).error(msg, exc_info=True) return msg, 500 if err == "AggrMethod cannot be applied": r = { "error": "AggrMethod cannot be applied", "description": "AggrMethod cannot be applied on type TEXT and BOOLEAN." } logging.getLogger(__name__).info("AggrMethod cannot be applied") return r, 404 if entities: res = _prepare_response( entities, attrs, entity_type, entity_ids, aggr_method, aggr_period, from_date, to_date, ) logging.getLogger(__name__).info("Query processed successfully") logging.warning( "usage of id and type rather than entityId and entityType from version 0.9" ) return res r = { "error": "Not Found", "description": "No records were found for such query." } logging.getLogger(__name__).info("No value found for query") return r, 404