Skip to content

Commit

Permalink
Merge pull request #423 from noharm-ai/develop
Browse files Browse the repository at this point in the history
v4.18-beta
  • Loading branch information
marceloarocha authored Dec 17, 2024
2 parents 9cb7594 + 232a4db commit daf60d3
Show file tree
Hide file tree
Showing 15 changed files with 326 additions and 114 deletions.
3 changes: 2 additions & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


class Config:
VERSION = "v4.17-beta"
VERSION = "v4.18-beta"
FRONTEND_VERSION = "4.0.13"
ENV = getenv("ENV") or NoHarmENV.DEVELOPMENT.value
SECRET_KEY = getenv("SECRET_KEY") or "secret_key"
Expand Down Expand Up @@ -33,6 +33,7 @@ class Config:

NIFI_BUCKET_NAME = getenv("NIFI_BUCKET_NAME") or ""
NIFI_SQS_QUEUE_REGION = getenv("NIFI_SQS_QUEUE_REGION") or ""
NIFI_LOG_GROUP_NAME = getenv("NIFI_LOG_GROUP_NAME") or ""

CACHE_BUCKET_NAME = getenv("CACHE_BUCKET_NAME") or ""
CACHE_BUCKET_ID = getenv("CACHE_BUCKET_ID") or ""
Expand Down
2 changes: 2 additions & 0 deletions mobile.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from routes.reports.reports_config_rpt import app_rpt_config
from routes.reports.reports_culture import app_rpt_culture
from routes.reports.reports_antimicrobial import app_rpt_antimicrobial
from routes.reports.reports_exams import app_rpt_exams
from routes.reports.reports_prescription_history import app_rpt_prescription_history
from routes.regulation.regulation import app_regulation

Expand Down Expand Up @@ -114,6 +115,7 @@
app.register_blueprint(app_rpt_antimicrobial)
app.register_blueprint(app_rpt_config)
app.register_blueprint(app_rpt_prescription_history)
app.register_blueprint(app_rpt_exams)

app.register_blueprint(app_regulation)

Expand Down
1 change: 1 addition & 0 deletions models/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ class NifiQueueActionTypeEnum(Enum):
TERMINATE_PROCESS = "TERMINATE_PROCESS"
CUSTOM_CALLBACK = "CUSTOM_CALLBACK"
REFRESH_TEMPLATE = "REFRESH_TEMPLATE"
UPDATE_PROPERTY = "UPDATE_PROPERTY"


class DrugAlertTypeEnum(Enum):
Expand Down
4 changes: 3 additions & 1 deletion models/requests/regulation_movement_request.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from pydantic import BaseModel
from typing import Optional


class RegulationMovementRequest(BaseModel):
id: int
id: Optional[int] = None
ids: Optional[list[int]] = None
action: int
actionData: dict
actionDataTemplate: list[dict]
Expand Down
12 changes: 1 addition & 11 deletions models/segment.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from datetime import date, timedelta
from sqlalchemy import desc, asc
from sqlalchemy import asc

from .main import db

Expand Down Expand Up @@ -62,12 +61,3 @@ class Exams(db.Model):
typeExam = db.Column("tpexame", db.String, primary_key=True)
value = db.Column("resultado", db.Float, nullable=False)
unit = db.Column("unidade", db.String, nullable=True)

def findByPatient(idPatient):
return (
db.session.query(Exams)
.filter(Exams.idPatient == idPatient)
.filter(Exams.date >= (date.today() - timedelta(days=90)))
.order_by(asc(Exams.typeExam), desc(Exams.date))
.all()
)
15 changes: 15 additions & 0 deletions repository/exams_repository.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from datetime import date, timedelta
from sqlalchemy import desc, asc

from models.main import db
from models.segment import Exams


def get_exams_by_patient(idPatient: int, days: int):
return (
db.session.query(Exams)
.filter(Exams.idPatient == idPatient)
.filter(Exams.date >= (date.today() - timedelta(days=days)))
.order_by(asc(Exams.typeExam), desc(Exams.date))
.all()
)
16 changes: 8 additions & 8 deletions routes/admin/admin_integration_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@ def get_template():
return admin_integration_remote_service.get_template()


@app_admin_integration_remote.route(
"/admin/integration-remote/template-date", methods=["GET"]
)
@api_endpoint()
def get_template_date():
return admin_integration_remote_service.get_template_date()


@app_admin_integration_remote.route(
"/admin/integration-remote/queue-status", methods=["GET"]
)
Expand All @@ -46,3 +38,11 @@ def push_queue_request():
action_type=request_data.get("actionType", None),
data=request_data,
)


@app_admin_integration_remote.route(
"/admin/integration-remote/get-errors", methods=["GET"]
)
@api_endpoint()
def get_errors():
return admin_integration_remote_service.get_errors()
2 changes: 1 addition & 1 deletion routes/names.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def proxy_name(idPatient):
)

if client_id == "noharm-internal":
url = url.replace("{idPatient}", str(idPatient))
url = url.replace("{idPatient}", str(int(idPatient)))
params = dict(config["getname"]["params"])
else:
params = dict(config["getname"]["params"], **{"cd_paciente": idPatient})
Expand Down
12 changes: 12 additions & 0 deletions routes/reports/reports_exams.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from flask import Blueprint, request

from decorators.api_endpoint_decorator import api_endpoint
from services.reports import reports_exams_service

app_rpt_exams = Blueprint("app_rpt_exams", __name__)


@app_rpt_exams.route("/reports/exams/raw", methods=["GET"])
@api_endpoint()
def get_raw_exams():
return reports_exams_service.get_raw_exams(id_patient=request.args.get("idPatient"))
7 changes: 7 additions & 0 deletions routes/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ def create_ticket():
)


# deprecated
@app_support.route("/support/list-tickets", methods=["GET"])
@api_endpoint()
def list_tickets():
return support_service.list_tickets()


@app_support.route("/support/list-tickets/v2", methods=["GET"])
@api_endpoint()
def list_tickets_v2():
return support_service.list_tickets_v2()
137 changes: 86 additions & 51 deletions services/admin/admin_integration_remote_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import dateutil as pydateutil
from utils import status
from markupsafe import escape
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from sqlalchemy import desc
from sqlalchemy.orm import Session
from botocore.exceptions import ClientError
Expand Down Expand Up @@ -33,7 +33,7 @@ def get_file_url(schema: str, filename="template") -> tuple[str, str]:
"Bucket": Config.NIFI_BUCKET_NAME,
"Key": _get_resource_name(schema=schema, filename=filename),
},
ExpiresIn=3600,
ExpiresIn=120,
),
cache_data["updatedAt"],
)
Expand Down Expand Up @@ -64,19 +64,6 @@ def _get_cache_data(client, schema, filename="current"):
return {"exists": False, "updatedAt": None}


@has_permission(Permission.ADMIN_INTEGRATION_REMOTE)
def get_template_date(user_context: User):
client = boto3.client("s3")
cache_data = _get_cache_data(
client=client, schema=user_context.schema, filename="template"
)

if cache_data != None:
return {"updatedAt": cache_data["updatedAt"]}

return {"updatedAt": None}


@has_permission(Permission.ADMIN_INTEGRATION_REMOTE)
def get_template(user_context: User):
template_url, template_updated_at = get_file_url(
Expand All @@ -88,6 +75,9 @@ def get_template(user_context: User):
diagnostics_url, diagnostics_updated_at = get_file_url(
schema=user_context.schema, filename="diagnostics"
)
bulletin_url, bulletin_updated_at = get_file_url(
schema=user_context.schema, filename="bulletin"
)

if not template_url:
raise ValidationError(
Expand Down Expand Up @@ -127,6 +117,9 @@ def get_template(user_context: User):
"status": status_url,
"diagnostics": diagnostics_url,
"updatedAt": dateutils.to_iso(template_updated_at),
"statusUpdatedAt": status_updated_at,
"bulletin": bulletin_url,
"bulletinUpdatedAt": bulletin_updated_at,
"queue": queue_results,
}

Expand Down Expand Up @@ -198,9 +191,18 @@ def _send_to_sqs(queue: NifiQueue, schema: str):
region_name=Config.NIFI_SQS_QUEUE_REGION,
),
)
response = sqs.get_queue_url(
QueueName=schema,
)

try:
response = sqs.get_queue_url(
QueueName=schema,
)
except ClientError:
raise ValidationError(
"Fila inexistente",
"errors.businessRules",
status.HTTP_400_BAD_REQUEST,
)

queue_url = response["QueueUrl"]
body_data = {
"schema": schema,
Expand Down Expand Up @@ -259,57 +261,90 @@ def _get_new_queue(id_processor: str, action_type: str, data: dict):
elif NifiQueueActionTypeEnum.REFRESH_TEMPLATE.value == action_type:
queue.url = f"nifi-api/system-diagnostics"
queue.method = "GET"
elif NifiQueueActionTypeEnum.UPDATE_PROPERTY.value == action_type:
queue.url = f"nifi-api/processors/{escape(id_processor)}"
queue.method = "PUT"
queue.body = {
"id": id_processor,
"config": {"properties": data["properties"]},
}

return queue


@has_permission(Permission.ADMIN_INTEGRATION_REMOTE)
def get_queue_status(id_queue_list, user_context: User):
queue_results = []
update_status = False

engine = db.engines["report"]
with Session(engine) as session:
session.connection(
execution_options={"schema_translate_map": {None: user_context.schema}}
)
queue_list = (
session.query(NifiQueue).filter(NifiQueue.id.in_(id_queue_list)).all()
)

for q in queue_list:
if q.responseCode == status.HTTP_200_OK:
update_status = True

queue_results.append(
{
"id": q.id,
"url": q.url,
"body": q.body,
"method": q.method,
"extra": q.extra,
"responseCode": q.responseCode,
"response": q.response,
"responseAt": dateutils.to_iso(q.responseAt),
"createdAt": dateutils.to_iso(q.createdAt),
}
if id_queue_list:
engine = db.engines["report"]
with Session(engine) as session:
session.connection(
execution_options={"schema_translate_map": {None: user_context.schema}}
)
queue_list = (
session.query(NifiQueue).filter(NifiQueue.id.in_(id_queue_list)).all()
)

status_url = None
status_updated_at = None
if update_status:
status_url, status_updated_at = get_file_url(
schema=user_context.schema, filename="status"
)
for q in queue_list:
queue_results.append(
{
"id": q.id,
"url": q.url,
"body": q.body,
"method": q.method,
"extra": q.extra,
"responseCode": q.responseCode,
"response": q.response,
"responseAt": dateutils.to_iso(q.responseAt),
"createdAt": dateutils.to_iso(q.createdAt),
}
)

status_url, status_updated_at = get_file_url(
schema=user_context.schema, filename="status"
)

bulletin_url, bulletin_updated_at = get_file_url(
schema=user_context.schema, filename="bulletin"
)

return {
"queue": queue_results,
"updateStatus": update_status,
"statusUrl": status_url,
"statusUpdatedAt": status_updated_at,
"bulletinUrl": bulletin_url,
"bulletinUpdatedAt": bulletin_updated_at,
}


@has_permission(Permission.ADMIN_INTEGRATION_REMOTE)
def get_errors(user_context: User):
client = boto3.client("logs", region_name=Config.NIFI_SQS_QUEUE_REGION)

response = client.get_log_events(
logGroupName=Config.NIFI_LOG_GROUP_NAME,
logStreamName=f"nifi/{user_context.schema}",
startTime=int(
(datetime.now(tz=timezone.utc) - timedelta(minutes=60)).timestamp() * 1000
),
endTime=int(datetime.now(tz=timezone.utc).timestamp()) * 1000,
)

results = []
for event in response.get("events", []):
results.append(
{
"message": event.get("message"),
"date": datetime.fromtimestamp(
int(event.get("timestamp")) / 1000
).isoformat(),
}
)

return results


def _validate_custom_endpoint(endpoint: str):
pattern1 = re.compile(
"^nifi-api\/flowfile-queues\/[\w-]{36}\/flowfiles\/[\w-]{36}\/content$"
Expand Down
3 changes: 2 additions & 1 deletion services/exams_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from models.prescription import Patient
from models.segment import Exams, SegmentExam
from models.notes import ClinicalNotes
from repository import exams_repository
from services import memory_service, cache_service
from decorators.has_permission_decorator import has_permission, Permission
from exception.validation_error import ValidationError
Expand Down Expand Up @@ -88,7 +89,7 @@ def get_exams_by_admission(admission_number: int, id_segment: int):
)

# TODO: refactor
examsList = Exams.findByPatient(patient.idPatient)
examsList = exams_repository.get_exams_by_patient(patient.idPatient, days=90)
segExam = SegmentExam.refDict(idSegment=id_segment)

perc = {
Expand Down
Loading

0 comments on commit daf60d3

Please sign in to comment.