Commit a1a8e7aa by arun.uday

v4

parent bb069f5f
# using mongo aggregation operations
import uvicorn
from scripts.config.applications_config import uvicorn_app, uvicorn_port
from scripts.config.applications_config import uvicorn_port
from scripts.logging.loggers import logger
# starting the application
if __name__ == "__main__":
try:
print("Redis task")
uvicorn.run(uvicorn_app, port=int(uvicorn_port))
uvicorn.run("main:app", port=int(uvicorn_port))
except Exception as e:
logger.error("Interruption occurred: ", e)
# pip install python-multipart
[path]
base_path = scripts/
sub_path = temp/
log_path = log/
[file]
file_name_json = data.json
......
# using mongo aggregation operations
import uvicorn
from fastapi import FastAPI
from scripts.config.applications_config import uvicorn_port
from scripts.logging.loggers import logger
from scripts.services.app_services_run import assign_doctor
app = FastAPI()
app.include_router(assign_doctor)
# starting the application
if __name__ == "__main__":
try:
print("Redis task")
uvicorn.run(app, port=int(uvicorn_port))
except Exception as e:
logger.error("Interruption occurred: ", e)
# pip install python-multipart
......@@ -9,4 +9,3 @@ config.read("conf/applications.conf")
doctors_count = config.get("api_path", "no_doctors")
close_day = config.get("api_path", "close_day")
patient_details = config.get("api_path", "patient_details")
......@@ -9,6 +9,7 @@ config.read("conf/applications.conf")
# path
base_path = config.get("path", 'base_path')
sub_path = config.get("path", "sub_path")
log_path = config.get("path", "log_path")
# uvicorn
uvicorn_port = config.get("uvicorn", "uvicorn_port")
......
from scripts.core.handlers.next_doc_existing import assign_doc_exist
from scripts.core.handlers.queue_check import set_patient_queue
from scripts.logging.loggers import logger
# check if the patient exists in the redis db
def patient_data_check(conn_queue, conn_patient, client, dict_data, doctors_counter):
try:
# check if the patient data exists in the db
if not conn_patient.exists(dict_data['patient_id']):
......@@ -13,3 +14,5 @@ def patient_data_check(conn_queue, conn_patient, client, dict_data, doctors_coun
set_patient_queue(conn_queue, conn_patient, client, dict_data, next_doc, doctors_counter)
else:
assign_doc_exist(conn_queue, conn_patient, client, dict_data)
except Exception as e:
logger.error("Exception occurred while checking for patients: ", e)
from scripts.logging.loggers import logger
def delete_queue(doctors_counter, conn_queue):
try:
# deleting the queue
for doctors in range(0, doctors_counter):
if not conn_queue.exists(f'doctor{doctors}'):
print(doctors)
else:
# delete doctors
conn_queue.delete(f'doctor{doctors}')
conn_queue.set('next_doc', 0)
conn_queue.delete("Doctors")
doctors_counter -= 1
continue
except Exception as e:
logger.error("Deleting queue error: ", e)
from scripts.logging.loggers import logger
def set_no_doctors(conn_queue, no_doctors):
try:
# entering the number of doctors in to the redis db
conn_queue.set("Doctors", no_doctors)
# creating the queue for doctors if it doesn't exist
for doctors in range(0, no_doctors):
if not conn_queue.exists(f'doctor{doctors}'):
conn_queue.set(f'doctor{doctors}', '')
else:
continue
return no_doctors
except Exception as e:
logger.error("Exception occurred while adding doctors: ", e)
from paho.mqtt.client import Client
from scripts.core.handlers.check_patient import patient_data_check
from scripts.core.handlers.generating_patient_data import generate_patients
from scripts.logging.loggers import logger
def generate_patients_publish(mqtt_host, port, conn_queue, conn_patient, doctors_counter, patient_data_val):
try:
client = Client()
# creating the paho client to listen to the port
client.connect(mqtt_host, int(port))
# generating the patients
dict_data = generate_patients(patient_data_val)
# checking if the next doctor queue exists
if not conn_queue.exists('next_doc'):
conn_queue.set('next_doc', 0)
# checking the patient datas in the db for the queue
patient_data_check(conn_queue, conn_patient, client, dict_data, doctors_counter)
except Exception as e:
logger.error("Exception occurred while generating and publishing: ", e)
# generating the patient details
def generate_patients(model_data):
from scripts.logging.loggers import logger
def generate_patients(model_data):
try:
# generating the user data to dictionary
dict_val = dict(model_data)
return dict_val
except Exception as e:
logger.error("Exception occurred while generating patients: ", e)
# assigning the doctors to new patients
import time
from scripts.logging.loggers import logger
def assign_new_doctor(conn_queue, conn_patient, client, dict_data, next_doc):
def assign_new_doctor(conn_queue, conn_patient, client, dict_data, next_doc):
try:
avi_doc = conn_queue.get(f'doctor{next_doc}')
# check if the doctor queue is empty
......@@ -21,3 +23,5 @@ def assign_new_doctor(conn_queue, conn_patient, client, dict_data, next_doc):
patient_id = dict_data['patient_id']
# publish the doctor
client.publish(f'doctor{next_doc}', patient_id)
except Exception as e:
logger.error("Exception occurred while assigning new doctor: ", e)
# assigning existing patients to the doctors
import time
from scripts.logging.loggers import logger
def assign_doc_exist(conn_queue, conn_patient, client, dict_data):
def assign_doc_exist(conn_queue, conn_patient, client, dict_data):
try:
# getting the doctor id from the patient db
doctor = conn_patient.hmget(dict_data['patient_id'], 'doctor')
......@@ -19,3 +21,5 @@ def assign_doc_exist(conn_queue, conn_patient, client, dict_data):
# publishing the doctor
client.publish(doctor[0].decode(), patient_id)
except Exception as e:
logger.error("Exception occurred while assigning existing patient: ", e)
from scripts.core.handlers.next_doc_assign import assign_new_doctor
from scripts.logging.loggers import logger
# creating a round-robin for the new patient for the doctor
def set_patient_queue(conn_queue, conn_patient, client, dict_data, next_doc, doctors_counter):
try:
# check if the next doctor is not the last index
if int(next_doc) != doctors_counter:
# assign the doctor
......@@ -20,3 +21,5 @@ def set_patient_queue(conn_queue, conn_patient, client, dict_data, next_doc, doc
# assigning new doctor to the patient
assign_new_doctor(conn_queue, conn_patient, client, dict_data, 0)
conn_queue.set('next_doc', 1)
except Exception as e:
logger.error("Exception occurred while checking the queue: ", e)
import logging
import os
from logging.handlers import RotatingFileHandler
from scripts.config import applications_config
def get_logger():
"""
Creates a rotating log
"""
__logger__ = logging.getLogger('')
__logger__.setLevel(logging.INFO)
log_formatter = '%(asctime)s - %(levelname)-6s - %(message)s'
time_format = "%Y-%m-%d %H:%M:%S"
file_path = applications_config.base_path + applications_config.sub_path + applications_config.log_path
formatter = logging.Formatter(log_formatter, time_format)
if not os.path.exists(file_path):
os.makedirs(file_path)
log_file = os.path.join(f"{file_path}{applications_config.topic_name}.log")
temp_handler = RotatingFileHandler(log_file, maxBytes=1)
temp_handler.setFormatter(formatter)
__logger__.addHandler(temp_handler)
return __logger__
logger = get_logger()
from fastapi import FastAPI
from fastapi import APIRouter
from scripts.config import api_path_config
from scripts.config.applications_config import mqtt_host, port
from scripts.core.database.redis_db import conn_queue, conn_patient
from scripts.core.engine.models.patient_model import Patient
from paho.mqtt.client import Client
from scripts.core.handlers.check_patient import patient_data_check
from scripts.core.handlers.generating_patient_data import generate_patients
from scripts.core.handlers.deleting_doctor_queue import delete_queue
from scripts.core.handlers.doctors_number import set_no_doctors
from scripts.core.handlers.generate_publish import generate_patients_publish
from scripts.database.redis_db import conn_queue, conn_patient
# count the number of doctors for the day
doctors_counter = 0
app = FastAPI()
assign_doctor = APIRouter()
# generating the doctors for the day
@app.get(api_path_config.doctors_count)
@assign_doctor.get(api_path_config.doctors_count)
def get_doctors(no_doctors: int):
try:
global doctors_counter
doctors_counter += no_doctors
# entering the number of doctors in to the redis db
conn_queue.set("Doctors", no_doctors)
# creating the queue for doctors if it doesn't exist
for doctors in range(0, no_doctors):
if not conn_queue.exists(f'doctor{doctors}'):
conn_queue.set(f'doctor{doctors}', '')
else:
continue
doctors_counter += set_no_doctors(conn_queue, no_doctors)
except Exception as e:
print(e)
else:
......@@ -38,22 +26,11 @@ def get_doctors(no_doctors: int):
# closing the doctors for the day
@app.post(api_path_config.close_day)
@assign_doctor.post(api_path_config.close_day)
def get_doctors():
try:
global doctors_counter
# deleting the queue
for doctors in range(0, doctors_counter):
if not conn_queue.exists(f'doctor{doctors}'):
print(doctors)
else:
# delete doctors
conn_queue.delete(f'doctor{doctors}')
conn_queue.set('next_doc', 0)
conn_queue.delete("Doctors")
doctors_counter -= 1
continue
delete_queue(doctors_counter, conn_queue)
except Exception as e:
print(e)
else:
......@@ -61,7 +38,7 @@ def get_doctors():
# getting the producing the patient details for the consumer
@app.post(api_path_config.patient_details)
@assign_doctor.post(api_path_config.patient_details)
def patient_data(body: Patient):
try:
global doctors_counter
......@@ -69,20 +46,7 @@ def patient_data(body: Patient):
# getting the patient details from the user
patient_data_val = Patient(patient_id=body.patient_id, description=body.description,
age=body.age, name=body.name)
client = Client()
# creating the paho client to listen to the port
client.connect(mqtt_host, int(port))
# generating the patients
dict_data = generate_patients(patient_data_val)
# checking if the next doctor queue exists
if not conn_queue.exists('next_doc'):
conn_queue.set('next_doc', 0)
# checking the patient datas in the db for the queue
patient_data_check(conn_queue, conn_patient, client, dict_data, doctors_counter)
generate_patients_publish(mqtt_host, port, conn_queue, conn_patient, doctors_counter, patient_data_val)
except Exception as e:
print(e)
else:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment