示例#1
0
 def test_create_record_additional_attr_mysql(self) -> None:
     test_user = User(first_name='test_first',
                      last_name='test_last',
                      name='test_first test_last',
                      email='*****@*****.**',
                      github_username='******',
                      team_name='test_team',
                      employee_type='FTE',
                      manager_email='*****@*****.**',
                      slack_id='slack',
                      is_active=True,
                      updated_at=1,
                      role_name='swe',
                      enable_notify=True)
     record = test_user.create_next_record()
     serialized_record = mysql_serializer.serialize_record(record)
     self.assertEqual(serialized_record['email'], '*****@*****.**')
     self.assertEqual(serialized_record['role_name'], 'swe')
class DashboardUsage(GraphSerializable, TableSerializable):
    """
    A model that encapsulate Dashboard usage between Dashboard and User
    """
    def __init__(self,
                 dashboard_group_id: Optional[str],
                 dashboard_id: Optional[str],
                 email: str,
                 view_count: int,
                 should_create_user_node: Optional[bool] = False,
                 product: Optional[str] = '',
                 cluster: Optional[str] = 'gold',
                 **kwargs: Any) -> None:
        """

        :param dashboard_group_id:
        :param dashboard_id:
        :param email:
        :param view_count:
        :param should_create_user_node: Enable this if it is fine to create/update User node with only with email
        address. Please be advised that other fields will be emptied. Current use case is to create anonymous user.
        For example, Mode dashboard does not provide which user viewed the dashboard and anonymous user can be used
        to show the usage.
        :param product:
        :param cluster:
        :param kwargs:
        """
        self._dashboard_group_id = dashboard_group_id
        self._dashboard_id = dashboard_id
        self._email = email
        self._view_count = int(view_count)
        self._product = product
        self._cluster = cluster
        self._user_model = User(email=email)
        self._should_create_user_node = bool(should_create_user_node)
        self._relation_iterator = self._create_relation_iterator()
        self._record_iterator = self._create_record_iterator()

    def create_next_node(self) -> Union[GraphNode, None]:
        if self._should_create_user_node:
            return self._user_model.create_next_node()

        return None

    def create_next_relation(self) -> Union[GraphRelationship, None]:
        try:
            return next(self._relation_iterator)
        except StopIteration:
            return None

    def _create_relation_iterator(self) -> Iterator[GraphRelationship]:
        relationship = GraphRelationship(
            start_label=DashboardMetadata.DASHBOARD_NODE_LABEL,
            end_label=User.USER_NODE_LABEL,
            start_key=DashboardMetadata.DASHBOARD_KEY_FORMAT.format(
                product=self._product,
                cluster=self._cluster,
                dashboard_group=self._dashboard_group_id,
                dashboard_name=self._dashboard_id),
            end_key=User.get_user_model_key(email=self._email),
            type=READ_REVERSE_RELATION_TYPE,
            reverse_type=READ_RELATION_TYPE,
            attributes={READ_RELATION_COUNT_PROPERTY: self._view_count})
        yield relationship

    def create_next_record(self) -> Union[RDSModel, None]:
        try:
            return next(self._record_iterator)
        except StopIteration:
            return None

    def _create_record_iterator(self) -> Iterator[RDSModel]:
        if self._should_create_user_node:
            user_record = self._user_model.create_next_record()
            if user_record:
                yield user_record

        dashboard_usage_record = RDSDashboardUsage(
            user_rk=User.get_user_model_key(email=self._email),
            dashboard_rk=DashboardMetadata.DASHBOARD_KEY_FORMAT.format(
                product=self._product,
                cluster=self._cluster,
                dashboard_group=self._dashboard_group_id,
                dashboard_name=self._dashboard_id),
            read_count=self._view_count)
        yield dashboard_usage_record

    def __repr__(self) -> str:
        return f'DashboardUsage({self._dashboard_group_id!r}, {self._dashboard_id!r}, ' \
               f'{self._email!r}, {self._view_count!r}, {self._should_create_user_node!r}, ' \
               f'{self._product!r}, {self._cluster!r})'