Пример #1
0
    def post(self, project_gid, algorithm_module, algorithm_classname):
        """
        :generic method of launching Analyzers
        """
        model_file = self.extract_file_from_request()
        destination_folder = RestResource.get_destination_folder()
        h5_path = RestResource.save_temporary_file(model_file,
                                                   destination_folder)

        try:
            project = self.project_service.find_project_lazy_by_gid(
                project_gid)
        except ProjectServiceException:
            raise InvalidIdentifierException(INVALID_PROJECT_GID_MESSAGE %
                                             project_gid)

        algorithm = FlowService.get_algorithm_by_module_and_class(
            algorithm_module, algorithm_classname)
        if algorithm is None:
            raise InvalidIdentifierException(
                'No algorithm found for: %s.%s' %
                (algorithm_module, algorithm_classname))

        try:
            adapter_instance = ABCAdapter.build_adapter(algorithm)
            view_model = adapter_instance.get_view_model_class()()

            view_model_h5 = ViewModelH5(h5_path, view_model)
            view_model_gid = view_model_h5.gid.load()

            # TODO: use logged user
            user_id = project.fk_admin
            operation = self.operation_service.prepare_operation(
                user_id, project.id, algorithm.id,
                algorithm.algorithm_category, view_model_gid.hex, None, {})
            storage_path = self.files_helper.get_project_folder(
                project, str(operation.id))

            if isinstance(adapter_instance, ABCUploader):

                for key, value in adapter_instance.get_form_class(
                ).get_upload_information().items():
                    data_file = self.extract_file_from_request(
                        file_name=key, file_extension=value)
                    data_file_path = RestResource.save_temporary_file(
                        data_file, destination_folder)
                    file_name = os.path.basename(data_file_path)
                    upload_field = getattr(view_model_h5, key)
                    upload_field.store(os.path.join(storage_path, file_name))
                    shutil.move(data_file_path, storage_path)

            shutil.move(h5_path, storage_path)
            os.rmdir(destination_folder)
            view_model_h5.close()
            OperationService().launch_operation(operation.id, True)
        except Exception as excep:
            self.logger.error(excep, exc_info=True)
            raise ServiceException(str(excep))

        return operation.gid, HTTP_STATUS_CREATED
Пример #2
0
    def launch_operation(self, current_user_id, model_file, project_gid,
                         algorithm_module, algorithm_classname, fetch_file):
        temp_folder = FilesHelper.create_temp_folder()
        model_h5_path = FilesHelper.save_temporary_file(
            model_file, temp_folder)

        try:
            project = self.project_service.find_project_lazy_by_gid(
                project_gid)
        except ProjectServiceException:
            raise InvalidIdentifierException()

        algorithm = FlowService.get_algorithm_by_module_and_class(
            algorithm_module, algorithm_classname)
        if algorithm is None:
            raise InvalidIdentifierException(
                'No algorithm found for: %s.%s' %
                (algorithm_module, algorithm_classname))

        try:
            adapter_instance = ABCAdapter.build_adapter(algorithm)
            view_model = adapter_instance.get_view_model_class()()

            view_model_h5 = ViewModelH5(model_h5_path, view_model)
            view_model_gid = view_model_h5.gid.load()

            operation = self.operation_service.prepare_operation(
                current_user_id, project.id, algorithm.id,
                algorithm.algorithm_category, view_model_gid.hex, None, {})
            storage_path = self.files_helper.get_project_folder(
                project, str(operation.id))

            if isinstance(adapter_instance, ABCUploader):
                for key, value in adapter_instance.get_form_class(
                ).get_upload_information().items():
                    data_file = fetch_file(request_file_key=key,
                                           file_extension=value)
                    data_file_path = FilesHelper.save_temporary_file(
                        data_file, temp_folder)
                    file_name = os.path.basename(data_file_path)
                    upload_field = getattr(view_model_h5, key)
                    upload_field.store(os.path.join(storage_path, file_name))
                    shutil.move(data_file_path, storage_path)

            shutil.move(model_h5_path, storage_path)
            os.rmdir(temp_folder)
            view_model_h5.close()
            OperationService().launch_operation(operation.id, True)
            return operation.gid
        except Exception as excep:
            self.logger.error(excep, exc_info=True)
            raise ServiceException(str(excep))
Пример #3
0
    def launch_simulation(self, current_user_id, zip_directory, project_gid):
        try:
            project = self.project_service.find_project_lazy_by_gid(
                project_gid)
        except ProjectServiceException:
            raise InvalidIdentifierException()

        try:
            simulator_h5_name = DirLoader(
                zip_directory, None).find_file_for_has_traits_type(Simulator)
            simulator_file = os.path.join(zip_directory, simulator_h5_name)
        except IOError:
            raise InvalidInputException(
                'No Simulator h5 file found in the archive')

        try:
            simulator_algorithm = AlgorithmService(
            ).get_algorithm_by_module_and_class(SimulatorAdapter.__module__,
                                                SimulatorAdapter.__name__)
            simulation = self.simulator_service.prepare_simulation_on_server(
                user_id=current_user_id,
                project=project,
                algorithm=simulator_algorithm,
                zip_folder_path=zip_directory,
                simulator_file=simulator_file)
            return simulation.gid
        except Exception as excep:
            self.logger.error(excep, exc_info=True)
            raise ServiceException(str(excep))
Пример #4
0
 def _check_permission(self, logged_user_id):
     try:
         project = self.project_dao.get_project_lazy_by_gid(
             self.resource_identifier)
     except (ProjectServiceException, NoResultFound):
         raise InvalidIdentifierException()
     return self.check_project_permission(logged_user_id, project.id)
Пример #5
0
 def _check_permission(self, logged_user_id):
     operation = ProjectService.load_operation_by_gid(
         self.resource_identifier)
     if operation is None:
         raise InvalidIdentifierException()
     return self.check_project_permission(logged_user_id,
                                          operation.fk_launched_in)
Пример #6
0
    def get(self, project_gid):
        """
        :return a list of project's Operation entities
        """
        page_number = request.args.get(Strings.PAGE_NUMBER.value)
        if page_number is None:
            page_number = 1
        try:
            page_number = int(page_number)
        except ValueError:
            raise InvalidInputException(message="Invalid page number")

        try:
            project = self.project_service.find_project_lazy_by_gid(
                project_gid)
        except ProjectServiceException:
            raise InvalidIdentifierException(INVALID_PROJECT_GID_MESSAGE %
                                             project_gid)

        _, _, operations, pages = self.project_service.retrieve_project_full(
            project.id, current_page=int(page_number))
        return {
            "operations":
            [OperationDto(operation) for operation in operations],
            "pages": pages
        }
Пример #7
0
    def get_operation_status(operation_gid):
        operation = ProjectService.load_operation_by_gid(operation_gid)
        if operation is None:
            get_logger().warning("Invalid operation GID: {}".format(operation_gid))
            raise InvalidIdentifierException()

        return operation.status
Пример #8
0
    def get_project_operations(self, project_gid, page_number):
        try:
            project = self.project_service.find_project_lazy_by_gid(project_gid)
        except ProjectServiceException:
            raise InvalidIdentifierException()

        _, _, operations, pages = self.project_service.retrieve_project_full(project.id, current_page=int(page_number))
        return [OperationDto(operation) for operation in operations], pages
Пример #9
0
    def get_datatypes_in_project(self, project_gid):
        try:
            project = self.project_service.find_project_lazy_by_gid(project_gid)
        except ProjectServiceException:
            raise InvalidIdentifierException()

        datatypes = self.project_service.get_datatypes_in_project(project.id)
        return [DataTypeDto(datatype) for datatype in datatypes]
Пример #10
0
    def get(self, operation_gid):
        """
        :return status of an operation
        """
        operation = ProjectService.load_operation_by_gid(operation_gid)
        if operation is None:
            raise InvalidIdentifierException(INVALID_OPERATION_GID_MESSAGE % operation_gid)

        return operation.status
Пример #11
0
 def get(self, datatype_gid):
     """
     :return the available operations for that datatype, as a list of Algorithm instances
     """
     categories = dao.get_launchable_categories()
     datatype = dao.get_datatype_by_gid(datatype_gid)
     if datatype is None:
         raise InvalidIdentifierException(INVALID_DATATYPE_GID_MESSAGE.format(datatype_gid))
     _, filtered_adapters, _ = self.flow_service.get_launchable_algorithms_for_datatype(datatype, categories)
     return [AlgorithmDto(algorithm) for algorithm in filtered_adapters]
Пример #12
0
 def get(self, username):
     """
     :return a list of user's projects
     """
     user = UserService.get_user_by_name(username)
     if user is None:
         raise InvalidIdentifierException(
             'No user registered with username: %s' % username)
     projects = ProjectService.retrieve_all_user_projects(user_id=user.id)
     return [ProjectDto(project) for project in projects]
Пример #13
0
    def launch_operation(self, current_user_id, model_file, project_gid,
                         algorithm_module, algorithm_classname, fetch_file):
        temp_folder = create_temp_folder()
        model_h5_path = save_temporary_file(model_file, temp_folder)

        try:
            project = self.project_service.find_project_lazy_by_gid(
                project_gid)
        except ProjectServiceException:
            raise InvalidIdentifierException()

        try:
            algorithm = AlgorithmService.get_algorithm_by_module_and_class(
                algorithm_module, algorithm_classname)
            if algorithm is None:
                raise InvalidIdentifierException(
                    'No algorithm found for: %s.%s' %
                    (algorithm_module, algorithm_classname))

            adapter_instance = ABCAdapter.build_adapter(algorithm)
            view_model = h5.load_view_model_from_file(model_h5_path)
            if isinstance(adapter_instance, ABCUploader):
                with ViewModelH5(model_h5_path, view_model) as view_model_h5:
                    for key, value in adapter_instance.get_form_class(
                    ).get_upload_information().items():
                        data_file = fetch_file(request_file_key=key,
                                               file_extension=value)
                        data_file_path = save_temporary_file(
                            data_file, temp_folder)
                        view_model_h5.store_metadata_param(key, data_file_path)
            view_model = h5.load_view_model_from_file(model_h5_path)

            operation = self.operation_service.prepare_operation(
                current_user_id, project, algorithm, view_model=view_model)
            if os.path.exists(model_h5_path):
                os.remove(model_h5_path)

            OperationService().launch_operation(operation.id, True)
            return operation.gid
        except Exception as excep:
            self.logger.error(excep, exc_info=True)
            raise ServiceException(str(excep))
Пример #14
0
 def get(self, datatype_gid):
     """
     :given a guid, this function will download the H5 full data
     """
     index = ABCAdapter.load_entity_by_gid(datatype_gid)
     if index is None:
         raise InvalidIdentifierException(INVALID_DATATYPE_GID_MESSAGE.format(datatype_gid))
     h5_file = h5_file_for_index(index)
     last_index = h5_file.path.rfind('\\')
     file_name = h5_file.path[last_index + 1:]
     return flask.send_file(h5_file.path, as_attachment=True, attachment_filename=file_name)
Пример #15
0
    def get(self, project_gid):
        """
        :return a list of DataType instances (subclasses) associated with the current project
        """
        try:
            project = self.project_service.find_project_lazy_by_gid(project_gid)
        except ProjectServiceException:
            raise InvalidIdentifierException(INVALID_PROJECT_GID_MESSAGE % project_gid)

        datatypes = self.project_service.get_datatypes_in_project(project.id)
        return [DataTypeDto(datatype) for datatype in datatypes]
Пример #16
0
    def get_operations_results(operation_gid):
        operation = ProjectService.load_operation_lazy_by_gid(operation_gid)
        if operation is None:
            get_logger().warning("Invalid operation GID: {}".format(operation_gid))
            raise InvalidIdentifierException()

        data_types = ProjectService.get_results_for_operation(operation.id)
        if data_types is None:
            return []

        return [DataTypeDto(datatype) for datatype in data_types]
Пример #17
0
    def get(self, operation_gid):
        """
        :return list of DataType instances (subclasses), representing the results of that operation if it has finished and
        None, if the operation is still running, has failed or simply has no results.
        """
        operation = ProjectService.load_operation_lazy_by_gid(operation_gid)
        if operation is None:
            raise InvalidIdentifierException(INVALID_OPERATION_GID_MESSAGE % operation_gid)

        data_types = ProjectService.get_results_for_operation(operation.id)
        if data_types is None:
            return []

        return [DataTypeDto(datatype) for datatype in data_types]
Пример #18
0
 def _check_permission(self, logged_user_id):
     datatype = self.datatype_dao.get_datatype_by_gid(
         self.resource_identifier)
     if datatype is None:
         raise InvalidIdentifierException()
     if self.check_project_permission(
             logged_user_id, datatype.parent_operation.fk_launched_in):
         return True
     links = self.datatype_dao.get_links_for_datatype(datatype.id)
     if links is not None:
         for link in links:
             if self.check_project_permission(logged_user_id,
                                              link.fk_to_project):
                 return True
     return False
Пример #19
0
    def add_members_to_project(self, current_user_id, project_gid, new_members_gid):
        try:
            project = self.project_service.find_project_lazy_by_gid(project_gid)
        except Exception:
            raise InvalidIdentifierException("Invalid project identifier.")

        if current_user_id != project.fk_admin:
            raise AuthorizationRequestException("Your are not allowed to edit given project")

        new_members_id = []
        for gid in new_members_gid:
            user = self.user_service.get_user_by_gid(gid)
            if user is None:
                raise InvalidInputException("Invalid user gid {}".format(gid))
            new_members_id.append(user.id)
        self.project_dao.add_members_to_project(project.id, new_members_id)
Пример #20
0
    def post(self, project_gid):
        """
        :start a simulation using a project id and a zip archive with the simulator data serialized
        """
        file = self.extract_file_from_request(
            request_file_key=RequestFileKey.SIMULATION_FILE_KEY.value,
            file_extension=FilesHelper.TVB_ZIP_FILE_EXTENSION)
        destination_folder = RestResource.get_destination_folder()
        zip_path = RestResource.save_temporary_file(file, destination_folder)

        try:
            project = self.project_service.find_project_lazy_by_gid(
                project_gid)
        except ProjectServiceException:
            raise InvalidIdentifierException(INVALID_PROJECT_GID_MESSAGE %
                                             project_gid)

        result = FilesHelper().unpack_zip(zip_path, os.path.dirname(zip_path))
        if len(result) == 0:
            raise InvalidInputException("Empty zip archive")

        folder_path = os.path.dirname(result[0])
        simulator_algorithm = FlowService().get_algorithm_by_module_and_class(
            SimulatorAdapter.__module__, SimulatorAdapter.__name__)
        try:
            simulator_h5_name = DirLoader(
                folder_path, None).find_file_for_has_traits_type(Simulator)
            simulator_file = os.path.join(folder_path, simulator_h5_name)
        except IOError:
            raise InvalidInputException(
                'No Simulator h5 file found in the archive')

        try:
            current_user = get_current_user()
            operation = self.simulator_service.prepare_simulation_on_server(
                user_id=current_user.id,
                project=project,
                algorithm=simulator_algorithm,
                zip_folder_path=folder_path,
                simulator_file=simulator_file)
        except Exception as excep:
            self.logger.error(excep, exc_info=True)
            raise ServiceException(str(excep))

        return operation.gid, HTTP_STATUS_CREATED
Пример #21
0
    def put(self, project_gid):
        """
        Add members to the given project
        :param project_gid: project gid
        :param
        """
        try:
            project = self.project_service.find_project_lazy_by_gid(project_gid)
        except Exception:
            raise InvalidIdentifierException("Invalid project identifier.")

        if get_current_user().id != project.fk_admin:
            raise AuthorizationRequestException("Your are not allowed to edit given project")

        input_data = flask.request.json
        new_members_gid = input_data[
            FormKeyInput.NEW_MEMBERS_GID.value] if FormKeyInput.NEW_MEMBERS_GID.value in input_data else []
        new_members_id = []
        for gid in new_members_gid:
            user = self.user_service.get_user_by_gid(gid)
            if user is None:
                raise InvalidInputException("Invalid user gid {}".format(gid))
            new_members_id.append(user.id)
        self.project_dao.add_members_to_project(project.id, new_members_id)