Skip to content

Commit

Permalink
Merge pull request #327 from noharm-ai/develop
Browse files Browse the repository at this point in the history
v3.10-beta
  • Loading branch information
marceloarocha authored Jun 21, 2024
2 parents 33b4231 + cab0b1d commit a8439d2
Show file tree
Hide file tree
Showing 10 changed files with 258 additions and 331 deletions.
2 changes: 1 addition & 1 deletion mobile.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@

@app.route("/version", methods=["GET"])
def getVersion():
return {"status": "success", "data": "v3.09-beta"}, status.HTTP_200_OK
return {"status": "success", "data": "v3.10-beta"}, status.HTTP_200_OK


@app.route("/exc", methods=["GET"])
Expand Down
1 change: 1 addition & 0 deletions models/appendix.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ class NifiQueue(db.Model):
url = db.Column("url", db.String, nullable=False)
method = db.Column("method", db.String, nullable=False)
body = db.Column("body", postgresql.JSONB, nullable=True)
extra = db.Column("extra", postgresql.JSONB, nullable=True)
runStatus = db.Column("run_status", db.Boolean, nullable=False)
responseCode = db.Column("response_code", db.Integer, nullable=True)
response = db.Column("response", postgresql.JSONB, nullable=True)
Expand Down
10 changes: 10 additions & 0 deletions models/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,13 @@ class UserAuditTypeEnum(Enum):
FORGOT_PASSWORD = 4
UPDATE_PASSWORD = 5
REMOVE_CLINICAL_NOTE_ANNOTATION = 6


class NifiQueueActionTypeEnum(Enum):
SET_STATE = "SET_STATE"
REFRESH_STATE = "REFRESH_STATE"
CLEAR_QUEUE = "CLEAR_QUEUE"
LIST_QUEUE = "LIST_QUEUE"
CLEAR_STATE = "CLEAR_STATE"
TERMINATE_PROCESS = "TERMINATE_PROCESS"
CUSTOM_CALLBACK = "CUSTOM_CALLBACK"
296 changes: 9 additions & 287 deletions models/prescription.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,293 +205,6 @@ def getHeaders(admissionNumber, aggDate, idSegment, is_pmc=False, is_cpoe=False)

return headers

def findRelation(
idPrescription,
admissionNumber,
idPatient,
aggDate=None,
is_cpoe=False,
is_pmc=False,
):
pd1 = db.aliased(PrescriptionDrug)
pd2 = db.aliased(PrescriptionDrug)
m1 = db.aliased(Drug)
m2 = db.aliased(Drug)
p1 = db.aliased(Prescription)
p2 = db.aliased(Prescription)

if aggDate is None:
relation = db.session.query(
pd1.id,
Relation,
m1.name,
m2.name,
pd1.update,
pd1.intravenous,
pd2.intravenous,
DrugAttributes.whiteList,
)
relation = (
relation.join(
pd2,
and_(pd2.idPrescription == pd1.idPrescription, pd2.id != pd1.id),
)
.join(m1, m1.id == pd1.idDrug)
.join(m2, m2.id == pd2.idDrug)
.outerjoin(
DrugAttributes,
and_(
DrugAttributes.idDrug == pd1.idDrug,
DrugAttributes.idSegment == pd1.idSegment,
),
)
.join(
Relation,
and_(Relation.sctida == m1.sctid, Relation.sctidb == m2.sctid),
)
.filter(pd1.idPrescription == idPrescription)
)
else:
if is_cpoe:
daterange = partial(func.tsrange, type_=TSRANGE)
relation = (
db.session.query(
pd1.id,
Relation,
m1.name,
m2.name,
p1.expire,
pd1.intravenous,
pd2.intravenous,
DrugAttributes.whiteList,
)
.select_from(p1)
.join(p2, p2.admissionNumber == admissionNumber)
.join(pd1, pd1.idPrescription == p1.id)
.join(
pd2,
and_(pd2.idPrescription == p2.id, pd2.id != pd1.id),
)
.join(m1, m1.id == pd1.idDrug)
.join(m2, m2.id == pd2.idDrug)
.join(
Relation,
and_(Relation.sctida == m1.sctid, Relation.sctidb == m2.sctid),
)
.outerjoin(
DrugAttributes,
and_(
DrugAttributes.idDrug == pd1.idDrug,
DrugAttributes.idSegment == pd1.idSegment,
),
)
.filter(p1.admissionNumber == admissionNumber)
.filter(p1.idSegment != None)
.filter(p1.concilia == None)
.filter(p2.concilia == None)
.filter(
daterange(p1.date, p1.expire, "[]").overlaps(
daterange(p2.date, p2.expire, "[]")
)
)
)

else:
relation = (
db.session.query(
pd1.id,
Relation,
m1.name,
m2.name,
p1.expire,
pd1.intravenous,
pd2.intravenous,
DrugAttributes.whiteList,
)
.select_from(p1)
.join(p2, p2.admissionNumber == admissionNumber)
.join(pd1, pd1.idPrescription == p1.id)
.join(pd2, and_(pd2.idPrescription == p2.id, pd2.id != pd1.id))
.join(m1, m1.id == pd1.idDrug)
.join(m2, m2.id == pd2.idDrug)
.join(
Relation,
or_(
and_(
Relation.sctida == m1.sctid, Relation.sctidb == m2.sctid
),
and_(
Relation.sctidb == m1.sctid, Relation.sctida == m2.sctid
),
),
)
.outerjoin(
DrugAttributes,
and_(
DrugAttributes.idDrug == pd1.idDrug,
DrugAttributes.idSegment == pd1.idSegment,
),
)
.filter(p1.admissionNumber == admissionNumber)
.filter(func.date(p2.expire) == func.date(p1.expire))
.filter(p1.idSegment != None)
)

relation = get_period_filter(relation, p1, aggDate, is_pmc, is_cpoe)
relation = get_period_filter(relation, p2, aggDate, is_pmc, is_cpoe)

if is_pmc:
p_sub = db.aliased(Prescription)
max_date_query = (
db.session.query(
func.date(
func.coalesce(func.max(p_sub.date), func.now())
- func.cast("1 MONTH", INTERVAL)
)
)
.select_from(p_sub)
.filter(p_sub.admissionNumber == admissionNumber)
.subquery()
)

relation = relation.filter(func.date(p1.date) > max_date_query).filter(
func.date(p2.date) > max_date_query
)

relation = (
relation.filter(Relation.active == True)
.filter(pd1.suspendedDate == None)
.filter(pd2.suspendedDate == None)
)

if is_cpoe:
interaction = relation.filter(
or_(
Relation.kind.in_(["it", "dt", "dm", "iy"]),
and_(Relation.kind == "sl", pd1.cpoe_group == pd2.cpoe_group),
)
)
else:
interaction = relation.filter(
or_(
Relation.kind.in_(["it", "dt", "dm", "iy"]),
(
and_(
Relation.kind == "sl",
pd1.solutionGroup == pd2.solutionGroup,
pd1.idPrescription == pd2.idPrescription,
)
),
)
)

q_allergy = (
db.session.query(Allergy.idDrug.label("idDrug"))
.select_from(Allergy)
.filter(Allergy.idPatient == idPatient)
.filter(Allergy.active == True)
.subquery()
)

al = db.aliased(q_allergy)

xreactivity = (
db.session.query(
pd1.id,
Relation,
m1.name,
m2.name,
pd1.update,
pd1.intravenous,
pd1.intravenous.label("intravenous2"),
DrugAttributes.whiteList,
)
.join(al, al.c.idDrug != pd1.idDrug)
.join(m1, m1.id == pd1.idDrug)
.join(m2, m2.id == al.c.idDrug)
.join(
Relation,
or_(
and_(Relation.sctida == m1.sctid, Relation.sctidb == m2.sctid),
and_(Relation.sctida == m2.sctid, Relation.sctidb == m1.sctid),
),
)
.outerjoin(
DrugAttributes,
and_(
DrugAttributes.idDrug == pd1.idDrug,
DrugAttributes.idSegment == pd1.idSegment,
),
)
.filter(Relation.active == True)
.filter(pd1.suspendedDate == None)
.filter(Relation.kind.in_(["rx"]))
)

if aggDate is None:
xreactivity = xreactivity.filter(pd1.idPrescription == idPrescription)
else:
xreactivity = (
xreactivity.join(Prescription, Prescription.id == pd1.idPrescription)
.filter(Prescription.admissionNumber == admissionNumber)
.filter(
between(
func.date(aggDate),
func.date(Prescription.date),
func.coalesce(
func.date(Prescription.expire), func.date(aggDate)
),
)
)
.filter(Prescription.idSegment != None)
)

if is_cpoe:
relations = interaction.union(xreactivity).all()
else:
relations = (
interaction.union(xreactivity)
.order_by(
desc(Relation.kind), desc(DrugAttributes.whiteList), desc(pd1.id)
)
.all()
)

results = {}
pairs = []
for r in relations:
if aggDate is None:
key = str(r[1].sctida) + "-" + str(r[1].sctidb) + str(r[1].kind)
else:
key = (
str(r[1].sctida)
+ "-"
+ str(r[1].sctidb)
+ str(r[4].day if r[4] else 0)
+ str(r[1].kind)
)

if key in pairs:
continue

if r[1].kind == "iy" and (r[5] != True or r[6] != True):
continue

alert = typeRelations[r[1].kind] + ": "
alert += (
strNone(r[1].text) + " (" + strNone(r[2]) + " e " + strNone(r[3]) + ")"
)

if r[0] in results:
if alert not in results[r[0]]:
results[r[0]].append(alert)
else:
results[r[0]] = [alert]

pairs.append(key)

return results

def checkPrescriptions(
admissionNumber, aggDate, idSegment, userId, is_cpoe, is_pmc
):
Expand Down Expand Up @@ -985,6 +698,7 @@ def findByPrescription(
Substance,
period_cpoe.label("period_cpoe"),
Prescription.date,
MeasureUnitConvert.factor.label("measure_unit_convert_factor"),
)
.outerjoin(Outlier, Outlier.id == PrescriptionDrug.idOutlier)
.outerjoin(Drug, Drug.id == PrescriptionDrug.idDrug)
Expand All @@ -997,6 +711,14 @@ def findByPrescription(
MeasureUnit.idHospital == Prescription.idHospital,
),
)
.outerjoin(
MeasureUnitConvert,
and_(
MeasureUnitConvert.idSegment == PrescriptionDrug.idSegment,
MeasureUnitConvert.idDrug == PrescriptionDrug.idDrug,
MeasureUnitConvert.idMeasureUnit == MeasureUnit.id,
),
)
.outerjoin(
Frequency,
and_(
Expand Down
43 changes: 43 additions & 0 deletions routes/admin/integration_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,46 @@ def set_state():
return {"status": "error", "message": str(e), "code": e.code}, e.httpStatus

return tryCommit(db, result)


@app_admin_integration_remote.route(
"/admin/integration-remote/queue-status", methods=["GET"]
)
@jwt_required()
def queue_status():
user = User.find(get_jwt_identity())
dbSession.setSchema(user.schema)
os.environ["TZ"] = "America/Sao_Paulo"

try:
result = integration_remote_service.get_queue_status(
id_queue_list=request.args.getlist("idQueueList[]"),
user=user,
)
except ValidationError as e:
return {"status": "error", "message": str(e), "code": e.code}, e.httpStatus

return tryCommit(db, result)


@app_admin_integration_remote.route(
"/admin/integration-remote/push-queue-request", methods=["POST"]
)
@jwt_required()
def push_queue_request():
user = User.find(get_jwt_identity())
dbSession.setSchema(user.schema)
os.environ["TZ"] = "America/Sao_Paulo"
request_data = request.get_json()

try:
result = integration_remote_service.push_queue_request(
id_processor=request_data.get("idProcessor", None),
action_type=request_data.get("actionType", None),
data=request_data,
user=user,
)
except ValidationError as e:
return {"status": "error", "message": str(e), "code": e.code}, e.httpStatus

return tryCommit(db, result)
Loading

0 comments on commit a8439d2

Please sign in to comment.