-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Mode Batch dashboard charrt API
Signed-off-by: feng-tao <[email protected]>
- Loading branch information
Showing
8 changed files
with
191 additions
and
7 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
2 changes: 2 additions & 0 deletions
2
databuilder/extractor/dashboard/mode_analytics/batch/__init__.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,2 @@ | ||
# Copyright Contributors to the Amundsen project. | ||
# SPDX-License-Identifier: Apache-2.0 |
87 changes: 87 additions & 0 deletions
87
...builder/extractor/dashboard/mode_analytics/batch/mode_dashboard_charts_batch_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,87 @@ | ||
# Copyright Contributors to the Amundsen project. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import logging | ||
|
||
from pyhocon import ConfigTree, ConfigFactory | ||
from typing import Any | ||
|
||
from databuilder import Scoped | ||
from databuilder.extractor.base_extractor import Extractor | ||
from databuilder.extractor.dashboard.mode_analytics.mode_dashboard_utils import ModeDashboardUtils | ||
from databuilder.rest_api.mode_analytics.mode_paginated_rest_api_query import ModePaginatedRestApiQuery | ||
from databuilder.rest_api.rest_api_query import RestApiQuery | ||
from databuilder.rest_api.base_rest_api_query import RestApiQuerySeed | ||
from databuilder.extractor.dashboard.mode_analytics.mode_dashboard_constants import ORGANIZATION | ||
from databuilder.transformer.dict_to_model import DictToModel, MODEL_CLASS | ||
|
||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class ModeDashboardChartsBatchExtractor(Extractor): | ||
""" | ||
Mode dashboard chart extractor leveraging batch / discovery endpoint. | ||
The detail could be found in https://mode.com/help/articles/discovery-api/#list-charts-for-an-organization | ||
""" | ||
# config to include the charts from all space | ||
INCLUDE_ALL_SPACE = 'include_all_space' | ||
|
||
def init(self, conf: 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 | ||
) | ||
|
||
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.dashboard.dashboard_chart.DashboardChart'}))) | ||
self._transformer = dict_to_model_transformer | ||
|
||
def extract(self) -> Any: | ||
|
||
record = self._extractor.extract() | ||
if not record: | ||
return None | ||
return self._transformer.transform(record=record) | ||
|
||
def get_scope(self) -> str: | ||
return 'extractor.mode_dashboard_chart_batch' | ||
|
||
def _build_restapi_query(self) -> RestApiQuery: | ||
""" | ||
Build a paginated REST API based on Mode discovery API | ||
:return: | ||
""" | ||
params = ModeDashboardUtils.get_auth_params(conf=self._conf, discover_auth=True) | ||
|
||
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) | ||
|
||
chart_url_template = 'http://app.mode.com/batch/{organization}/charts' | ||
if self._conf.get_bool(ModeDashboardChartsBatchExtractor.INCLUDE_ALL_SPACE, default=False): | ||
chart_url_template += '?include_spaces=all' | ||
json_path = '(charts[*].[space_token,report_token,query_token,token,chart_title,chart_type])' | ||
field_names = ['dashboard_group_id', | ||
'dashboard_id', | ||
'query_id', | ||
'chart_id', | ||
'chart_name', | ||
'chart_type'] | ||
chart_batch_query = ModePaginatedRestApiQuery(query_to_join=seed_query, | ||
url=chart_url_template, | ||
params=params, | ||
json_path=json_path, | ||
pagination_json_path=json_path, | ||
field_names=field_names, | ||
skip_no_result=True) | ||
return chart_batch_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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Copyright Contributors to the Amundsen project. | ||
# SPDX-License-Identifier: Apache-2.0 |
2 changes: 2 additions & 0 deletions
2
tests/unit/extractor/dashboard/mode_analytics/batch/__init__.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,2 @@ | ||
# Copyright Contributors to the Amundsen project. | ||
# SPDX-License-Identifier: Apache-2.0 |
58 changes: 58 additions & 0 deletions
58
...it/extractor/dashboard/mode_analytics/batch/test_mode_dashboard_charts_batch_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,58 @@ | ||
# Copyright Contributors to the Amundsen project. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import unittest | ||
from mock import patch | ||
from pyhocon import ConfigFactory | ||
|
||
from databuilder import Scoped | ||
from databuilder.extractor.dashboard.mode_analytics.batch.\ | ||
mode_dashboard_charts_batch_extractor import ModeDashboardChartsBatchExtractor | ||
|
||
|
||
class TestModeDashboardChartsBatchExtractor(unittest.TestCase): | ||
def setUp(self) -> None: | ||
config = ConfigFactory.from_dict({ | ||
'extractor.mode_dashboard_chart_batch.organization': 'amundsen', | ||
'extractor.mode_dashboard_chart_batch.mode_user_token': 'amundsen_user_token', | ||
'extractor.mode_dashboard_chart_batch.mode_password_token': 'amundsen_password_token', | ||
'extractor.mode_dashboard_chart_batch.mode_bearer_token': 'amundsen_bearer_token', | ||
}) | ||
self.config = config | ||
|
||
def test_dashboard_chart_extractor_empty_record(self) -> None: | ||
extractor = ModeDashboardChartsBatchExtractor() | ||
extractor.init(Scoped.get_scoped_conf(conf=self.config, scope=extractor.get_scope())) | ||
|
||
with patch('databuilder.rest_api.rest_api_query.requests.get'): | ||
record = extractor.extract() | ||
self.assertIsNone(record) | ||
|
||
def test_dashboard_chart_extractor_actual_record(self) -> None: | ||
extractor = ModeDashboardChartsBatchExtractor() | ||
extractor.init(Scoped.get_scoped_conf(conf=self.config, scope=extractor.get_scope())) | ||
|
||
with patch('databuilder.extractor.restapi.rest_api_extractor.RestAPIExtractor.extract') as mock_get: | ||
mock_get.return_value = { | ||
'organization': 'amundsen', | ||
'is_active': None, | ||
'updated_at': None, | ||
'do_not_update_empty_attribute': True, | ||
'dashboard_group_id': 'ggg', | ||
'dashboard_id': 'ddd', | ||
'query_id': 'yyy', | ||
'chart_id': 'xxx', | ||
'chart_name': 'some chart', | ||
'chart_type': 'bigNumber', | ||
'product': 'mode' | ||
} | ||
|
||
record = extractor.extract() | ||
self.assertEquals(record._dashboard_group_id, 'ggg') | ||
self.assertEquals(record._dashboard_id, 'ddd') | ||
self.assertEquals(record._chart_name, 'some chart') | ||
self.assertEquals(record._product, 'mode') | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |