def get_with(asset_id, asset_type, organization_id=None):
    """ Gets an asset from database
    Request parameters:
        - asset_id: database id of the asset
        - asset_type: type of the requested asset, can be "device" or "gateway".
        - organization_id (optional): when given, asserts that received organization
            matchs the asset's organization
    Returns:
        - Model object of requested asset
    """
    if asset_type == "device":
        asset = Device.query.get(asset_id)
        if not asset:
            raise Error.NotFound(
                f"Asset with id {asset_id} and type {asset_type} not found")
        device_session = db.session.query(DeviceSession).\
            filter(DeviceSession.device_id==asset_id).\
            order_by(DeviceSession.last_activity.desc()).\
            first()
        asset.dev_addr = device_session.dev_addr if device_session else None
        asset.hex_id = asset.dev_eui
    elif asset_type == "gateway":
        asset = db.session.query(Gateway).\
            filter(Gateway.id == asset_id).\
            first()
        if not asset:
            raise Error.NotFound(
                f"Asset with id {asset_id} and type {asset_type} not found")
        asset.hex_id = asset.gw_hex_id
    else:
        raise Error.BadRequest(
            f"Invalid asset_type: {asset_type}. Valid values are \'device\' or \'gateway\'"
        )
    if organization_id and asset.organization_id != organization_id:
        raise Error.Forbidden(
            "User's organization's different from asset organization")
    asset.type = asset_type
    return asset
Ejemplo n.º 2
0
def get_with(asset_id, asset_type, organization_id=None):
    """ Gets an asset from database
    Request parameters:
        - asset_id: database id of the asset
        - asset_type: type of the requested asset, can be "device" or "gateway".
        - organization_id (optional): when given, asserts that received organization
            matchs the asset's organization
    Returns:
        - Model object of requested asset
    """
    if asset_type == "device":
        asset_query = db.session.query(
            Device.id,
            Device.organization_id,
            Device.dev_eui.label('hex_id'),
            expression.literal_column('\'Device\'').label('type'),
            Device.name,
            Device.app_name,
            DataCollector.name.label('data_collector'),
            PolicyItem.parameters.label('policy_parameters'),
            Device.connected.label('connected'),
            Device.last_activity,
            Device.activity_freq,
            Device.activity_freq_variance,
            Device.npackets_up,
            Device.npackets_down,
            Device.npackets_lost.label('packet_loss'),
            Device.max_rssi,
            Device.max_lsnr,
            Device.ngateways_connected_to,
            Device.payload_size,
            Device.last_packets_list
            ).filter(Device.id == asset_id).\
                join(DataCollector, Device.data_collector_id == DataCollector.id).\
                join(Policy, Policy.id == DataCollector.policy_id).\
                join(PolicyItem, and_(Policy.id == PolicyItem.policy_id, PolicyItem.alert_type_code == 'LAF-401')).\
                join(RowProcessed, RowProcessed.analyzer == 'packet_analyzer').\
                join(Packet, Packet.id == RowProcessed.last_row).\
                join(DeviceCounters, and_(
                    DeviceCounters.device_id == Device.id,
                    DeviceCounters.counter_type.in_(dev_wanted_counters),
                    DeviceCounters.last_update + func.make_interval(0,0,0,1) > Packet.date
                    ), isouter=True).\
                group_by(Device.id, DataCollector.name, PolicyItem.parameters)

        asset_query = add_counters_columns(asset_query, dev_wanted_counters,
                                           DeviceCounters)
        asset = asset_query.first()

    elif asset_type == "gateway":
        asset_query = db.session.query(
            Gateway.id,
            Gateway.organization_id,
            Gateway.gw_hex_id.label('hex_id'),
            expression.literal_column('\'Gateway\'').label('type'),
            Gateway.name,
            expression.null().label('app_name'),
            DataCollector.name.label('data_collector'),
            expression.null().label('policy_parameters'),
            Gateway.connected.label('connected'),
            Gateway.last_activity,
            Gateway.activity_freq,
            cast(expression.null(), Float).label('activity_freq_variance'),
            Gateway.npackets_up,
            Gateway.npackets_down,
            cast(expression.null(), Float).label('packet_loss'),
            cast(expression.null(), Float).label('max_rssi'),
            cast(expression.null(), Float).label('max_lsnr'),
            cast(expression.null(), Float).label('ngateways_connected_to'),
            cast(expression.null(), Float).label('payload_size'),
            expression.null().label('last_packets_list')
            ).filter(Gateway.id == asset_id).\
                join(DataCollector, Gateway.data_collector_id == DataCollector.id).\
                join(RowProcessed, RowProcessed.analyzer == 'packet_analyzer').\
                join(Packet, Packet.id == RowProcessed.last_row).\
                join(GatewayCounters, and_(
                    GatewayCounters.gateway_id == Gateway.id,
                    GatewayCounters.counter_type.in_(gtw_wanted_counters),
                    GatewayCounters.last_update + func.make_interval(0,0,0,1) > Packet.date
                    ), isouter=True).\
                group_by(Gateway.id, DataCollector.name)

        asset_query = add_counters_columns(asset_query, gtw_wanted_counters,
                                           GatewayCounters)
        asset = asset_query.first()
    else:
        raise Error.BadRequest(
            f"Invalid asset_type: {asset_type}. Valid values are \'device\' or \'gateway\'"
        )
    if not asset:
        raise Error.NotFound(
            f"Asset with id {asset_id} and type {asset_type} not found")
    if organization_id and asset.organization_id != organization_id:
        raise Error.Forbidden(
            "User's organization's different from asset organization")
    return asset