Ejemplo n.º 1
0
def get_bookmark() -> Response:
    """
    Call metadata service to fetch a specified user's bookmarks.
    If no 'user_id' is specified, it will fetch the logged-in user's bookmarks
    :param user_id: (optional) the user whose bookmarks are fetched.
    :return: a JSON object with an array of bookmarks under 'bookmarks' key
    """
    try:
        user_id = request.args.get('user_id')
        if user_id is None:
            if app.config['AUTH_USER_METHOD']:
                user_id = app.config['AUTH_USER_METHOD'](app).user_id
            else:
                raise Exception('AUTH_USER_METHOD is not configured')

        url = '{0}{1}/{2}/follow/'.format(app.config['METADATASERVICE_BASE'],
                                          USER_ENDPOINT, user_id)

        response = request_metadata(url=url, method=request.method)
        status_code = response.status_code

        tables = response.json().get('table')
        table_bookmarks = [marshall_table_partial(table) for table in tables]

        return make_response(
            jsonify({
                'msg': 'success',
                'bookmarks': table_bookmarks
            }), status_code)
    except Exception as e:
        message = 'Encountered exception: ' + str(e)
        logging.exception(message)
        return make_response(jsonify({'msg': message}),
                             HTTPStatus.INTERNAL_SERVER_ERROR)
Ejemplo n.º 2
0
def popular_tables() -> Response:
    """
    call the metadata service endpoint to get the current popular tables
    :return: a json output containing an array of popular table metadata as 'popular_tables'

    Schema Defined Here:
    https://github.com/lyft/amundsenmetadatalibrary/blob/master/metadata_service/api/popular_tables.py
    """
    try:
        url = app.config['METADATASERVICE_BASE'] + POPULAR_TABLES_ENDPOINT
        response = request_metadata(url=url)
        status_code = response.status_code

        if status_code == HTTPStatus.OK:
            message = 'Success'
            response_list = response.json().get('popular_tables')
            top4 = response_list[0:min(len(response_list), app.
                                       config['POPULAR_TABLE_COUNT'])]
            popular_tables = [
                marshall_table_partial(result) for result in top4
            ]
        else:
            message = 'Encountered error: Request to metadata service failed with status code ' + str(
                status_code)
            logging.error(message)
            popular_tables = [{}]

        payload = jsonify({'results': popular_tables, 'msg': message})
        return make_response(payload, status_code)
    except Exception as e:
        message = 'Encountered exception: ' + str(e)
        logging.exception(message)
        payload = jsonify({'results': [{}], 'msg': message})
        return make_response(payload, HTTPStatus.INTERNAL_SERVER_ERROR)
Ejemplo n.º 3
0
def get_user_own() -> Response:
    """
    Calls metadata service to GET owned resources
    :return: a JSON object with an array of owned resources
    """
    try:
        user_id = get_query_param(request.args, 'user_id')

        url = '{0}{1}/{2}/own/'.format(app.config['METADATASERVICE_BASE'],
                                       USER_ENDPOINT, user_id)
        response = request_metadata(url=url, method=request.method)
        status_code = response.status_code
        owned_tables_raw = response.json().get('table')
        owned_tables = [
            marshall_table_partial(table) for table in owned_tables_raw
        ]
        return make_response(jsonify({
            'msg': 'success',
            'own': owned_tables
        }), status_code)

    except Exception as e:
        message = 'Encountered exception: ' + str(e)
        logging.exception(message)
        return make_response(jsonify({'msg': message}),
                             HTTPStatus.INTERNAL_SERVER_ERROR)
Ejemplo n.º 4
0
def popular_resources() -> Response:
    """
    call the metadata service endpoint to get the current popular tables, dashboards etc.
    this takes a required query parameter "types", that is a comma separated string of requested resource types
    :return: a json output containing an array of popular table metadata as 'popular_tables'

    Schema Defined Here:
    https://github.com/lyft/amundsenmetadatalibrary/blob/master/metadata_service/api/popular_tables.py
    """
    try:
        if app.config['AUTH_USER_METHOD'] and app.config[
                'POPULAR_RESOURCES_PERSONALIZATION']:
            user_id = app.config['AUTH_USER_METHOD'](app).user_id
        else:
            user_id = ''

        resource_types = get_query_param(request.args, 'types')

        service_base = app.config['METADATASERVICE_BASE']
        count = app.config['POPULAR_RESOURCES_COUNT']
        url = f'{service_base}{POPULAR_RESOURCES_ENDPOINT}/{user_id}?limit={count}&types={resource_types}'

        response = request_metadata(url=url)
        status_code = response.status_code

        if status_code == HTTPStatus.OK:
            message = 'Success'
            json_response = response.json()
            tables = json_response.get(ResourceType.Table.name, [])
            popular_tables = [
                marshall_table_partial(result) for result in tables
            ]
            dashboards = json_response.get(ResourceType.Dashboard.name, [])
            popular_dashboards = [
                marshall_dashboard_partial(dashboard)
                for dashboard in dashboards
            ]
        else:
            message = 'Encountered error: Request to metadata service failed with status code ' + str(
                status_code)
            logging.error(message)
            popular_tables = []
            popular_dashboards = []

        all_popular_resources = {
            to_label(resource_type=ResourceType.Table): popular_tables,
            to_label(resource_type=ResourceType.Dashboard): popular_dashboards
        }

        payload = jsonify({'results': all_popular_resources, 'msg': message})
        return make_response(payload, status_code)
    except Exception as e:
        message = 'Encountered exception: ' + str(e)
        logging.exception(message)
        payload = jsonify({'results': [{}], 'msg': message})
        return make_response(payload, HTTPStatus.INTERNAL_SERVER_ERROR)
Ejemplo n.º 5
0
def popular_tables() -> Response:
    """
    call the metadata service endpoint to get the current popular tables
    :return: a json output containing an array of popular table metadata as 'popular_tables'

    Schema Defined Here:
    https://github.com/lyft/amundsenmetadatalibrary/blob/master/metadata_service/api/popular_tables.py
    """
    try:
        if app.config['AUTH_USER_METHOD'] and app.config[
                'POPULAR_TABLE_PERSONALIZATION']:
            user_id = app.config['AUTH_USER_METHOD'](app).user_id
        else:
            user_id = ''

        service_base = app.config['METADATASERVICE_BASE']
        count = app.config['POPULAR_TABLE_COUNT']
        url = f'{service_base}{POPULAR_TABLES_ENDPOINT}/{user_id}?limit={count}'

        response = request_metadata(url=url)
        status_code = response.status_code

        if status_code == HTTPStatus.OK:
            message = 'Success'
            response_list = response.json().get('popular_tables')
            popular_tables = [
                marshall_table_partial(result) for result in response_list
            ]
        else:
            message = 'Encountered error: Request to metadata service failed with status code ' + str(
                status_code)
            logging.error(message)
            popular_tables = [{}]

        payload = jsonify({'results': popular_tables, 'msg': message})
        return make_response(payload, status_code)
    except Exception as e:
        message = 'Encountered exception: ' + str(e)
        logging.exception(message)
        payload = jsonify({'results': [{}], 'msg': message})
        return make_response(payload, HTTPStatus.INTERNAL_SERVER_ERROR)