def api_solution_info_view(): try: problem_id = request.json.get('problem_id') secret = request.json.get('secret') if problem_id == "": raise MessageException('One input of testcase is empty !') if secret == "": raise MessageException('One input of testcase is empty !') judged_result_dict = db.get_problem_judge_and_result_status( problem_id, secret) return jsonify(judged_result_dict), status.HTTP_200_OK except MessageException as e: return jsonify({ 'status': 'error', 'message': str(e) }), status.HTTP_400_BAD_REQUEST except Exception as e: logger.critical( 'Unknow error happend in webclient_views.api_solution_info_view() !' ) logger.critical(traceback.format_exc()) return jsonify({ 'status': 'error', 'message': 'Unknow error happend !' }), status.HTTP_400_BAD_REQUEST
def __init__(self, tmp_path=TMP_UPLOAD_PATH, file_path=UPLOAD_PATH): self.tmp_path = tmp_path self.file_path = file_path if not os.path.isabs(self.tmp_path): raise MessageException( 'tmp_path :{} is not absolute dir in Storage.__init__()'. format(self.tmp_path)) if not os.path.isabs(self.file_path): raise MessageException( 'file_path :{} is not absolute dir in Storage.__init__()'. format(self.file_path))
def __init__(self, host='127.0.0.1', port=6379, db=0): self.host = host self.port = port self.db = db #各个状态列队在内存中的标识,用prefix:key的方式来表示 #self.file_prefix = "file:" #浏览阶段,软件初始化时将文件内容信息从目录中读入redis缓存,并根据缓存在判断文件是否存在 self.task_prefix = "task:" #上传前期,根据上传对项目key来生成对应的上传编号 self.upload_prefix = "upload:" #分片上传节点,用列表方式记录了每个分片信息,列表最后用success来表示分片是否上传完成 self.merge_prefix = "merge:" #合并节点 self.download_prefix = "download:" #下载阶段 self.file_list_key = "file_list" #文件的哈希列表,用于下载和浏览,以(filename,status)的方式进行存放 self.file_deleting_list_key = "file_deleting_list" #待删除文件的哈希列表,待后端异步任务进行删除 try: logger.debug("Connect to redis ... in Database.__init__()") self.connection_pool = redis.ConnectionPool( host=self.host, port=self.port, db=self.db, decode_responses=True) #password self.connection = redis.StrictRedis( connection_pool=self.connection_pool) except Exception as e: msg_str = "Error in conncetion redis ! in Database.__init__()" logger.error(msg_str) raise MessageException(msg_str)
def get_merging_content_size_of_key(self, key, clip_list): clip_list_len = len(clip_list) - 1 #不计算'success' 这个标记 if "success" not in clip_list: msg_str = "You are download a key:\"{}\" which is not merge successfull in FileStorage.get_merging_content_size_of_key()".format( key) logger.critical(msg_str) raise MessageException(msg_str) last_chunk_count = clip_list_len - 1 #最后一个分片的名字 number_of_5m_chunk = clip_list_len - 1 #5M分片的数量,只有最后一个分片大小不为5M last_clip_abs_path = "{0}/{1}{2}".format(self.tmp_path, key, last_chunk_count) #根据分片计算文件大小 clip_total_size = number_of_5m_chunk * 5 * 1024 * 1024 clip_total_size = clip_total_size + os.stat( str(last_clip_abs_path)).st_size ''' for item_clip in clip_list: if item_clip=='success': continue try: clip_abs_path = os.path.join(self.tmp_path, item_clip) clip_total_size = clip_total_size + os.stat(str(clip_abs_path)).st_size except IOError: continue ''' return clip_total_size
def get_prediction(data: List[float]) -> str: # Convert into nd-array try: data = np.array(data).reshape(1, -1) pred = app.model.predict(data)[0] except Exception as e: raise MessageException("Model could not be loaded.", 500, str(e)) return pred
def get_download_realname_by_key(self, key): key_with_prefix = self.download_prefix + key if self.connection.exists(key_with_prefix): result_str = self.connection.get(key_with_prefix) result_dict = json.loads(result_str) return result_dict['realname'] else: raise MessageException('the key not exist in')
def delete_downloadable_file_list_by_key(self, key): file_list_key = self.file_list_key if not self.connection.hexists(file_list_key, key): raise MessageException("file not exist") self.connection.hdel(file_list_key, key) file_deleting_list_key = self.file_deleting_list_key self.connection.hset(file_deleting_list_key, key, DownloadFileStatus.PRESENT)
def initialize_logger(): # Initialize session n_attemps = 3 while n_attemps > 0: # Initialize logger app.whylabs_logger = app.whylabs_session.logger( dataset_name=os.environ["WHYLABS_DEFAULT_DATASET_ID"], dataset_timestamp=datetime.datetime.now(datetime.timezone.utc), with_rotation_time=os.environ["ROTATION_TIME"], ) if app.whylabs_logger is not None: break else: n_attemps -= 1 if n_attemps <= 0: raise MessageException("Logger could not be initialized.", 500)
def api_solution_create_view(): try: code = request.json.get('code') lang = request.json.get('lang') test_cases = request.json.get('test_cases') notify = request.json.get('notify') if code == None: raise MessageException('Code is empty !') if lang == None: raise MessageException('Lang is empty !') if test_cases == None: raise MessageException('Test_cases is empty !') for one_case in test_cases: try: input = one_case['input'] output = one_case['output'] if input == None: raise MessageException('One input of testcase is empty !') if output == None: raise MessageException('One input of testcase is empty !') except KeyError: raise MessageException('Test case key is incorrect !') #main logic return_dict = db.add_problem(request.json) return jsonify(return_dict), status.HTTP_201_CREATED except MessageException as e: return jsonify({ 'status': 'error', 'message': str(e) }), status.HTTP_400_BAD_REQUEST except QueueFullException as e: return jsonify({ 'status': 'error', 'message': str(e) }), status.HTTP_400_BAD_REQUEST except Exception as e: logger.critical( 'Unknow error happend in webclient_views.api_solution_create_view() !' ) logger.critical(traceback.format_exc()) return jsonify({ 'status': 'error', 'message': 'Unknow error happend !' }), status.HTTP_400_BAD_REQUEST
def keyInfo(self, key): file_path = os.path.join(self.file_path, key) if not os.path.exists(file_path): raise MessageException('file {} not exist'.format(file_path)) key_info_dict = {'key': key, 'file_path': file_path} return key_info_dict