Ejemplo n.º 1
0
    def copy_study(
        self,
        src_uuid: str,
        dest_study_name: str,
        group_ids: List[str],
        params: RequestParameters,
    ) -> str:
        src_study = self._get_study(src_uuid)
        self._assert_permission(params.user, src_study,
                                StudyPermissionType.READ)

        if not isinstance(src_study, RawStudy):
            raise StudyTypeUnsupported(
                f"Study {src_uuid} with type {src_study.type} not recognized")

        dest_study = deepcopy(src_study)
        dest_study.id = str(uuid4())
        dest_study.name = dest_study_name
        dest_study.workspace = DEFAULT_WORKSPACE_NAME
        dest_study.path = str(self.study_service.get_default_workspace_path() /
                              dest_study.id)

        study = self.study_service.copy_study(src_study, dest_study)
        self._save_study(study, params.user, group_ids)
        self.event_bus.push(
            Event(EventType.STUDY_CREATED, study.to_json_summary()))
        return str(study.id)
def test_lifecycle():
    eventbus = LocalEventBus()
    event = Event("test", "foo")
    eventbus.push_event(event)
    assert eventbus.get_events() == [event]
    eventbus.clear_events()
    assert len(eventbus.get_events()) == 0
    def get_events(self) -> List[Event]:
        try:
            event = self.pubsub.get_message(ignore_subscribe_messages=True)
            if event is not None:
                return [Event(**json.loads(event["data"]))]
        except Exception as e:
            logger.error("Failed to retrieve or parse event !", exc_info=e)

        return []
Ejemplo n.º 4
0
    def delete_output(self, uuid: str, output_name: str,
                      params: RequestParameters) -> None:
        study = self._get_study(uuid)
        self._assert_permission(params.user, study, StudyPermissionType.WRITE)
        if not isinstance(study, RawStudy):
            raise StudyTypeUnsupported(
                f"Study {uuid} with type {study.type} not recognized")

        self.study_service.delete_output(study, output_name)
        self.event_bus.push(
            Event(EventType.STUDY_EDITED, study.to_json_summary()))
Ejemplo n.º 5
0
 def update(self, job_result: JobResult) -> None:
     job_result.completion_date = datetime.utcnow()
     self.repository.save(job_result)
     self.event_bus.push(
         Event(
             EventType.STUDY_JOB_COMPLETED,
             {
                 "jid": str(job_result.id),
                 "sid": job_result.study_id
             },
         ))
Ejemplo n.º 6
0
    def sync_studies_on_disk(self, folders: List[StudyFolder]) -> None:

        # delete orphan studies on database
        paths = [str(f.path) for f in folders]
        for study in self.repository.get_all():
            if isinstance(
                    study,
                    RawStudy) and (study.workspace != DEFAULT_WORKSPACE_NAME
                                   and study.path not in paths):
                logger.info(
                    f"Study={study.id} is not present in disk and will be deleted"
                )
                self.event_bus.push(
                    Event(EventType.STUDY_DELETED, study.to_json_summary()))
                self.repository.delete(study.id)

        # Add new studies
        paths = [
            study.path for study in self.repository.get_all()
            if isinstance(study, RawStudy)
        ]
        for folder in folders:
            if str(folder.path) not in paths:
                study = RawStudy(
                    id=str(uuid4()),
                    name=folder.path.name,
                    path=str(folder.path),
                    workspace=folder.workspace,
                    owner=None,
                    groups=folder.groups,
                    public_mode=PublicMode.FULL
                    if len(folder.groups) == 0 else PublicMode.NONE,
                )

                study.content_status = self._analyse_study(study)

                logger.info(
                    f"Study={study.id} appears on disk and will be added")
                self.event_bus.push(
                    Event(EventType.STUDY_CREATED, study.to_json_summary()))
                self.repository.save(study)
Ejemplo n.º 7
0
def test_lifecycle():
    event_bus = build_eventbus(MagicMock(), Config(), autostart=True)
    test_bucket = []
    lid = event_bus.add_listener(lambda event: test_bucket.append(event))
    event = Event("test", "foo")
    event_bus.push(event)
    autoretry(lambda: len(test_bucket) == 1, 2)

    event_bus.remove_listener(lid)
    test_bucket.clear()
    event_bus.push(event)
    autoretry(lambda: len(test_bucket) == 0, 2)
Ejemplo n.º 8
0
    def edit_study(self, route: str, new: JSON,
                   params: RequestParameters) -> JSON:
        uuid, url = StorageServiceUtils.extract_info_from_url(route)
        study = self._get_study(uuid)
        self._assert_permission(params.user, study, StudyPermissionType.WRITE)
        if not isinstance(study, RawStudy):
            raise StudyTypeUnsupported(
                f"Study {uuid} with type {study.type} not recognized")

        updated = self.study_service.edit_study(study, url, new)
        self.event_bus.push(
            Event(EventType.STUDY_EDITED, study.to_json_summary()))
        return updated
Ejemplo n.º 9
0
    def upload_matrix(self, path: str, data: bytes,
                      params: RequestParameters) -> None:
        uuid, matrix_path = StorageServiceUtils.extract_info_from_url(path)
        study = self._get_study(uuid)
        self._assert_permission(params.user, study, StudyPermissionType.WRITE)
        if not isinstance(study, RawStudy):
            raise StudyTypeUnsupported(
                f"Study {uuid} with type {study.type} not recognized")

        self.importer_service.upload_matrix(study, matrix_path, data)

        self.event_bus.push(
            Event(EventType.STUDY_EDITED, study.to_json_summary()))
Ejemplo n.º 10
0
def test_lifecycle():
    redis_client = Mock()
    pubsub_mock = Mock()
    redis_client.pubsub.return_value = pubsub_mock
    eventbus = RedisEventBus(RedisConfig(), redis_client)
    pubsub_mock.subscribe.assert_called_once_with("events")

    event = Event("test", "foo")
    serialized = json.dumps(dataclasses.asdict(event))
    pubsub_mock.get_message.return_value = {"data": serialized}
    eventbus.push_event(event)
    redis_client.publish.assert_called_once_with("events", serialized)
    assert eventbus.get_events() == [event]
Ejemplo n.º 11
0
 def create_study(self, study_name: str, group_ids: List[str],
                  params: RequestParameters) -> str:
     sid = str(uuid4())
     study_path = str(self.study_service.get_default_workspace_path() / sid)
     raw = RawStudy(
         id=sid,
         name=study_name,
         workspace=DEFAULT_WORKSPACE_NAME,
         path=study_path,
     )
     raw = self.study_service.create_study(raw)
     self._save_study(raw, params.user, group_ids)
     self.event_bus.push(
         Event(EventType.STUDY_CREATED, raw.to_json_summary()))
     return str(raw.id)
Ejemplo n.º 12
0
 def import_study(
     self,
     stream: IO[bytes],
     group_ids: List[str],
     params: RequestParameters,
 ) -> str:
     sid = str(uuid4())
     path = str(self.study_service.get_default_workspace_path() / sid)
     study = RawStudy(id=sid, workspace=DEFAULT_WORKSPACE_NAME, path=path)
     study = self.importer_service.import_study(study, stream)
     status = self._analyse_study(study)
     self._save_study(
         study,
         owner=params.user,
         group_ids=group_ids,
         content_status=status,
     )
     self.event_bus.push(
         Event(EventType.STUDY_CREATED, study.to_json_summary()))
     return str(study.id)
Ejemplo n.º 13
0
    def run_study(self, study_uuid: str, params: RequestParameters) -> UUID:
        study_info = self.storage_service.get_study_information(
            uuid=study_uuid, params=params)
        study_version = study_info["antares"]["version"]
        study_path = self.storage_service.get_study_path(study_uuid, params)
        job_uuid: UUID = self.launcher.run_study(study_path, study_version)

        self.repository.save(
            JobResult(
                id=str(job_uuid),
                study_id=study_uuid,
                job_status=JobStatus.RUNNING,
            ))
        self.event_bus.push(
            Event(
                EventType.STUDY_JOB_STARTED,
                {
                    "jid": str(job_uuid),
                    "sid": study_uuid
                },
            ))

        return job_uuid