def _get_correct_examinations(): examination_list = Examination.objects(status_name='completed', data_inserimento=datetime(2016, 2, 2, 0, 0)) patient_id = [e.id_patient.id for e in examination_list] counter = Counter(patient_id) items_ = counter.elements() c = items_.next() while counter[c] <= 2: c = items_.next() return Examination.objects(id_patient=c, data_inserimento=datetime(2016, 2, 2, 0, 0), status_name='completed')
def read(self, **query): if 'data_inserimento' in query.keys(): if isinstance(query['data_inserimento'], unicode): query['data_inserimento'] = date_from_json(query['data_inserimento']) if 'id_patient' in query.keys(): query['id_patient'] = self._get_patient(str(query['id_patient'])) if 'id_examination' in query.keys(): query['id'] = query.pop('id_examination') if query != {}: logger.debug(query) examination = Examination.objects(**query) if examination.count() == 0: message = Message(ExaminationNoRecordHeader(), data=examination) return message message = Message(ExaminationCorrectHeader(), data=examination) return message
def _check_examination(self, list_id_examinations): if len(list_id_examinations) == 0: message = "No examination" return Message(ReportErrorHeader(message=message), data=None) examination_list = Examination.objects(id__in=list_id_examinations) patient_list = [] data_examination = [] status_list = [] for e in examination_list: patient_list.append(e.id_patient.id) data_examination.append(e.data_inserimento) status_list.append(e.status_name) if len(np.unique(patient_list)) > 1: message = "Examinations are from different patient" logger.debug(np.unique(patient_list)) return Message(ReportErrorHeader(message=message), data=None) if len(np.unique(data_examination)) > 1: message = "Examinations are from different date" logger.debug(np.unique(data_examination)) return Message(ReportErrorHeader(message=message), data=None) if 'closed' in status_list: message = "Examinations are already reported" logger.debug(np.unique(status_list)) return Message(ReportErrorHeader(message=message), data=None) return examination_list
def _get_examination(self, id_examination): """Deprecated""" examination = Examination.objects(id=str(id_examination)) if examination == None: logger.error("No examination in the database") return 'ERR' self._currentExamination = examination.first() return examination
def run(database_config, n_loaded=50): db_config = parse_db_config(database_config) _ = easyris_connect(**db_config) Report.drop_collection() user = User.objects(username='******').first() examination_list = Examination.objects(status_name='completed', data_inserimento=datetime.datetime(year=2016, month=02, day=02)) # Trovo i pazienti con piu esami id_patient_list = [e.id_patient.id_patient for e in examination_list] counter = collections.Counter(id_patient_list) if len(counter) < n_loaded: n_loaded = len(counter) / 2 print n_loaded for i in range(n_loaded): id_patient = counter.keys()[i] patient = Patient.objects(id_patient=id_patient).first() patient_examination = Examination.objects(id_patient=patient.id, status_name='completed', data_inserimento=datetime.datetime(year=2016, month=02, day=02)) dates_ = np.unique([e.data_inserimento for e in patient_examination]) for d_ in dates_: data_ = datetime.datetime(d_.year, d_.month, d_.day) same_date_examination = Examination.objects(id_patient=patient.id, data_inserimento=data_, status_name='completed') report_log = ReportAction( user=user, action='create' ) report = Report( id_examination = [e for e in same_date_examination], report_text = "Il paziente bla bla bla", action_list = [report_log], id_patient = id_patient, status=SuspendedReportStatus(), status_name='suspended' ) report.save() for e in same_date_examination: e.modify(status=ClosedExaminationStatus(), status_name='closed') e.save()
def create(self, **query): pt_controller = PatientController() pt_message = pt_controller.read(id_patient=query['id_patient']) if pt_message.header.code == 101: return pt_message query['id_patient'] = pt_message.data[0] logger.debug(pt_message.data[0].first_name) if 'data_inserimento' in query.keys(): query['data_inserimento'] = parse_date(query['data_inserimento']) logger.debug(query['data_inserimento']) query['id_creator'] = User.objects(username=query['id_creator']).first() list_examination = query.pop('exams') if len(list_examination) == 0: message = Message(ExaminationErrorHeader(message='No Examination in Request')) logger.error(message.header.message) return message examination = None for i, exam_ in enumerate(list_examination): typology = Typology.objects(examination_name=exam_['nome']).first() priority = Priority.objects(priority_name=exam_['priority']).first() query = self._check_fields(query, id_priority=priority, id_typology=typology, ) if not isinstance(query, dict): # It is a message! return query qs = Examination.objects(**query) logger.debug(query) logger.debug("No. of examinations: "+str(len(qs))) if len(qs) != 0: continue examination = Examination(**query) pacs_data = _build_pacs_data(examination) send_to_pacs.apply_async(link_error=pacs_error_handler.s(), kwargs={'data':pacs_data}, ) #send_to_pacs(pacs_data) try: examination.save() except (FieldDoesNotExist, NotUniqueError, SaveConditionError) as err: txt_message = "On %s examination: %s" % (str(i+1), err.message) logger.error(txt_message) message = Message(ExaminationErrorHeader(message=txt_message)) return message logger.debug("Examination created: "+str(examination.data_inserimento)) examination = self._get_examination(examination.id) examination.status = NewExaminationStatus(examination) if examination == None: message = "Examination already stored" examination = qs else: message = 'Examination created correctly' message = Message(ExaminationCorrectHeader(message=message), data=examination) self._currentExamination = examination return message