forked from amundsen-io/amundsen
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Ingest Mode Analytics user (amundsen-io#282)
* Ingest Mode Analytics user * Flake8 * Update * Added doc
- Loading branch information
1 parent
b83f168
commit c27e9e3
Showing
8 changed files
with
253 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
databuilder/databuilder/extractor/dashboard/mode_analytics/mode_dashboard_user_extractor.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import logging | ||
from typing import Any # noqa: F401 | ||
|
||
from pyhocon import ConfigTree, ConfigFactory # noqa: F401 | ||
from requests.auth import HTTPBasicAuth | ||
|
||
from databuilder import Scoped | ||
from databuilder.extractor.base_extractor import Extractor | ||
from databuilder.extractor.dashboard.mode_analytics.mode_dashboard_constants import ORGANIZATION, MODE_ACCESS_TOKEN, \ | ||
MODE_PASSWORD_TOKEN | ||
from databuilder.extractor.dashboard.mode_analytics.mode_dashboard_utils import ModeDashboardUtils | ||
from databuilder.rest_api.base_rest_api_query import RestApiQuerySeed | ||
from databuilder.rest_api.rest_api_failure_handlers import HttpFailureSkipOnStatus | ||
from databuilder.rest_api.rest_api_query import RestApiQuery | ||
from databuilder.transformer.base_transformer import ChainedTransformer | ||
from databuilder.transformer.dict_to_model import DictToModel, MODEL_CLASS | ||
from databuilder.transformer.remove_field_transformer import RemoveFieldTransformer, FIELD_NAMES | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class ModeDashboardUserExtractor(Extractor): | ||
""" | ||
An Extractor that extracts all Mode Dashboard user and add mode_user_id attribute to User model. | ||
""" | ||
|
||
def init(self, conf): | ||
# type: (ConfigTree) -> None | ||
self._conf = conf | ||
|
||
restapi_query = self._build_restapi_query() | ||
self._extractor = ModeDashboardUtils.create_mode_rest_api_extractor( | ||
restapi_query=restapi_query, | ||
conf=self._conf | ||
) | ||
|
||
# Remove all unnecessary fields because User model accepts all attributes and push it to Neo4j. | ||
transformers = [] | ||
|
||
remove_fields_transformer = RemoveFieldTransformer() | ||
remove_fields_transformer.init( | ||
conf=Scoped.get_scoped_conf(self._conf, remove_fields_transformer.get_scope()).with_fallback( | ||
ConfigFactory.from_dict( | ||
{FIELD_NAMES: ['organization', 'mode_user_resource_path', 'product']}))) | ||
transformers.append(remove_fields_transformer) | ||
|
||
dict_to_model_transformer = DictToModel() | ||
dict_to_model_transformer.init( | ||
conf=Scoped.get_scoped_conf(self._conf, dict_to_model_transformer.get_scope()).with_fallback( | ||
ConfigFactory.from_dict( | ||
{MODEL_CLASS: 'databuilder.models.user.User'}))) | ||
transformers.append(dict_to_model_transformer) | ||
|
||
self._transformer = ChainedTransformer(transformers=transformers) | ||
|
||
def extract(self): | ||
# type: () -> Any | ||
|
||
record = self._extractor.extract() | ||
if not record: | ||
return None | ||
|
||
return self._transformer.transform(record=record) | ||
|
||
def get_scope(self): | ||
# type: () -> str | ||
return 'extractor.mode_dashboard_owner' | ||
|
||
def _build_restapi_query(self): | ||
""" | ||
Build REST API Query. To get Mode Dashboard owner, it needs to call three APIs (spaces API, reports | ||
API, and user API) joining together. | ||
:return: A RestApiQuery that provides Mode Dashboard owner | ||
""" | ||
# type: () -> RestApiQuery | ||
|
||
# Seed query record for next query api to join with | ||
seed_record = [{ | ||
'organization': self._conf.get_string(ORGANIZATION), | ||
'is_active': None, | ||
'updated_at': None, | ||
'do_not_update_empty_attribute': True, | ||
}] | ||
seed_query = RestApiQuerySeed(seed_record=seed_record) | ||
|
||
# memberships | ||
# https://mode.com/developer/api-reference/management/organization-memberships/#listMemberships | ||
memberships_url_template = 'https://app.mode.com/api/{organization}/memberships' | ||
params = {'auth': HTTPBasicAuth(self._conf.get_string(MODE_ACCESS_TOKEN), | ||
self._conf.get_string(MODE_PASSWORD_TOKEN))} | ||
|
||
json_path = '(_embedded.memberships[*].member_username) | (_embedded.memberships[*]._links.user.href)' | ||
field_names = ['mode_user_id', 'mode_user_resource_path'] | ||
mode_user_ids_query = RestApiQuery(query_to_join=seed_query, url=memberships_url_template, params=params, | ||
json_path=json_path, field_names=field_names, | ||
skip_no_result=True, json_path_contains_or=True) | ||
|
||
# https://mode.com/developer/api-reference/management/users/ | ||
user_url_template = 'https://app.mode.com{mode_user_resource_path}' | ||
|
||
json_path = 'email' | ||
field_names = ['email'] | ||
failure_handler = HttpFailureSkipOnStatus(status_codes_to_skip={404}) | ||
mode_user_email_query = RestApiQuery(query_to_join=mode_user_ids_query, url=user_url_template, | ||
params=params, json_path=json_path, field_names=field_names, | ||
skip_no_result=True, can_skip_failure=failure_handler.can_skip_failure) | ||
|
||
return mode_user_email_query |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
databuilder/databuilder/transformer/remove_field_transformer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import logging | ||
from typing import Any, Dict # noqa: F401 | ||
|
||
from pyhocon import ConfigTree # noqa: F401 | ||
|
||
from databuilder.transformer.base_transformer import Transformer | ||
|
||
FIELD_NAMES = 'field_names' # field name to be removed | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class RemoveFieldTransformer(Transformer): | ||
""" | ||
Remove field in Dict by specifying list of fields (keys). | ||
""" | ||
|
||
def init(self, conf): | ||
# type: (ConfigTree) -> None | ||
self._field_names = conf.get_list(FIELD_NAMES) | ||
|
||
def transform(self, record): | ||
# type: (Dict[str, Any]) -> Dict[str, Any] | ||
|
||
for k in self._field_names: | ||
if k in record: | ||
del record[k] | ||
|
||
return record | ||
|
||
def get_scope(self): | ||
# type: () -> str | ||
return 'transformer.remove_field' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,3 +57,5 @@ unicodecsv==0.14.1,<1.0 | |
|
||
httplib2>=0.18.0 | ||
unidecode | ||
|
||
requests==2.23.0,<3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
databuilder/tests/unit/transformer/test_remove_field_transformer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import unittest | ||
|
||
from pyhocon import ConfigFactory | ||
|
||
from databuilder.transformer.remove_field_transformer import RemoveFieldTransformer, FIELD_NAMES | ||
|
||
|
||
class TestRemoveFieldTransformer(unittest.TestCase): | ||
|
||
def test_conversion(self): | ||
# type: () -> None | ||
|
||
transformer = RemoveFieldTransformer() | ||
config = ConfigFactory.from_dict({ | ||
FIELD_NAMES: ['foo', 'bar'], | ||
}) | ||
transformer.init(conf=config) | ||
|
||
actual = transformer.transform({ | ||
'foo': 'foo_val', | ||
'bar': 'bar_val', | ||
'baz': 'baz_val', | ||
}) | ||
expected = { | ||
'baz': 'baz_val' | ||
} | ||
self.assertDictEqual(expected, actual) | ||
|
||
def test_conversion_missing_field(self): | ||
# type: () -> None | ||
|
||
transformer = RemoveFieldTransformer() | ||
config = ConfigFactory.from_dict({ | ||
FIELD_NAMES: ['foo', 'bar'], | ||
}) | ||
transformer.init(conf=config) | ||
|
||
actual = transformer.transform({ | ||
'foo': 'foo_val', | ||
'baz': 'baz_val', | ||
'john': 'doe', | ||
}) | ||
expected = { | ||
'baz': 'baz_val', | ||
'john': 'doe' | ||
} | ||
self.assertDictEqual(expected, actual) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |