def __init__(self): if not HospitalServerInfo.is_init: self.inhospital_pat_data = dict() self.inhospital_pat_num = dict() self.discharge_pat_data = dict() self.discharge_pat_num = dict() self.ward_code_list = list() self.mongo_pull_utils = PullDataFromMDBUtils() self.logger = LogUtils().getLogger('backend') self.parameters = Properties() self.conf_dict = self.parameters.conf_dict.copy() self.dept_dict = self.parameters.dept_dict.copy() self.ward_dept = self.parameters.ward_dept.copy() self.hospital_code = self.parameters.properties.get( 'hospital_code') self.database_ip = self.parameters.properties.get('mongo_host') if cur_path.endswith('Test'): self.mr_client_url = self.conf_dict['mr_url'].get( self.database_ip, dict()).get('test', '') self.collection_name_doctor = self.mongo_pull_utils.mongodb_collection_name_doctor + '_test' # 医生端数据库 else: self.mr_client_url = self.conf_dict['mr_url'].get( self.database_ip, dict()).get('release', '') self.collection_name_doctor = self.mongo_pull_utils.mongodb_collection_name_doctor if not self.ward_code_list: self.ward_code_list = self.get_ward_code() HospitalServerInfo.is_init = True
def wrapper(*args, **kwargs): logger = LogUtils().getLogger('backend') try: return func(*args, **kwargs) except: logger.error(traceback.format_exc()) exc_type, exc_value, exc_traceback_obj = sys.exc_info() try: r = HttpResponse( json.dumps({ 'error_func': func.__name__, 'error_info': '.'.join(exc_value.args), 'error_type': exc_type.__name__, 'abnormal_info': ''.join(traceback.format_tb(exc_traceback_obj)), 'res_flag': False })) except: r = HttpResponse( json.dumps({ 'error_func': func.__name__, 'abnormal_info': ''.join(traceback.format_tb(exc_traceback_obj)), 'res_flag': False })) r.status_code = 500 return r
def runRegularScheduled(): """ 每天定时跑终末质控 """ logger = LogUtils().getLogger('scheduledTasks') try: runZhongmo() except: logger.error(traceback.format_exc()) return False
def runZhongmo(): """ 每天更新终末库数据 """ app = CheckMultiRecords(debug=True) logger = LogUtils().getLogger('scheduledTasks') parameter = Properties() try: day_delta = int(parameter.properties.get('extract_interval', '1')) + 1 # 质控比抽取间隔要多一天 today = datetime.now() yesterday = today - timedelta(day_delta) if today.day == 1: collection_ori = app.PushData.mongodb_collection_name + '_last_two_month' rename_collection = app.PushData.mongodb_collection_name + '_last_two_month_bak' collection = app.PushData.connectCollection( database_name=app.PushData.mongodb_database_name, collection_name=collection_ori) try: collection.rename(rename_collection, dropTarget=True) except: logger.warning('No collection {0}'.format(collection_ori)) collection_ori = app.PushData.mongodb_collection_name + '_last_month' rename_collection = app.PushData.mongodb_collection_name + '_last_two_month' collection = app.PushData.connectCollection( database_name=app.PushData.mongodb_database_name, collection_name=collection_ori) try: collection.rename(rename_collection, dropTarget=True) except: logger.warning('No collection {0}'.format(collection_ori)) collection_ori = app.PushData.mongodb_collection_name + '_this_month' rename_collection = app.PushData.mongodb_collection_name + '_last_month' collection = app.PushData.connectCollection( database_name=app.PushData.mongodb_database_name, collection_name=collection_ori) try: collection.rename(rename_collection, dropTarget=True) except: logger.warning('No collection {0}'.format(collection_ori)) app.time_limits = { "binganshouye.pat_visit.discharge_time": { "$gte": yesterday.strftime('%Y-%m-%d') } } app.PushData.mongodb_collection_name += '_zhongmo' app.process(True) app.process_supplement(collection_name=parameter.properties.get( 'collection_name', 'MedicalRecordQuality')) # 只存终末的三个月数据 return True except: logger.error(traceback.format_exc()) return False
def __init__(self): if not GainSynonym.is_init: self.parameters = Properties() self.logger = LogUtils().getLogger('synonym') self.log_info = """hospital_code: [{0}], version: [{1}], serverip: [{2}], request_add: [{3}], request_data: [{4}], response_text: [{5}], response_code: [{6}], error_type: [{7}], error_content: [{8}], abnormal_info: [\n{9}], take_times: [{10:.2f}]s""" self.hospital_code = self.parameters.properties.get('hospital_code') self.ver = self.parameters.properties.get('version') self.synonym_host = self.parameters.properties.get('synonym_host') self.synonym_port = self.parameters.properties.get('synonym_port') GainSynonym.is_init = True
def __init__(self): if not GainKnowledgeGraph.IS_INIT: self.parameters = Properties() self.logger = LogUtils().getLogger('es_search') self.log_info = """hospital_code: [{0}], version: [{1}], serverip: [{2}], request_add: [{3}], request_data: [{4}], response_text: [{5}], response_code: [{6}], error_type: [{7}], error_content: [{8}], abnormal_info: [\n{9}], take_times: [{10:.2f}]s""" knowledge_host = self.parameters.properties.get('knowledge_host') knowledge_port = self.parameters.properties.get('knowledge_port') self.hospital_code = self.parameters.properties.get('hospital_code') self.ver = self.parameters.properties.get('version') self.standard_add = 'http://{}:{}/med/cdss/standardFromAliasList.json'.format(knowledge_host, knowledge_port) self.relationship_add = 'http://{}:{}/med/cdss/isFatherOrSon.json'.format(knowledge_host, knowledge_port) self.medicine_add = 'http://{}:{}/med/edit/searchKnowledge.json'.format(knowledge_host, knowledge_port) GainKnowledgeGraph.IS_INIT = True
def hospital_server_info(): """ 定时请求电子病历服务器Webservice,获取在院/出院患者数据,医生患者数 """ logger = LogUtils().getLogger('scheduledTasks') try: app = HospitalServerInfo() print('start {0}'.format(app)) app.get_inhospitalpatdata() app.get_inhospitalpatnum() app.get_dischargepatdata() app.get_dischargepatnum() print('end {0}'.format(app)) except: logger.error(traceback.format_exc())
def __init__(self, debug=False): self.debug = debug # region 从properties文件中获取资源配置信息 self.parameters = Properties() self.logger = LogUtils().getLogger("MongoUtils") self.mongodb_host = self.parameters.properties.get('mongo_host') self.mongodb_port = eval(self.parameters.properties.get('mongo_port')) self.mongodb_database_name = self.parameters.properties.get('mongodb') self.mongodb_collection_name = self.parameters.properties.get('collection_name') self.mongodb_collection_name_doctor = '{}_doctor'.format(self.mongodb_collection_name) self.username_wr = self.parameters.properties.get('username_write_read') self.password_wr = self.parameters.properties.get('password_write_read') self.record_host = self.parameters.properties.get('record_host') self.record_port = eval(self.parameters.properties.get('record_port')) self.record_database = self.parameters.properties.get('record_mongodb') # endregion # 数据库的连接操作 self.client = MongoClient(host=self.mongodb_host, port=self.mongodb_port, connect=False) self.record_client = MongoClient(host=self.record_host, port=self.record_port, connect=False) self.record_db = self.record_client.get_database(name=self.record_database) if self.parameters.properties.get('record_auth') in ['true', 'True']: username = self.parameters.properties.get('record_username') password = self.parameters.properties.get('record_password') self.record_db.authenticate(username, password) self.authed = False
def __init__(self): if not GainESSearch.IS_INIT: self.parameters = Properties() self.logger = LogUtils().getLogger('es_search') self.log_info = """hospital_code: [{0}], version: [{1}], serverip: [{2}], request_add: [{3}], request_data: [{4}], response_text: [{5}], response_code: [{6}], error_type: [{7}], error_content: [{8}], abnormal_info: [\n{9}], take_times: [{10:.2f}]s""" es_host = self.parameters.properties.get('es_host') es_port = self.parameters.properties.get('es_port') self.hospital_code = self.parameters.properties.get('hospital_code') self.ver = self.parameters.properties.get('version') if es_port == '0': es_add = "{}/search/bysy".format(es_host) else: es_add = "{}:{}".format(es_host, es_port) self.id_add = 'http://{}/med/advanced/allVariableJilian.json'.format(es_add) self.record_add = 'http://{}/med/quality/getWenshuData.json'.format(es_add) self.record_list_add = 'http://{}/med/advanced/query/patients.json'.format(es_add) GainESSearch.IS_INIT = True
def processJsonFileBingan(request): logger_seg = LogUtils().getLogger('segment') app = ClientInterface() if request.method == 'POST': if request.content_type == 'text/plain': data = request.body data = data.decode('utf-8') if not data: data = '{}' json_file = json.loads(data) if os.path.abspath(os.path.dirname(__file__)).endswith('Test'): logger_seg.info(json_file) result = app.processJsonFileBingan( json_file=json_file) # 这里获取医生姓名和id return HttpResponse(json.dumps(result)) elif request.content_type == 'multipart/form-data': json_file = request.POST if os.path.abspath(os.path.dirname(__file__)).endswith('Test'): logger_seg.info(json_file) result = app.processJsonFileBingan(json_file=json_file) return HttpResponse(json.dumps(result)) else: return HttpResponse(json.dumps({}))
def removeTableMrq(): """ 每天从mrq表中删除春光入库了的数据 """ logger = LogUtils().getLogger('scheduledTasks') try: app_mongo = PushDataFromMDBUtils() db = app_mongo.connectDataBase(app_mongo.mongodb_database_name) tables = db.list_collection_names() for mrq_collection in tables: if not mrq_collection.startswith('mrq_'): continue collection_name = mrq_collection.replace('mrq_', '') conn_mrq = db.get_collection(mrq_collection) conn = db.get_collection(collection_name) conn_result = conn.find({}, {}) for data in conn_result: conn_mrq.delete_one({'_id': data['_id']}) return True except: logger.error(traceback.format_exc()) return False
def calcTotalDocument(): """ 每天定时查询近三个月病案首页文档数,记录出院患者ID及其科室 """ logger = LogUtils().getLogger('scheduledTasks') app_mongo = PushDataFromMDBUtils() app = StatisticPatientInfos() conn = app_mongo.connectCollection( database_name=app_mongo.mongodb_database_name, collection_name='MedicalRecordQuality_zhongmo') try: this_month = app.this_month() last_month = app.last_month() last_two_month = app.last_two_month() today = datetime.now() if today.day == 1: collection_ori = 'total_zhongmo_last_two_month' rename_collection = 'total_zhongmo_last_two_month_bak' collection = app_mongo.connectCollection( database_name=app_mongo.mongodb_database_name, collection_name=collection_ori) try: collection.rename(rename_collection, dropTarget=True) except: logger.warning('No collection {0}'.format(collection_ori)) collection_ori = 'total_zhongmo_last_month' rename_collection = 'total_zhongmo_last_two_month' collection = app_mongo.connectCollection( database_name=app_mongo.mongodb_database_name, collection_name=collection_ori) try: collection.rename(rename_collection, dropTarget=True) except: logger.warning('No collection {0}'.format(collection_ori)) collection_ori = 'total_zhongmo_this_month' rename_collection = 'total_zhongmo_last_month' collection = app_mongo.connectCollection( database_name=app_mongo.mongodb_database_name, collection_name=collection_ori) try: collection.rename(rename_collection, dropTarget=True) except: logger.warning('No collection {0}'.format(collection_ori)) mongo_result = conn.find( { 'pat_info.discharge_time': { '$gt': this_month[0], '$lt': this_month[1] } }, {'pat_info.dept_discharge_from_name': 1}) for data in mongo_result: push_data = {'_id': data['_id']} if not data.get('pat_info', dict()).get('dept_discharge_from_name'): continue push_data['dept'] = data['pat_info']['dept_discharge_from_name'] app_mongo.pushData('total_zhongmo_this_month', push_data) mongo_result = conn.find( { 'pat_info.discharge_time': { '$gt': last_month[0], '$lt': last_month[1] } }, {'pat_info.dept_discharge_from_name': 1}) for data in mongo_result: push_data = {'_id': data['_id']} if not data.get('pat_info', dict()).get('dept_discharge_from_name'): continue push_data['dept'] = data['pat_info']['dept_discharge_from_name'] app_mongo.pushData('total_zhongmo_last_month', push_data) mongo_result = conn.find( { 'pat_info.discharge_time': { '$gt': last_two_month[0], '$lt': last_two_month[1] } }, {'pat_info.dept_discharge_from_name': 1}) for data in mongo_result: push_data = {'_id': data['_id']} if not data.get('pat_info', dict()).get('dept_discharge_from_name'): continue push_data['dept'] = data['pat_info']['dept_discharge_from_name'] app_mongo.pushData('total_zhongmo_last_two_month', push_data) return True except: logger.error(traceback.format_exc()) return False
import logging from TestCases.FlashTestCases import FlashTestRun from Utils.LogUtils import LogUtils from Config.TestConfig import devices """ def main(): print('\n') test1 = Test() result = test1.testing('com254') for re in result: print(re) print('-------------------------------------------------------') """ if __name__ == '__main__': logging.debug('test running') ''' test = Test() while True: result = test.testing_write('COM195' , 'ff') time.sleep(2) ''' # main() test = FlashTestRun() test.test_EMMC_pull_and_push(devices, 2) LogUtils.com_log('com195') # log.logcat_and_dmesg(dev ='62b72966', logon =True)
def processJsonFile(request): logger_seg = LogUtils().getLogger('segment') logger = LogUtils().getLogger('backend') app = ClientInterface() if request.method == 'POST': if request.content_type == 'text/plain': data = request.body data = data.decode('utf-8') if not data: data = '{}' json_file = json.loads(data) if not json_file.get('wenshuxinxi', dict()).get('file_unique_id'): logger.error("[{}#{}] no [file_unique_id]".format( json_file.get('binganshouye', dict()).get('patient_id', ''), json_file.get('binganshouye', dict()).get('visit_id', ''))) if os.path.abspath(os.path.dirname(__file__)).endswith('Test'): logger_seg.info(json_file) result = app.processJsonFile(json_file=json_file) # 这里获取医生姓名和id return HttpResponse(json.dumps(result)) elif request.content_type == 'multipart/form-data': json_file = request.POST if not json_file.get('wenshuxinxi', dict()).get('file_unique_id'): logger.error("[{}#{}] no [file_unique_id]".format( json_file.get('binganshouye', dict()).get('patient_id', ''), json_file.get('binganshouye', dict()).get('visit_id', ''))) if os.path.abspath(os.path.dirname(__file__)).endswith('Test'): logger_seg.info(json_file) result = app.processJsonFile(json_file=json_file) return HttpResponse(json.dumps(result)) else: return HttpResponse(json.dumps({}))
#!/usr/bin/env python3 # -*- coding:utf-8 -* import os import sys cur_path = os.path.abspath(os.path.dirname(__file__)) root_path = os.path.split(cur_path)[0] sys.path.append(root_path) # 项目路径添加到系统路径 import traceback from MedicalQuality.mainProgram import CheckMultiRecords from Utils.LogUtils import LogUtils logger = LogUtils().getLogger("record_info") logger_info = LogUtils().getLogger("run_info") logger_error = LogUtils().getLogger("root") class CheckSegmentWord(CheckMultiRecords): def check_chief_content(self, time_limits): try: mongo_result = self.gain_info.collection_bingan.aggregate( [{ "$match": time_limits }, { "$lookup": { "from": 'ruyuanjilu', "localField": "_id", "foreignField": "_id", "as": 'ruyuanjilu' } }, {
class GainESSearch(object): IS_INIT = False def __init__(self): if not GainESSearch.IS_INIT: self.parameters = Properties() self.logger = LogUtils().getLogger('es_search') self.log_info = """hospital_code: [{0}], version: [{1}], serverip: [{2}], request_add: [{3}], request_data: [{4}], response_text: [{5}], response_code: [{6}], error_type: [{7}], error_content: [{8}], abnormal_info: [\n{9}], take_times: [{10:.2f}]s""" es_host = self.parameters.properties.get('es_host') es_port = self.parameters.properties.get('es_port') self.hospital_code = self.parameters.properties.get('hospital_code') self.ver = self.parameters.properties.get('version') if es_port == '0': es_add = "{}/search/bysy".format(es_host) else: es_add = "{}:{}".format(es_host, es_port) self.id_add = 'http://{}/med/advanced/allVariableJilian.json'.format(es_add) self.record_add = 'http://{}/med/quality/getWenshuData.json'.format(es_add) self.record_list_add = 'http://{}/med/advanced/query/patients.json'.format(es_add) GainESSearch.IS_INIT = True def __new__(cls, *args, **kwargs): """ 单例模式 """ if not hasattr(cls, 'instance'): cls.instance = super(GainESSearch, cls).__new__(cls) return cls.instance def _requestMethod(self, url, data, start_time, input_info): res = {'res_flag': False} r = requests.post(url, data=data, timeout=5) if r.status_code != 200: r = requests.post(url, data=data, timeout=5) try: result = r.json() time_cost = r.elapsed.total_seconds() except: HOST_NAME = socket.gethostname() HOST_IP = socket.gethostbyname(HOST_NAME) exc_type, exc_value, exc_traceback_obj = sys.exc_info() # sys.exc_info(),返回有关由except捕获的最近异常的信息 abnormal_info = ''.join(traceback.format_tb(exc_traceback_obj)) info = self.log_info.format(self.hospital_code, self.ver, HOST_IP, url, data, r.text, r.status_code, exc_type.__name__, exc_value, abnormal_info, time.time()-start_time) self.logger.error(info) res['error_source'] = 'es' res['input_data'] = input_info res['request_add'] = url res['response_status'] = r.status_code res['response_text'] = r.text res['error_type'] = exc_type.__name__ res['error_info'] = '.'.join(exc_value.args) res['abnormal_info'] = ''.join(traceback.format_tb(exc_traceback_obj)) return res res['res_flag'] = True res['response_time'] = time_cost res.update(result) return res def getId(self, expression): start_time = time.time() res = dict() res['result'] = set() res['res_flag'] = False data = { 'expressions': expression, "page": "0", "size": "10", "result": [[{"exp": "等于", "field": "住院病案首页_就诊信息_就诊次数", "flag": "1", "unit": "", "values": []}]] } r = requests.post(self.id_add, data=json.dumps(data)) if r.status_code == 200: result = json.loads(r.text) time_cost = r.elapsed.total_seconds() else: r = requests.post(self.id_add, data=json.dumps(data)) try: result = json.loads(r.text) time_cost = r.elapsed.total_seconds() except: HOST_NAME = socket.gethostname() HOST_IP = socket.gethostbyname(HOST_NAME) exc_type, exc_value, exc_traceback_obj = sys.exc_info() abnormal_info = ''.join(traceback.format_tb(exc_traceback_obj)) info = self.log_info.format(self.hospital_code, self.ver, HOST_IP, self.id_add, data, r.text, r.status_code, exc_type.__name__, exc_value, abnormal_info, time.time()-start_time) self.logger.error(info) res['error_source'] = 'es' res['expression'] = expression res['response_status'] = r.status_code res['response_text'] = r.text res['error_type'] = exc_type.__name__ res['error_info'] = '.'.join(exc_value.args) res['abnormal_info'] = ''.join(traceback.format_tb(exc_traceback_obj)) return res if 'result' in result: res['res_flag'] = True res['response_time'] = time_cost if isinstance(result.get('result'), list): for one_batch in result['result']: keys = set(one_batch.keys()) res['result'].update(keys) if 'Count' in result: count = int(result['Count']) if count != len(res['result']): res['count'] = len(res['result']) self.logger.warning('\nCount: {}\nlength: {}'.format(count, len(res), )) else: res['count'] = count else: res['error_info'] = 'No "result" in result...' if not res['res_flag']: res.update(result) res['error_source'] = 'es' res['expression'] = expression HOST_NAME = socket.gethostname() HOST_IP = socket.gethostbyname(HOST_NAME) info = self.log_info.format(self.hospital_code, self.ver, HOST_IP, self.record_add, data, r.text, r.status_code, 'res_flag is False', res, 'getId', time.time()-start_time) self.logger.warning(info) return res def getRecord(self, record_id, record_name, is_src=False): start_time = time.time() res = dict() res['res_flag'] = False data = { 'esId': record_id, 'wenshuName': record_name, 'isSrc': is_src } r = requests.post(self.record_add, data=data) if r.status_code == 200: result = json.loads(r.text) time_cost = r.elapsed.total_seconds() else: r = requests.post(self.record_add, data=data) try: result = json.loads(r.text) time_cost = r.elapsed.total_seconds() except: HOST_NAME = socket.gethostname() HOST_IP = socket.gethostbyname(HOST_NAME) exc_type, exc_value, exc_traceback_obj = sys.exc_info() abnormal_info = ''.join(traceback.format_tb(exc_traceback_obj)) info = self.log_info.format(self.hospital_code, self.ver, HOST_IP, self.record_add, data, r.text, r.status_code, exc_type.__name__, exc_value, abnormal_info, time.time()-start_time) self.logger.error(info) res['error_source'] = 'es' res['record_id'] = record_id res['record_name'] = record_name res['response_status'] = r.status_code res['response_text'] = r.text res['error_type'] = exc_type.__name__ res['error_info'] = '.'.join(exc_value.args) res['abnormal_info'] = ''.join(traceback.format_tb(exc_traceback_obj)) return res if result.get(record_id): res = json.loads(result.get(record_id)) res['res_flag'] = True res['response_time'] = time_cost if not res['res_flag']: res.update(result) res['error_source'] = 'es' res['record_id'] = record_id res['record_name'] = record_name HOST_NAME = socket.gethostname() HOST_IP = socket.gethostbyname(HOST_NAME) info = self.log_info.format(self.hospital_code, self.ver, HOST_IP, self.record_add, data, r.text, r.status_code, 'res_flag is False', res, 'getRecord', time.time()-start_time) self.logger.warning(info) return res def getRecordQuickly(self, patient_id, visit_id, record_name, record_type='2', is_src=False): """ 快速获取文书,type: 1表示门诊,2表示住院 """ record_id = '{}##{}#{}#{}'.format(self.hospital_code, record_type, patient_id, visit_id) return self.getRecord(record_id, record_name, is_src) def getEsIdByDate(self, start_date='', end_date=''): """ 按患者出院起止时间, 获取患者es查询ID """ result = dict() result['result'] = list() expression = list() if start_date and not end_date: # 有起始日期,没有结束日期 expression = [[{"field": "住院病案首页_就诊信息_出院时间", "exp": ">=", "flag": "or", "unit": "", "values": [start_date]}]] if end_date and not start_date: # 有结束日期,没有起始日期 expression = [[{"field": "住院病案首页_就诊信息_出院时间", "exp": "<", "flag": "or", "unit": "", "values": [end_date]}]] if start_date and end_date: # 有起始日期,也有结束日期 expression = [[{"field": "住院病案首页_就诊信息_出院时间", "exp": ">=", "flag": "or", "unit": "", "values": [start_date]}], [{"field": "住院病案首页_就诊信息_出院时间", "exp": "<", "flag": "or", "unit": "", "values": [end_date]}]] if not expression: return {'res_flag': False, 'info': 'No "start_date" or "end_date" in input para...'} es_result = self.getId(expression) if not es_result.get('res_flag'): return es_result result['res_flag'] = True result['result'] = list(es_result['result']) result['count'] = len(result['result']) return result def getPatientRecordList(self, patient_id, visit_id, record_type='2'): start_time = time.time() record_id = '{}##{}#{}#{}'.format(self.hospital_code, record_type, patient_id, visit_id) data = { 'expressions': [[{"field": "fieldId", "exp": "=", "flag": "or", "unit": "", "values": [record_id]}]], "page": "0", "size": "10", "resultField": ["文档列表_文档名"] } para = json.dumps(data) result = self._requestMethod(url=self.record_list_add, data=para, start_time=start_time, input_info=para) if not result.get('total'): result['res_flag'] = False return result if isinstance(result.get('hits'), list) and len(result.get('hits')) > 0: tmp = result['hits'][0] if isinstance(tmp, list) and len(tmp) > 0: res_tmp = tmp[0] if isinstance(res_tmp, dict) and isinstance(res_tmp.get('文档列表_文档名'), str): res_list = res_tmp.get('文档列表_文档名').split(',') result['result'] = res_list result.pop('hits') return result result['res_flag'] = False return result
class GainSynonym(object): is_init = False def __init__(self): if not GainSynonym.is_init: self.parameters = Properties() self.logger = LogUtils().getLogger('synonym') self.log_info = """hospital_code: [{0}], version: [{1}], serverip: [{2}], request_add: [{3}], request_data: [{4}], response_text: [{5}], response_code: [{6}], error_type: [{7}], error_content: [{8}], abnormal_info: [\n{9}], take_times: [{10:.2f}]s""" self.hospital_code = self.parameters.properties.get('hospital_code') self.ver = self.parameters.properties.get('version') self.synonym_host = self.parameters.properties.get('synonym_host') self.synonym_port = self.parameters.properties.get('synonym_port') GainSynonym.is_init = True def __new__(cls, *args, **kwargs): """ 单例模式 """ if not hasattr(cls, 'instance'): cls.instance = super(GainSynonym, cls).__new__(cls) return cls.instance def get_same_word(self, word): """ 获取同义词 """ start_time = time.time() res = dict() if not (self.synonym_host and self.synonym_port): res = {'res_flag': False, 'error_source': 'synonym', 'error_info': 'No synonym_host or synonym_port...'} return res data = {'word': word} add = 'http://{}:{}/med/cdss/sameWord.json'.format(self.synonym_host, self.synonym_port) r = requests.post(add, data=data) if r.status_code == 200: result = json.loads(r.text) time_cost = r.elapsed.total_seconds() res['res_flag'] = True else: r = requests.post(add, data=data) try: result = json.loads(r.text) time_cost = r.elapsed.total_seconds() res['res_flag'] = True except: HOST_NAME = socket.gethostname() HOST_IP = socket.gethostbyname(HOST_NAME) exc_type, exc_value, exc_traceback_obj = sys.exc_info() abnormal_info = ''.join(traceback.format_tb(exc_traceback_obj)) info = self.log_info.format(self.hospital_code, self.ver, HOST_IP, add, data, r.text, r.status_code, exc_type.__name__, exc_value, abnormal_info, time.time()-start_time) self.logger.error(info) res['res_flag'] = False res['error_source'] = 'synonym' res['response_status'] = r.status_code res['response_text'] = r.text res['error_type'] = exc_type.__name__ res['error_info'] = '.'.join(exc_value.args) res['abnormal_info'] = ''.join(traceback.format_tb(exc_traceback_obj)) return res if res.get('res_flag'): res['response_time'] = time_cost res['result'] = list(set(result)) return res def get_child_disease(self, word): """ 获取子疾病 """ result = list() if not (self.synonym_host and self.synonym_port): self.logger.error('No synonym_host or synonym_port...') return result data = {'word': word} add = 'http://{}:{}/med/cdss/getAllChildDisease.json'.format(self.synonym_host, self.synonym_port) r = requests.post(add, data=data) if r.status_code == 200: result = json.loads(r.text) else: r = requests.post(add, data=data) try: result = json.loads(r.text) except: info = '\nvalue: {}\nstatus_code: {}'.format(data, r.status_code) self.logger.error(info) return result.get(word, list())
class HospitalServerInfo(object): # 是否初始化 is_init = False def __init__(self): if not HospitalServerInfo.is_init: self.inhospital_pat_data = dict() self.inhospital_pat_num = dict() self.discharge_pat_data = dict() self.discharge_pat_num = dict() self.ward_code_list = list() self.mongo_pull_utils = PullDataFromMDBUtils() self.logger = LogUtils().getLogger('backend') self.parameters = Properties() self.conf_dict = self.parameters.conf_dict.copy() self.dept_dict = self.parameters.dept_dict.copy() self.ward_dept = self.parameters.ward_dept.copy() self.hospital_code = self.parameters.properties.get( 'hospital_code') self.database_ip = self.parameters.properties.get('mongo_host') if cur_path.endswith('Test'): self.mr_client_url = self.conf_dict['mr_url'].get( self.database_ip, dict()).get('test', '') self.collection_name_doctor = self.mongo_pull_utils.mongodb_collection_name_doctor + '_test' # 医生端数据库 else: self.mr_client_url = self.conf_dict['mr_url'].get( self.database_ip, dict()).get('release', '') self.collection_name_doctor = self.mongo_pull_utils.mongodb_collection_name_doctor if not self.ward_code_list: self.ward_code_list = self.get_ward_code() HospitalServerInfo.is_init = True def __new__(cls, *args, **kwargs): """ 单例模式 """ if not hasattr(cls, 'instance'): cls.instance = super(HospitalServerInfo, cls).__new__(cls) return cls.instance def get_ward_code(self): result = list() for k, v in self.dept_dict.items(): if v in self.ward_dept: result.append(k) return result def get_inhospitalpatdata(self): """ 获取所有病区在院患者信息 """ web_service = Client(self.mr_client_url) for ward_code in self.ward_code_list: mr_data = web_service.service.EmrInHospitalData(ward_code) self.inhospital_pat_data[ward_code] = json.loads(mr_data) return self.inhospital_pat_data def get_dischargepatdata(self): """ 获取所有病区出院患者信息,并更新doctor库的质控信息 """ conn = self.mongo_pull_utils.connection( collection_name=self.collection_name_doctor) web_service = Client(self.mr_client_url) for ward_code in self.ward_code_list: mr_data = web_service.service.EmrOutHospitalData(ward_code) self.discharge_pat_data[ward_code] = json.loads(mr_data) patient_info = self.discharge_pat_data[ward_code].get( 'DISCHARGE_PATIENT_INFO', list()) for data in patient_info: # 通过电子病历服务器数据更新mongo中的医生端质控数据 key = data.get('PATIENT_ID', '') + '#' + data.get( 'VISIT_ID', '') + '#' + self.hospital_code mongo_data = conn.find_one({'_id': key}, {'pat_info': 1}) if not mongo_data: # 没有质控数据则跳过 continue if data.get('ADMISSION_DATE_TIME') and data.get( 'ADMISSION_DATE_TIME') != mongo_data.get( 'pat_info', dict()).get('admission_time'): conn.update({'_id': key}, { '$set': { 'pat_info.admission_time': data.get('ADMISSION_DATE_TIME') } }, upsert=True) # 更新入院时间 if data.get('DISCHARGE_DATE_TIME') and data.get( 'DISCHARGE_DATE_TIME') != mongo_data.get( 'pat_info', dict()).get('discharge_time'): conn.update({'_id': key}, { '$set': { 'pat_info.discharge_time': data.get('DISCHARGE_DATE_TIME') } }, upsert=True) # 更新出院时间 if data.get('DEPT_ADMISSION_TO_NAME') and data.get( 'DEPT_ADMISSION_TO_NAME') != mongo_data.get( 'pat_info', dict()).get('district_admission_to_name' ): # dept 和 district 反了 ward = data.get('DEPT_ADMISSION_TO_NAME') conn.update({'_id': key}, { '$set': { 'pat_info.district_admission_to_name': ward } }, upsert=True) # 更新入院病区 conn.update({'_id': key}, { '$set': { 'pat_info.dept_admission_to_name': self.ward_dept.get(ward) } }, upsert=True) # 更新入院科室 if data.get('DEPT_DISCHARGE_FROM_NAME') and data.get( 'DEPT_DISCHARGE_FROM_NAME') != mongo_data.get( 'pat_info', dict()).get('dept_discharge_from_name'): ward = data.get('DEPT_DISCHARGE_FROM_NAME') conn.update({'_id': key}, { '$set': { 'pat_info.district_discharge_from_name': ward } }, upsert=True) # 更新出院病区 conn.update({'_id': key}, { '$set': { 'pat_info.dept_discharge_from_name': self.ward_dept.get(ward) } }, upsert=True) # 更新出院科室 return self.discharge_pat_data def get_inhospitalpatnum(self): """ 获取所有病区医生在院病人数 """ web_service = Client(self.mr_client_url) mr_data = web_service.service.EmrInHospitalPatNum() self.inhospital_pat_num = json.loads(mr_data) return self.inhospital_pat_num def get_dischargepatnum(self): """ 获取所有病区医生出院病人数 """ web_service = Client(self.mr_client_url) mr_data = web_service.service.EmrOutHospitalPatNum() self.discharge_pat_num = json.loads(mr_data) return self.discharge_pat_num def get_artificial_data(self, patient_id='', visit_id=''): """ 根据patient_id和visit_id获取该患者病历文书人工质控结果,并存入终末质控 """ web_service = Client(self.mr_client_url) mr_data = web_service.service.EmrArtificialQCData(patient_id, visit_id) result = json.loads(mr_data) if not result: return result try: one_record = result.get('DISCHARGE_PATIENT_INFO', list())[0] if not isinstance(one_record, dict): return {} except: self.logger.error(traceback.format_exc()) return result conn = self.mongo_pull_utils.connection() key = patient_id + '#' + visit_id + '#' + self.hospital_code if not conn.find_one({'_id': key}, {'_id': 1}): # 规则质控中无该文书质控信息 push_data = {'_id': key} push_data['pat_info'] = dict() push_data['pat_info']['patient_id'] = patient_id push_data['pat_info']['visit_id'] = visit_id push_data['pat_info']['machine_score'] = 0 push_data['pat_info']['person_name'] = one_record.get('姓名', '') push_data['pat_info'][ 'district_admission_to_name'] = one_record.get('入院病区', '') push_data['pat_info'][ 'district_discharge_from_name'] = one_record.get('出院病区', '') push_data['pat_info'][ 'dept_admission_to_name'] = self.ward_dept.get( one_record.get('入院病区', ''), '') push_data['pat_info'][ 'dept_discharge_from_name'] = self.ward_dept.get( one_record.get('出院病区', ''), '') push_data['pat_info']['admission_time'] = one_record.get( '入院时间', '') push_data['pat_info']['discharge_time'] = one_record.get( '出院时间', '') push_data['pat_info']['html'] = list() push_data['pat_value'] = list() push_data['del_value'] = list() push_data['content'] = list() else: push_data = conn.find_one({'_id': key}) if push_data.get('pat_info', dict( )).get('district_admission_to_name') != one_record.get('入院病区', ''): push_data['pat_info'][ 'district_admission_to_name'] = one_record.get('入院病区', '') push_data['pat_info'][ 'dept_admission_to_name'] = self.ward_dept.get( one_record.get('入院病区', ''), '') if push_data.get('pat_info', dict()).get( 'district_discharge_from_name') != one_record.get( '出院病区', ''): push_data['pat_info'][ 'district_discharge_from_name'] = one_record.get( '出院病区', '') push_data['pat_info'][ 'dept_discharge_from_name'] = self.ward_dept.get( one_record.get('出院病区', ''), '') if push_data.get('pat_info', dict()).get('admission_time') != one_record.get( '入院时间', ''): push_data['pat_info']['admission_time'] = one_record.get( '入院时间', '') if push_data.get('pat_info', dict()).get('discharge_time') != one_record.get( '出院时间', ''): push_data['pat_info']['discharge_time'] = one_record.get( '出院时间', '') push_data['arti_control_date'] = one_record.get('质控日期', '') # 人工质控日期 push_data['record_quality_doctor'] = one_record.get('质控医生姓名', '') push_data['status'] = True push_data['artificial_value'] = result.get('DISCHARGE_PATIENT_INFO', list()) save_status = conn.update({'_id': key}, {"$set": push_data}, upsert=True) if save_status and isinstance( save_status, dict) and 'updatedExisting' in save_status: return result else: self.logger.error('update id {0} in {2} failed\n\t{1}'.format( key, save_status, self.mongo_pull_utils.mongodb_collection_name)) return {} def get_auto_data(self, patient_id='', visit_id=''): """ 根据patient_id和visit_id获取该患者病历文书自动质控结果,并存入终末质控 """ web_service = Client(self.mr_client_url) mr_data = web_service.service.EmrAutoQCData(patient_id, visit_id) result = json.loads(mr_data) if not result: return result try: one_record = result.get('AUTO_QC_DATA', list())[0] if not isinstance(one_record, dict): return {} except: self.logger.error(traceback.format_exc()) return result conn = self.mongo_pull_utils.connection() key = patient_id + '#' + visit_id + '#' + self.hospital_code if not conn.find_one({'_id': key}, {'_id': 1}): # 规则质控中无该文书质控信息 push_data = {'_id': key} push_data['pat_info'] = dict() push_data['pat_info']['patient_id'] = patient_id push_data['pat_info']['visit_id'] = visit_id push_data['pat_info']['machine_score'] = 0 push_data['pat_info']['person_name'] = one_record.get('NAME', '') push_data['pat_info']['html'] = list() push_data['pat_value'] = list() push_data['del_value'] = list() push_data['content'] = list() else: push_data = conn.find_one({'_id': key}) push_data['status'] = True push_data['auto_value'] = result.get('AUTO_QC_DATA', list()) save_status = conn.update({'_id': key}, {"$set": push_data}, upsert=True) if save_status and isinstance( save_status, dict) and 'updatedExisting' in save_status: return result else: self.logger.error('update id {0} in {2} failed\n\t{1}'.format( key, save_status, self.mongo_pull_utils.mongodb_collection_name)) return {} def get_finalQC_id(self): """ 定时请求增量数据表,获取增量数据id """ yesterday = datetime.now() - timedelta(days=1) suffix = yesterday.strftime("%Y%m%d") collection_name = 'hzlb_listlsb' + suffix conn = self.mongo_pull_utils.connection( collection_name=collection_name) query_result = conn.find({}, {'_id': 1}) result = set() for data in query_result: if len(data.get('_id', '').split('#')) < 3: continue pivid = data.get('_id', '').split('#')[:2] result.add((pivid[0], pivid[1])) return result def calc_doctor_stat(self): """ 定时计算医生端上月统计数据 """ conn = self.mongo_pull_utils.connection( collection_name=self.collection_name_doctor) # 医生端数据库
class GainKnowledgeGraph(object): IS_INIT = False def __init__(self): if not GainKnowledgeGraph.IS_INIT: self.parameters = Properties() self.logger = LogUtils().getLogger('es_search') self.log_info = """hospital_code: [{0}], version: [{1}], serverip: [{2}], request_add: [{3}], request_data: [{4}], response_text: [{5}], response_code: [{6}], error_type: [{7}], error_content: [{8}], abnormal_info: [\n{9}], take_times: [{10:.2f}]s""" knowledge_host = self.parameters.properties.get('knowledge_host') knowledge_port = self.parameters.properties.get('knowledge_port') self.hospital_code = self.parameters.properties.get('hospital_code') self.ver = self.parameters.properties.get('version') self.standard_add = 'http://{}:{}/med/cdss/standardFromAliasList.json'.format(knowledge_host, knowledge_port) self.relationship_add = 'http://{}:{}/med/cdss/isFatherOrSon.json'.format(knowledge_host, knowledge_port) self.medicine_add = 'http://{}:{}/med/edit/searchKnowledge.json'.format(knowledge_host, knowledge_port) GainKnowledgeGraph.IS_INIT = True def __new__(cls, *args, **kwargs): """ 单例模式 """ if not hasattr(cls, 'instance'): cls.instance = super(GainKnowledgeGraph, cls).__new__(cls) return cls.instance def _requestMethod(self, url, data, start_time, input_info): res = {'res_flag': False} r = requests.post(url, data=data, timeout=5) if r.status_code != 200: r = requests.post(url, data=data, timeout=5) try: result = r.json() time_cost = r.elapsed.total_seconds() except: HOST_NAME = socket.gethostname() HOST_IP = socket.gethostbyname(HOST_NAME) exc_type, exc_value, exc_traceback_obj = sys.exc_info() abnormal_info = ''.join(traceback.format_tb(exc_traceback_obj)) info = self.log_info.format(self.hospital_code, self.ver, HOST_IP, url, data, r.text, r.status_code, exc_type.__name__, exc_value, abnormal_info, time.time()-start_time) self.logger.error(info) res['error_source'] = 'knowledge_graph' res['input_data'] = input_info res['request_add'] = url res['response_status'] = r.status_code res['response_text'] = r.text res['error_type'] = exc_type.__name__ res['error_info'] = '.'.join(exc_value.args) res['abnormal_info'] = ''.join(traceback.format_tb(exc_traceback_obj)) return res res['res_flag'] = True res['response_time'] = time_cost res.update(result) return res def getStandardName(self, *args): """ 获取标准名称 """ start_time = time.time() word_list = list(args) data = {'word': json.dumps(word_list)} result = self._requestMethod(url=self.standard_add, data=data, start_time=start_time, input_info=word_list) return result def getRelationship(self, word1, word2, word_type=''): """ 两个词是否具有上下有关系 :param word1: 词1 :param word2: 词2 :param word_type: 词类型 :return: "result" 为true 或者 false """ start_time = time.time() result = dict() if not word_type: type_list = ['disease', 'medicine', 'lab', 'lab_sub', 'exam', 'operation', 'symptom_sign'] else: type_list = [word_type] for i in type_list: data = {'onto1': word1, 'onto2': word2, 'type': i} result = self._requestMethod(url=self.relationship_add, data=data, start_time=start_time, input_info=data) if result.get('res_flag'): if result.get('result') is True: return result result['res_flag'] = False return result def getMedicineCategory(self, medicine_name): """ 获取药品的分类 :param medicine_name: :return: """ start_time = time.time() data = {'wiki': {'$or': {'medicine_obj.name': medicine_name, 'medicine_obj.alias': medicine_name}}, 'result': {'wiki': ['medicine_obj.category', 'medicine_obj.name']}} data = json.dumps(data) result = self._requestMethod(url=self.medicine_add, data=data, start_time=start_time, input_info=medicine_name) if result.get('res_flag'): if result.get('msg') == 'success': result_list = list(result.get('result', dict()).get('hit', dict()).values()) res_list = list() for i in result_list: category_list = i.get('medicine_obj.category', list()) res_list.extend(category_list) res = dict() res['res_flag'] = True res['response_time'] = result.get('response_time', '') res['result'] = res_list return res result['res_flag'] = False return result def getTargetCategory(self, input_category, target_category, start_time=''): """ 输入类别是否是目标类别的子类 :param input_category: :param target_category: :param start_time: :return: """ if not start_time: start_time = time.time() for category in input_category: res = dict() res['res_flag'] = False data = {'wiki': {'medicine_category_obj.name': category}, 'result': {'wiki': ['medicine_category_obj.attribution', 'medicine_category_obj.name']}} data = json.dumps(data) result = self._requestMethod(url=self.medicine_add, data=data, start_time=start_time, input_info=category) if result.get('res_flag'): res_list = list() if result.get('msg') == 'success': result_list = list(result.get('result', dict()).get('hit', dict()).values()) for i in result_list: category_list = i.get('medicine_category_obj.attribution', list()) res_list.extend(category_list) if set(category_list) & set(target_category): res['res_flag'] = True res['response_time'] = result.get('response_time', '') return res if 'root' in res_list or not res_list: return res return self.getTargetCategory(res_list, target_category, start_time) return {'res_flag': False} def getSameContent(self, key_content, value_content): """ 找出是否有同义词或具有上下级关系的词,但未找出所有相有关系的词 :param key_content: 单个词 或者 由词组成的可迭代对象 :param value_content: 单个词 或者 由词组成的可迭代对象 """ if isinstance(key_content, str): key_standard = self.getStandardName(key_content) else: key_content = set(key_content) key_standard = self.getStandardName(*key_content) if not key_standard.get('res_flag'): return {} if isinstance(value_content, str): value_standard = self.getStandardName(value_content) else: value_content = set(value_content) value_standard = self.getStandardName(*value_content) if not value_standard.get('res_flag'): return {} key_dict = dict([(k, v) for k, v in key_standard.items() if isinstance(v, str)]) value_dict = dict([(k, v) for k, v in value_standard.items() if isinstance(v, str)]) if not (key_dict and value_dict): return {} key_set = set(key_dict.values()) value_set = set(value_dict.values()) same_word = key_set & value_set synonym_dict = dict() result = dict() for i in same_word: synonym_dict[i] = i for key_k, key_v in key_dict.items(): if key_k in result: continue for value_k, value_v in value_dict.items(): if synonym_dict.get(key_v) == value_v: result[key_k] = value_k break relation_res = self.getRelationship(key_v, value_v) if relation_res.get('res_flag'): result[key_k] = value_k break return result def medicineIsTargetCategory(self, medicine, target_category): """ 药品是否属于目标类别 """ # 获取药物标准名 if isinstance(medicine, str): medicine_standard = self.getStandardName(medicine) else: medicine = set(medicine) medicine_standard = self.getStandardName(*medicine) if not medicine_standard.get('res_flag'): return {} medicine_dict = dict([(k, v) for k, v in medicine_standard.items() if isinstance(v, str)]) if not medicine_dict: return {} medicine_set = set(medicine_dict.values()) input_category = set() for i in medicine_set: # 获取标准名药物的类别 category_result = self.getMedicineCategory(i) if category_result.get('res_flag') is True: input_category.update(category_result.get('result', list())) return self.getTargetCategory(input_category, target_category)