Skip to content

Commit

Permalink
Fix loading related resources
Browse files Browse the repository at this point in the history
This fix ensures all the nested related resources are loaded too.

Signed-off-by: Elly Kitoto <junkmailstoelly@gmail.com>
  • Loading branch information
ellykits committed Sep 16, 2024
1 parent b232cce commit 398c987
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ constructor(
val relatedResourcesQueue = ArrayDeque<Pair<Resource, List<ResourceConfig>?>>().apply {
addFirst(Pair(resource, relatedResourcesConfigs))
}
while (!relatedResourcesQueue.isEmpty()) {
while (relatedResourcesQueue.isNotEmpty()) {
val (currentResource, currentRelatedResourceConfigs) = relatedResourcesQueue.removeFirst()
val relatedResourceCountConfigs = currentRelatedResourceConfigs
?.asSequence()
Expand Down Expand Up @@ -525,7 +525,7 @@ constructor(
}

val searchResults = searchIncludedResources(
relatedResourcesConfigs = relatedResourcesConfigs,
relatedResourcesConfigs = currentRelatedResourceConfigs,
resource = currentResource,
configComputedRuleValues = configComputedRuleValues
)
Expand Down Expand Up @@ -579,7 +579,7 @@ constructor(
if (!id.isNullOrBlank()) {
relatedResourceWrapper.relatedResourceMap[id] =
relatedResourceWrapper.relatedResourceMap.getOrPut(id) { mutableListOf() }.apply {
addAll(value)
addAll(value.distinctBy { it.logicalId })
}
value.forEach { resource ->
with(resourceConfigs?.relatedResources) {
Expand All @@ -601,7 +601,7 @@ constructor(
},
): Long =
kotlin
.runCatching { withContext(dispatcherProvider.io()) { fhirEngine.count(this@count) } }
.runCatching { fhirEngine.count(this@count) }
.onSuccess { count -> onSuccess(count) }
.onFailure { throwable -> onFailure(throwable) }
.getOrDefault(0)
Expand Down Expand Up @@ -630,13 +630,12 @@ constructor(
relatedResourcesConfigs?.revIncludeRelatedResourceConfigs(true)

search.apply {
val thisSearch = this
reverseIncludeResourceConfigs?.forEach { resourceConfig ->
revInclude(
resourceConfig.resource,
ReferenceClientParam(resourceConfig.searchParameter),
) {
thisSearch.applyConfiguredSortAndFilters(
(this as Search).applyConfiguredSortAndFilters(
resourceConfig = resourceConfig,
sortData = true,
configComputedRuleValues = configComputedRuleValues,
Expand All @@ -649,7 +648,7 @@ constructor(
resourceConfig.resource,
ReferenceClientParam(resourceConfig.searchParameter),
) {
thisSearch.applyConfiguredSortAndFilters(
(this as Search).applyConfiguredSortAndFilters(
resourceConfig = resourceConfig,
sortData = true,
configComputedRuleValues = configComputedRuleValues,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,73 +97,79 @@ constructor(
fhirResourceConfig: FhirResourceConfig?,
paramsMap: Map<String, String>?,
): Long {
val registerConfiguration = retrieveRegisterConfiguration(registerId, paramsMap)
val fhirResource = fhirResourceConfig ?: registerConfiguration.fhirResource
val baseResourceConfig = fhirResource.baseResource
val configComputedRuleValues = registerConfiguration.configRules.configRulesComputedValues()
val filterByRelatedEntityLocation = registerConfiguration.filterDataByRelatedEntityLocation
val filterActiveResources = registerConfiguration.activeResourceFilters
if (filterByRelatedEntityLocation) {
val syncLocationIds = context.retrieveRelatedEntitySyncLocationIds()
val locationIds =
syncLocationIds
.map { retrieveFlattenedSubLocations(it).map { subLocation -> subLocation.logicalId }}
.asSequence()
.flatten()
.toHashSet()
val countSearch =
Search(baseResourceConfig.resource).apply {
applyConfiguredSortAndFilters(
resourceConfig = baseResourceConfig,
sortData = false,
filterActiveResources = filterActiveResources,
configComputedRuleValues = configComputedRuleValues,
)
}
val totalCount = fhirEngine.count(countSearch)
var searchResultsCount = 0L
var pageNumber = 0
var count = 0
while (count < totalCount) {
val baseResourceSearch =
createSearch(
baseResourceConfig = baseResourceConfig,
filterActiveResources = filterActiveResources,
configComputedRuleValues = configComputedRuleValues,
currentPage = pageNumber,
count = COUNT,
)
searchResultsCount += fhirEngine.search<Resource>(baseResourceSearch)
.asSequence()
.map { it.resource }
.filter { resource ->
when (resource.resourceType) {
ResourceType.Location -> locationIds.contains(resource.logicalId)
else -> resource.meta.tag.any {
it.system == context.getString(R.string.sync_strategy_related_entity_location_system)
&& locationIds.contains(it.code)
return withContext(dispatcherProvider.io()) {
val registerConfiguration = retrieveRegisterConfiguration(registerId, paramsMap)
val fhirResource = fhirResourceConfig ?: registerConfiguration.fhirResource
val baseResourceConfig = fhirResource.baseResource
val configComputedRuleValues = registerConfiguration.configRules.configRulesComputedValues()
val filterByRelatedEntityLocation = registerConfiguration.filterDataByRelatedEntityLocation
val filterActiveResources = registerConfiguration.activeResourceFilters
if (filterByRelatedEntityLocation) {
val syncLocationIds = context.retrieveRelatedEntitySyncLocationIds()
val locationIds =
syncLocationIds
.map { retrieveFlattenedSubLocations(it).map { subLocation -> subLocation.logicalId } }
.asSequence()
.flatten()
.toHashSet()
val countSearch =
Search(baseResourceConfig.resource).apply {
applyConfiguredSortAndFilters(
resourceConfig = baseResourceConfig,
sortData = false,
filterActiveResources = filterActiveResources,
configComputedRuleValues = configComputedRuleValues,
)
}
val totalCount = fhirEngine.count(countSearch)
var searchResultsCount = 0L
var pageNumber = 0
var count = 0
while (count < totalCount) {
val baseResourceSearch =
createSearch(
baseResourceConfig = baseResourceConfig,
filterActiveResources = filterActiveResources,
configComputedRuleValues = configComputedRuleValues,
currentPage = pageNumber,
count = COUNT,
)
searchResultsCount += fhirEngine.search<Resource>(baseResourceSearch)
.asSequence()
.map { it.resource }
.filter { resource ->
when (resource.resourceType) {
ResourceType.Location -> locationIds.contains(resource.logicalId)
else -> resource.meta.tag.any {
it.system == context.getString(R.string.sync_strategy_related_entity_location_system)
&& locationIds.contains(it.code)
}
}
}.count().toLong()
count += COUNT
pageNumber++
}
searchResultsCount
} else {
val search =
Search(baseResourceConfig.resource).apply {
applyConfiguredSortAndFilters(
resourceConfig = baseResourceConfig,
sortData = false,
filterActiveResources = registerConfiguration.activeResourceFilters,
configComputedRuleValues = configComputedRuleValues,
)
}
}.count().toLong()
count += COUNT
pageNumber++
}
return searchResultsCount
}
val search =
Search(baseResourceConfig.resource).apply {
applyConfiguredSortAndFilters(
resourceConfig = baseResourceConfig,
sortData = false,
filterActiveResources = registerConfiguration.activeResourceFilters,
configComputedRuleValues = configComputedRuleValues,
search.count(
onFailure = {
Timber.e(
it,
"Error counting register data for register id: ${registerConfiguration.id}"
)
},
)
}
return search.count(
onFailure = {
Timber.e(it, "Error counting register data for register id: ${registerConfiguration.id}")
},
)
}
}

override suspend fun loadProfileData(
Expand Down

0 comments on commit 398c987

Please sign in to comment.