Пример #1
0
    def exists_by_id(id_: ObjectId):
        """Check if the given Object ID exists in MongoDB.

        Args:
            id_ (ObjectId): Model ID.
        """
        return bool(ModelDO.objects(id=id_))
Пример #2
0
    def to_model_do(self):
        """Convert business object to plain object."""
        input_dos = list(map(IOShape.to_io_shape_po, self.inputs))
        output_dos = list(map(IOShape.to_io_shape_po, self.outputs))

        model_do = ModelDO(
            name=self.name,
            framework=self.framework.value,
            engine=self.engine.value,
            version=self.version.ver,
            dataset=self.dataset,
            metric={key.name.lower(): val
                    for key, val in self.metric.items()},
            inputs=input_dos,
            outputs=output_dos,
            task=self.task.value,
            parent_model_id=self.parent_model_id,
            status=self.status.value,
            model_status=[item.value for item in self.model_status],
            creator=self.creator,
            create_time=self.create_time,
        )
        if self._id is not None:
            model_do.id = ObjectId(self._id)
            # patch save weight to Grid FS
            model_do.weight = self.weight.gridfs_file
            # compare with the newly loaded weight and the stored weight in MongoDB
            if self.weight.is_dirty():
                new_md5 = hashlib.md5(self.weight.weight).hexdigest()
                if new_md5 != self.weight.md5:
                    model_do.weight.replace(
                        self.weight.weight,
                        filename=self.weight.filename,
                        content_type=self.weight.content_type)
                self.weight.clear_dirty_flag()
        else:
            model_do.weight.put(self.weight.weight,
                                filename=self.weight.filename,
                                content_type=self.weight.content_type)

        # convert profile result
        if self.profile_result is not None:
            model_do.profile_result = self.profile_result.to_profile_result_po(
            )

        return model_do
Пример #3
0
    def get_models_by_parent_id(parent_id: str) -> List[ModelDO]:
        """Get a list of model plain objects given parent id.

        Args:
            parent_id (str): the parent model id of current model if this model is derived from a pre-existing one

        Return:
            List[ModelDO]: A list of model plain objects. An empty list will be returned if no such model.
        """
        return ModelDO.objects(parent_model_id=parent_id)
Пример #4
0
    def get_models_by_task(task: int) -> List[ModelDO]:
        """Get a list of model plain objects given task.

        Args:
            task (Task): Model predictive or descriptive task name

        Return:
            List[ModelDO]: A list of model plain objects. An empty list will be returned if no such model.
        """
        return ModelDO.objects(task=task)
Пример #5
0
    def get_model_by_id(id_: ObjectId) -> ModelDO:
        """Get model plain object given model ID.

        Args:
            id_ (ObjectId): Model ID.

        Return:
            ModelDO: Model plain object. None for model PO not found.
        """
        return ModelDO.objects(id=id_).first()
Пример #6
0
    def delete_model_by_id(id_: ObjectId) -> int:
        """Delete model given model ID.

        Args:
            id_ (ObjectId): Model ID.

        Return:
            int: number of affected rows.
        """
        return ModelDO.objects(id=id_).delete()
Пример #7
0
    def save_model(model: ModelDO, force_insert=False) -> ModelDO:
        """Save a model PO.

        Args:
            model (ModelDO): Model plain object to be saved.
            force_insert (bool): Only try to create a new document. Default to `False`.

        Returns:
            ModelPo: Updated model plain object.
        """
        return model.save(force_insert=force_insert)
Пример #8
0
    def register_static_profiling_result(
            id_: ObjectId,
            static_profiling_result: StaticProfileResultDO) -> int:
        """Register static profiling result.

        Args:
            id_ (objectId): ID of the model, where the static profiling result is added.
            static_profiling_result (StaticProfileResultPO): Static profiling result.

        Return:
            int: number of affected rows.
        """
        return ModelDO.objects(id=id_).update_one(
            set__profile_result__static_profile_result=static_profiling_result)
Пример #9
0
    def update_model(
            id_: ObjectId,
            **kwargs) -> ModelDO:  # TODO: try ModelPO.objects(...).update()?
        """
        Update or register model PO.

        Args:
            id_ (ObjectId): Model ID.
            **kwargs: Keyword arguments to be updated.

        Returns:
            ModelDO:
        """
        return ModelDO.objects(id=id_).update(**kwargs)
Пример #10
0
    def get_models(**kwargs) -> List[ModelDO]:
        """Get a list of model plain object given model name, framework and engine.

        Args:
            kwargs (dict): A dictionary of arguments:
                name (str): model name.
                task: Model task.
                framework: Model framework.
                engine: Model engine.
                version: Model version.
        Return:
            List[ModelDO]: A list of model plain objects.
        """
        return ModelDO.objects(**kwargs).order_by('name', 'task', 'framework',
                                                  'engine', '-version')
Пример #11
0
    def delete_dynamic_profiling_result(
        id_: ObjectId,
        ip: str,
        device_id: str,
    ) -> None:
        """Delete dynamic profiling result.

        Args:
            id_ (ObjectId): ID of the model.
            ip (str): IP address of dynamic profiling result to be deleted.
            device_id (str): Device ID of dynamic profiling result to be deleted.
        """
        return ModelDO.objects(id=id_, ).update_one(
            pull__profile_result__dynamic_profile_results__ip=ip,
            pull__profile_result__dynamic_profile_results__device_id=device_id)
Пример #12
0
    def register_dynamic_profiling_result(
            id_: ObjectId,
            dynamic_profiling_result: DynamicProfileResultDO) -> int:
        """Register dynamic profiling result.

        Args:
            id_ (ObjectId): ID of the model, where the static profiling result is appended.
            dynamic_profiling_result (DynamicProfileResultPO): Dynamic profiling result.

        Return:
            int: number of affected rows.
        """
        return ModelDO.objects(
            id=id_).update_one(push__profile_result__dynamic_profile_results=
                               dynamic_profiling_result)
Пример #13
0
    def exists_by_primary_keys(**kwargs):
        """

        Args:
            **kwargs: Keyword arguments of primary keys. Supported values:
                name (str): Model name.
                task (str): Model task.
                engine (int): Driving engine enum value.
                framework (int): Model framework enum value.
                version (int): Model version number.

        Returns:
            bool: Existence of the model.
        """
        return bool(ModelDO.objects(**kwargs))
Пример #14
0
    def update_dynamic_profiling_result(
            id_: ObjectId,
            dynamic_profiling_result: DynamicProfileResultDO) -> int:
        """Update dynamic profiling result.

        Args:
            id_ (ObjectId): ID of the model.
            dynamic_profiling_result (DynamicProfileResultPO): Dynamic profiling result to be updated.

        Return:
            int: number of affected rows.
        """
        return ModelDO.objects(
            id=id_,
            profile_result__dynamic_profile_results__ip=dynamic_profiling_result
            .ip,
            profile_result__dynamic_profile_results__device_id=
            dynamic_profiling_result.device_id).update(
                set__profile_result__dynamic_profile_results__S=
                dynamic_profiling_result)
Пример #15
0
    def exists_dynamic_profiling_result_by_pks(
        id_: ObjectId,
        ip: str,
        device_id: str,
    ) -> bool:
        """Check if the dynamic profiling result exists.

        Args:
            id_ (ObjectId): ID of the model.
            ip (str): IP address of dynamic profiling result to be deleted.
            device_id (str): Device ID of dynamic profiling result to be deleted.

        Return:
             bool: `True` for existence, `False` otherwise.
        """
        return bool(
            ModelDO.objects(
                id=id_,
                profile_result__dynamic_profile_results__ip=ip,
                profile_result__dynamic_profile_results__device_id=device_id))