Search in sources :

Example 6 with RiskAssessment

use of org.hl7.fhir.r4.model.RiskAssessment in project himss_2021_sepsis_detection by redhat-na-ssa.

the class DebeziumStreamListener method processMessage.

@KafkaListener(topics = "${listener.destination.debezium-stream}", containerFactory = "debeziumListenerContainerFactory")
public void processMessage(@Payload String cloudEvent, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic, @Header(KafkaHeaders.RECEIVED_PARTITION_ID) int partition, Acknowledgment ack) throws IOException {
    GZIPInputStream is = null;
    try {
        JsonNode rootNode = objectMapper.readTree(cloudEvent);
        JsonNode after = rootNode.get("data").get("payload").get("after");
        JsonNode resType = after.get("res_type");
        log.debug("processMessage() topic = " + topic + " : resType = " + resType.asText());
        if (FHIRUtil.PATIENT.equals(resType.asText())) {
            JsonNode resId = after.get("res_id");
            JsonNode resText = after.get("res_text");
            byte[] bytes = resText.binaryValue();
            is = new GZIPInputStream(new ByteArrayInputStream(bytes));
            String fhirJson = IOUtils.toString(is, "UTF-8");
            Patient patientObj = fhirCtx.newJsonParser().parseResource(Patient.class, fhirJson);
            patientObj.setId(resId.asText());
            log.info("processMessage() bytes length = " + bytes.length + " : fhir json = \n" + fhirJson + "\n fhir resourceType: " + patientObj.getResourceType().name() + " : patientId = " + resId);
            if (patientObj.getId() != null) {
                // Start Business Process
                fhirProcessMgmt.startProcess(patientObj);
            } else {
                log.error("processMessage() no res_id for patient: " + fhirJson);
            }
        } else if (FHIRUtil.RISK_ASSESSMENT.equals(resType.asText())) {
            JsonNode resId = after.get("res_id");
            JsonNode resText = after.get("res_text");
            byte[] bytes = resText.binaryValue();
            is = new GZIPInputStream(new ByteArrayInputStream(bytes));
            String fhirJson = IOUtils.toString(is, "UTF-8");
            RiskAssessment raObj = fhirCtx.newJsonParser().parseResource(RiskAssessment.class, fhirJson);
            fhirProcessMgmt.signalProcess(raObj);
        } else {
            log.warn("Will not process message with FHIR type: " + resType.asText());
        }
    } catch (Exception x) {
        log.error("Unable to process the following debezium stream event: \n" + cloudEvent);
        x.printStackTrace();
    } finally {
        ack.acknowledge();
        if (is != null)
            is.close();
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) RiskAssessment(org.hl7.fhir.r4.model.RiskAssessment) ByteArrayInputStream(java.io.ByteArrayInputStream) Patient(org.hl7.fhir.r4.model.Patient) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) KafkaListener(org.springframework.kafka.annotation.KafkaListener)

Example 7 with RiskAssessment

use of org.hl7.fhir.r4.model.RiskAssessment in project himss_2021_sepsis_detection by redhat-na-ssa.

the class RiskAssessmentWIH method executeWorkItem.

@Override
public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
    Map<String, Object> parameters = workItem.getParameters();
    Patient patient = (Patient) parameters.get(FHIRUtil.PATIENT);
    if (patient == null) {
        throw new RuntimeException("executeWorkItem() must pass value for " + FHIRUtil.PATIENT);
    }
    PatientVitals vitals = (PatientVitals) parameters.get(FHIRUtil.PATIENT_VITALS);
    if (vitals == null)
        throw new RuntimeException("executeWorkItem() must pass value for " + FHIRUtil.PATIENT_VITALS);
    String sepsisResponse = (String) parameters.get(FHIRUtil.SEPSIS_RESPONSE);
    if (sepsisResponse == null)
        throw new RuntimeException("executeWorkItem() must pass value for " + FHIRUtil.SEPSIS_RESPONSE);
    String correlationKey = runtimeService.getProcessInstanceById(workItem.getProcessInstanceId()).getCorrelationKey();
    log.info("executeWorkItem() will send generate RiskAssessment command regarding patientId = " + patient.getId() + " : correlationKey = " + correlationKey + " : sepsisResponse = " + sepsisResponse + " : obsId = " + vitals.getObservationId());
    try {
        String patientPayload = fhirCtx.newJsonParser().setPrettyPrint(false).encodeResourceToString(patient);
        ObjectNode rootNode = objectMapper.createObjectNode();
        rootNode.put(FHIRUtil.PATIENT, patientPayload);
        rootNode.put(FHIRUtil.SEPSIS_RESPONSE, sepsisResponse);
        rootNode.put(FHIRUtil.OBSERVATION_ID, vitals.getObservationId());
        rootNode.put(FHIRUtil.CORRELATION_KEY, correlationKey);
        String cloudEventPayload = objectMapper.writeValueAsString(rootNode);
        CloudEvent cloudEvent = CloudEventBuilder.v1().withId(correlationKey).withSource(URI.create("")).withType(FHIRUtil.GENERATE_RISK_ASSESSMENT).withTime(OffsetDateTime.now()).withData(cloudEventPayload.getBytes()).build();
        producer.send(this.generateRiskAssessmentCommandDestination, cloudEvent);
    } catch (Exception x) {
        x.printStackTrace();
        throw new RuntimeException(x);
    }
    manager.completeWorkItem(workItem.getId(), workItem.getParameters());
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) PatientVitals(com.redhat.naps.process.model.PatientVitals) Patient(org.hl7.fhir.r4.model.Patient) CloudEvent(io.cloudevents.CloudEvent)

Example 8 with RiskAssessment

use of org.hl7.fhir.r4.model.RiskAssessment in project himss_2021_sepsis_detection by redhat-na-ssa.

the class RiskAssessmentService method publishRiskAssessment.

public void publishRiskAssessment(Patient patient, String sepsisResponse, String observationId, String correlationKey) throws JsonProcessingException {
    log.info("createRiskAssessment() patient = " + patient.getId() + " : sepsisResponse = " + sepsisResponse + " : obsId = " + observationId);
    String uid = UUID.randomUUID().toString();
    RiskAssessment assessment = createRiskAssessment(patient, sepsisResponse, observationId, correlationKey);
    String cEventString = generateCloudEventJson(uid, assessment, RiskAssessmentUtils.MESSAGE_TYPE_EVENT);
    Message<String> record = KafkaRecord.of(uid, cEventString);
    eventChannel.send(record);
    eventBus.send(RiskAssessmentUtils.POST_TO_FHIR_SERVER, assessment);
// postRiskAssessmentToFhirServer(assessment);
}
Also used : RiskAssessment(org.hl7.fhir.r4.model.RiskAssessment)

Example 9 with RiskAssessment

use of org.hl7.fhir.r4.model.RiskAssessment in project himss_2021_sepsis_detection by redhat-na-ssa.

the class CloudEventTest method kafkaCloudEventTest.

// TO-DO:  Need to switch to Binary encoding
@Disabled
@Test
public void kafkaCloudEventTest() throws JsonProcessingException {
    // Send Patient as CloudEvent
    Patient patient = createPatient();
    String uid = UUID.randomUUID().toString();
    String cEventString = generateCloudEventJson(uid, patient, RiskAssessmentUtils.MESSAGE_TYPE_COMMAND);
    InMemorySource<Message<String>> generateRACommandSource = connector.source(RiskAssessmentUtils.COMMAND_CHANNEL);
    /*  By default, this test class will send message with the following metadata type:
         *      io.smallrye.reactive.messaging.kafka.OutgoingKafkaRecordMetadata
         * 
         *  To be consistent with metadata types in production, need to switch to use of:
         *      io.smallrye.reactive.messaging.kafka.impl.ce.DefaultIncomingKafkaCloudEventMetadata
         * 
         *  TO-DO:  The following is being coded because during testing it doesn't appear that "cloud-event=true" on the channels has any affect.
         */
    IncomingCloudEventMetadata<String> cloudEventMetadata = new DefaultIncomingCloudEventMetadata<>(builder.withId("id").withSource(URI.create("test://cloud.event")).withType(RiskAssessmentUtils.MESSAGE_TYPE_COMMAND).withTimestamp(OffsetDateTime.now().toZonedDateTime()).build());
    DefaultIncomingKafkaCloudEventMetadata kafkaCloudEventMetadata = new DefaultIncomingKafkaCloudEventMetadata<>(cloudEventMetadata);
    Message<String> record = KafkaRecord.of(uid, cEventString).addMetadata(kafkaCloudEventMetadata);
    generateRACommandSource.send(record);
    // Consume CloudEvent with RiskAssessment
    InMemorySink consumeRASink = connector.sink(RiskAssessmentUtils.EVENT_CHANNEL);
    await().atMost(Duration.ofSeconds(15)).<List<? extends Message<String>>>until(consumeRASink::received, t -> t.size() == 1);
}
Also used : DefaultIncomingKafkaCloudEventMetadata(io.smallrye.reactive.messaging.kafka.impl.ce.DefaultIncomingKafkaCloudEventMetadata) Message(org.eclipse.microprofile.reactive.messaging.Message) DefaultIncomingCloudEventMetadata(io.smallrye.reactive.messaging.ce.impl.DefaultIncomingCloudEventMetadata) Patient(org.hl7.fhir.r4.model.Patient) InMemorySink(io.smallrye.reactive.messaging.connectors.InMemorySink) List(java.util.List) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 10 with RiskAssessment

use of org.hl7.fhir.r4.model.RiskAssessment in project himss_2021_sepsis_detection by redhat-na-ssa.

the class FhirServerTest method riskAssessmentPredictionTest.

@Test
public void riskAssessmentPredictionTest() throws IOException {
    String filePath = "/fhir/RiskAssessment.json";
    InputStream fStream = null;
    String oJson = null;
    try {
        fStream = this.getClass().getResourceAsStream(filePath);
        if (fStream != null) {
            oJson = IOUtils.toString(fStream, "UTF-8");
            RiskAssessment rAssessment = (RiskAssessment) fhirCtx.newJsonParser().parseResource(oJson);
            RiskAssessmentPredictionComponent raPredictionComponent = rAssessment.getPredictionFirstRep();
            Property cProp = raPredictionComponent.getOutcome().getChildByName("coding");
            Coding coding = (Coding) cProp.getValues().get(0);
            String code = coding.getCode();
            log.info("riskAssessmentPredictionTest() code = " + code);
        } else {
            log.error("riskAssessmentTest() resource not found: " + filePath);
            return;
        }
    } finally {
        if (fStream != null)
            fStream.close();
    }
}
Also used : RiskAssessment(org.hl7.fhir.r4.model.RiskAssessment) RiskAssessmentPredictionComponent(org.hl7.fhir.r4.model.RiskAssessment.RiskAssessmentPredictionComponent) Coding(org.hl7.fhir.r4.model.Coding) InputStream(java.io.InputStream) Property(org.hl7.fhir.r4.model.Property) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)7 Turtle (org.hl7.fhir.dstu3.utils.formats.Turtle)4 RiskAssessment (org.hl7.fhir.r4.model.RiskAssessment)4 Coding (org.hl7.fhir.r4.model.Coding)3 Patient (org.hl7.fhir.r4.model.Patient)3 RiskAssessmentPredictionComponent (org.hl7.fhir.r4.model.RiskAssessment.RiskAssessmentPredictionComponent)3 QuarkusTest (io.quarkus.test.junit.QuarkusTest)2 Complex (org.hl7.fhir.dstu2016may.formats.RdfGenerator.Complex)2 Complex (org.hl7.fhir.dstu3.utils.formats.Turtle.Complex)2 Property (org.hl7.fhir.r4.model.Property)2 Complex (org.hl7.fhir.r4.utils.formats.Turtle.Complex)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 PatientVitals (com.redhat.naps.process.model.PatientVitals)1 CloudEvent (io.cloudevents.CloudEvent)1 DefaultIncomingCloudEventMetadata (io.smallrye.reactive.messaging.ce.impl.DefaultIncomingCloudEventMetadata)1 InMemorySink (io.smallrye.reactive.messaging.connectors.InMemorySink)1 DefaultIncomingKafkaCloudEventMetadata (io.smallrye.reactive.messaging.kafka.impl.ce.DefaultIncomingKafkaCloudEventMetadata)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1