Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

15153: update FHIRConvert to create empty report/lineages for items with no next action #15224

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 84 additions & 56 deletions prime-router/src/main/kotlin/fhirengine/engine/FHIRConverter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,13 @@ class FHIRConverter(
// TODO: https://github.com/CDCgov/prime-reportstream/issues/14287
FhirPathUtils

val fhirBundles = process(format, queueMessage, actionLogger)
val processedItems = process(format, queueMessage, actionLogger)

if (fhirBundles.isNotEmpty()) {
// processedItems can be empty in three scenarios:
// - the blob had no contents, i.e. an empty file was submitted
// - the format is HL7 and the contents were not parseable, so the number of items is unknown
mkalish marked this conversation as resolved.
Show resolved Hide resolved
// - the format is unexpected like CSV
if (processedItems.isNotEmpty()) {
return LogMeasuredTime.measureAndLogDurationWithReturnedValue(
"Applied sender transform and routed"
) {
Expand All @@ -137,68 +141,92 @@ class FHIRConverter(
)

maybeParallelize(
fhirBundles.size,
Streams.mapWithIndex(fhirBundles.stream()) { bundle, index ->
processedItems.size,
Streams.mapWithIndex(processedItems.stream()) { bundle, index ->
adegolier marked this conversation as resolved.
Show resolved Hide resolved
Pair(bundle, index)
},
"Applying sender transforms and routing"
).map { (bundle, bundleIndex) ->
).map { (processedItem, itemIndex) ->
// conduct FHIR Transform
transformer?.process(bundle)

// make a 'report'
val report = Report(
MimeFormat.FHIR,
emptyList(),
parentItemLineageData = listOf(
Report.ParentItemLineageData(queueMessage.reportId, bundleIndex.toInt() + 1)
),
metadata = this.metadata,
topic = queueMessage.topic,
nextAction = TaskAction.destination_filter
)
if (processedItem.bundle == null) {
val report = Report(
MimeFormat.FHIR,
emptyList(),
parentItemLineageData = listOf(
Report.ParentItemLineageData(queueMessage.reportId, itemIndex.toInt() + 1)
),
metadata = this.metadata,
topic = queueMessage.topic,
nextAction = TaskAction.none
)
val noneEvent = ProcessEvent(
Event.EventAction.NONE,
report.id,
Options.None,
emptyMap(),
emptyList()
)
actionHistory.trackCreatedReport(noneEvent, report)
null
} else {
// We know from the null check above that this cannot be null
val bundle = processedItem.bundle!!
transformer?.process(bundle)

// make a 'report'
val report = Report(
MimeFormat.FHIR,
emptyList(),
parentItemLineageData = listOf(
Report.ParentItemLineageData(queueMessage.reportId, itemIndex.toInt() + 1)
mkalish marked this conversation as resolved.
Show resolved Hide resolved
),
metadata = this.metadata,
topic = queueMessage.topic,
nextAction = TaskAction.destination_filter
)

// create route event
val routeEvent = ProcessEvent(
Event.EventAction.DESTINATION_FILTER,
report.id,
Options.None,
emptyMap(),
emptyList()
)
// create route event
val routeEvent = ProcessEvent(
Event.EventAction.DESTINATION_FILTER,
report.id,
Options.None,
emptyMap(),
emptyList()
)

// upload to blobstore
val bodyBytes = FhirTranscoder.encode(bundle).toByteArray()
val blobInfo = BlobAccess.uploadBody(
MimeFormat.FHIR,
bodyBytes,
report.id.toString(),
queueMessage.blobSubFolderName,
routeEvent.eventAction
)
// upload to blobstore
val bodyBytes = FhirTranscoder.encode(bundle).toByteArray()
val blobInfo = BlobAccess.uploadBody(
MimeFormat.FHIR,
bodyBytes,
report.id.toString(),
queueMessage.blobSubFolderName,
routeEvent.eventAction
)

// track created report
actionHistory.trackCreatedReport(routeEvent, report, blobInfo = blobInfo)
azureEventService.trackEvent(
ReportCreatedEvent(
report.id,
queueMessage.topic
// track created report
actionHistory.trackCreatedReport(routeEvent, report, blobInfo = blobInfo)
azureEventService.trackEvent(
ReportCreatedEvent(
report.id,
queueMessage.topic
)
)
)

FHIREngineRunResult(
routeEvent,
report,
blobInfo.blobUrl,
FhirDestinationFilterQueueMessage(
report.id,
FHIREngineRunResult(
routeEvent,
report,
blobInfo.blobUrl,
BlobAccess.digestToString(blobInfo.digest),
queueMessage.blobSubFolderName,
queueMessage.topic
FhirDestinationFilterQueueMessage(
report.id,
blobInfo.blobUrl,
BlobAccess.digestToString(blobInfo.digest),
queueMessage.blobSubFolderName,
queueMessage.topic
)
)
)
}.collect(Collectors.toList())
}
}.collect(Collectors.toList()).filterNotNull()
}
} else {
val nextEvent = ProcessEvent(
Expand Down Expand Up @@ -264,7 +292,7 @@ class FHIRConverter(
queueMessage: ReportPipelineMessage,
actionLogger: ActionLogger,
routeReportWithInvalidItems: Boolean = true,
): List<Bundle> {
): List<IProcessedItem<*>> {
val validator = queueMessage.topic.validator
val rawReport = queueMessage.downloadContent()
return if (rawReport.isBlank()) {
Expand Down Expand Up @@ -309,7 +337,7 @@ class FHIRConverter(
}

val areAllItemsParsedAndValid = processedItems.all { it.getError() == null }
val bundles = processedItems.mapNotNull { item ->
val bundles = processedItems.map { item ->
val error = item.getError()
if (error != null) {
actionLogger.getItemLogger(error.index + 1, item.getTrackingId()).error(error)
Expand All @@ -321,7 +349,7 @@ class FHIRConverter(
.warn(this)
}
}
item.bundle
item
}

withLoggingContext(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,9 @@ class FHIRConverterIntegrationTests {
fhirFunctions.doConvert(queueMessage, 1, createFHIRConverter())

ReportStreamTestDatabaseContainer.testDatabaseAccess.transact { txn ->
val routedReports = fetchChildReports(receiveReport, txn, 2)
val (routedReports, _) = fetchChildReports(
receiveReport, txn, 4
).partition { it.nextAction != TaskAction.none }
// Verify that the expected FHIR bundles were uploaded
val reportAndBundles =
routedReports.map {
Expand Down Expand Up @@ -346,7 +348,9 @@ class FHIRConverterIntegrationTests {
fhirFunctions.doConvert(queueMessage, 1, createFHIRConverter())

ReportStreamTestDatabaseContainer.testDatabaseAccess.transact { txn ->
val routedReports = fetchChildReports(receiveReport, txn, 2)
val (routedReports, _) = fetchChildReports(
receiveReport, txn, 4
).partition { it.nextAction != TaskAction.none }
// Verify that the expected FHIR bundles were uploaded
val reportAndBundles =
routedReports.map {
Expand Down Expand Up @@ -425,7 +429,9 @@ class FHIRConverterIntegrationTests {
fhirFunctions.doConvert(queueMessage, 1, createFHIRConverter())

ReportStreamTestDatabaseContainer.testDatabaseAccess.transact { txn ->
val routedReports = fetchChildReports(receiveReport, txn, 1)
val (routedReports, _) = fetchChildReports(
receiveReport, txn, 2
).partition { it.nextAction != TaskAction.none }
// Verify that the expected FHIR bundles were uploaded
val reportAndBundles =
routedReports.map {
Expand Down
Loading
Loading