Skip to content

Commit

Permalink
fix: [ANDROAPP-6398] Tei dashbard takes too long to load if option se…
Browse files Browse the repository at this point in the history
…t is too big (#3851)

* fix: [ANDROAPP-6398] Tei dashbard takes too long to load if option set is too big

Signed-off-by: Pablo <[email protected]>

* fix sonarcloud warnings

Signed-off-by: Pablo <[email protected]>

* remove unused code

* fix unit test

---------

Signed-off-by: Pablo <[email protected]>
  • Loading branch information
Balcan authored Nov 13, 2024
1 parent 0dfdaee commit fa7bd8e
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class TEIDataFragment : FragmentGlobalAbstract(), TEIDataContracts.View {
return FragmentTeiDataBinding.inflate(inflater, container, false).also { binding ->
this.binding = binding
dashboardViewModel.groupByStage.observe(viewLifecycleOwner) { group ->
showLoadingProgress(false)
showLoadingProgress(true)
presenter.onGroupingChanged(group)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ class TeiDataRepositoryImpl(
override fun displayOrganisationUnit(programUid: String): Boolean {
return d2.organisationUnitModule().organisationUnits()
.byProgramUids(listOf(programUid))
.blockingGet().size > 1
.blockingCount() > 1
}

override fun enrollmentOrgUnitInCaptureScope(enrollmentOrgUnit: String): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ class TroubleshootingRepository(
.blockingGet().toRuleVariableList(
d2.trackedEntityModule().trackedEntityAttributes(),
d2.dataElementModule().dataElements(),
d2.optionModule().options(),
).mapNotNull {
val ruleValueType = it.fieldType
val valueKey = when (it) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@ fun List<ProgramRuleAction>.toRuleActionList(): List<RuleAction> {
fun List<ProgramRuleVariable>.toRuleVariableList(
attributeRepository: TrackedEntityAttributeCollectionRepository,
dataElementRepository: DataElementCollectionRepository,
optionRepository: OptionCollectionRepository,
): List<RuleVariable> {
return filter {
when {
return mapNotNull {
val allowVariable = when {
it.dataElement() != null -> {
dataElementRepository.uid(it.dataElement()?.uid()).blockingExists()
}
Expand All @@ -75,8 +74,11 @@ fun List<ProgramRuleVariable>.toRuleVariableList(

else -> isCalculatedValue(it)
}
}.map {
it.toRuleVariable(attributeRepository, dataElementRepository, optionRepository)
if (allowVariable) {
it.toRuleVariable(attributeRepository, dataElementRepository)
} else {
null
}
}
}

Expand Down Expand Up @@ -325,7 +327,6 @@ fun ProgramRuleAction.toRuleEngineObject(): RuleAction {
fun ProgramRuleVariable.toRuleVariable(
attributeRepository: TrackedEntityAttributeCollectionRepository,
dataElementRepository: DataElementCollectionRepository,
optionRepository: OptionCollectionRepository,
): RuleVariable {
val valueType = when (programRuleVariableSourceType()) {
ProgramRuleVariableSourceType.DATAELEMENT_NEWEST_EVENT_PROGRAM_STAGE,
Expand All @@ -348,14 +349,7 @@ fun ProgramRuleVariable.toRuleVariable(
}

val useCodeForOptionSet = useCodeForOptionSet() ?: false
val options = getOptions(
useCodeForOptionSet,
dataElement()?.uid(),
trackedEntityAttribute()?.uid(),
attributeRepository,
dataElementRepository,
optionRepository,
)
val options = emptyList<Option>()

return when (programRuleVariableSourceType()) {
ProgramRuleVariableSourceType.CALCULATED_VALUE ->
Expand Down Expand Up @@ -417,33 +411,6 @@ fun ProgramRuleVariable.toRuleVariable(
}
}

fun getOptions(
useCodeForOptionSet: Boolean,
dataElementUid: String?,
trackedEntityAttributeUid: String?,
attributeRepository: TrackedEntityAttributeCollectionRepository,
dataElementRepository: DataElementCollectionRepository,
optionRepository: OptionCollectionRepository,
): List<Option> {
if (useCodeForOptionSet) {
return emptyList()
}

return if (dataElementUid != null) {
dataElementRepository.uid(dataElementUid).blockingGet()?.optionSet()?.uid()
?.let { optionSetUid ->
optionRepository.byOptionSetUid().eq(optionSetUid).blockingGet()
}?.map { option -> Option(option.name()!!, option.code() ?: "") } ?: emptyList()
} else if (trackedEntityAttributeUid != null) {
attributeRepository.uid(trackedEntityAttributeUid).blockingGet()?.optionSet()?.uid()
?.let { optionSetUid ->
optionRepository.byOptionSetUid().eq(optionSetUid).blockingGet()
}?.map { option -> Option(option.name()!!, option.code() ?: "") } ?: emptyList()
} else {
emptyList()
}
}

fun ValueType.toRuleValueType(): RuleValueType {
return when {
isInteger || isNumeric -> RuleValueType.NUMERIC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.dhis2.mobileProgramRules

import android.os.Build
import android.text.TextUtils.isEmpty
import io.reactivex.Single
import kotlinx.datetime.Instant
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toLocalDateTime
Expand Down Expand Up @@ -69,23 +68,9 @@ class RulesRepository(private val d2: D2) {
.toRuleVariableList(
d2.trackedEntityModule().trackedEntityAttributes(),
d2.dataElementModule().dataElements(),
d2.optionModule().options(),
)
}

fun ruleVariablesProgramStages(programUid: String): Single<List<RuleVariable>> {
return d2.programModule().programRuleVariables().byProgramUid().eq(programUid).get()
.toFlowable().flatMapIterable { list -> list }
.map {
it.toRuleVariable(
d2.trackedEntityModule().trackedEntityAttributes(),
d2.dataElementModule().dataElements(),
d2.optionModule().options(),
)
}
.toList()
}

suspend fun constants(): Map<String, String> {
return d2.constantModule().constants().blockingGet()
.associate { constant ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,6 @@ class RuleEngineExtensionsTest {
val ruleVariable = programRuleVariable.toRuleVariable(
attributeRepository = trackedEntityAttributeCollectionRepository,
dataElementRepository = dataElementRepository,
optionRepository = optionRepository,
)
assertEquals(programRuleVariable.name(), ruleVariable.name)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ class RuleValidationHelperImpl @Inject constructor(
it.toRuleVariableList(
d2.trackedEntityModule().trackedEntityAttributes(),
d2.dataElementModule().dataElements(),
d2.optionModule().options(),
)
}
}
Expand Down

0 comments on commit fa7bd8e

Please sign in to comment.