Skip to content

Commit

Permalink
Fix redundant count of related resources
Browse files Browse the repository at this point in the history
Signed-off-by: Elly Kitoto <[email protected]>
  • Loading branch information
ellykits committed Nov 26, 2024
1 parent 557e024 commit 58f1d66
Showing 1 changed file with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -940,12 +940,6 @@ constructor(
reverseIncludes
.groupBy { "${it.resource.name}_${it.searchParameter}".lowercase() }
.mapValues { it.value.first() }
val includedResourcesCountConfigs =
relatedResourceConfigs
.asSequence()
.flatMap { it.relatedResources }
.filter { it.resultAsCount && !it.searchParameter.isNullOrEmpty() }
.toList()

searchResults.forEach { searchResult: SearchResult<Resource> ->
// Create new repository data if none exist (subsequent queries will have repository data)
Expand All @@ -961,38 +955,38 @@ constructor(

searchResult.included?.forEach { entry ->
// Add the forward included resources to the relatedResourcesMap
val relatedResourceConfig = forwardIncludesMap[entry.key]
val fwdIncludedResourceConfig = forwardIncludesMap[entry.key]
updateRepositoryResourceData(
resources = entry.value,
relatedResourceConfig = relatedResourceConfig,
relatedResourceConfig = fwdIncludedResourceConfig,
repositoryResourceData = repositoryResourceData,
relatedResourcesQueue = relatedResourcesQueue,
)
if (entry.value.isNotEmpty()) {
if (entry.value.isNotEmpty() && fwdIncludedResourceConfig != null) {
handleCountResults(
resources = entry.value,
repositoryResourceData = repositoryResourceData,
countConfigs = includedResourcesCountConfigs,
countConfigs = extractCountConfigs(fwdIncludedResourceConfig),
configComputedRuleValues = configComputedRuleValues,
)
}
}
searchResult.revIncluded?.forEach { entry ->
val (resourceType, searchParam) = entry.key
val name = "${resourceType.name}_$searchParam".lowercase()
val relatedResourceConfig = reverseIncludesMap[name]
val revIncludedResourceConfig = reverseIncludesMap[name]
// Add the reverse included resources to the relatedResourcesMap
updateRepositoryResourceData(
resources = entry.value,
relatedResourceConfig = relatedResourceConfig,
relatedResourceConfig = revIncludedResourceConfig,
repositoryResourceData = repositoryResourceData,
relatedResourcesQueue = relatedResourcesQueue,
)
if (entry.value.isNotEmpty()) {
if (entry.value.isNotEmpty() && revIncludedResourceConfig != null) {
handleCountResults(
resources = entry.value,
repositoryResourceData = repositoryResourceData,
countConfigs = includedResourcesCountConfigs,
countConfigs = extractCountConfigs(revIncludedResourceConfig),
configComputedRuleValues = configComputedRuleValues,
)
}
Expand All @@ -1007,6 +1001,11 @@ constructor(
return groupAndBatchQueriedResources(relatedResourcesQueue)
}

private fun extractCountConfigs(relatedResourceConfig: ResourceConfig) =
relatedResourceConfig.relatedResources
.filter { it.resultAsCount && !it.searchParameter.isNullOrEmpty() }
.toList()

private fun updateRepositoryResourceData(
resources: List<Resource>,
relatedResourceConfig: ResourceConfig?,
Expand Down

0 comments on commit 58f1d66

Please sign in to comment.