示例#1
0
    def get_user_record(self) -> RDSModel:
        record_attr_map = {
            RDSUser.email: self.email,
            RDSUser.is_active: self.is_active,
            RDSUser.profile_url: self.profile_url or '',
            RDSUser.first_name: self.first_name or '',
            RDSUser.last_name: self.last_name or '',
            RDSUser.full_name: self.full_name or '',
            RDSUser.github_username: self.github_username or '',
            RDSUser.team_name: self.team_name or '',
            RDSUser.employee_type: self.employee_type or '',
            RDSUser.slack_id: self.slack_id or '',
            RDSUser.role_name: self.role_name or '',
            RDSUser.updated_at: self.updated_at or 0
        }

        record = RDSUser(rk=User.get_user_model_key(email=self.email))
        # set value for attributes of user record if the value is not empty
        # or the flag allows to update empty values
        for attr, value in record_attr_map.items():
            if value or not self.do_not_update_empty_attribute:
                record.__setattr__(attr.key, value)

        if self.manager_email:
            record.manager_rk = self.get_user_model_key(
                email=self.manager_email)

        return record
示例#2
0
    def test_get_users(self, mock_rds_client: Any) -> None:
        user = RDSUser(employee_type='teamMember',
                       full_name='test_full_name',
                       is_active=True,
                       profile_url='test_profile',
                       github_username='******',
                       slack_id='test_id',
                       last_name='test_last_name',
                       first_name='test_first_name',
                       team_name='test_team',
                       email='test_email')
        user.manager = RDSUser(full_name='test_manager_fullname')

        mock_client = MagicMock()
        mock_rds_client.return_value = mock_client

        mock_create_session = MagicMock()
        mock_client.create_session.return_value = mock_create_session

        mock_session = MagicMock()
        mock_create_session.__enter__.return_value = mock_session

        mock_session_query = MagicMock()
        mock_session.query.return_value = mock_session_query

        mock_session_query_filter = MagicMock()
        mock_session_query.filter.return_value = mock_session_query_filter

        mock_session_query_filter.all.return_value = [user]

        expected = [
            UserEntity(email='test_email',
                       first_name='test_first_name',
                       last_name='test_last_name',
                       full_name='test_full_name',
                       is_active=True,
                       profile_url='test_profile',
                       github_username='******',
                       team_name='test_team',
                       slack_id='test_id',
                       employee_type='teamMember',
                       manager_fullname='')
        ]
        proxy = MySQLProxy()
        actual = proxy.get_users()

        self.assertEqual(expected, actual)
    def _create_record_iterator(self) -> Iterator[RDSModel]:
        for owner in self.owners:
            if owner:
                user_record = RDSUser(
                    rk=self.get_owner_model_key(owner),
                    email=owner
                )
                yield user_record

                table_owner_record = RDSTableOwner(
                    table_rk=self.get_metadata_model_key(),
                    user_rk=self.get_owner_model_key(owner)
                )
                yield table_owner_record
示例#4
0
    def _create_record_iterator(self) -> Iterator[RDSModel]:
        if self.user_email:
            yield RDSUser(
                rk=User.get_user_model_key(email=self.user_email),
                email=self.user_email
            )

        if self.start_label == TableMetadata.TABLE_NODE_LABEL:
            yield RDSTableUsage(user_rk=User.get_user_model_key(email=self.user_email),
                                table_rk=self.start_key,
                                read_count=self.read_count)
        elif self.start_label == DashboardMetadata.DASHBOARD_NODE_LABEL:
            yield RDSDashboardUsage(
                user_rk=User.get_user_model_key(email=self.user_email),
                dashboard_rk=self.start_key,
                read_count=self.read_count,
            )
        else:
            raise Exception(f'{self.start_label} usage is not table serializable')
示例#5
0
    def _create_record_iterator(self) -> Iterator[RDSModel]:
        for email in self.owner_emails:
            if email:
                user_record = RDSUser(rk=User.get_user_model_key(email=email),
                                      email=email)
                yield user_record

                if self.start_label == TableMetadata.TABLE_NODE_LABEL:
                    yield RDSTableOwner(
                        table_rk=self.start_key,
                        user_rk=User.get_user_model_key(email=email),
                    )
                elif self.start_label == DashboardMetadata.DASHBOARD_NODE_LABEL:
                    yield RDSDashboardOwner(
                        dashboard_rk=self.start_key,
                        user_rk=User.get_user_model_key(email=email))
                else:
                    raise Exception(
                        f'{self.start_label}<>Owner relationship is not table serializable'
                    )
示例#6
0
    def test_get_dashboard(self, mock_rds_client: Any) -> None:
        # dashboard_metadata
        dashboard = RDSDashboard(
            rk='foo_dashboard://gold.bar/dashboard_id',
            name='dashboard name',
            dashboard_url='http://www.foo.bar/dashboard_id',
            created_timestamp=123456789)
        dashboard_group = RDSDashboardGroup(
            name='group_name', dashboard_group_url='http://www.group_url.com')
        dashboard_group.cluster = RDSCluster(name='cluster_name')
        dashboard.group = dashboard_group
        dashboard.description = RDSDashboardDescription(
            description='description')
        dashboard.execution = [
            RDSDashboardExecution(rk='dashboard_last_successful_execution',
                                  timestamp=9876543210),
            RDSDashboardExecution(rk='dashboard_last_execution',
                                  timestamp=987654321,
                                  state='good_state')
        ]
        dashboard.timestamp = RDSDashboardTimestamp(timestamp=123456654321)
        dashboard.tags = [
            RDSTag(rk='tag_key1', tag_type='default'),
            RDSTag(rk='tag_key2', tag_type='default')
        ]
        dashboard.badges = [RDSBadge(rk='golden', category='table_status')]
        dashboard.owners = [
            RDSUser(email='test_email',
                    first_name='test_first_name',
                    last_name='test_last_name',
                    full_name='test_full_name',
                    is_active=True,
                    github_username='******',
                    team_name='test_team',
                    slack_id='test_id',
                    employee_type='teamMember'),
            RDSUser(email='test_email2',
                    first_name='test_first_name2',
                    last_name='test_last_name2',
                    full_name='test_full_name2',
                    is_active=True,
                    github_username='******',
                    team_name='test_team2',
                    slack_id='test_id2',
                    employee_type='teamMember')
        ]
        dashboard.usage = [RDSDashboardUsage(read_count=100)]

        mock_client = MagicMock()
        mock_rds_client.return_value = mock_client

        mock_create_session = MagicMock()
        mock_client.create_session.return_value = mock_create_session

        mock_session = MagicMock()
        mock_create_session.__enter__.return_value = mock_session

        mock_session_query = MagicMock()
        mock_session.query.return_value = mock_session_query

        mock_session_query_filter = MagicMock()
        mock_session_query.filter.return_value = mock_session_query_filter

        mock_session_query_filter.first.return_value = dashboard

        # queries
        query1 = RDSDashboardQuery(name='query1')
        query2 = RDSDashboardQuery(name='query2',
                                   url='http://foo.bar/query',
                                   query_text='SELECT * FROM foo.bar')
        query1.charts = [RDSDashboardChart(name='chart1')]
        query2.charts = [RDSDashboardChart(name='chart2')]
        queries = [query1, query2]

        # tables
        database1 = RDSDatabase(name='db1')
        database2 = RDSDatabase(name='db2')
        cluster1 = RDSCluster(name='cluster1')
        cluster2 = RDSCluster(name='cluster2')
        schema1 = RDSSchema(name='schema1')
        schema2 = RDSSchema(name='schema2')
        table1 = RDSTable(name='table1')
        table2 = RDSTable(name='table2')
        description1 = RDSTableDescription(description='table description 1')

        schema1.cluster = cluster1
        cluster1.database = database1
        schema2.cluster = cluster2
        cluster2.database = database2
        table1.schema = schema1
        table2.schema = schema2
        table1.description = description1
        tables = [table1, table2]

        mock_session_query_filter_options = MagicMock()
        mock_session_query_filter.options.return_value = mock_session_query_filter_options

        mock_session_query_filter_options.all.side_effect = [queries, tables]

        expected = DashboardDetail(
            uri='foo_dashboard://gold.bar/dashboard_id',
            cluster='cluster_name',
            group_name='group_name',
            group_url='http://www.group_url.com',
            product='foo',
            name='dashboard name',
            url='http://www.foo.bar/dashboard_id',
            description='description',
            created_timestamp=123456789,
            last_successful_run_timestamp=9876543210,
            updated_timestamp=123456654321,
            last_run_timestamp=987654321,
            last_run_state='good_state',
            owners=[
                User(email='test_email',
                     first_name='test_first_name',
                     last_name='test_last_name',
                     full_name='test_full_name',
                     is_active=True,
                     github_username='******',
                     team_name='test_team',
                     slack_id='test_id',
                     employee_type='teamMember',
                     manager_fullname=''),
                User(email='test_email2',
                     first_name='test_first_name2',
                     last_name='test_last_name2',
                     full_name='test_full_name2',
                     is_active=True,
                     github_username='******',
                     team_name='test_team2',
                     slack_id='test_id2',
                     employee_type='teamMember',
                     manager_fullname='')
            ],
            frequent_users=[],
            chart_names=['chart1', 'chart2'],
            query_names=['query1', 'query2'],
            queries=[
                DashboardQuery(name='query1'),
                DashboardQuery(name='query2',
                               url='http://foo.bar/query',
                               query_text='SELECT * FROM foo.bar')
            ],
            tables=[
                PopularTable(database='db1',
                             name='table1',
                             description='table description 1',
                             cluster='cluster1',
                             schema='schema1'),
                PopularTable(database='db2',
                             name='table2',
                             cluster='cluster2',
                             schema='schema2'),
            ],
            tags=[
                Tag(tag_type='default', tag_name='tag_key1'),
                Tag(tag_type='default', tag_name='tag_key2')
            ],
            badges=[Badge(badge_name='golden', category='table_status')],
            recent_view_count=100)

        proxy = MySQLProxy()
        actual = proxy.get_dashboard(id='dashboard_id')

        self.assertEqual(expected, actual)
示例#7
0
    def test_get_table(self, mock_rds_client: Any) -> None:
        database = RDSDatabase(name='hive')
        cluster = RDSCluster(name='gold')
        schema = RDSSchema(name='foo_schema')
        schema.cluster = cluster
        cluster.database = database

        table = RDSTable(name='foo_table')
        table.schema = schema
        table.description = RDSTableDescription(description='foo description')

        col1 = RDSColumn(name='bar_id_1', type='varchar', sort_order=0)
        col1.description = RDSColumnDescription(
            description='bar col description')
        col1.stats = [
            RDSColumnStat(stat_type='avg',
                          start_epoch='1',
                          end_epoch='1',
                          stat_val='1')
        ]

        col2 = RDSColumn(name='bar_id_2', type='bigint', sort_order=1)
        col2.description = RDSColumnDescription(
            description='bar col2 description')
        col2.stats = [
            RDSColumnStat(stat_type='avg',
                          start_epoch='2',
                          end_epoch='2',
                          stat_val='2')
        ]
        col2.badges = [RDSBadge(rk='primary key', category='column')]
        columns = [col1, col2]

        table.watermarks = [
            RDSTableWatermark(
                rk='hive://gold.test_schema/test_table/high_watermark/',
                partition_key='ds',
                partition_value='fake_value',
                create_time='fake_time'),
            RDSTableWatermark(
                rk='hive://gold.test_schema/test_table/low_watermark/',
                partition_key='ds',
                partition_value='fake_value',
                create_time='fake_time')
        ]

        table.application = RDSApplication(
            application_url='airflow_host/admin/airflow/tree?dag_id=test_table',
            description='DAG generating a table',
            name='Airflow',
            id='dag/task_id')
        table.timestamp = RDSTableTimestamp(last_updated_timestamp=1)

        table.owners = [
            RDSUser(rk='*****@*****.**', email='*****@*****.**')
        ]
        table.tags = [RDSTag(rk='test', tag_type='default')]
        table.badges = [RDSBadge(rk='golden', category='table_status')]
        table.source = RDSTableSource(rk='some key',
                                      source_type='github',
                                      source='/source_file_loc')
        table.programmatic_descriptions = [
            RDSTableProgrammaticDescription(description_source='s3_crawler',
                                            description='Test Test Test'),
            RDSTableProgrammaticDescription(
                description_source='quality_report', description='Test Test')
        ]

        readers = [RDSTableUsage(user_rk='*****@*****.**', read_count=5)]

        mock_client = MagicMock()
        mock_rds_client.return_value = mock_client

        mock_create_session = MagicMock()
        mock_client.create_session.return_value = mock_create_session

        mock_session = MagicMock()
        mock_create_session.__enter__.return_value = mock_session

        mock_session_query = MagicMock()
        mock_session.query.return_value = mock_session_query

        mock_session_query_filter = MagicMock()
        mock_session_query.filter.return_value = mock_session_query_filter
        mock_session_query_filter.first.return_value = table

        mock_session_query_filter_orderby = MagicMock()
        mock_session_query_filter.order_by.return_value = mock_session_query_filter_orderby

        mock_session_query_filter_orderby_limit = MagicMock()
        mock_session_query_filter_orderby.limit.return_value = mock_session_query_filter_orderby_limit
        mock_session_query_filter_orderby_limit.all.return_value = readers

        mock_session_query_filter_options = MagicMock()
        mock_session_query_filter.options.return_value = mock_session_query_filter_options
        mock_session_query_filter_options.all.return_value = columns

        proxy = MySQLProxy()
        actual_table = proxy.get_table(table_uri='dummy_uri')

        expected = Table(
            database='hive',
            cluster='gold',
            schema='foo_schema',
            name='foo_table',
            tags=[Tag(tag_name='test', tag_type='default')],
            badges=[Badge(badge_name='golden', category='table_status')],
            table_readers=[
                Reader(user=User(email='*****@*****.**'), read_count=5)
            ],
            description='foo description',
            watermarks=[
                Watermark(watermark_type='high_watermark',
                          partition_key='ds',
                          partition_value='fake_value',
                          create_time='fake_time'),
                Watermark(watermark_type='low_watermark',
                          partition_key='ds',
                          partition_value='fake_value',
                          create_time='fake_time')
            ],
            columns=[
                Column(name='bar_id_1',
                       description='bar col description',
                       col_type='varchar',
                       sort_order=0,
                       stats=[
                           Stat(start_epoch=1,
                                end_epoch=1,
                                stat_type='avg',
                                stat_val='1')
                       ],
                       badges=[]),
                Column(name='bar_id_2',
                       description='bar col2 description',
                       col_type='bigint',
                       sort_order=1,
                       stats=[
                           Stat(start_epoch=2,
                                end_epoch=2,
                                stat_type='avg',
                                stat_val='2')
                       ],
                       badges=[
                           Badge(badge_name='primary key', category='column')
                       ])
            ],
            owners=[User(email='*****@*****.**')],
            table_writer=Application(
                application_url=
                'airflow_host/admin/airflow/tree?dag_id=test_table',
                description='DAG generating a table',
                name='Airflow',
                id='dag/task_id'),
            last_updated_timestamp=1,
            source=Source(source='/source_file_loc', source_type='github'),
            is_view=False,
            programmatic_descriptions=[
                ProgrammaticDescription(source='quality_report',
                                        text='Test Test'),
                ProgrammaticDescription(source='s3_crawler',
                                        text='Test Test Test')
            ])

        self.assertEqual(str(expected), str(actual_table))