From 4af30b1994c83cb3cae55c0e0beb863d7b5fdb06 Mon Sep 17 00:00:00 2001 From: Marcelo Arocha Date: Tue, 5 Nov 2024 21:48:36 -0300 Subject: [PATCH 1/7] add global score cache --- utils/prescriptionutils.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/utils/prescriptionutils.py b/utils/prescriptionutils.py index 569f1b70..85d5707c 100644 --- a/utils/prescriptionutils.py +++ b/utils/prescriptionutils.py @@ -107,7 +107,6 @@ def getFeatures(result, agg_date: datetime = None, intervals_for_agg_date=False) drug_attributes[attr] = 0 for d in drugList: - drugIDs.append(d["idDrug"]) if d["idSubstance"] != None: substanceIDs.append(d["idSubstance"]) @@ -190,6 +189,8 @@ def getFeatures(result, agg_date: datetime = None, intervals_for_agg_date=False) if "high" in alert_levels: alert_level = "high" + global_score = pScore + av + am + exams + alerts + diff + return { "alergy": allergy, "allergy": allergy, @@ -220,6 +221,7 @@ def getFeatures(result, agg_date: datetime = None, intervals_for_agg_date=False) "drugAttributes": drug_attributes, "intervals": intervals, "departmentList": list(department_list), + "globalScore": global_score, } From 608623b72382f2d7a05826d8e53e206b0d75693a Mon Sep 17 00:00:00 2001 From: Marcelo Arocha Date: Wed, 6 Nov 2024 08:22:40 -0300 Subject: [PATCH 2/7] add global score cache --- services/prescription_view_service.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/services/prescription_view_service.py b/services/prescription_view_service.py index 7d263486..ffd2962d 100644 --- a/services/prescription_view_service.py +++ b/services/prescription_view_service.py @@ -198,7 +198,7 @@ def _build_headers( "solution": [], "procedures": [], "interventions": drugsInterv, - "alertExams": [], + "alertExams": 0, "complication": 0, } ) @@ -208,7 +208,7 @@ def _build_headers( "solution": solutions, "procedures": [], "interventions": solutionsInterv, - "alertExams": [], + "alertExams": 0, "complication": 0, } ) @@ -218,7 +218,7 @@ def _build_headers( "solution": [], "procedures": procedures, "interventions": proceduresInterv, - "alertExams": [], + "alertExams": 0, "complication": 0, } ) From e2768ca4a713c81b1648334cd0a84127a4177441 Mon Sep 17 00:00:00 2001 From: Marcelo Arocha Date: Wed, 6 Nov 2024 10:22:37 -0300 Subject: [PATCH 3/7] remove unused code --- models/prescription.py | 58 ------------------- services/admin/admin_drug_service.py | 2 +- services/admin/admin_exam_service.py | 1 - .../admin/admin_integration_status_service.py | 3 +- services/admin/admin_segment_service.py | 4 +- .../admin/admin_unit_conversion_service.py | 2 +- services/drug_service.py | 2 +- services/prescription_view_service.py | 3 +- services/segment_service.py | 3 +- 9 files changed, 9 insertions(+), 69 deletions(-) diff --git a/models/prescription.py b/models/prescription.py index 2bc0987e..e42ee21e 100644 --- a/models/prescription.py +++ b/models/prescription.py @@ -2,11 +2,9 @@ from sqlalchemy import case, cast, literal, and_, func, desc, asc, or_ from sqlalchemy.sql.expression import literal_column, case from sqlalchemy.dialects import postgresql -from sqlalchemy.dialects.postgresql import INTERVAL from .main import db, User, DrugAttributes, Outlier, Substance, Drug from .appendix import Department, Notes, MeasureUnit, Frequency, MeasureUnitConvert -from .segment import Segment from utils import prescriptionutils @@ -49,62 +47,6 @@ def getFuturePrescription(idPrescription, admissionNumber): .first() ) - def lastDeptbyAdmission(idPrescription, admissionNumber, ref_date): - return ( - db.session.query(Department.name) - .select_from(Prescription) - .outerjoin( - Department, - and_( - Department.id == Prescription.idDepartment, - Department.idHospital == Prescription.idHospital, - ), - ) - .filter(Prescription.admissionNumber == admissionNumber) - .filter(Prescription.id < idPrescription) - .filter( - Prescription.date - > (func.date(ref_date) - func.cast("1 month", INTERVAL)) - ) - .order_by(desc(Prescription.id)) - .first() - ) - - def getPrescriptionBasic(): - return ( - db.session.query( - Prescription, - Patient, - literal("0"), - literal("0"), - Department.name.label("department"), - Segment.description, - Patient.observation, - Prescription.notes, - Patient.alert, - Prescription.prescriber, - User.name, - Prescription.insurance, - ) - .outerjoin(Patient, Patient.admissionNumber == Prescription.admissionNumber) - .outerjoin( - Department, - and_( - Department.id == Prescription.idDepartment, - Department.idHospital == Prescription.idHospital, - ), - ) - .outerjoin(Segment, Segment.id == Prescription.idSegment) - .outerjoin(User, Prescription.user == User.id) - ) - - def getPrescription(idPrescription): - return ( - Prescription.getPrescriptionBasic() - .filter(Prescription.id == idPrescription) - .first() - ) - def getHeaders(admissionNumber, aggDate, idSegment, is_pmc=False, is_cpoe=False): q = ( db.session.query(Prescription, Department.name, User.name) diff --git a/services/admin/admin_drug_service.py b/services/admin/admin_drug_service.py index a6485413..325e66ce 100644 --- a/services/admin/admin_drug_service.py +++ b/services/admin/admin_drug_service.py @@ -5,7 +5,6 @@ from models.main import db, User, PrescriptionAgg from models.prescription import ( - Segment, MeasureUnit, MeasureUnitConvert, Outlier, @@ -13,6 +12,7 @@ DrugAttributes, Substance, ) +from models.segment import Segment from models.enums import DrugAdminSegment, DrugAttributesAuditTypeEnum from services.admin import admin_ai_service from services import drug_service as main_drug_service diff --git a/services/admin/admin_exam_service.py b/services/admin/admin_exam_service.py index 00d9ba36..02d0ef19 100644 --- a/services/admin/admin_exam_service.py +++ b/services/admin/admin_exam_service.py @@ -3,7 +3,6 @@ from markupsafe import escape as escape_html from models.main import db, User -from models.prescription import Segment from models.segment import Segment, SegmentExam, Exams from services.admin import admin_integration_service from services import data_authorization_service diff --git a/services/admin/admin_integration_status_service.py b/services/admin/admin_integration_status_service.py index 1712c522..07cbd0d4 100644 --- a/services/admin/admin_integration_status_service.py +++ b/services/admin/admin_integration_status_service.py @@ -2,7 +2,6 @@ from models.main import db, User, PrescriptionAgg from models.prescription import ( - Segment, Outlier, Drug, DrugAttributes, @@ -14,7 +13,7 @@ SegmentDepartment, InterventionReason, ) -from models.segment import SegmentExam +from models.segment import SegmentExam, Segment from models.enums import MemoryEnum from services.admin import admin_memory_service from exception.validation_error import ValidationError diff --git a/services/admin/admin_segment_service.py b/services/admin/admin_segment_service.py index 4206879e..4c1f315f 100644 --- a/services/admin/admin_segment_service.py +++ b/services/admin/admin_segment_service.py @@ -1,9 +1,9 @@ from sqlalchemy import func, and_, asc from models.main import db, User -from models.prescription import Segment, Department +from models.prescription import Department from models.appendix import SegmentDepartment -from models.segment import Hospital +from models.segment import Hospital, Segment from models.enums import IntegrationStatusEnum from services.admin import admin_integration_status_service from decorators.has_permission_decorator import has_permission, Permission diff --git a/services/admin/admin_unit_conversion_service.py b/services/admin/admin_unit_conversion_service.py index 4df9dda6..8827835f 100644 --- a/services/admin/admin_unit_conversion_service.py +++ b/services/admin/admin_unit_conversion_service.py @@ -11,8 +11,8 @@ Drug, MeasureUnit, Substance, - Segment, ) +from models.segment import Segment from models.enums import IntegrationStatusEnum from services import drug_service as main_drug_service from services.admin import ( diff --git a/services/drug_service.py b/services/drug_service.py index f3f824e3..b5170174 100644 --- a/services/drug_service.py +++ b/services/drug_service.py @@ -19,8 +19,8 @@ DrugAttributes, MeasureUnitConvert, Substance, - Segment, ) +from models.segment import Segment from models.enums import DrugAdminSegment, DrugAttributesAuditTypeEnum from services import data_authorization_service from exception.validation_error import ValidationError diff --git a/services/prescription_view_service.py b/services/prescription_view_service.py index ffd2962d..51f7e5fe 100644 --- a/services/prescription_view_service.py +++ b/services/prescription_view_service.py @@ -12,9 +12,8 @@ PrescriptionDrug, PrescriptionAudit, Department, - Segment, ) -from models.segment import Exams +from models.segment import Exams, Segment from models.enums import ( MemoryEnum, FeatureEnum, diff --git a/services/segment_service.py b/services/segment_service.py index 09bcad75..c7c45c8d 100644 --- a/services/segment_service.py +++ b/services/segment_service.py @@ -1,7 +1,8 @@ from sqlalchemy import asc, func from models.main import db -from models.prescription import Department, Segment +from models.prescription import Department +from models.segment import Segment from models.appendix import SegmentDepartment from decorators.has_permission_decorator import has_permission, Permission From f791ac282050381a4ba8bee4f1b681d8b0652617 Mon Sep 17 00:00:00 2001 From: Marcelo Arocha Date: Wed, 6 Nov 2024 10:35:32 -0300 Subject: [PATCH 4/7] add score variation --- services/prescription_agg_service.py | 53 +++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/services/prescription_agg_service.py b/services/prescription_agg_service.py index beb00131..68e975ab 100644 --- a/services/prescription_agg_service.py +++ b/services/prescription_agg_service.py @@ -1,6 +1,6 @@ from sqlalchemy import desc, text, select, func, and_ from flask_sqlalchemy.session import Session -from datetime import date, datetime +from datetime import date, datetime, timedelta from models.main import db, User, dbSession from models.prescription import ( @@ -108,9 +108,13 @@ def create_agg_prescription_by_prescription( id_prescription=pAgg.id, user_context=user_context ) - pAgg.features = prescriptionutils.getFeatures( + features = prescriptionutils.getFeatures( result=agg_data, agg_date=pAgg.date, intervals_for_agg_date=True ) + score_variation = _get_score_variation(prescription=pAgg) + features.update({"scoreVariation": score_variation}) + + pAgg.features = features pAgg.aggDrugs = pAgg.features["drugIDs"] pAgg.aggDeps = pAgg.features["departmentList"] @@ -199,10 +203,16 @@ def create_agg_prescription_by_date( id_prescription=agg_p.id, user_context=user_context ) - agg_p.update = datetime.today() - agg_p.features = prescriptionutils.getFeatures(result=agg_data) + features = prescriptionutils.getFeatures( + result=agg_data, agg_date=agg_p.date, intervals_for_agg_date=True + ) + score_variation = _get_score_variation(prescription=agg_p) + features.update({"scoreVariation": score_variation}) + + agg_p.features = features agg_p.aggDrugs = agg_p.features["drugIDs"] agg_p.aggDeps = agg_p.features["departmentList"] + agg_p.update = datetime.today() internal_prescription_ids = internal_prescription_ids = ( prescriptionutils.get_internal_prescription_ids(result=agg_data) @@ -331,3 +341,38 @@ def _update_patient_conciliation_status(prescription: Prescription): ): patient.st_conciliation = PatientConciliationStatusEnum.CREATED.value db.session.flush() + + +def _get_score_variation(prescription: Prescription): + new_score = int(prescription.features.get("globalScore", 0)) + initial_value = { + "variation": 100, + "currentGlobalScore": new_score, + "previousGlobalScore": 0, + } + + previous_prescription = ( + db.session.query(Prescription) + .filter( + Prescription.id + == prescriptionutils.gen_agg_id( + admission_number=prescription.admissionNumber, + id_segment=prescription.idSegment, + pdate=prescription.date - timedelta(days=1), + ) + ) + .first() + ) + + if not previous_prescription: + return initial_value + + previous_score = int(previous_prescription.features.get("globalScore", 0)) + if previous_score == 0: + return initial_value + + variation = (new_score - previous_score) / previous_score * 100 + + return initial_value.update( + {"variation": round(variation, 2), "previousGlobalScore": previous_score} + ) From c0f7995e644ec77fba3c2ec686f6d928c987e5c0 Mon Sep 17 00:00:00 2001 From: Marcelo Arocha Date: Wed, 6 Nov 2024 10:46:07 -0300 Subject: [PATCH 5/7] add score variation --- services/prescription_agg_service.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/services/prescription_agg_service.py b/services/prescription_agg_service.py index 68e975ab..20f0dacd 100644 --- a/services/prescription_agg_service.py +++ b/services/prescription_agg_service.py @@ -111,7 +111,7 @@ def create_agg_prescription_by_prescription( features = prescriptionutils.getFeatures( result=agg_data, agg_date=pAgg.date, intervals_for_agg_date=True ) - score_variation = _get_score_variation(prescription=pAgg) + score_variation = _get_score_variation(prescription=pAgg, features=features) features.update({"scoreVariation": score_variation}) pAgg.features = features @@ -206,7 +206,7 @@ def create_agg_prescription_by_date( features = prescriptionutils.getFeatures( result=agg_data, agg_date=agg_p.date, intervals_for_agg_date=True ) - score_variation = _get_score_variation(prescription=agg_p) + score_variation = _get_score_variation(prescription=agg_p, features=features) features.update({"scoreVariation": score_variation}) agg_p.features = features @@ -343,8 +343,8 @@ def _update_patient_conciliation_status(prescription: Prescription): db.session.flush() -def _get_score_variation(prescription: Prescription): - new_score = int(prescription.features.get("globalScore", 0)) +def _get_score_variation(prescription: Prescription, features: dict): + new_score = int(features.get("globalScore", 0)) initial_value = { "variation": 100, "currentGlobalScore": new_score, From 5b181acd21ee19058d63285625f91c4b8137f544 Mon Sep 17 00:00:00 2001 From: Marcelo Arocha Date: Thu, 7 Nov 2024 08:01:35 -0300 Subject: [PATCH 6/7] fix score variation --- services/cache_service.py | 10 +++++++++- services/prescription_agg_service.py | 5 ++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/services/cache_service.py b/services/cache_service.py index 8a8061b0..912d35f3 100644 --- a/services/cache_service.py +++ b/services/cache_service.py @@ -22,7 +22,15 @@ def get_range(key: str, days_ago: int): now = time.time() min_date = now - (days_ago * 24 * 60 * 60) - cache_data = redis_client.zrangebyscore(key, min=min_date, max=now) + try: + cache_data = redis_client.zrangebyscore(key, min=min_date, max=now) + except TimeoutError: + logging.basicConfig() + logger = logging.getLogger("noharm.backend") + logger.error( + f"redis timeout error: {key}", + ) + return None if cache_data: result = [] diff --git a/services/prescription_agg_service.py b/services/prescription_agg_service.py index 20f0dacd..d927b63b 100644 --- a/services/prescription_agg_service.py +++ b/services/prescription_agg_service.py @@ -373,6 +373,9 @@ def _get_score_variation(prescription: Prescription, features: dict): variation = (new_score - previous_score) / previous_score * 100 - return initial_value.update( + variation_data = initial_value + variation_data.update( {"variation": round(variation, 2), "previousGlobalScore": previous_score} ) + + return variation_data From c9b9236a6df019a0b85641316c91aca1af2e6c3f Mon Sep 17 00:00:00 2001 From: Marcelo Arocha Date: Thu, 7 Nov 2024 20:49:53 -0300 Subject: [PATCH 7/7] v4.07-beta --- config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.py b/config.py index 31f5f5f8..2eadcd24 100644 --- a/config.py +++ b/config.py @@ -5,7 +5,7 @@ class Config: - VERSION = "v4.06-beta" + VERSION = "v4.07-beta" FRONTEND_VERSION = "4.0.5" ENV = getenv("ENV") or NoHarmENV.DEVELOPMENT.value SECRET_KEY = getenv("SECRET_KEY") or "secret_key"