Skip to content

Commit 1d53a2f

Browse files
perf: Collect orgUnitGroups in RuleContextRequirements [DHIS2-21245]
1 parent 4ec44fb commit 1d53a2f

10 files changed

Lines changed: 360 additions & 76 deletions

File tree

api/rule-engine.api

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,22 @@ public final class org/hisp/dhis/rules/api/ItemValueType : java/lang/Enum {
6565

6666
public final class org/hisp/dhis/rules/api/RuleContextRequirements {
6767
public static final field Companion Lorg/hisp/dhis/rules/api/RuleContextRequirements$Companion;
68-
public fun <init> (Z)V
68+
public fun <init> ()V
69+
public fun <init> (ZZZZLjava/util/Set;)V
70+
public synthetic fun <init> (ZZZZLjava/util/Set;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
6971
public final fun component1 ()Z
70-
public final fun copy (Z)Lorg/hisp/dhis/rules/api/RuleContextRequirements;
71-
public static synthetic fun copy$default (Lorg/hisp/dhis/rules/api/RuleContextRequirements;ZILjava/lang/Object;)Lorg/hisp/dhis/rules/api/RuleContextRequirements;
72+
public final fun component2 ()Z
73+
public final fun component3 ()Z
74+
public final fun component4 ()Z
75+
public final fun component5 ()Ljava/util/Set;
76+
public final fun copy (ZZZZLjava/util/Set;)Lorg/hisp/dhis/rules/api/RuleContextRequirements;
77+
public static synthetic fun copy$default (Lorg/hisp/dhis/rules/api/RuleContextRequirements;ZZZZLjava/util/Set;ILjava/lang/Object;)Lorg/hisp/dhis/rules/api/RuleContextRequirements;
7278
public fun equals (Ljava/lang/Object;)Z
7379
public final fun getNeedsAllEvents ()Z
80+
public final fun getNeedsAttributes ()Z
81+
public final fun getNeedsDataValues ()Z
82+
public final fun getNeedsEnrollment ()Z
83+
public final fun getOrgUnitGroups ()Ljava/util/Set;
7484
public fun hashCode ()I
7585
public fun toString ()Ljava/lang/String;
7686
}

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ sonarqube = "7.2.3.7755"
1010
kotlinxDatetime = "0.7.1"
1111
kotlinJsWrappers = "1.0.0-pre.830"
1212
slf4jApi = "1.7.36"
13-
expressionParser = "1.4.0"
13+
expressionParser = "1.4.1"
1414

1515
[libraries]
1616
kotlin-plugin = { group = "org.jetbrains.kotlin", name = "kotlin-gradle-plugin", version.ref = "kotlin" }
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package org.hisp.dhis.rules.api
22

33
data class RuleContextRequirements(
4-
val needsAllEvents: Boolean,
4+
val needsAllEvents: Boolean = false,
5+
val needsEnrollment: Boolean = false,
6+
val needsDataValues: Boolean = false,
7+
val needsAttributes: Boolean = false,
8+
val orgUnitGroups: Set<String> = emptySet(),
59
) {
610
companion object {
7-
val NONE = RuleContextRequirements(needsAllEvents = false)
11+
val NONE = RuleContextRequirements()
812
}
913
}

src/commonMain/kotlin/org/hisp/dhis/rules/engine/RuleEngineAnalyzer.kt

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,49 @@ package org.hisp.dhis.rules.engine
33
import org.hisp.dhis.rules.api.RuleContextRequirements
44
import org.hisp.dhis.rules.models.Rule
55
import org.hisp.dhis.rules.models.RuleVariable
6+
import org.hisp.dhis.rules.models.RuleVariableAttribute
7+
import org.hisp.dhis.rules.models.RuleVariableCurrentEvent
68
import org.hisp.dhis.rules.models.RuleVariableNewestEvent
79
import org.hisp.dhis.rules.models.RuleVariableNewestStageEvent
810
import org.hisp.dhis.rules.models.RuleVariablePreviousEvent
11+
import org.hisp.dhis.rules.utils.RuleEngineUtils.ENV_VAR_ENROLLMENT_COUNT
12+
import org.hisp.dhis.rules.utils.RuleEngineUtils.ENV_VAR_ENROLLMENT_DATE
13+
import org.hisp.dhis.rules.utils.RuleEngineUtils.ENV_VAR_ENROLLMENT_ID
14+
import org.hisp.dhis.rules.utils.RuleEngineUtils.ENV_VAR_ENROLLMENT_STATUS
15+
import org.hisp.dhis.rules.utils.RuleEngineUtils.ENV_VAR_EVENT_COUNT
16+
import org.hisp.dhis.rules.utils.RuleEngineUtils.ENV_VAR_INCIDENT_DATE
17+
import org.hisp.dhis.rules.utils.RuleEngineUtils.ENV_VAR_TEI_COUNT
918

1019
internal object RuleEngineAnalyzer {
11-
private val MULTI_EVENT_ENV_VARS = setOf("event_count")
20+
private val MULTI_EVENT_ENV_VARS = setOf(ENV_VAR_EVENT_COUNT)
21+
private val ENROLLMENT_ENV_VARS = setOf(
22+
ENV_VAR_ENROLLMENT_COUNT,
23+
ENV_VAR_ENROLLMENT_DATE,
24+
ENV_VAR_ENROLLMENT_ID,
25+
ENV_VAR_ENROLLMENT_STATUS,
26+
ENV_VAR_INCIDENT_DATE,
27+
ENV_VAR_TEI_COUNT,
28+
)
1229

1330
fun analyzeContextRequirements(
1431
rules: List<Rule>,
1532
variables: List<RuleVariable>,
1633
): RuleContextRequirements {
1734
val envVars = mutableSetOf<String>()
1835
val referencedVarNames = mutableSetOf<String>()
36+
val orgUnitGroups = mutableSetOf<String>()
1937

2038
for (rule in rules) {
2139
rule.conditionExpression.getOrNull()?.let {
2240
envVars += it.collectProgramVariablesNames()
2341
referencedVarNames += it.collectProgramRuleVariableNames()
42+
orgUnitGroups += it.collectInOrgUnitGroups()
2443
}
2544
for (action in rule.actions) {
2645
action.dataExpression.getOrNull()?.let {
2746
envVars += it.collectProgramVariablesNames()
2847
referencedVarNames += it.collectProgramRuleVariableNames()
48+
orgUnitGroups += it.collectInOrgUnitGroups()
2949
}
3050
}
3151
}
@@ -39,6 +59,20 @@ internal object RuleEngineAnalyzer {
3959
v is RuleVariablePreviousEvent
4060
}
4161

42-
return RuleContextRequirements(needsAllEvents)
62+
val needsDataValues = referencedVarNames.any { name ->
63+
val v = byName[name]
64+
v is RuleVariableNewestEvent ||
65+
v is RuleVariableNewestStageEvent ||
66+
v is RuleVariablePreviousEvent ||
67+
v is RuleVariableCurrentEvent
68+
}
69+
70+
val needsAttributes = referencedVarNames.any { name ->
71+
byName[name] is RuleVariableAttribute
72+
}
73+
74+
val needsEnrollment = envVars.any { it in ENROLLMENT_ENV_VARS } || needsAttributes
75+
76+
return RuleContextRequirements(needsAllEvents, needsEnrollment, needsDataValues, needsAttributes, orgUnitGroups)
4377
}
4478
}

src/commonMain/kotlin/org/hisp/dhis/rules/utils/RuleEngineUtils.kt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,23 @@ package org.hisp.dhis.rules.utils
3232
* @author Zubair Asghar
3333
*/
3434
internal object RuleEngineUtils {
35-
const val ENV_VAR_CURRENT_DATE = "current_date"
3635
const val ENV_VAR_COMPLETED_DATE = "completed_date"
37-
const val ENV_VAR_EVENT_DATE = "event_date"
38-
const val ENV_VAR_EVENT_COUNT = "event_count"
36+
const val ENV_VAR_CURRENT_DATE = "current_date"
3937
const val ENV_VAR_DUE_DATE = "due_date"
40-
const val ENV_VAR_EVENT_ID = "event_id"
38+
const val ENV_VAR_ENROLLMENT_COUNT = "enrollment_count"
4139
const val ENV_VAR_ENROLLMENT_DATE = "enrollment_date"
4240
const val ENV_VAR_ENROLLMENT_ID = "enrollment_id"
43-
const val ENV_VAR_ENROLLMENT_COUNT = "enrollment_count"
44-
const val ENV_VAR_INCIDENT_DATE = "incident_date"
45-
const val ENV_VAR_TEI_COUNT = "tei_count"
41+
const val ENV_VAR_ENROLLMENT_STATUS = "enrollment_status"
42+
const val ENV_VAR_ENVIRONMENT = "environment"
43+
const val ENV_VAR_EVENT_COUNT = "event_count"
44+
const val ENV_VAR_EVENT_DATE = "event_date"
45+
const val ENV_VAR_EVENT_ID = "event_id"
4646
const val ENV_VAR_EVENT_STATUS = "event_status"
47+
const val ENV_VAR_INCIDENT_DATE = "incident_date"
4748
const val ENV_VAR_OU = "org_unit"
48-
const val ENV_VAR_ENROLLMENT_STATUS = "enrollment_status"
49+
const val ENV_VAR_OU_CODE = "orgunit_code"
50+
const val ENV_VAR_PROGRAM_NAME = "program_name"
4951
const val ENV_VAR_PROGRAM_STAGE_ID = "program_stage_id"
5052
const val ENV_VAR_PROGRAM_STAGE_NAME = "program_stage_name"
51-
const val ENV_VAR_PROGRAM_NAME = "program_name"
52-
const val ENV_VAR_ENVIRONMENT = "environment"
53-
const val ENV_VAR_OU_CODE = "orgunit_code"
53+
const val ENV_VAR_TEI_COUNT = "tei_count"
5454
}

src/commonMain/kotlin/org/hisp/dhis/rules/utils/RuleEventUtils.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,3 @@ internal fun filterRules(
5858
ruleEvent: RuleEvent,
5959
): List<Rule> =
6060
rules.filter { it.programStage.isNullOrEmpty() || it.programStage == ruleEvent.programStage }
61-
62-
internal fun isAllowedAction(ruleAction: RuleAction, attributeType: AttributeType): Boolean =
63-
ruleAction.attributeType() == null ||
64-
ruleAction.attributeType() == attributeType.name ||
65-
ruleAction.attributeType() == AttributeType.UNKNOWN.name

0 commit comments

Comments
 (0)