Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alerts perf fixes #326

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions services/alert_service.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime
from sqlalchemy import or_, and_
from sqlalchemy import text

from models.prescription import PrescriptionDrug, Allergy
from models.main import db, Drug, Relation, Substance
Expand Down Expand Up @@ -101,28 +101,33 @@ def find_relations(drug_list, id_patient: int, is_cpoe: bool):
if len(overlap_drugs) == 0:
return {"alerts": {}, "list": {}, "stats": {}}

uniq_overlap_drugs = []
uniq_overlap_keys = []
for d in overlap_drugs:
key = f"""{d["from"]["sctid"]}-{d["to"]["sctid"]}"""
key = f"""({d["from"]["sctid"]},{d["to"]["sctid"]})"""
if key not in uniq_overlap_keys:
uniq_overlap_drugs.append(d)
uniq_overlap_keys.append(key)

query = db.session.query(Relation).filter(Relation.active == True)
query = query.filter(
or_(
and_(
Relation.sctida == i["from"]["sctid"],
Relation.sctidb == i["to"]["sctid"],
)
for i in uniq_overlap_drugs
query = text(
f"""
with cruzamento as (
select * from (values {",".join(uniq_overlap_keys)}) AS t (sctida, sctidb)
)
select
r.sctida,
r.sctidb,
r.tprelacao as "kind",
r.texto as "text"
from
public.relacao r
inner join cruzamento c on (r.sctida = c.sctida and r.sctidb = c.sctidb)
where
r.ativo = true
"""
)

active_relations = {}

for item in query.all():
for item in db.session.execute(query).all():
key = f"{item.sctida}-{item.sctidb}-{item.kind}"
active_relations[key] = {
"sctida": item.sctida,
Expand Down
1 change: 0 additions & 1 deletion services/intervention_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,6 @@ def _get_price_kit(id_prescription, prescription_drug: PrescriptionDrug, user: U
PrescriptionDrug.cpoe_group == group,
)
)
.filter(PrescriptionDrug.suspendedDate == None)
.all()
)

Expand Down
Loading