From 271c41238d5fdee2c8f52896a50f8f875ef527e5 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Fri, 23 Feb 2024 18:51:15 -0300 Subject: [PATCH 1/5] Update notes.py --- models/notes.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/models/notes.py b/models/notes.py index f16ebbc8..bbcffd12 100644 --- a/models/notes.py +++ b/models/notes.py @@ -103,6 +103,7 @@ def getTotalIfExists(admissionNumber): .select_from(ClinicalNotes) .filter(ClinicalNotes.admissionNumber == admissionNumber) .filter(ClinicalNotes.isExam == None) + .filter(ClinicalNotes.date > (datetime.today() - timedelta(days=60))) ) total = qNotes.scalar() @@ -146,6 +147,7 @@ def getSigns(admissionNumber): .select_from(ClinicalNotes) .filter(ClinicalNotes.admissionNumber == admissionNumber) .filter(func.length(ClinicalNotes.signsText) > 0) + .filter(ClinicalNotes.date > (datetime.today() - timedelta(days=60))) .order_by(desc(ClinicalNotes.date)) .first() ) @@ -156,6 +158,7 @@ def getInfo(admissionNumber): .select_from(ClinicalNotes) .filter(ClinicalNotes.admissionNumber == admissionNumber) .filter(func.length(ClinicalNotes.infoText) > 0) + .filter(ClinicalNotes.date > (datetime.today() - timedelta(days=60))) .order_by(desc(ClinicalNotes.date)) .first() ) @@ -168,6 +171,7 @@ def getAllergies(admissionNumber): .select_from(ClinicalNotes) .filter(ClinicalNotes.admissionNumber == admissionNumber) .filter(func.length(ClinicalNotes.allergyText) > 0) + .filter(ClinicalNotes.date > (datetime.today() - timedelta(days=60))) .group_by(ClinicalNotes.allergyText) .order_by(desc("maxdate")) .limit(50) From 2138e0a3352fa094711083770076d8c8e211f114 Mon Sep 17 00:00:00 2001 From: Marcelo Arocha Date: Sat, 24 Feb 2024 09:57:24 -0300 Subject: [PATCH 2/5] clinical notes: cutoff date --- models/notes.py | 20 ++++++++++++++++---- routes/prescription.py | 8 ++++++-- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/models/notes.py b/models/notes.py index bbcffd12..1cde7a70 100644 --- a/models/notes.py +++ b/models/notes.py @@ -94,8 +94,14 @@ def getCountIfExists(admissionNumber, pmc=False): else: return empty_return - def getTotalIfExists(admissionNumber): + def getTotalIfExists(admissionNumber, admission_date=None): if ClinicalNotes.exists(): + cutoff_date = ( + datetime.today() - timedelta(days=120) + if admission_date == None + else admission_date + ) + qNotes = ( db.session.query( func.count().label("total"), @@ -103,7 +109,7 @@ def getTotalIfExists(admissionNumber): .select_from(ClinicalNotes) .filter(ClinicalNotes.admissionNumber == admissionNumber) .filter(ClinicalNotes.isExam == None) - .filter(ClinicalNotes.date > (datetime.today() - timedelta(days=60))) + .filter(ClinicalNotes.date >= cutoff_date) ) total = qNotes.scalar() @@ -163,7 +169,13 @@ def getInfo(admissionNumber): .first() ) - def getAllergies(admissionNumber): + def getAllergies(admissionNumber, admission_date=None): + cutoff_date = ( + datetime.today() - timedelta(days=120) + if admission_date == None + else admission_date + ) + return ( db.session.query( ClinicalNotes.allergyText, func.max(ClinicalNotes.date).label("maxdate") @@ -171,7 +183,7 @@ def getAllergies(admissionNumber): .select_from(ClinicalNotes) .filter(ClinicalNotes.admissionNumber == admissionNumber) .filter(func.length(ClinicalNotes.allergyText) > 0) - .filter(ClinicalNotes.date > (datetime.today() - timedelta(days=60))) + .filter(ClinicalNotes.date >= cutoff_date) .group_by(ClinicalNotes.allergyText) .order_by(desc("maxdate")) .limit(50) diff --git a/routes/prescription.py b/routes/prescription.py index 79a64c19..8c766ce9 100644 --- a/routes/prescription.py +++ b/routes/prescription.py @@ -358,7 +358,9 @@ def getPrescription( clinicalNotesCount = ClinicalNotes.getCountIfExists( prescription[0].admissionNumber, is_pmc ) - notesTotal = ClinicalNotes.getTotalIfExists(prescription[0].admissionNumber) + notesTotal = ClinicalNotes.getTotalIfExists( + prescription[0].admissionNumber, admission_date=patient.admissionDate + ) notesSigns = None notesInfo = None notesAllergies = [] @@ -368,7 +370,9 @@ def getPrescription( notesSigns = ClinicalNotes.getSigns(prescription[0].admissionNumber) notesInfo = ClinicalNotes.getInfo(prescription[0].admissionNumber) - allergies = ClinicalNotes.getAllergies(prescription[0].admissionNumber) + allergies = ClinicalNotes.getAllergies( + prescription[0].admissionNumber, admission_date=patient.admissionDate + ) dialysis = ClinicalNotes.getDialysis(prescription[0].admissionNumber) for a in allergies: From df1628e44119cf031dd70aabb255765f7418c1ea Mon Sep 17 00:00:00 2001 From: Marcelo Arocha Date: Sat, 24 Feb 2024 09:58:12 -0300 Subject: [PATCH 3/5] v2.25-beta --- mobile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mobile.py b/mobile.py index 3aad01ee..bb878362 100644 --- a/mobile.py +++ b/mobile.py @@ -107,7 +107,7 @@ @app.route("/version", methods=["GET"]) def getVersion(): - return {"status": "success", "data": "v2.24-beta"}, status.HTTP_200_OK + return {"status": "success", "data": "v2.25-beta"}, status.HTTP_200_OK if __name__ == "__main__": From 5e040614e3f95c4bd775cd754e9cfe326da206e5 Mon Sep 17 00:00:00 2001 From: Marcelo Arocha Date: Mon, 26 Feb 2024 08:17:59 -0300 Subject: [PATCH 4/5] fix notes cutoff date --- models/notes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/models/notes.py b/models/notes.py index 1cde7a70..73a0322e 100644 --- a/models/notes.py +++ b/models/notes.py @@ -100,7 +100,7 @@ def getTotalIfExists(admissionNumber, admission_date=None): datetime.today() - timedelta(days=120) if admission_date == None else admission_date - ) + ) - timedelta(days=1) qNotes = ( db.session.query( @@ -174,7 +174,7 @@ def getAllergies(admissionNumber, admission_date=None): datetime.today() - timedelta(days=120) if admission_date == None else admission_date - ) + ) - timedelta(days=1) return ( db.session.query( From af073f24e60b8205a1d78bd242a96cbb537dfcd8 Mon Sep 17 00:00:00 2001 From: Marcelo Arocha Date: Mon, 26 Feb 2024 11:00:41 -0300 Subject: [PATCH 5/5] add prescriptionDrug audit --- models/enums.py | 4 ++ models/prescription.py | 26 ++++++++++++- services/prescription_agg_service.py | 55 +++++++++++++++++----------- 3 files changed, 62 insertions(+), 23 deletions(-) diff --git a/models/enums.py b/models/enums.py index 173b4b7a..c12f3b21 100644 --- a/models/enums.py +++ b/models/enums.py @@ -61,6 +61,10 @@ class PrescriptionAuditTypeEnum(Enum): UNCHECK = 2 +class PrescriptionDrugAuditTypeEnum(Enum): + PROCESSED = 1 + + class DrugAdminSegment(Enum): ADULT = 5 KIDS = 7 diff --git a/models/prescription.py b/models/prescription.py index 3a608c20..b0a15a8b 100644 --- a/models/prescription.py +++ b/models/prescription.py @@ -164,6 +164,20 @@ def getHeaders(admissionNumber, aggDate, idSegment, is_pmc=False, is_cpoe=False) if not is_cpoe: q = q.filter(Prescription.idSegment == idSegment) + else: + # discard all suspended + active_count = ( + db.session.query(func.count().label("count")) + .filter(PrescriptionDrug.idPrescription == Prescription.id) + .filter( + or_( + PrescriptionDrug.suspendedDate == None, + func.date(PrescriptionDrug.suspendedDate) >= aggDate, + ) + ) + .as_scalar() + ) + q = q.filter(active_count > 0) prescriptions = q.all() @@ -887,7 +901,6 @@ class PrescriptionDrug(db.Model): suspendedDate = db.Column("dtsuspensao", db.DateTime, nullable=True) checked = db.Column("checado", db.Boolean, nullable=True) period = db.Column("periodo", db.Integer, nullable=True) - processedDate = db.Column("dtprocessada", db.DateTime, nullable=True) update = db.Column("update_at", db.DateTime, nullable=True) user = db.Column("update_by", db.Integer, nullable=True) @@ -1033,6 +1046,17 @@ def findByPrescriptionDrug(idPrescriptionDrug, future, is_cpoe=False): ) +class PrescriptionDrugAudit(db.Model): + __tablename__ = "presmed_audit" + + id = db.Column("idpresmed_audit", db.Integer, nullable=False, primary_key=True) + auditType = db.Column("tp_audit", db.Integer, nullable=False) + idPrescriptionDrug = db.Column("fkpresmed", db.Integer, nullable=False) + extra = db.Column("extra", postgresql.JSON, nullable=True) + createdAt = db.Column("created_at", db.DateTime, nullable=False) + createdBy = db.Column("created_by", db.Integer, nullable=False) + + class Intervention(db.Model): __tablename__ = "intervencao" diff --git a/services/prescription_agg_service.py b/services/prescription_agg_service.py index c87b46c1..84cb428e 100644 --- a/services/prescription_agg_service.py +++ b/services/prescription_agg_service.py @@ -5,6 +5,7 @@ from models.main import db from models.appendix import * from models.prescription import * +from models.enums import PrescriptionDrugAuditTypeEnum from routes.prescription import getPrescription from routes.utils import getFeatures from services import prescription_drug_service, prescription_service @@ -94,7 +95,7 @@ def create_agg_prescription_by_prescription( if newPrescAgg: db.session.add(pAgg) - _set_processed_by_prescription(id_prescription=id_prescription) + _log_processed_date(id_prescription_array=[id_prescription], schema=schema) def create_agg_prescription_by_date(schema, admission_number, p_date, is_cpoe): @@ -151,31 +152,41 @@ def create_agg_prescription_by_date(schema, admission_number, p_date, is_cpoe): ) ) - if create_new: - db.session.add(agg_p) - - _set_processed_by_agg(agg_prescription=agg_p, is_cpoe=is_cpoe) + internal_prescription_ids = [] + for h in resultAgg["data"]["headers"]: + internal_prescription_ids.append(h) + _log_processed_date( + id_prescription_array=internal_prescription_ids, schema=schema + ) -def _set_processed_by_agg(agg_prescription: Prescription, is_cpoe): - prescription_query = prescription_service.get_query_prescriptions_by_agg( - agg_prescription=agg_prescription, is_cpoe=is_cpoe, only_id=True - ) - - db.session.query(PrescriptionDrug).filter( - PrescriptionDrug.idPrescription.in_(prescription_query) - ).update( - {"processedDate": datetime.today()}, - synchronize_session=False, - ) + if create_new: + db.session.add(agg_p) -def _set_processed_by_prescription(id_prescription): - db.session.query(PrescriptionDrug).filter( - PrescriptionDrug.idPrescription == id_prescription - ).update( - {"processedDate": datetime.today()}, - synchronize_session=False, +def _log_processed_date(id_prescription_array, schema): + query = f""" + insert into {schema}.presmed_audit ( + tp_audit, fkpresmed, created_at, created_by + ) + select + :auditType, + fkpresmed, + :createdAt, + 0 + from + {schema}.presmed + where + fkprescricao = any(:prescriptionArray) + """ + + db.session.execute( + query, + { + "auditType": PrescriptionDrugAuditTypeEnum.PROCESSED.value, + "prescriptionArray": id_prescription_array, + "createdAt": datetime.today(), + }, )