def _prepare_response(entities, attr_name, entity_type, entity_ids, aggr_method, aggr_period, from_date, to_date): values = {} for e in entities: matched_attr = lookup_string_match(e, attr_name) values[e['id']] = matched_attr['values'] if matched_attr else [] if aggr_method and not aggr_period: # Use fromDate / toDate indexes = [from_date or '', to_date or ''] else: indexes = {} for e in entities: indexes[e['id']] = e['index'] # Preserve given order of ids (if any) entries = [] if entity_ids: for ei in entity_ids: if ei in values: if aggr_method and not aggr_period: index = indexes else: index = indexes[ei] i = { 'entityId': ei, 'index': index, 'values': values[ei], } entries.append(i) values.pop(ei) # Proceed with the rest of the values in order of keys for e_id in sorted(values.keys()): index = [] if aggr_method and not aggr_period else indexes[e_id] i = { 'entityId': e_id, 'index': index, 'values': values[e_id], } entries.append(i) res = { 'entityType': entity_type, 'attrName': attr_name, 'entities': entries } return res
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): """ 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', None) entities = None entity_ids = None if id_: entity_ids = [s.strip() for s in id_.split(',') if s] try: with CrateTranslatorInstance() as trans: entities = 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, 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 Exception as e: msg = "Something went wrong with QL. Error: {}".format(e) logging.getLogger().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: index = [ from_date or '', to_date or '' ] 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} return res r = { "error": "Not Found", "description": "No records were found for such 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_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 CrateTranslatorInstance() 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: 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().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'], 'attrName': attr_name, 'index': index, 'values': matched_attr['values'] if matched_attr else [] } 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', '/') entities = None try: with translator_for(fiware_s) as trans: entities, err = 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) logging.warning( "usage of id and type rather than entityId and entityType from version 0.9") 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 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 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_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