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)

        # Constructing URL using resource path via TemplateVariableSubstitutionTransformer
        transformers = []
        chart_url_transformer = TemplateVariableSubstitutionTransformer()
        chart_url_transformer.init(conf=Scoped.get_scoped_conf(
            self._conf, chart_url_transformer.get_scope()).with_fallback(
                ConfigFactory.from_dict(
                    {
                        FIELD_NAME: 'chart_url',
                        TEMPLATE: 'https://app.mode.com{chart_url}'
                    })))

        transformers.append(chart_url_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.dashboard.dashboard_chart.DashboardChart'
            })))
        transformers.append(dict_to_model_transformer)

        self._transformer = ChainedTransformer(transformers=transformers)
Ejemplo n.º 2
0
    def init(self, conf: ConfigTree) -> None:
        self._conf = conf

        self.dashboard_group_ids_to_skip = self._conf.get_list(
            DASHBOARD_GROUP_IDS_TO_SKIP, [])

        restapi_query = self._build_restapi_query()
        self._extractor = ModeDashboardUtils.create_mode_rest_api_extractor(
            restapi_query=restapi_query, conf=self._conf)

        # Payload from RestApiQuery has timestamp which is ISO8601. Here we are using TimestampStringToEpoch to
        # transform into epoch and then using DictToModel to convert Dictionary to Model
        transformers: List[Transformer] = []
        timestamp_str_to_epoch_transformer = TimestampStringToEpoch()
        timestamp_str_to_epoch_transformer.init(conf=Scoped.get_scoped_conf(
            self._conf,
            timestamp_str_to_epoch_transformer.get_scope()).with_fallback(
                ConfigFactory.from_dict({
                    FIELD_NAME: 'created_timestamp',
                })))

        transformers.append(timestamp_str_to_epoch_transformer)

        dashboard_group_url_transformer = TemplateVariableSubstitutionTransformer(
        )
        dashboard_group_url_transformer.init(conf=Scoped.get_scoped_conf(
            self._conf, dashboard_group_url_transformer.get_scope()
        ).with_fallback(
            ConfigFactory.from_dict({
                VAR_FIELD_NAME:
                'dashboard_group_url',
                TEMPLATE:
                'https://app.mode.com/{organization}/spaces/{dashboard_group_id}'
            })))

        transformers.append(dashboard_group_url_transformer)

        dashboard_url_transformer = TemplateVariableSubstitutionTransformer()
        dashboard_url_transformer.init(conf=Scoped.get_scoped_conf(
            self._conf, dashboard_url_transformer.get_scope()
        ).with_fallback(
            ConfigFactory.from_dict({
                VAR_FIELD_NAME:
                'dashboard_url',
                TEMPLATE:
                'https://app.mode.com/{organization}/reports/{dashboard_id}'
            })))
        transformers.append(dashboard_url_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.dashboard.dashboard_metadata.DashboardMetadata'
            })))
        transformers.append(dict_to_model_transformer)

        self._transformer = ChainedTransformer(transformers=transformers)
    def test_conversion(self) -> None:

        transformer = TemplateVariableSubstitutionTransformer()
        config = ConfigFactory.from_dict({
            FIELD_NAME: 'baz',
            TEMPLATE: 'Hello {foo}'
        })
        transformer.init(conf=config)

        actual = transformer.transform({'foo': 'bar'})
        expected = {'foo': 'bar', 'baz': 'Hello bar'}
        self.assertDictEqual(expected, actual)
    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)

        # Constructing URL using several ID via TemplateVariableSubstitutionTransformer
        transformers = []
        variable_substitution_transformer = TemplateVariableSubstitutionTransformer(
        )
        variable_substitution_transformer.init(conf=Scoped.get_scoped_conf(
            self._conf,
            variable_substitution_transformer.get_scope()).with_fallback(
                ConfigFactory.from_dict({
                    FIELD_NAME:
                    'url',
                    TEMPLATE:
                    'https://app.mode.com/{organization}'
                    '/reports/{dashboard_id}/queries/{query_id}'
                })))

        transformers.append(variable_substitution_transformer)

        # Escape backslash as it breaks Cypher statement.
        replace_transformer = RegexStrReplaceTransformer()
        replace_transformer.init(conf=Scoped.get_scoped_conf(
            self._conf, replace_transformer.get_scope()).with_fallback(
                ConfigFactory.from_dict({
                    REGEX_REPLACE_TUPLE_LIST: [('\\', '\\\\')],
                    ATTRIBUTE_NAME:
                    'query_text'
                })))
        transformers.append(replace_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.dashboard.dashboard_query.DashboardQuery'
            })))
        transformers.append(dict_to_model_transformer)

        self._transformer = ChainedTransformer(transformers=transformers)