def find_schema_by_id(self, topic_id: TopicId, tenant_id: TenantId) -> Optional[TopicSchema]: if not self.principalService.is_super_admin(): if self.principalService.get_tenant_id() != tenant_id: raise Exception('Forbidden') schema = CacheService.topic().get_schema(topic_id) if schema is not None: if schema.get_topic().tenantId != tenant_id: return None return schema storage_service = TopicStorageService(ask_meta_storage(), ask_snowflake_generator(), self.principalService) storage_service.begin_transaction() try: # noinspection PyTypeChecker topic: Topic = storage_service.find_by_id(topic_id) if topic is None: return None CacheService.topic().put(topic) schema = CacheService.topic().get_schema(topic.topicId) if schema is not None: if schema.get_topic().tenantId != tenant_id: return None return schema finally: storage_service.close_transaction()
def prepare_data(self): data_source = DataSource(dataSourceId='1', dataSourceCode='test', dataSourceType=DataSourceType.MYSQL, host='localhost', port='3306', username='******', password='******', name='watchmen', tenantId='1') data_source_service = get_data_source_service( create_fake_principal_service()) data_source_service.begin_transaction() data_source_service.create(data_source) data_source_service.commit_transaction() CacheService.data_source().put(data_source) topic1 = Topic(topicId='1', name='topic1', type=TopicType.DISTINCT, kind=TopicKind.BUSINESS, factors=[ Factor(factorId='1', name='topic1_id', type=FactorType.SEQUENCE), Factor(factorId='2', name='topic1_enabled', type=FactorType.BOOLEAN) ], dataSourceId=data_source.dataSourceId, tenantId='1') topic_service = get_topic_service(create_fake_principal_service()) topic_service.begin_transaction() topic_service.create(topic1) topic_service.commit_transaction() CacheService.topic().put(topic1) space = Space(spaceId='1', name='Space1', topicIds=[topic1.topicId], tenantId='1') space_service = get_space_service(create_fake_principal_service()) space_service.begin_transaction() space_service.create(space) space_service.commit_transaction() connected_space = ConnectedSpace(connectId='1', name='ConnectedSpace1', spaceId=space.spaceId, isTemplate=False, userId='1', tenantId='1') connected_space_service = get_connected_space_service( create_fake_principal_service()) connected_space_service.begin_transaction() connected_space_service.create(connected_space) connected_space_service.commit_transaction()
def ask_topic_data_entity_helper(schema: TopicSchema) -> TopicDataEntityHelper: """ ask topic data entity helper, from cache first """ data_entity_helper = CacheService.topic().get_entity_helper( schema.get_topic().topicId) if data_entity_helper is None: if is_raw_topic(schema.get_topic()): data_entity_helper = RawTopicDataEntityHelper(schema) else: data_entity_helper = RegularTopicDataEntityHelper(schema) CacheService.topic().put_entity_helper(data_entity_helper) return data_entity_helper
def find_by_id(self, topic_id: TopicId) -> Optional[Topic]: topic = CacheService.topic().get(topic_id) if topic is not None: if topic.tenantId != self.principalService.get_tenant_id(): raise DataKernelException( f'Topic[id={topic_id}] not belongs to current tenant[id={self.principalService.get_tenant_id()}].') return topic storage_service = TopicStorageService(ask_meta_storage(), ask_snowflake_generator(), self.principalService) storage_service.begin_transaction() try: # noinspection PyTypeChecker topic: Topic = storage_service.find_by_id(topic_id) if topic is None: return None CacheService.topic().put(topic) return topic finally: storage_service.close_transaction()
def post_save_topic(topic: Topic, topic_service: TopicService) -> None: build_topic_index(topic, topic_service) CacheService.topic().put(topic)
def post_delete_topic(topic_id: TopicId, topic_service: TopicService) -> None: remove_topic_index(topic_id, topic_service) CacheService.topic().remove(topic_id)
async def clear_all_topic_cache(principal_service: PrincipalService = Depends( get_super_admin_principal)) -> None: CacheService.topic().clear()