Пример #1
0
def get_assets_payload():
    """ Get all assets from uframe, process in ooi-ui-services list of assets (asset_list) and
    assets_dict by (key) asset id. Update cache for asset_list and assets_dict.
    """
    try:
        get_vocab()
        # Get uframe connect and timeout information
        uframe_url, timeout, timeout_read = get_uframe_assets_info()
        url = '/'.join([uframe_url, 'assets'])
        payload = requests.get(url, timeout=(timeout, timeout_read))
        if payload.status_code != 200:
            message = '(%d) Failed to get uframe assets.' % payload.status
            current_app.logger.info(message)
            return internal_server_error(message)

        result = payload.json()
        data, assets_dict = _compile_assets(result)
        if "error" not in data:
            cache.set('asset_list', data, timeout=CACHE_TIMEOUT)
            data = cache.get('asset_list')

        return data

    except requests.exceptions.ConnectionError as err:
        message = "ConnectionError getting uframe assets; %s" % str(err)
        current_app.logger.info(message)
        return internal_server_error(message)
    except requests.exceptions.Timeout as err:
        message = "Timeout getting uframe assets;  %s" % str(err)
        current_app.logger.info(message)
        return internal_server_error(message)
    except Exception as err:
        message = "Error getting uframe assets; %s" % str(err)
        current_app.logger.info(message)
        raise
Пример #2
0
def get_asset(id):
    """ Get asset by id.
    Object response for the GET(id) request.  This response is NOT cached.
    """
    try:
        uframe_url, timeout, timeout_read = get_uframe_assets_info()
        if id == 0:
            error = 'Zero (0) is an invalid asset id value.'
            current_app.logger.info(error)
            return make_response(error, 400)

        url = '/'.join([uframe_url, 'assets', str(id)])
        payload = requests.get(url, timeout=(timeout, timeout_read))
        if payload.status_code != 200:
            error = 'Unable to locate an asset with an id of %d.' % id
            current_app.logger.info(error)
            return make_response(error, 400)
        data = payload.json()
        data_list = [data]
        result, _ = _compile_assets(data_list)
        return jsonify(**result[0])

    except ConnectionError:
        error = 'Error: ConnectionError during GET request for asset with id %d.' % id
        current_app.logger.info(error)
        return bad_request(error)
    except Timeout:
        error = 'Error: Timeout during GET request for asset with id %d.' % id
        current_app.logger.info(error)
        return bad_request(error)
    except Exception as err:
        error = 'Error processing GET request for asset with id %d. %s' % (id, str(err))
        current_app.logger.info(error)
        return bad_request(error)
Пример #3
0
def update_asset(id):
    try:
        data = json.loads(request.data)

        if 'asset_class' in data:
            data['@class'] = data.pop('asset_class')

        url = current_app.config['UFRAME_ASSETS_URL']\
            + '/%s/%s' % ('assets', id)

        response = requests.put(url,
                                data=json.dumps(data),
                                headers=_uframe_headers())

        if response.status_code == 200:
            asset_cache = cache.get('asset_list')
            data_list = []
            data_list.append(data)
            data = _compile_assets(data_list)
            if asset_cache:
                cache.delete('asset_list')
                for row in asset_cache:
                    if row['id'] == id:
                        row.update(data[0])

            if "error" not in asset_cache:
                cache.set('asset_list', asset_cache, timeout=CACHE_TIMEOUT)
        return response.text, response.status_code

    except requests.exceptions.ConnectionError as e:
        error = "Error: Cannot connect to uframe.  %s" % e
        print error
        return make_response(error, 500)
Пример #4
0
def create_asset():
    """ Create a new asset, the return will be uframe asset format (not ooi-ui-services format).
    Cache ('asset_list') is updated with new asset
    Either a success or an error message.
    Login required.
    """
    debug = False
    try:
        data = json.loads(request.data)
        if valid_create_asset_request_data(data):
            if debug: print '\n debug validated required fields...'

        url = current_app.config['UFRAME_ASSETS_URL'] + '/%s' % 'assets'
        if 'lastModifiedTimestamp' in data:
            del data['lastModifiedTimestamp']
        if 'asset_class' in data:
            data['@class'] = data.pop('asset_class')

        # Create asset in uframe
        response = requests.post(url, data=json.dumps(data), headers=_uframe_headers())

        if response.status_code == 201:
            json_response = json.loads(response.text)
            data['assetId'] = json_response['id']
            data['tense'] = 'NEW'
            data_list = [data]
            try:
                compiled_data, _ = _compile_assets(data_list)
            except Exception:
                raise

            if not compiled_data or compiled_data is None:
                raise Exception('_compile_assets returned empty or None result.')

            # Update asset cache ('asset_list')
            asset_cache = cache.get('asset_list')
            if asset_cache:
                cache.delete('asset_list')
                asset_cache.append(compiled_data[0])
                cache.set('asset_list', asset_cache, timeout=CACHE_TIMEOUT)
        else:
            return bad_request('Failed to create asset!')

        return response.text, response.status_code

    except ConnectionError:
        message = 'ConnectionError during create asset.'
        current_app.logger.info(message)
        return bad_request(message)
    except Timeout:
        message = 'Timeout during during create asset.'
        current_app.logger.info(message)
        return bad_request(message)
    except Exception as err:
        message = str(err)
        current_app.logger.info(message)
        return bad_request(message)
Пример #5
0
def update_asset(id):
    """ Update asset by id.
    Last writer wins; new format of request.data to be handled (post 5/31):
        {"assetInfo.array":"EnduranceAss","assetInfo.assembly":"testass","oper":"edit","id":"227"}
    """
    try:
        data = json.loads(request.data)

        if 'asset_class' in data:
            data['@class'] = data.pop('asset_class')

        url = current_app.config['UFRAME_ASSETS_URL'] + '/%s/%s' % ('assets', id)
        response = requests.put(url, data=json.dumps(data), headers=_uframe_headers())
        if response.status_code != 200:
            message = '(%d) Failed to update asset %d.' % (response.status_code, id)
            return bad_request(message)

        if response.status_code == 200:
            data_list = [data]
            try:
                compiled_data, _ = _compile_assets(data_list)
            except Exception:
                raise

            if not compiled_data or compiled_data is None:
                raise Exception('_compile_assets returned empty or None result.')

            asset_cache = cache.get('asset_list')
            if "error" in asset_cache:
                message = 'Error returned in \'asset_list\' cache; unable to update cache.'
                return bad_request(message)

            if asset_cache:
                cache.delete('asset_list')
                for row in asset_cache:
                    if row['id'] == id:
                        row.update(compiled_data[0])
                        break
                cache.set('asset_list', asset_cache, timeout=CACHE_TIMEOUT)

        return response.text, response.status_code

    except ConnectionError:
        message = 'Error: ConnectionError during update asset request (id: %d)' % id
        current_app.logger.info(message)
        return bad_request(message)
    except Timeout:
        message = 'Error: Timeout during during update asset request (id: %d)' % id
        current_app.logger.info(message)
        return bad_request(message)
    except Exception as err:
        message = str(err)
        current_app.logger.info(message)
        return bad_request(message)
Пример #6
0
def compile_assets():
    try:
        print '\n debug - *** tasks - compile_assets()'
        with current_app.test_request_context():
            print "[+] Starting asset cache reset..."
            cache = Cache(config={'CACHE_TYPE': 'redis', 'CACHE_REDIS_DB': 0})
            cache.init_app(current_app)
            url = current_app.config['UFRAME_ASSETS_URL'] + '/%s' % ('assets')
            payload = requests.get(url)
            if payload.status_code is 200:

                # Cache assets_list
                data = payload.json()
                assets, asset_rds = _compile_assets(data)
                if "error" not in assets:
                    cache.set('asset_list', assets, timeout=CACHE_TIMEOUT)
                    print "[+] Asset list cache reset"

                    # Cache assets_dict (based on success of _compile_assets returning assets)
                    assets_dict = get_assets_dict_from_list(assets)
                    if not assets_dict:
                        message = 'Warning: get_assets_dict_from_list returned empty assets_dict.'
                        print '\n debug -- message: ', message
                        current_app.logger.info(message)
                    if isinstance(assets_dict, dict):
                        cache.set('assets_dict', assets_dict, timeout=CACHE_TIMEOUT)
                        print "[+] Assets dictionary cache reset"
                    else:
                        print "[-] Error in Assets dictionary cache update"
                else:
                    print "[-] Error in asset_list and asset_dict cache update"

                # Cache assets_rd
                if asset_rds:
                    cache.set('asset_rds', asset_rds, timeout=CACHE_TIMEOUT)
                    print "[+] Asset reference designators cache reset..."
                else:
                    print "[-] Error in asset_rds cache update"

            else:
                print "[-] Error in cache update"
    except Exception as err:
        message = 'compile_assets exception: %s' % err.message
        current_app.logger.warning(message)
        raise Exception(message)
Пример #7
0
def get_asset(id):
    '''
    Object response for the GET(id) request.  This response is NOT cached.
    '''
    try:
        url = current_app.config['UFRAME_ASSETS_URL']\
            + '/%s/%s' % ('assets', id)
        payload = requests.get(url)

        data = payload.json()
        data_list = []
        data_list.append(data)
        data = _compile_assets(data_list)
        return jsonify(**data[0])

    except requests.exceptions.ConnectionError as e:
        error = "Error: Cannot connect to uframe.  %s" % e
        print error
        return make_response(error, 500)
Пример #8
0
def create_asset():
    '''
    Create a new asset, the return will be right from uframe if all goes well.
    Either a success or an error message.
    Login required.
    '''
    try:
        data = json.loads(request.data)
        url = current_app.config['UFRAME_ASSETS_URL']\
            + '/%s' % ('assets')

        if 'lastModifiedTimestamp' in data:
            del data['lastModifiedTimestamp']

        if 'asset_class' in data:
            data['@class'] = data.pop('asset_class')

        response = requests.post(url,
                                 data=json.dumps(data),
                                 headers=_uframe_headers())

        if response.status_code == 201:
            json_response = json.loads(response.text)
            data['id'] = json_response['id']
            data['tense'] = 'NEW'
            data_list = []
            data_list.append(data)
            data = _compile_assets(data_list)

            asset_cache = cache.get('asset_list')
            if asset_cache:
                cache.delete('asset_list')
                asset_cache.append(data[0])
                cache.set('asset_list', asset_cache, timeout=CACHE_TIMEOUT)

        return response.text, response.status_code

    except requests.exceptions.ConnectionError as e:
        error = "Error: Cannot connect to uframe.  %s" % e
        return make_response(error, 500)
Пример #9
0
def get_assets(use_min=False, normal_data=False, reset=False):
    '''
    Listing GET request of all assets.  This method is cached for 1 hour.
    add in helped params to bypass the json response and minification
    '''
    try:
        cached = cache.get('asset_list')

        if cached and reset is not True:
            data = cached
        else:

            url = current_app.config['UFRAME_ASSETS_URL']\
                + '/%s' % ('assets')

            payload = requests.get(url)

            if payload.status_code != 200:
                try:
                    return jsonify({"assets": payload.json()}),\
                        payload.status_code
                except AttributeError:
                    try:
                        return jsonify({"assets": 'Undefined response'}),\
                            payload.status_code
                    except Exception as e:
                        return make_response(
                            "unhandled exception: %s.  Line # %s"
                            % (e, sys.exc_info()[2].tb_lineno), 500)

            data = payload.json()

            data = _compile_assets(data)

            if "error" not in data:
                cache.set('asset_list', data, timeout=CACHE_TIMEOUT)

    except requests.exceptions.ConnectionError as e:
        error = "Error: Cannot connect to uframe.  %s" % e
        print error
        return make_response(error, 500)

    try:
        sort_by = ''
        if request.args.get('sort') and request.args.get('sort') != "":
            sort_by = request.args.get('sort')
        else:
            sort_by = 'ref_des'
        data = sorted(data, key=itemgetter(sort_by))

    except Exception as e:
        print e
        pass

    if request.args.get('min') == 'True' or use_min is True:
        showDeployments = False
        deploymentEvents = []
        if request.args.get('deployments') == 'True':
            showDeployments = True
        for obj in data:
            try:
                if 'metaData' in obj:
                    del obj['metaData']
                if 'events' in obj:
                    if showDeployments and obj['events'] is not None:
                        for event in obj['events']:
                            if event['eventClass'] == '.DeploymentEvent':
                                deploymentEvents.append(event)
                        del obj['events']
                        obj['events'] = deploymentEvents
                        deploymentEvents = []
                    else:
                        del obj['events']
                if 'manufactureInfo' in obj:
                    del obj['manufactureInfo']
                if 'notes' in obj:
                    del obj['notes']
                if 'physicalInfo' in obj:
                    del obj['physicalInfo']
                if 'attachments' in obj:
                    del obj['attachments']
                if 'purchaseAndDeliveryInfo' in obj:
                    del obj['purchaseAndDeliveryInfo']
                if 'lastModifiedTimestamp' in obj:
                    del obj['lastModifiedTimestamp']
            except Exception:
                raise

    if request.args.get('concepts') and request.args.get('concepts') != "":
        return_list = []
        search_term = str(request.args.get('concepts')).split()
        search_set = set(search_term)
        for subset in search_set:
            for item in data:
                print item['ref_des']
                if subset.lower() in str(item['ref_des']).lower():
                    return_list.append(item)
        data = return_list

    if request.args.get('search') and request.args.get('search') != "":
        return_list = []
        search_term = str(request.args.get('search')).split()
        search_set = set(search_term)
        try:
            for subset in search_set:
                if len(return_list) > 0:
                    ven_subset = []
                    return_list = deepcopy(data)
                    for item in return_list:
                        if subset.lower() in\
                                str(item['assetInfo']['name']).lower():
                            ven_subset.append(item)
                        elif subset.lower() in\
                                str(item['assetInfo']['longName']).lower():
                            ven_subset.append(item)
                        elif subset.lower() in\
                                str(item['ref_des']).lower():
                            ven_subset.append(item)
                        elif subset.lower() in\
                                str(item['assetInfo']['type']).lower():
                            ven_subset.append(item)
                        elif subset.lower() in\
                                str(item['assetInfo']['array']).lower():
                            ven_subset.append(item)
                        elif subset.lower() in\
                                str(item['events']).lower():
                            ven_subset.append(item)
                        elif subset.lower() in\
                                str(item['metaData']).lower():
                            ven_subset.append(item)
                        elif subset.lower() in\
                                str(item['tense']).lower():
                            ven_subset.append(item)
                    data = ven_subset
                else:
                    for item in data:
                        if subset.lower() in\
                                str(item['assetInfo']['name']).lower():
                            return_list.append(item)
                        elif subset.lower() in\
                                str(item['assetInfo']['longName']).lower():
                            return_list.append(item)
                        elif subset.lower() in\
                                str(item['ref_des']).lower():
                            return_list.append(item)
                        elif subset.lower() in\
                                str(item['assetInfo']['type']).lower():
                            return_list.append(item)
                        elif subset.lower() in\
                                str(item['assetInfo']['array']).lower():
                            return_list.append(item)
                        elif subset.lower() in\
                                str(item['events']).lower():
                            return_list.append(item)
                        elif subset.lower() in\
                                str(item['metaData']).lower():
                            return_list.append(item)
                        elif subset.lower() in\
                                str(item['tense']).lower():
                            return_list.append(item)
                    data = return_list
        except KeyError as e:
            pass

    if request.args.get('startAt'):
        start_at = int(request.args.get('startAt'))
        count = int(request.args.get('count'))
        total = int(len(data))
        data_slice = data[start_at:(start_at + count)]
        result = jsonify({"count": count,
                          "total": total,
                          "startAt": start_at,
                          "assets": data_slice})
        return result
    else:
        if normal_data:
            result = data
        else:
            result = jsonify({'assets': data})
        return result