Skip to content

Commit

Permalink
Merge pull request #296 from noharm-ai/develop
Browse files Browse the repository at this point in the history
 v2.25-beta
  • Loading branch information
marceloarocha authored Feb 26, 2024
2 parents ac7f469 + af073f2 commit 80bc876
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 28 deletions.
2 changes: 1 addition & 1 deletion mobile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
4 changes: 4 additions & 0 deletions models/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ class PrescriptionAuditTypeEnum(Enum):
UNCHECK = 2


class PrescriptionDrugAuditTypeEnum(Enum):
PROCESSED = 1


class DrugAdminSegment(Enum):
ADULT = 5
KIDS = 7
Expand Down
20 changes: 18 additions & 2 deletions models/notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,22 @@ 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
) - timedelta(days=1)

qNotes = (
db.session.query(
func.count().label("total"),
)
.select_from(ClinicalNotes)
.filter(ClinicalNotes.admissionNumber == admissionNumber)
.filter(ClinicalNotes.isExam == None)
.filter(ClinicalNotes.date >= cutoff_date)
)

total = qNotes.scalar()
Expand Down Expand Up @@ -146,6 +153,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()
)
Expand All @@ -156,18 +164,26 @@ 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()
)

def getAllergies(admissionNumber):
def getAllergies(admissionNumber, admission_date=None):
cutoff_date = (
datetime.today() - timedelta(days=120)
if admission_date == None
else admission_date
) - timedelta(days=1)

return (
db.session.query(
ClinicalNotes.allergyText, func.max(ClinicalNotes.date).label("maxdate")
)
.select_from(ClinicalNotes)
.filter(ClinicalNotes.admissionNumber == admissionNumber)
.filter(func.length(ClinicalNotes.allergyText) > 0)
.filter(ClinicalNotes.date >= cutoff_date)
.group_by(ClinicalNotes.allergyText)
.order_by(desc("maxdate"))
.limit(50)
Expand Down
26 changes: 25 additions & 1 deletion models/prescription.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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"

Expand Down
8 changes: 6 additions & 2 deletions routes/prescription.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -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:
Expand Down
55 changes: 33 additions & 22 deletions services/prescription_agg_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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(),
},
)


Expand Down

0 comments on commit 80bc876

Please sign in to comment.