def __init__(self): """ 初始化 MySQL 连接参数 """ logger = initiate_log(__name__) try: self.config = { 'host': db_config.host, 'database': db_config.db, 'user': db_config.user, 'password': db_config.passwd, 'raise_on_warnings': False, 'charset': 'utf8', } self.cnx = connect(**self.config) except Error as err: if err.errno == errorcode.ER_ACCESS_DENIED_ERROR: err_msg = "Something is wrong with your user name or password" logger.error(err_msg) elif err.errno == errorcode.ER_BAD_DB_ERROR: err_msg = "Database does not exist" logger.error(err_msg) else: err_msg = err.msg logger.error(err_msg) raise DpException(ErrorConstants.ec_sys_error, ErrorConstants.error_code_message .get(ErrorConstants.ec_sys_error) + err_msg) finally: self.cursor = self.cnx.cursor(dictionary=True) logger.info("database connect success.")
def prepare_dcm_data(request_vo: DcmSingleRecordRequest): """ 准备配码数据:主要涉及往 case_dcm 和 case_dcm_data 表中插入数据 :param request_vo: :return: """ mysql_client = MySQLConnector() sql = """ insert into case_dcm(`cd`, `an_w`, `name_w`, `df_w`, `spec_w`, `company_w`, `note`) values ("{cd}", {an_w}, {name_w}, {df_w}, {spec_w}, {company_w}, "{note}") on duplicate key update updatetime="{updatetime}", an_w=values(`an_w`), name_w=values(`name_w`), df_w=values(`df_w`), spec_w=values(`spec_w`), company_w=values(`company_w`); """.format( cd=Constants.dcm_single_code, an_w=Constants.dcm_weight.get(Constants.dcm_w_name_an_w) if request_vo.an_s != '' else 0, name_w=Constants.dcm_weight.get(Constants.dcm_w_name_name_w) if request_vo.name_s != '' else 0, df_w=Constants.dcm_weight.get(Constants.dcm_w_name_df_w) if request_vo.df_s != '' else 0, spec_w=Constants.dcm_weight.get(Constants.dcm_w_name_spec_w) if request_vo.spec_s != '' else 0, company_w=Constants.dcm_weight.get(Constants.dcm_w_name_company_w) if request_vo.company_s != '' else 0, note='single dcm request data', updatetime=format_time()) mysql_client.manipulate_data(sql) mysql_client = MySQLConnector() sql = """ select id from case_dcm where cd = "{case_cd}"; """.format(case_cd=Constants.dcm_single_code) case_dcm = mysql_client.query_data(sql=sql) if case_dcm: case_id = case_dcm[0].get('id') else: case_id = 0 if case_id == 0: raise DpException( ErrorConstants.ec_dcm_internal_error, ErrorConstants.error_code_message.get( ErrorConstants.ec_dcm_internal_error)) mysql_client = MySQLConnector() sql = """ replace into case_dcm_data(`case_id`, `id_s`, `an_s`, `name_s`, `df_s`, `spec_s`, `company_s`) VALUES ({case_id}, {id_s}, "{an_s}", "{name_s}", "{df_s}", "{spec_s}", "{company_s}") """.format(case_id=case_id, id_s=Constants.dcm_single_id_s, an_s=request_vo.an_s, name_s=request_vo.name_s, df_s=request_vo.df_s, spec_s=request_vo.spec_s, company_s=request_vo.company_s) mysql_client.manipulate_data(sql=sql)
def connection_listener(state): if state == KazooState.SUSPENDED: # handle being disconnected from zookeeper err_msg = "disconnect from zookeeper" logger.error(err_msg) raise DpException( ErrorConstants.ec_sys_error, ErrorConstants.error_code_message.get(ErrorConstants.ec_sys_error) + err_msg)
def wrapper(request_vo: DcmSingleRecordRequest, script_name, sp_name, *args): mysql_client = MySQLConnector() sql = """ select * from case_dcm where state = "{state}" """.format(state=Constants.dcm_state_work_in_process) case_dcm = mysql_client.query_data(sql=sql) if case_dcm: raise DpException( ErrorConstants.ec_dcm_tasK_conflict, ErrorConstants.error_code_message.get( ErrorConstants.ec_dcm_tasK_conflict)) result = func(request_vo, script_name, sp_name, *args) return result
def wrapper(self, node_path_env: str, node_path_conf: str, conf_dict: dict = {}): stat = self.zk.exists(node_path_conf) if stat is None: err_msg = "%s is not exists".format(node_path_conf) logger.error(err_msg) raise DpException( ErrorConstants.ec_sys_error, ErrorConstants.error_code_message.get( ErrorConstants.ec_sys_error) + err_msg) @self.zk.ChildrenWatch(node_path_env) def watch_children(children): logger.warning("Children of %s are now: %s\n", node_path_env, children) @self.zk.DataWatch(node_path_conf) def watch_node(data, stat, event: WatchedEvent): logger.warning("Version: %s, data: %s, event is %s\n", stat.version, data.decode("utf-8"), event) data = self.zk.get(node_path_conf) conf_data = data[0].decode("utf-8") try: conf_dict = json.loads(conf_data) except: err_msg = "can't convert conf data to dict, conf_data is %s".format( conf_data) logger.error(err_msg) raise DpException( ErrorConstants.ec_sys_error, ErrorConstants.error_code_message.get( ErrorConstants.ec_sys_error) + err_msg) func(self, node_path_conf, node_path_env, conf_dict)
def get_db_config(self, node_path_env: str, node_path_conf: str, conf_dict: dict = {}): test_dict = {} test_dict["host"] = "a" test_dict.get("host") try: db_config.host = conf_dict.get("host") db_config.user = conf_dict.get("user") db_config.passwd = conf_dict.get("passwd") db_config.db = conf_dict.get("db") db_config.port = conf_dict.get("port") except: err_msg = "param of mysql config is deficiency: %s".format( conf_dict) logger.error(err_msg) raise DpException( ErrorConstants.ec_sys_error, ErrorConstants.error_code_message.get( ErrorConstants.ec_sys_error) + err_msg) else: logger.debug(db_config)
def wrapper(self, dto: DcmResDTO): """ 将标记为正确的单条配码数据插入 case_dcm_res 中 :param self: class instance :param dto: 请求参数 :return: """ ''' 查询 case_dcm_data 表中的 sme_score, 当 sme_score=0 时,不允许更新 case_dcm_res 表中的 sme_score ''' connect = MySQLConnector() sql = """ select sme_score from {table_name} where case_id = {case_id} and id_s = {id_s} """.format(table_name=TableName.tbn_case_dcm_data, case_id=dto.case_id, id_s=dto.id_s) data = connect.query_data(sql=sql) if len(data) <= 0: raise DpException() else: sme_score_dcm_data = data[0]['sme_score'] if sme_score_dcm_data == 0: raise DpException( error_code=ErrorConstants. ec_dcm_update_res_sme_score_forbidden, error_message=ErrorConstants.error_code_message.get( ErrorConstants.ec_dcm_update_res_sme_score_forbidden)) connect = MySQLConnector() sql = """ select note, sme_score from {table_name} where case_id = {case_id} and id_s = {id_s} and id_m = {id_m} """.format(table_name=TableName.tbn_case_dcm_res, case_id=dto.case_id, id_s=dto.id_s, id_m=dto.id_m) data = connect.query_data(sql=sql) if len(data) <= 0: if dto.sme_score == 0: return connect = MySQLConnector() sql = """ INSERT ignore INTO {table_name} ( `case_id`, `id_s`, `id_m`, `score`, `sub_score`, `rank`, `an_score`, `an_s`, `an_s_srch`, `an_m`, `an_m_srch`, `name_score`, `name_s`, `name_s_srch`, `name_m`, `name_m_srch`, `df_score`, `df_s`, `df_s_srch`, `df_m`, `df_m_srch`, `spec_score`, `spec_s`, `spec_s_srch`, `spec_m`, `spec_m_srch`, `company_score`, `company_s`, `company_s_srch`, `company_m`, `company_m_srch`, `src`, `srch_t`, `srch_iter`, `note`) VALUES( {case_id}, {id_s}, {id_m}, {score}, {sub_score}, {rank}, {an_score}, '{an_s}', '{an_s_srch}', '{an_m}', '{an_m_srch}', {name_score}, '{name_s}', '{name_s_srch}', '{name_m}', '{name_m_srch}', {df_score}, '{df_s}', '{df_s_srch}', '{df_m}', '{df_m_srch}', {spec_score}, '{spec_s}', '{spec_s_srch}', '{spec_m}', '{spec_m_srch}', {company_score}, '{company_s}', '{company_s_srch}', '{company_m}', '{company_m_srch}', '{src}', '{srch_t}', '{srch_iter}', '{note}' ) """.format(table_name=TableName.tbn_case_dcm_res, case_id=dto.case_id, id_s=dto.id_s, id_m=dto.id_m, score=dto.score, sub_score=dto.sub_score, rank=dto.rank, an_score=dto.an_score, an_s=dto.an_s, an_s_srch=dto.an_s_srch, an_m=dto.an_m, an_m_srch=dto.an_m_srch, name_score=dto.name_score, name_s=dto.name_s, name_s_srch=dto.name_s_srch, name_m=dto.name_m, name_m_srch=dto.name_m_srch, df_score=dto.df_score, df_s=dto.df_s, df_s_srch=dto.df_s_srch, df_m=dto.df_m, df_m_srch=dto.df_m_srch, spec_score=dto.spec_score, spec_s=dto.spec_s, spec_s_srch=dto.spec_s_srch, spec_m=dto.spec_m, spec_m_srch=dto.spec_m_srch, company_score=dto.company_score, company_s=dto.company_s, company_s_srch=dto.company_s_srch, company_m=dto.company_m, company_m_srch=dto.company_m_srch, src=dto.p_src, srch_t=dto.srch_t, srch_iter=dto.srch_iter, note=dto.p_note) connect.manipulate_data(sql=sql) else: # 异常情况: original_sme_score = data[0]['sme_score'] if dto.sme_score == 0: if original_sme_score is None or original_sme_score == 0: return else: if original_sme_score == 1: return dto.p_note = data[0]['note'] func(self, dto) # 更新 case_dcm_data 表中的数据,取 res 中 rank=1 的数据 self.update_dcm_data(dto.case_id, dto.id_s, int(dto.sme_score))