Ejemplo n.º 1
0
 def __init__(self, name=None):
     super().__init__(name=name)
     self.core_service = CoreAPI()
     self.id = None
     self.response = None
     self.progress = 0
     self.pbar = None
Ejemplo n.º 2
0
 def __init__(self):
     self.get_model = CoreAPI().get_multimodels_by_id
     self.download_model = CoreAPI().get_models_download_by_id
     self.task_status = CoreStatus.PENDING
     self.id = None
     self.name = None
     self.exp_id = None
     self.attributes = None
     self.predictionPipeline = None
     self.hyperparameters = None
     self.created_at = None
     self.updated_at = None
     self.completed_at = None
Ejemplo n.º 3
0
 def __init__(self):
     self.get_model = CoreAPI().get_models_by_id
     self.download_model = CoreAPI().get_models_download_by_id
     self.task_status = CoreStatus.PENDING
     self.id = None
     self.key = None
     self.name = None
     self.exp_id = None
     self.importances = None
     self.attributes = None
     self.hyperparameters = None
     self.created_at = None
     self.updated_at = None
     self.completed_at = None
Ejemplo n.º 4
0
    def create(cls, username, password, host):
        """Create context instance and init necessary variable and objects.

        Setting the user, password, and host for the funture connection when
        calling APIs, and create an event loop if it isn't exist. Check if the
        connection is healthy after args be set.

        Args:
            username (str): User name for login Decanter Core server
            password (str): Password name for login Decanter Core server
            host (str): Decanter Core server URL.

        Returns:
            :class:`~decanter.core.context.Context>`

        """
        context = cls()
        context.close()
        Context.USERNAME = username
        Context.PASSWORD = password
        Context.HOST = host

        # get the current event loop
        # it will create a new event loop if it does not exist
        Context.LOOP = get_or_create_eventloop()

        # if the current loop is closed create a new one
        if Context.LOOP.is_closed():
            asyncio.set_event_loop(asyncio.new_event_loop())
            Context.LOOP = get_or_create_eventloop()
            logger.debug('[Context] create and set new event loop')
        context.healthy()
        Context.api = CoreAPI()
        return context
Ejemplo n.º 5
0
 def __init__(self, task, jobs=None, name=None):
     self.id = None
     self.status = CoreStatus.PENDING
     self.result = None
     self.task = task
     self.jobs = jobs
     self.name = name
     self.core_service = CoreAPI()
Ejemplo n.º 6
0
    def create(cls, exp_id, name=None):
        """Create Experiment by exp_id.

        Args:
            exp_id (str): ObjectId in 24 hex digits.
            name (:obj:`str`, optional): Name to track Job progress.

        Returns:
            :class:`~decanter.core.jobs.experiment.Experiment`: Experiment object
                with the specific id.
        """
        core_service = CoreAPI()
        exp_resp = check_response(
            core_service.get_experiments_by_id(exp_id)).json()
        exp = cls(train_input=None)
        exp.update_result(exp_resp)
        exp.status = CoreStatus.DONE
        exp.name = name
        return exp
Ejemplo n.º 7
0
class CoreTask(Task):
    """Handle Decanter Core Action's result

    Handle the task relate to Decanter Core server, such as upload data,
    training, prediction.

    Attributes:
        core_service (str): Status of task.
        id (str): Task identifier created by creating a task.
        response (dict): Responses from the request api.
        progress (float): Progress of the task process.
        name (str): Name of task for tracking process.
    """
    BAR_CNT = 0
    'int: The position of progress bar to avoid overlapping.'

    def __init__(self, name=None):
        super().__init__(name=name)
        self.core_service = CoreAPI()
        self.id = None
        self.response = None
        self.progress = 0
        self.pbar = None

    async def update(self):
        """Update the response from Decanter server.

        Get the task from sending api request and update the result
        of response.
        """
        func = partial(self.core_service.get_tasks_by_id, task_id=self.id)
        self.response = await Context.LOOP.run_in_executor(None, func)
        if self.status in CoreStatus.DONE_STATUS:
            return
        self.response = check_response(self.response).json()
        self.update_task_response()
        logger.debug(
            '[Task]\'%s\' done update. status: %s', self.name, self.status)

    def update_task_response(self):
        """Update the result from response

        Update progress, and status. Update progress bar due to the
        progress key value of response.
        """
        logger.debug(
            '[Task] \'%s\' start update task resp. status: %s',
            self.name, self.status)

        def update_pbar(resp_progress):
            diff = int((resp_progress - self.progress)*100)
            self.pbar.update(diff)
            self.progress = resp_progress

        for key_ in [CoreKeys.id, CoreKeys.progress, CoreKeys.result, CoreKeys.status]:
            attr, key = key_.name, key_.value
            try:
                if key_ == CoreKeys.progress:
                    update_pbar(self.response[key])
                setattr(self, attr, self.response[key])
            except KeyError as err:
                logger.debug(str(err))

    @abc.abstractmethod
    def run(self):
        """Execute Decanter Core task.
        Raises:
            NotImplementedError: If child class do not implement this function.
        """
        raise NotImplementedError('Please Implement run method in CoreTask')

    def run_core_task(self, api_func, **kwargs):
        """Start running Decanter Core task by calling api_func.

        Args:
            api_func (func): CoreAPI function.
            kwargs: Parameters for api_func.
        """
        logger.debug('[%s] \'%s\' start.', self.__class__.__name__, self.name)
        self.response = check_response(
            api_func(**kwargs), key=CoreKeys.id.value)
        self.response = self.response.json()
        self.id = self.response[CoreKeys.id.value]
        logger.debug(
            '[%s] \'%s\' upload task id: %s',
            self.__class__.__name__, self.name, self.id)

        self.pbar = tqdm(
            total=100, position=CoreTask.BAR_CNT, leave=True,
            bar_format='{l_bar}{bar}', desc='Progress %s' % self.name)
        CoreTask.BAR_CNT += 1

        self.status = CoreStatus.RUNNING

    def stop(self):
        """Stop undone task in Decanter Core server

        Send the stop task api to stop the running or pending task.
        """
        if self.id is not None:
            check_response(self.core_service.put_tasks_stop_by_id(self.id))
        logger.info(
            '[CoreTask] Stop Task %s id:%s while %s',
            self.name, self.id, self.status)
        self.status = CoreStatus.FAIL
Ejemplo n.º 8
0
 def get_ts_algorithms():
     core_api = CoreAPI()
     response = json.loads(getattr(core_api.get_info(),
                                   '_content'))['time_series']['algos']
     algos = [x['key'] for x in response]
     return algos