def post_log(self, logs, modified_at=None): """Post logs If fail to post the ``logs``, the data is cached and try to post them next time with new one. Args: logs (list): stats data to post. modified_at (datetime): last modified time. """ if modified_at is None: modified_at = make_timestamp() self.cached_logs.extend(logs) log_req = { 'log': { 'values': self.cached_logs, 'modifiedAt': modified_at, 'reset': self.first_reset, } } log_res, msg = urlopen('POST', self.logs_url, data=log_req) if log_res is None: print('post log error, URL: {}, Message: {}'.format( self.logs_url, msg)) return if log_res['logs']['totalLogCount'] >= 0: self.first_reset = False self.cached_logs = []
def post_args(self, conditions): arg_req = {'argument': conditions} res, msg = urlopen('POST', self.args_url, data=arg_req) if res is None: print('post args error, URL: {}, Message: {}'.format( self.args_url, msg)) return False return True
def wait_for_running(self): wait_count = 0 wait_limit = 30 # wail until 3 second while wait_count <= wait_limit: res, msg = urlopen('GET', self.url + 'test/check', content_type='plain/text', return_type='text') if msg == '': break time.sleep(0.1) wait_count += 1 return wait_count <= wait_limit
def setup_project(self, project_name=None): """Setup project on ChainerUI server Project path on default is working directory. First, check the project path is registered or not, and if not found, register the project path. Second, if ``project_name`` is set and changed, update the project name. Args: project_name (str): If set, update the project name. If set ``None`` (on default), try to get environment variable, ``'CHAINERUI_PROJECT_NAME'`` if set. Returns: bool: When succeed to setup, return ``True`` """ project_path = self.project_path if project_name is None: project_name = os.getenv('CHAINERUI_PROJECT_NAME', None) # check the path exists or not check_url = '{}?path_name={}'.format(self.projects_url, project_path) project_res, msg = urlopen('GET', check_url) # if project is not found, create a project if project_res is None: if (project_path + '\' is not found') not in msg: print('connection error, URL: {}, Message: {}'.format( check_url, msg)) return False # if message includes pathname, means not found error # continue to process to register this project project_req = { 'project': { 'path_name': project_path, 'name': project_name, 'crawlable': self.crawlable, }, } project_res, msg = urlopen('POST', self.projects_url, data=project_req) if project_res is None: print('register error, URL: {}, Message: {}'.format( self.projects_url, msg)) return False project_id = project_res['project']['id'] if project_name is not None and\ project_res['project']['name'] != project_name: # update project name project_req = {'project': {'name': project_name}} project_res, msg = urlopen('PUT', self.project_url, data=project_req) if project_res is None: print('update error, URL: {}, Message: {}'.format( self.project_url, msg)) # fail to update project name, but the target project has been # registered, so not return False self.project_id = project_id return True
def register_result(self, result_name=None, overwrite_result=False): """Register result on ChaienrUI server Basically result path is same as project path, but to be unique key, add start time on the result path. The result path is virtual one, not make physical directory. If ``overwrite_result`` set ``True``, the client tool not add start time and continue to use same result record. Args: result_name (str): If set, update the result name. If set ``None`` (on default), try to get environment variable, ``'CHAINERUI_RESULT_NAME'`` if set. overwrite_result (bool): If set ``True``, not set start time on the result path and overwrite data on the result. Returns: bool: When succeed to setup, return ``True`` """ now = datetime.datetime.now() result_path = self.project_path if result_name is None: result_name = os.getenv('CHAINERUI_RESULT_NAME', None) if overwrite_result: check_url = '{}?path_name={}'.format(self.results_url, result_path) result_res, msg = urlopen('GET', check_url) if result_res is None: if (result_path + '\' is not found') not in msg: print('connection error, URL: {}, Message: {}'.format( check_url, msg)) return False else: # result is found, overwrite on the result record self.result_id = result_res['result']['id'] self.first_reset = True if result_name is not None and\ result_res['result']['name'] != result_name: # update result name result_req = {'result': {'name': result_name}} result_res, msg = urlopen('PUT', self.result_url, data=result_req) if result_res is None: print('update error, URL: {}, Message: {}'.format( self.result_url, msg)) # fail to update result name, but the target result # has been found, so not return False return True else: # result path is required unique key, make dummy path but not # make the physical directory. result_path += '-' + format_datetime(now) if result_name is not None: result_name += '-' + format_datetime(now) result_req = { 'result': { 'pathName': result_path, 'name': result_name, 'crawlable': False, 'logModifiedAt': make_timestamp(now), } } result_res, msg = urlopen('POST', self.results_url, data=result_req) if result_res is None: print('register error, URL: {}, Message: {}'.format( self.results_url, msg)) return False self.result_id = result_res['result']['id'] return True
def shutdown(self): urlopen('GET', self.url + 'test/shutdown', content_type='plain/text', return_type='text') self.join()