Beispiel #1
0
        public static async Task <object> Run([HttpTrigger("post")] HttpRequestMessage req, TraceWriter log)
        {
            log.Info($"{nameof(PatientAdmissionFunc)} triggered on {DateTime.UtcNow}");

            using (var patientTelemetry =
                       new PatientTelemetry(Environment.GetEnvironmentVariable(CommonConstants.InstumentationKey,
                                                                               EnvironmentVariableTarget.Process),
                                            nameof(PatientAdmissionFunc)))
            {
                try
                {
                    var userIdentity  = req.GetRequestContext().Principal.Identity as ClaimsIdentity;
                    var authorization = new ADAuthorization(userIdentity);

                    patientTelemetry.Caller(authorization.GetCallerName());

                    if (false == await authorization.Authorize(CommonConstants.CareLineManagerRole))
                    {
                        patientTelemetry.Unauthorized();
                        return(req.CreateResponse(HttpStatusCode.Unauthorized, new
                        {
                            result = $"Unauthorized {HttpStatusCode.Unauthorized}"
                        }));
                    }

                    // Get request data
                    dynamic data = await req.Content.ReadAsAsync <object>();

                    if (data == null)
                    {
                        return(req.CreateResponse(HttpStatusCode.BadRequest, new
                        {
                            result = "bad request"
                        }));
                    }

                    // convert the input to the corresponding objects
                    InPatient          inPatient    = data.patient.ToObject <InPatient>();
                    Encounter          encounter    = data.encounter.ToObject <Encounter>();
                    List <Observation> observations = data.observations.ToObject <List <Observation> >();
                    List <Condition>   conditions   = data.conditions.ToObject <List <Condition> >();

                    //get resource from key vault
                    var appResources = await Settings.GetAppResources();

                    new SqlEncryptionHelper();

                    //get patient data from FHIR data
                    var patient = FHIRMapping.PopulatePatientData(inPatient, encounter, observations, conditions);

                    try
                    {
                        var predictLengthOfStayService =
                            new PredictLengthOfStayService(appResources.PredictLengthOfStayServiceEndpoint,
                                                           appResources.PredictLengthOfStayServiceApiKey);

                        patientTelemetry.DependencyStarted();
                        patient.PredictedLengthOfStay = await predictLengthOfStayService.PredictLengthOfStay(patient);

                        patientTelemetry.DependencyCompleted(nameof(PredictLengthOfStayService),
                                                             "PredictLengthOfStay", true);
                    }
                    catch (PredictLengthOfStayServiceException ex)
                    {
                        patientTelemetry.DependencyCompleted(nameof(PredictLengthOfStayService),
                                                             "PredictLengthOfStay", false);
                        patientTelemetry.Error(ex);
                    }

                    var hospital = new Hospital(appResources.PatientDbConnectionString);
                    var id       = await hospital.AdmitPatient(patient);

                    if (id == -1)
                    {
                        return(req.CreateResponse(HttpStatusCode.OK, new
                        {
                            result = "invalid encounter id"
                        }));
                    }

                    var existingPatient = await hospital.GetPatient(id);

                    patientTelemetry.Success();
                    return(req.CreateResponse(HttpStatusCode.OK, new
                    {
                        encounterId = existingPatient.Eid,
                        name = $"{existingPatient.FirstName} {existingPatient.MiddleName} {existingPatient.LastName}",
                        gender = existingPatient.Gender,
                        admittedOn = existingPatient.Vdate
                    }));
                }
                catch (AuthorizationException ex)
                {
                    patientTelemetry.Error(ex);

                    return(req.CreateResponse(HttpStatusCode.ExpectationFailed, new
                    {
                        error = ex.Message
                    }));
                }
                catch (Exception ex)
                {
                    patientTelemetry.Error(ex);

                    return(req.CreateResponse(HttpStatusCode.InternalServerError, new
                    {
                        result = "failed"
                    }));
                }
            }
        }
Beispiel #2
0
        public static async Task <object> Run([HttpTrigger(WebHookType = "genericJson")] HttpRequestMessage req, TraceWriter log)
        {
            log.Info($"{nameof(PatientDataBulkImportFunc)} triggered on {DateTime.UtcNow}");

            using (var patientTelemetry = new PatientTelemetry(
                       Environment.GetEnvironmentVariable(CommonConstants.InstumentationKey, EnvironmentVariableTarget.Process),
                       nameof(PatientDataBulkImportFunc)))
            {
                try
                {
                    string jsonContent = await req.Content.ReadAsStringAsync();

                    StorageEvent[] message            = JsonConvert.DeserializeObject <StorageEvent[]>(jsonContent);
                    string         patientDataBlobUrl = message[0].Data.url;

                    patientTelemetry.Request(nameof(PatientDataBulkImportFunc));

                    //filter event type
                    if (message[0].EventType != Events.BlobCreatedEvent)
                    {
                        patientTelemetry.Unsupported();

                        return(req.CreateResponse(HttpStatusCode.OK, new
                        {
                            result = $"cannot process event {message[0].EventType}"
                        }));
                    }

                    //get resouces from key vault
                    var appResources = await Settings.GetAppResources();

                    new SqlEncryptionHelper();

                    patientTelemetry.IngestionStarted();

                    var patientDataIngestion = new PatientDataIngestion(appResources.StorageAccessKey,
                                                                        appResources.PatientDbConnectionString, appResources.PatientDataTableName);

                    var totalRowsProcessed = await patientDataIngestion.Process(patientDataBlobUrl);

                    var totalRecordsStored = await patientDataIngestion.GetTotalPatientRecordsStored();

                    patientTelemetry.IngestionCompleted();

                    patientTelemetry.Metrics(
                        new Dictionary <string, double> {
                        { Metrics.RowsProcessed, totalRowsProcessed },
                        { Metrics.RowsStored, totalRecordsStored }
                    });

                    patientTelemetry.Success();
                    return(req.CreateResponse(HttpStatusCode.OK, new
                    {
                        result = "success"
                    }));
                }
                catch (Exception ex)
                {
                    patientTelemetry.Error(ex);

                    return(req.CreateResponse(HttpStatusCode.InternalServerError, new
                    {
                        result = "failed"
                    }));
                }
            }
        }
Beispiel #3
0
        public static async Task <object> Run([HttpTrigger("put")] HttpRequestMessage req, TraceWriter log)
        {
            log.Info($"{nameof(PatientDischargeFunc)} triggered on {DateTime.UtcNow}");

            using (var patientTelemetry =
                       new PatientTelemetry(
                           Environment.GetEnvironmentVariable(CommonConstants.InstumentationKey, EnvironmentVariableTarget.Process),
                           nameof(PatientDischargeFunc)))
            {
                try
                {
                    //authorize user
                    var userIdentity  = req.GetRequestContext().Principal.Identity as ClaimsIdentity;
                    var authorization = new ADAuthorization(userIdentity);

                    patientTelemetry.Caller(authorization.GetCallerName());

                    if (false == await authorization.Authorize(CommonConstants.CareLineManagerRole))
                    {
                        patientTelemetry.Unauthorized();
                        return(req.CreateResponse(HttpStatusCode.Unauthorized, new
                        {
                            result = $"Unauthorized {HttpStatusCode.Unauthorized}"
                        }));
                    }

                    // Get request data
                    dynamic data = await req.Content.ReadAsAsync <object>();

                    if (data == null)
                    {
                        return(req.CreateResponse(HttpStatusCode.BadRequest, new
                        {
                            result = "bad request"
                        }));
                    }

                    // convert the input to the corresponding objects
                    InPatient inPatient  = data.patient.ToObject <InPatient>();
                    Encounter encounter  = data.encounter.ToObject <Encounter>();
                    int       patientEid = encounter.Id;

                    //get resources from key vault
                    var appResources = await Settings.GetAppResources();

                    new SqlEncryptionHelper();

                    patientTelemetry.DischargingPatient();

                    var hospital = new Hospital(appResources.PatientDbConnectionString);
                    var id       = await hospital.DischargePatient(patientEid, encounter.Period.End);

                    if (id == -1)
                    {
                        return(req.CreateResponse(HttpStatusCode.OK, new
                        {
                            error = "invalid encounter id"
                        }));
                    }

                    var existingPatient = await hospital.GetPatient(id);

                    patientTelemetry.PatientDischarged();

                    patientTelemetry.Success();
                    return(req.CreateResponse(HttpStatusCode.OK, new
                    {
                        encounterId = existingPatient.Eid,
                        name = $"{existingPatient.FirstName} {existingPatient.MiddleName} {existingPatient.LastName}",
                        dischargedOn = existingPatient.Discharged,
                        daysStayed = existingPatient.Lengthofstay
                    }));
                }
                catch (AuthorizationException ex)
                {
                    patientTelemetry.Error(ex);

                    return(req.CreateResponse(HttpStatusCode.ExpectationFailed, new
                    {
                        ex.Message
                    }));
                }
                catch (Exception ex)
                {
                    patientTelemetry.Error(ex);
                    return(req.CreateResponse(HttpStatusCode.InternalServerError, new
                    {
                        result = "failed"
                    }));
                }
            }
        }