Skip to content

Commit

Permalink
Merge pull request #414 from noharm-ai/develop
Browse files Browse the repository at this point in the history
v4.12-beta
  • Loading branch information
marceloarocha authored Nov 28, 2024
2 parents 5740232 + 7fed40b commit 6c0b8a8
Show file tree
Hide file tree
Showing 21 changed files with 370 additions and 19 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ jobs:
python mobile.py &
sleep 10
- name: Run Coverage Report
env:
CACHE_BUCKET_ID: ${{ secrets.CACHE_BUCKET_ID }}
CACHE_BUCKET_KEY: ${{ secrets.CACHE_BUCKET_KEY }}
CACHE_BUCKET_NAME: ${{ secrets.CACHE_BUCKET_NAME }}
run: python -m pytest --cov-report xml --cov=.
- name: Submit Coverage Report
if: ${{ env.CC_TEST_REPORTER_ID != '' }}
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/deploy-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ jobs:
psql -h localhost -U postgres -d noharm -a -f database/noharm-triggers.sql -v ON_ERROR_STOP=1
psql -h localhost -U postgres -d noharm -a -f database/noharm-insert.sql -v ON_ERROR_STOP=1
- name: Run Python Tests
env:
CACHE_BUCKET_ID: ${{ secrets.CACHE_BUCKET_ID }}
CACHE_BUCKET_KEY: ${{ secrets.CACHE_BUCKET_KEY }}
CACHE_BUCKET_NAME: ${{ secrets.CACHE_BUCKET_NAME }}
run: python -m pytest -v
deploy:
needs: [build]
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/deploy-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ jobs:
psql -h localhost -U postgres -d noharm -a -f database/noharm-triggers.sql -v ON_ERROR_STOP=1
psql -h localhost -U postgres -d noharm -a -f database/noharm-insert.sql -v ON_ERROR_STOP=1
- name: Run Python Tests
env:
CACHE_BUCKET_ID: ${{ secrets.CACHE_BUCKET_ID }}
CACHE_BUCKET_KEY: ${{ secrets.CACHE_BUCKET_KEY }}
CACHE_BUCKET_NAME: ${{ secrets.CACHE_BUCKET_NAME }}
run: python -m pytest -v
deploy:
needs: [build]
Expand Down
10 changes: 0 additions & 10 deletions .github/workflows/deploy-test-manual.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,6 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
# - name: Setup PostgreSQL
# run: |
# git clone https://github.com/noharm-ai/database
# psql -h localhost -U postgres -d noharm -a -f database/noharm-public.sql -v ON_ERROR_STOP=1
# psql -h localhost -U postgres -d noharm -a -f database/noharm-create.sql -v ON_ERROR_STOP=1
# psql -h localhost -U postgres -d noharm -a -f database/noharm-newuser.sql -v ON_ERROR_STOP=1
# psql -h localhost -U postgres -d noharm -a -f database/noharm-triggers.sql -v ON_ERROR_STOP=1
# psql -h localhost -U postgres -d noharm -a -f database/noharm-insert.sql -v ON_ERROR_STOP=1
# - name: Run Python Tests
# run: python -m pytest -v
deploy:
needs: [build]
runs-on: [ubuntu-latest]
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/deploy-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ jobs:
psql -h localhost -U postgres -d noharm -a -f database/noharm-triggers.sql -v ON_ERROR_STOP=1
psql -h localhost -U postgres -d noharm -a -f database/noharm-insert.sql -v ON_ERROR_STOP=1
- name: Run Python Tests
env:
CACHE_BUCKET_ID: ${{ secrets.CACHE_BUCKET_ID }}
CACHE_BUCKET_KEY: ${{ secrets.CACHE_BUCKET_KEY }}
CACHE_BUCKET_NAME: ${{ secrets.CACHE_BUCKET_NAME }}
run: python -m pytest -v
deploy:
needs: [build]
Expand Down
2 changes: 1 addition & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


class Config:
VERSION = "v4.11-beta"
VERSION = "v4.12-beta"
FRONTEND_VERSION = "4.0.7"
ENV = getenv("ENV") or NoHarmENV.DEVELOPMENT.value
SECRET_KEY = getenv("SECRET_KEY") or "secret_key"
Expand Down
8 changes: 8 additions & 0 deletions models/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,11 @@ class PatientConciliationStatusEnum(Enum):

class AppFeatureFlagEnum(Enum):
REDIS_CACHE = "redisCache"


class FrequencyEnum(Enum):
SN = 33
ACM = 44
CONT = 55
NOW = 66
UNDEFINED = 99
2 changes: 2 additions & 0 deletions models/regulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class RegSolicitation(db.Model):
id_reg_solicitation_type = db.Column(
"fkreg_tipo_solicitacao", db.BigInteger, nullable=False
)
id_department = db.Column("fksetor", db.BigInteger, nullable=False)

risk = db.Column("risco", db.Integer, nullable=True)
cid = db.Column("cid", db.String, nullable=True)
Expand All @@ -36,6 +37,7 @@ class RegSolicitationType(db.Model):
id = db.Column("fkreg_tipo_solicitacao", db.BigInteger, primary_key=True)
name = db.Column("nome", db.String, nullable=False)
status = db.Column("status", db.Integer, nullable=False)
tp_type = db.Column("tp_tipo", db.Integer, nullable=False)

created_at = db.Column("created_at", db.DateTime, nullable=False)
created_by = db.Column("created_by", db.BigInteger, nullable=True)
Expand Down
7 changes: 7 additions & 0 deletions models/requests/regulation_prioritization_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ class Order(BaseModel):
class RegulationPrioritizationRequest(BaseModel):
startDate: datetime
endDate: Optional[datetime] = None
scheduleStartDate: Optional[datetime] = None
scheduleEndDate: Optional[datetime] = None
transportationStartDate: Optional[datetime] = None
transportationEndDate: Optional[datetime] = None
idDepartmentList: Optional[list[int]] = None
typeType: Optional[int] = None
riskList: Optional[list[int]] = None
typeList: Optional[list[str]] = []
stageList: Optional[list[int]] = []
limit: int
Expand Down
41 changes: 40 additions & 1 deletion repository/regulation/reg_solicitation_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from datetime import timedelta

from models.main import db, User
from models.prescription import Patient
from models.prescription import Patient, Department
from models.regulation import RegSolicitation, RegSolicitationType, RegMovement
from models.requests.regulation_prioritization_request import (
RegulationPrioritizationRequest,
Expand All @@ -15,13 +15,15 @@ def get_prioritization(request_data: RegulationPrioritizationRequest):
RegSolicitation,
RegSolicitationType,
Patient,
Department,
func.count().over().label("total"),
)
.outerjoin(
RegSolicitationType,
RegSolicitation.id_reg_solicitation_type == RegSolicitationType.id,
)
.outerjoin(Patient, RegSolicitation.admission_number == Patient.admissionNumber)
.outerjoin(Department, Department.id == RegSolicitation.id_department)
)

if request_data.startDate:
Expand All @@ -33,6 +35,40 @@ def get_prioritization(request_data: RegulationPrioritizationRequest):
<= (request_data.endDate + timedelta(hours=23, minutes=59))
)

if request_data.scheduleStartDate:
query = query.filter(
RegSolicitation.schedule_date >= request_data.scheduleStartDate.date()
)

if request_data.scheduleEndDate:
query = query.filter(
RegSolicitation.schedule_date
<= (request_data.scheduleEndDate + timedelta(hours=23, minutes=59))
)

if request_data.transportationStartDate:
query = query.filter(
RegSolicitation.transportation_date
>= request_data.transportationStartDate.date()
)

if request_data.transportationEndDate:
query = query.filter(
RegSolicitation.transportation_date
<= (request_data.transportationEndDate + timedelta(hours=23, minutes=59))
)

if request_data.riskList:
query = query.filter(RegSolicitation.risk.in_(request_data.riskList))

if request_data.idDepartmentList:
query = query.filter(
RegSolicitation.id_department.in_(request_data.idDepartmentList)
)

if request_data.typeType:
query = query.filter(RegSolicitationType.tp_type == request_data.typeType)

if request_data.typeList:
query = query.filter(
RegSolicitation.id_reg_solicitation_type.in_(request_data.typeList)
Expand All @@ -48,6 +84,9 @@ def get_prioritization(request_data: RegulationPrioritizationRequest):
nullslast(direction(getattr(RegSolicitation, order.field)))
)

if order.field in ["birthdate"]:
query = query.order_by(nullslast(direction(getattr(Patient, order.field))))

query = query.limit(request_data.limit).offset(request_data.offset)

return query.all()
Expand Down
36 changes: 36 additions & 0 deletions routes/names.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import logging
from flask import Blueprint, request
from flask_jwt_extended import jwt_required, get_jwt_identity
from jwt import encode
from datetime import datetime, timedelta, timezone

from models.main import User, dbSession, db
from models.appendix import SchemaConfig
Expand Down Expand Up @@ -125,6 +127,40 @@ def proxy_multiple():
return names, status.HTTP_200_OK


@app_names.route("/names/auth-token", methods=["GET"])
@jwt_required()
def auth_token():
user = User.find(get_jwt_identity())
dbSession.setSchema(user.schema)

schema_config = (
db.session.query(SchemaConfig)
.filter(SchemaConfig.schemaName == user.schema)
.first()
)

getname_config = (
schema_config.config.get("getname", {}) if schema_config.config else {}
)
key = getname_config.get("secret", "")

if not key:
return {
"status": "error",
"message": "Invalid key",
}, status.HTTP_400_BAD_REQUEST

token = encode(
payload={
"exp": datetime.now(tz=timezone.utc) + timedelta(minutes=2),
"iss": "noharm",
},
key=key,
)

return {"status": "success", "data": token}


def _get_token(config):
token_url = config["getname"]["token"]["url"]
params = config["getname"]["token"]["params"]
Expand Down
40 changes: 39 additions & 1 deletion services/admin/admin_ai_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import tempfile
import numpy
import re
import gc
import logging
from typing import List

from config import Config
from exception.validation_error import ValidationError
from models.prescription import Drug
from utils import status
from utils import status, stringutils


def get_substance(drugs: List[Drug]):
Expand Down Expand Up @@ -48,6 +50,42 @@ def get_substance(drugs: List[Drug]):
return drugs_array


def get_substance_by_drug_name(drug_names: list[str]):
drugs_dict = {}
drug_names_words = []

if not drug_names:
return drugs_dict

logging.basicConfig()
logger = logging.getLogger("noharm.backend")
drug_names_words = [stringutils.prepare_drug_name(n) for n in drug_names]

model_subst = _get_model("models/noharm-ml-subst.gz")
token_subst = _get_model("models/noharm-tk-subst.gz")

vector = token_subst.transform(drug_names_words)
prediction = model_subst.predict(vector)
probabilities = model_subst.predict_proba(vector).round(2)
MIN_PROB = 0.35

for idx, p in enumerate(prediction):
subst_idx = numpy.where(model_subst.classes_ == p)[0][0]
subst_prob = probabilities[idx][subst_idx]

if subst_prob >= MIN_PROB:
drugs_dict[drug_names[idx]] = p

logger.debug(
f"AI INFER SUBSTANCE {drug_names[idx]} - {drug_names_words[idx]} - {subst_prob} - {p}"
)

del model_subst, token_subst, vector, prediction, probabilities
gc.collect()

return drugs_dict


def get_factors(conversions):
if len(conversions) == 0:
return conversions
Expand Down
34 changes: 33 additions & 1 deletion services/admin/admin_integration_service.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from utils import status
from sqlalchemy import case, text
from datetime import datetime
Expand Down Expand Up @@ -130,7 +131,10 @@ def update_integration_config(

schema_config.status = status if status != None else schema_config.status
schema_config.nh_care = nh_care if nh_care != None else schema_config.nh_care
schema_config.config = config if config != None else schema_config.config
schema_config.config = _set_new_config(
old_config=schema_config.config if schema_config.config else {},
new_config=config,
)
schema_config.fl1 = bool(fl1) if fl1 != None else schema_config.fl1
schema_config.fl2 = bool(fl2) if fl2 != None else schema_config.fl2
schema_config.fl3 = bool(fl3) if fl3 != None else schema_config.fl3
Expand All @@ -148,6 +152,33 @@ def update_integration_config(
return _object_to_dto(schema_config_db)


def _set_new_config(old_config: dict, new_config: dict):
config = dict({}, **old_config)

if "getname" in new_config:
config["getname"] = {"type": new_config["getname"]["type"]}

if "auth" == config["getname"]["type"]:
config["getname"]["secret"] = new_config["getname"]["secret"]
elif "proxy" == config["getname"]["type"]:
config["getname"]["url"] = new_config["getname"]["url"]
config["getname"]["params"] = json.loads(new_config["getname"]["params"])
config["getname"]["token"] = {
"url": new_config["getname"]["token"]["url"],
"params": {
"client_id": new_config["getname"]["token"]["params"]["client_id"],
"client_secret": new_config["getname"]["token"]["params"][
"client_secret"
],
"grant_type": new_config["getname"]["token"]["params"][
"grant_type"
],
},
}

return config


@has_permission(Permission.INTEGRATION_UTILS)
def list_integrations(user_context: User):
integrations = (
Expand Down Expand Up @@ -178,4 +209,5 @@ def _object_to_dto(schema_config: SchemaConfig):
"fl2": schema_config.fl2,
"fl3": schema_config.fl3,
"fl4": schema_config.fl4,
"cpoe": schema_config.cpoe,
}
Loading

0 comments on commit 6c0b8a8

Please sign in to comment.