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 ] dashboards = response.json().get('dashboard', []) owned_dashboards = [ marshall_dashboard_partial(dashboard) for dashboard in dashboards ] all_owned = {'table': owned_tables, 'dashboard': owned_dashboards} return make_response(jsonify({ 'msg': 'success', 'own': all_owned }), status_code) except Exception as e: message = 'Encountered exception: ' + str(e) logging.exception(message) return make_response(jsonify({'msg': message}), HTTPStatus.INTERNAL_SERVER_ERROR)
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)
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 if status_code == HTTPStatus.OK: message = 'Success' tables = response.json().get('table') table_bookmarks = [ marshall_table_partial(table) for table in tables ] dashboards = response.json().get('dashboard', []) dashboard_bookmarks = [ marshall_dashboard_partial(dashboard) for dashboard in dashboards ] else: message = f'Encountered error: failed to get bookmark for user_id: {user_id}' logging.error(message) table_bookmarks = [] dashboard_bookmarks = [] all_bookmarks = { 'table': table_bookmarks, 'dashboard': dashboard_bookmarks } return make_response( jsonify({ 'msg': message, 'bookmarks': all_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)
def _search_dashboard(*, search_term: str, page_index: int, filters: Dict, search_type: str) -> Dict[str, Any]: """ Call the search service endpoint and return matching results Search service logic defined here: https://github.com/lyft/amundsensearchlibrary/blob/master/search_service/api/dashboard.py :return: a json output containing search results array as 'results' """ # Default results dashboards = { 'page_index': page_index, 'results': [], 'total_results': 0, } results_dict = { 'search_term': search_term, 'msg': '', 'dashboards': dashboards, } try: if has_filters(filters=filters, resource='dashboard'): query_json = generate_query_json(filters=filters, page_index=page_index, search_term=search_term) url_base = app.config['SEARCHSERVICE_BASE'] + SEARCH_DASHBOARD_FILTER_ENDPOINT response = request_search(url=url_base, headers={'Content-Type': 'application/json'}, method='POST', data=json.dumps(query_json)) else: url_base = app.config['SEARCHSERVICE_BASE'] + SEARCH_DASHBOARD_ENDPOINT url = f'{url_base}?query_term={search_term}&page_index={page_index}' response = request_search(url=url) status_code = response.status_code if status_code == HTTPStatus.OK: results_dict['msg'] = 'Success' results = response.json().get('results') dashboards['results'] = [marshall_dashboard_partial(result) for result in results] dashboards['total_results'] = response.json().get('total_results') else: message = 'Encountered error: Search request failed' results_dict['msg'] = message logging.error(message) results_dict['status_code'] = status_code return results_dict except Exception as e: message = 'Encountered exception: ' + str(e) results_dict['msg'] = message logging.exception(message) return results_dict
def _get_related_dashboards_metadata(*, url: str) -> Dict[str, Any]: results_dict = { 'dashboards': [], 'msg': '', } try: response = request_metadata(url=url) except ValueError as e: # envoy client BadResponse is a subclass of ValueError message = 'Encountered exception: ' + str(e) results_dict['msg'] = message results_dict['status_code'] = getattr(e, 'code', HTTPStatus.INTERNAL_SERVER_ERROR) logging.exception(message) return results_dict status_code = response.status_code results_dict['status_code'] = status_code if status_code != HTTPStatus.OK: message = f'Encountered {status_code} Error: Related dashboard metadata request failed' results_dict['msg'] = message logging.error(message) return results_dict try: dashboard_data_raw = response.json().get('dashboards', []) return { 'dashboards': [ marshall_dashboard_partial(dashboard) for dashboard in dashboard_data_raw ], 'msg': 'Success', 'status_code': status_code } except Exception as e: message = 'Encountered exception: ' + str(e) results_dict['msg'] = message logging.exception(message) # explicitly raise the exception which will trigger 500 api response results_dict['status_code'] = getattr(e, 'code', HTTPStatus.INTERNAL_SERVER_ERROR) return results_dict