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):
    """
    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

    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,)
    except AmbiguousNGSIIdError as e:
        return {
            "error": "AmbiguousNGSIIdError",
            "description": str(e)
        }, 409

    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']
        res = {
            'data': {
                'entityId': entities[0]['id'],
                'attrName': attr_name,
                'index': index,
                'values': entities[0][attr_name]['values']
            }
        }
        return res

    r = {
        "error": "Not Found",
        "description": "No records were found for such query."
    }
    return r, 404
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
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
Ejemplo n.º 5
0
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', None)

    entities = None
    try:
        with CrateTranslatorInstance() as trans:
            entities = 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 AmbiguousNGSIIdError as e:
        return {
            "error": "AmbiguousNGSIIdError",
            "description": str(e)
        }, 409

    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 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,
            'index': index,
            'attributes': attributes
        }
        return res

    r = {
        "error": "Not Found",
        "description": "No records were found for such query."
    }
    return r, 404
Ejemplo n.º 6
0
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
Ejemplo n.º 7
0
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_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
Ejemplo n.º 9
0
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
Ejemplo n.º 10
0
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
Ejemplo n.º 11
0
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):
    """
    See /entities/{entityId}/attrs/{attrName} in API Specification
    quantumleap.yml
    """
    r, c = _validate_query_params(aggr_period, aggr_method, attrs, options)
    if c != 200:
        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
    try:
        with CrateTranslatorInstance() as trans:
            entities = trans.query(
                attr_names=attrs,
                entity_type=type_,
                entity_id=entity_id,
                aggr_method=aggr_method,
                from_date=from_date,
                to_date=to_date,
                last_n=last_n,
                limit=limit,
                offset=offset,
                fiware_service=fiware_s,
                fiware_servicepath=fiware_sp,
            )
    except AmbiguousNGSIIdError as e:
        return {"error": "AmbiguousNGSIIdError", "description": str(e)}, 409

    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 aggr_method:
            index = []
        else:
            index = [str(e[CrateTranslator.TIME_INDEX_NAME]) for e in entities]

        ignore = ('type', 'id', CrateTranslator.TIME_INDEX_NAME)
        attrs = [at for at in sorted(entities[0].keys()) if at not in ignore]

        attributes = []
        for at in attrs:
            attributes.append({'attrName': at, 'values': []})

        for i, at in enumerate(attrs):
            for e in entities:
                attributes[i]['values'].append(e[at]['value'])

        res = {
            'data': {
                'entityId': entity_id,
                'index': index,
                'attributes': attributes
            }
        }
        return res

    r = {
        "error": "Not Found",
        "description": "No records were found for such query."
    }
    return r, 404