Skip to content

Commit

Permalink
feat: Updated popular_tables endpoint to allow optional user_id (#232)
Browse files Browse the repository at this point in the history
Signed-off-by: Josh Howard <[email protected]>
  • Loading branch information
joshthoward authored Nov 25, 2020
1 parent 3a96f7f commit 5680775
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 11 deletions.
4 changes: 3 additions & 1 deletion metadata_service/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ def create_app(*, config_module_class: str) -> Flask:

api = Api(api_bp)

api.add_resource(PopularTablesAPI, '/popular_tables/')
api.add_resource(PopularTablesAPI,
'/popular_tables/',
'/popular_tables/<path:user_id>')
api.add_resource(TableDetailAPI, '/table/<path:table_uri>')
api.add_resource(TableDescriptionAPI,
'/table/<path:id>/description')
Expand Down
7 changes: 4 additions & 3 deletions metadata_service/api/popular_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# SPDX-License-Identifier: Apache-2.0

from http import HTTPStatus
from typing import Iterable, List, Mapping, Union
from typing import Iterable, List, Mapping, Union, Optional

from amundsen_common.models.popular_table import (PopularTable,
PopularTableSchema)
Expand All @@ -21,8 +21,9 @@ def __init__(self) -> None:
self.client = get_proxy_client()

@swag_from('swagger_doc/popular_tables_get.yml')
def get(self) -> Iterable[Union[Mapping, int, None]]:
def get(self, user_id: Optional[str] = None) -> Iterable[Union[Mapping, int, None]]:
limit = request.args.get('limit', 10, type=int)
popular_tables: List[PopularTable] = self.client.get_popular_tables(num_entries=limit)
popular_tables: List[PopularTable] = self.client.get_popular_tables(num_entries=limit,
user_id=user_id)
popular_tables_json: str = PopularTableSchema(many=True).dump(popular_tables).data
return {'popular_tables': popular_tables_json}, HTTPStatus.OK
4 changes: 3 additions & 1 deletion metadata_service/proxy/atlas_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,9 @@ def _serialize_popular_tables(self, entities: list) -> List[PopularTable]:

return popular_tables

def get_popular_tables(self, *, num_entries: int) -> List[PopularTable]:
def get_popular_tables(self, *,
num_entries: int,
user_id: Optional[str] = None) -> List[PopularTable]:
"""
Generates a list of Popular tables to be shown on the home page of Amundsen.
:param num_entries: Number of popular tables to fetch
Expand Down
6 changes: 4 additions & 2 deletions metadata_service/proxy/base_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# SPDX-License-Identifier: Apache-2.0

from abc import ABCMeta, abstractmethod
from typing import Any, Dict, List, Union
from typing import Any, Dict, List, Union, Optional

from amundsen_common.models.popular_table import PopularTable
from amundsen_common.models.table import Table
Expand Down Expand Up @@ -84,7 +84,9 @@ def get_column_description(self, *,
pass

@abstractmethod
def get_popular_tables(self, *, num_entries: int) -> List[PopularTable]:
def get_popular_tables(self, *,
num_entries: int,
user_id: Optional[str] = None) -> List[PopularTable]:
pass

@abstractmethod
Expand Down
4 changes: 3 additions & 1 deletion metadata_service/proxy/gremlin_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1382,7 +1382,9 @@ def get_column_description(self, *, table_uri: str, column_name: str) -> Union[s

@timer_with_counter
@overrides
def get_popular_tables(self, *, num_entries: int = 10) -> List[PopularTable]:
def get_popular_tables(self, *,
num_entries: int = 10,
user_id: Optional[str] = None) -> List[PopularTable]:
"""
Retrieve popular tables. As popular table computation requires full scan of table and user relationship,
it will utilize cached method _get_popular_tables_uris.
Expand Down
4 changes: 3 additions & 1 deletion metadata_service/proxy/neo4j_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,9 @@ def _get_popular_tables_uris(self, num_entries: int) -> List[str]:
return [record['table_key'] for record in records]

@timer_with_counter
def get_popular_tables(self, *, num_entries: int) -> List[PopularTable]:
def get_popular_tables(self, *,
num_entries: int,
user_id: Optional[str] = None) -> List[PopularTable]:
"""
Retrieve popular tables. As popular table computation requires full scan of table and user relationship,
it will utilize cached method _get_popular_tables_uris.
Expand Down
6 changes: 4 additions & 2 deletions tests/unit/api/test_popular_tables_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ def test_should_get_popular_tables_with_default_limits(self) -> None:

self.assertEqual(response.json, {'popular_tables': API_RESPONSE})
self.assertEqual(response.status_code, HTTPStatus.OK)
self.mock_proxy.get_popular_tables.assert_called_with(num_entries=10)
self.mock_proxy.get_popular_tables.assert_called_with(num_entries=10,
user_id=None)

def test_should_get_popular_tables_with_requested_limits(self) -> None:
self.mock_proxy.get_popular_tables.return_value = CLIENT_RESPONSE

self.app.test_client().get('popular_tables/?limit=90')

self.mock_proxy.get_popular_tables.assert_called_with(num_entries=90)
self.mock_proxy.get_popular_tables.assert_called_with(num_entries=90,
user_id=None)

0 comments on commit 5680775

Please sign in to comment.