async def load_achievement_by_id( achievement_id: Optional[AchievementId], principal_service: PrincipalService = Depends(get_console_principal) ) -> Achievement: if is_blank(achievement_id): raise_400('Achievement id is required.') achievement_service = get_achievement_service(principal_service) # noinspection DuplicatedCode def action() -> Achievement: # noinspection PyTypeChecker achievement: Achievement = achievement_service.find_by_id( achievement_id) if achievement is None: raise_404() # user id must match current principal's if achievement.userId != principal_service.get_user_id(): raise_404() # tenant id must match current principal's if achievement.tenantId != principal_service.get_tenant_id(): raise_404() return achievement return trans_readonly(achievement_service, action)
async def find_buckets_for_export(principal_service: PrincipalService = Depends(get_admin_principal)) -> List[Bucket]: bucket_service = get_bucket_service(principal_service) def action() -> List[Bucket]: tenant_id: TenantId = principal_service.get_tenant_id() return bucket_service.find_all(tenant_id) return trans_readonly(bucket_service, action)
async def load_indicator_category( prefix: List[str], principal_service: PrincipalService = Depends(get_admin_principal)) -> List[str]: indicator_service = get_indicator_service(principal_service) def action() -> List[str]: # noinspection PyTypeChecker return indicator_service.find_available_categories(prefix, principal_service.get_tenant_id()) return trans_readonly(indicator_service, action)
async def find_all_indicators(principal_service: PrincipalService = Depends(get_console_principal)) -> List[Indicator]: indicator_service = get_indicator_service(principal_service) def action() -> List[Indicator]: tenant_id: TenantId = principal_service.get_tenant_id() indicators = indicator_service.find_all(tenant_id) return filter_indicators(indicators, indicator_service, principal_service) return trans_readonly(indicator_service, action)
async def find_my_inspections(principal_service: PrincipalService = Depends( get_console_principal)) -> List[Inspection]: inspection_service = get_inspection_service(principal_service) def action() -> List[Inspection]: # noinspection PyTypeChecker return inspection_service.find_all_by_user_id( principal_service.get_user_id(), principal_service.get_tenant_id()) return trans_readonly(inspection_service, action)
async def fetch_inspection_data( inspection_id: Optional[InspectionId], principal_service: PrincipalService = Depends(get_console_principal)) -> DataResultSet: if is_blank(inspection_id): raise_400('Inspection id is required.') inspection_service = get_inspection_service(principal_service) inspection = trans_readonly( inspection_service, lambda: do_load_inspection_by_id(inspection_id, inspection_service, principal_service)) return get_inspection_data_service(inspection, principal_service).find_data()
async def find_my_analysis(principal_service: PrincipalService = Depends( get_console_principal)) -> List[ObjectiveAnalysis]: analysis_service = get_analysis_service(principal_service) def action() -> List[ObjectiveAnalysis]: user_id: UserId = principal_service.get_user_id() tenant_id: TenantId = principal_service.get_tenant_id() # noinspection PyTypeChecker return analysis_service.find_all_by_user_id(user_id, tenant_id) return trans_readonly(analysis_service, action)
async def load_inspection_by_id( inspection_id: Optional[InspectionId], principal_service: PrincipalService = Depends(get_console_principal) ) -> Inspection: if is_blank(inspection_id): raise_400('Inspection id is required.') inspection_service = get_inspection_service(principal_service) return trans_readonly( inspection_service, lambda: do_load_inspection_by_id( inspection_id, inspection_service, principal_service))
def find_subjects_for_indicator( query_name: Optional[str], principal_service: PrincipalService = Depends(get_console_principal) ) -> List[SubjectForIndicator]: subject_service = get_subject_service(principal_service) def action() -> List[SubjectForIndicator]: if is_blank(query_name): subjects = subject_service.find_by_text( None, principal_service.get_tenant_id()) else: subjects = subject_service.find_by_text( query_name, principal_service.get_tenant_id()) connected_space_service = get_connected_space_service(subject_service) connect_ids = ArrayHelper(subjects).map( lambda x: x.connectId).distinct().to_list() connected_spaces = connected_space_service.find_templates_by_ids( connect_ids, None, principal_service.get_tenant_id()) connect_ids = ArrayHelper(connected_spaces).map( lambda x: x.connectId).to_list() connected_space_map: Dict[ConnectedSpaceId, ConnectedSpace] = ArrayHelper(connected_spaces) \ .to_map(lambda x: x.connectId, lambda x: x) subjects = ArrayHelper(subjects).filter( lambda x: x.connectId in connect_ids).to_list() space_ids = ArrayHelper(connected_spaces).map( lambda x: x.spaceId).distinct().to_list() space_service = get_space_service(subject_service) spaces = space_service.find_by_ids(space_ids, principal_service.get_tenant_id()) space_map: Dict[SpaceId, Space] = ArrayHelper(spaces).to_map( lambda x: x.spaceId, lambda x: x) topic_ids = ArrayHelper(spaces).map( lambda x: x.topicIds).flatten().distinct().to_list() topic_service = get_topic_service(subject_service) topics = topic_service.find_by_ids(topic_ids, principal_service.get_tenant_id()) topic_map: Dict[TopicId, Topic] = ArrayHelper(topics).to_map( lambda x: x.topicId, lambda x: x) def link_topics(subject: Subject) -> SubjectForIndicator: connect_id = subject.connectId connected_space = connected_space_map[connect_id] space_id = connected_space.spaceId space = space_map[space_id] all_topic_ids = space.topicIds all_topics = ArrayHelper(all_topic_ids).map( lambda x: topic_map[x]).to_list() return SubjectForIndicator(**subject.to_dict(), topics=all_topics) return ArrayHelper(subjects).map(link_topics).to_list() return trans_readonly(subject_service, action)
async def find_buckets_by_measure_methods( bucket_ids: List[BucketId], principal_service: PrincipalService = Depends(get_console_principal) ) -> List[Bucket]: if len(bucket_ids) == 0: return [] bucket_service = get_bucket_service(principal_service) def action() -> List[Bucket]: tenant_id: TenantId = principal_service.get_tenant_id() return bucket_service.find_by_ids(bucket_ids, tenant_id) return trans_readonly(bucket_service, action)
async def find_buckets_by_numeric_value( query_name: Optional[str], principal_service: PrincipalService = Depends(get_console_principal)) -> List[ Bucket]: bucket_service = get_bucket_service(principal_service) def action() -> List[Bucket]: tenant_id: TenantId = principal_service.get_tenant_id() if is_blank(query_name): # noinspection PyTypeChecker return bucket_service.find_numeric_value_by_text(None, tenant_id) else: # noinspection PyTypeChecker return bucket_service.find_numeric_value_by_text(query_name, tenant_id) return trans_readonly(bucket_service, action)
async def find_buckets_page_by_name( query_name: Optional[str], pageable: Pageable = Body(...), principal_service: PrincipalService = Depends(get_admin_principal)) -> QueryBucketDataPage: bucket_service = get_bucket_service(principal_service) def action() -> QueryBucketDataPage: tenant_id: TenantId = principal_service.get_tenant_id() if is_blank(query_name): # noinspection PyTypeChecker return bucket_service.find_page_by_text(None, tenant_id, pageable) else: # noinspection PyTypeChecker return bucket_service.find_page_by_text(query_name, tenant_id, pageable) return trans_readonly(bucket_service, action)
async def find_indicators_by_name( query_name: Optional[str], principal_service: PrincipalService = Depends(get_console_principal) ) -> List[Indicator]: indicator_service = get_indicator_service(principal_service) def action() -> List[Indicator]: tenant_id: TenantId = principal_service.get_tenant_id() if is_blank(query_name): # noinspection PyTypeChecker indicators = indicator_service.find_by_text(None, tenant_id) else: # noinspection PyTypeChecker indicators = indicator_service.find_by_text(query_name, tenant_id) return filter_indicators(indicators, indicator_service, principal_service) return trans_readonly(indicator_service, action)
async def load_bucket_by_id( bucket_id: Optional[BucketId], principal_service: PrincipalService = Depends(get_console_principal) ) -> Bucket: if is_blank(bucket_id): raise_400('Bucket id is required.') bucket_service = get_bucket_service(principal_service) def action() -> Bucket: # noinspection PyTypeChecker bucket: Bucket = bucket_service.find_by_id(bucket_id) if bucket is None: raise_404() # tenant id must match current principal's if bucket.tenantId != principal_service.get_tenant_id(): raise_404() return bucket return trans_readonly(bucket_service, action)
async def find_my_achievements_by_name( query_name: Optional[str], pageable: Pageable = Body(...), principal_service: PrincipalService = Depends(get_console_principal) ) -> QueryAchievementDataPage: achievement_service = get_achievement_service(principal_service) def action() -> QueryAchievementDataPage: user_id: UserId = principal_service.get_user_id() tenant_id: TenantId = principal_service.get_tenant_id() if is_blank(query_name): # noinspection PyTypeChecker return achievement_service.find_page_by_text( None, user_id, tenant_id, pageable) else: # noinspection PyTypeChecker return achievement_service.find_page_by_text( query_name, user_id, tenant_id, pageable) return trans_readonly(achievement_service, action)
async def find_relevant_indicators( indicator_id: Optional[IndicatorId], principal_service: PrincipalService = Depends(get_admin_principal) ) -> List[Indicator]: if is_blank(indicator_id): raise_400('Indicator id is required.') indicator_service = get_indicator_service(principal_service) def action() -> List[Indicator]: # noinspection PyTypeChecker indicator: Indicator = indicator_service.find_by_id(indicator_id) if indicator is None: raise_404() # tenant id must match current principal's if indicator.tenantId != principal_service.get_tenant_id(): raise_404() # TODO find relevant indicators return [] return trans_readonly(indicator_service, action)
def find_subject_for_indicator( subject_id: Optional[SubjectId], principal_service: PrincipalService = Depends(get_console_principal) ) -> SubjectForIndicator: if is_blank(subject_id): raise_400('Subject id is required.') subject_service = get_subject_service(principal_service) def action() -> SubjectForIndicator: subject: Optional[Subject] = subject_service.find_by_id(subject_id) if subject is None: raise_404() if subject.tenantId != principal_service.get_tenant_id(): raise_403() connected_space_service = get_connected_space_service(subject_service) connected_space: Optional[ ConnectedSpace] = connected_space_service.find_by_id( subject.connectId) if connected_space is None: raise IndicatorKernelException( f'Connected space not found for subject[id={subject_id}].') space_service = get_space_service(subject_service) space: Optional[Space] = space_service.find_by_id( connected_space.spaceId) if space is None: raise IndicatorKernelException( f'Space not found for subject[id={subject_id}].') topic_service = get_topic_service(subject_service) topics = topic_service.find_by_ids(space.topicIds, principal_service.get_tenant_id()) topic_map: Dict[TopicId, Topic] = ArrayHelper(topics).to_map( lambda x: x.topicId, lambda x: x) all_topic_ids = space.topicIds all_topics = ArrayHelper(all_topic_ids).map( lambda x: topic_map[x]).to_list() return SubjectForIndicator(**subject.to_dict(), topics=all_topics) return trans_readonly(subject_service, action)
async def find_buckets_by_measure_methods( query_bucket: QueryByBucket, principal_service: PrincipalService = Depends(get_console_principal) ) -> List[Bucket]: if query_bucket is None: raise_400('Measure method is required.') if query_bucket.methods is None or len(query_bucket.methods) == 0: raise_400('Measure method is required.') measure_methods: List[Tuple[MeasureMethod, Optional[EnumId]]] = ArrayHelper(query_bucket.methods) \ .filter(lambda x: x is not None and x.method is not None) \ .filter(lambda x: x.method != MeasureMethod or (x.method == MeasureMethod.ENUM and is_not_blank(x.enumId))) \ .map(lambda x: (x.method, x.enumId)) \ .to_list() if len(measure_methods) == 0: raise_400('Measure method is required.') bucket_service = get_bucket_service(principal_service) def action() -> List[Bucket]: tenant_id: TenantId = principal_service.get_tenant_id() return bucket_service.find_by_measure_method(measure_methods, tenant_id) return trans_readonly(bucket_service, action)
async def load_indicator_by_id( indicator_id: Optional[IndicatorId], principal_service: PrincipalService = Depends(get_admin_principal) ) -> Indicator: if is_blank(indicator_id): raise_400('Indicator id is required.') indicator_service = get_indicator_service(principal_service) def action() -> Indicator: # noinspection PyTypeChecker indicator: Indicator = indicator_service.find_by_id(indicator_id) if indicator is None: raise_404() # tenant id must match current principal's if indicator.tenantId != principal_service.get_tenant_id(): raise_404() indicators = filter_indicators([indicator], indicator_service, principal_service) if len(indicators) == 0: raise_404() return indicator return trans_readonly(indicator_service, action)
async def load_analysis_by_id( analysis_id: Optional[ObjectiveAnalysisId], principal_service: PrincipalService = Depends(get_console_principal) ) -> ObjectiveAnalysis: if is_blank(analysis_id): raise_400('Objective analysis id is required.') analysis_service = get_analysis_service(principal_service) # noinspection DuplicatedCode def action() -> ObjectiveAnalysis: # noinspection PyTypeChecker analysis: ObjectiveAnalysis = analysis_service.find_by_id(analysis_id) if analysis is None: raise_404() # user id must match current principal's if analysis.userId != principal_service.get_user_id(): raise_404() # tenant id must match current principal's if analysis.tenantId != principal_service.get_tenant_id(): raise_404() return analysis return trans_readonly(analysis_service, action)