jsinovassin commented on code in PR #659:
URL: https://github.com/apache/unomi/pull/659#discussion_r1579458467


##########
plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/conditions/PastEventConditionESQueryBuilder.java:
##########
@@ -131,35 +131,77 @@ private Condition getProfileIdsCondition(Set<String> ids, 
boolean shouldMatch) {
     }
 
     private Condition getProfileConditionForCounter(String 
generatedPropertyKey, Integer minimumEventCount, Integer maximumEventCount, 
boolean eventsOccurred) {
-        String generatedPropertyName = "systemProperties.pastEvents." + 
generatedPropertyKey;
+        Condition countCondition = new Condition();
+
+        
countCondition.setConditionType(definitionsService.getConditionType("nestedCondition"));
+        countCondition.setParameter("path", "systemProperties.pastEvents");
+
+        Condition subConditionCount = new 
Condition(definitionsService.getConditionType("profilePropertyCondition"));
+
+        Condition subConditionKey = 
getKeyEqualsCondition(generatedPropertyKey);
+
         ConditionType profilePropertyConditionType = 
definitionsService.getConditionType("profilePropertyCondition");
         if (eventsOccurred) {
-            Condition counterIsBetweenBoundaries = new Condition();
-            
counterIsBetweenBoundaries.setConditionType(profilePropertyConditionType);
-            counterIsBetweenBoundaries.setParameter("propertyName", 
generatedPropertyName);
-            counterIsBetweenBoundaries.setParameter("comparisonOperator", 
"between");
-            counterIsBetweenBoundaries.setParameter("propertyValuesInteger", 
Arrays.asList(minimumEventCount, maximumEventCount));
-            return counterIsBetweenBoundaries;
+            subConditionCount.setParameter("propertyName", 
"systemProperties.pastEvents.count");
+            subConditionCount.setParameter("comparisonOperator", "between");
+            subConditionCount.setParameter("propertyValuesInteger", 
Arrays.asList(minimumEventCount, maximumEventCount));
+
+            Condition booleanCondition = new 
Condition(definitionsService.getConditionType("booleanCondition"));
+            booleanCondition.setParameter("operator", "and");
+            booleanCondition.setParameter("subConditions", 
Arrays.asList(subConditionCount, subConditionKey));
+
+            countCondition.setParameter("subCondition", booleanCondition);
+            return countCondition;
+
         } else {
-            Condition counterMissing = new Condition();
-            counterMissing.setConditionType(profilePropertyConditionType);
-            counterMissing.setParameter("propertyName", generatedPropertyName);
-            counterMissing.setParameter("comparisonOperator", "missing");
-
-            Condition counterZero = new Condition();
-            counterZero.setConditionType(profilePropertyConditionType);
-            counterZero.setParameter("propertyName", generatedPropertyName);
+
+            // 1. Key not present in profile
+            Condition keyNestedCondition = new Condition();
+            
keyNestedCondition.setConditionType(definitionsService.getConditionType("nestedCondition"));
+            keyNestedCondition.setParameter("path", 
"systemProperties.pastEvents");
+
+            Condition keyEquals = new Condition(profilePropertyConditionType);
+            keyEquals.setParameter("propertyName", 
"systemProperties.pastEvents.key");
+            keyEquals.setParameter("comparisonOperator", "equals");
+            keyEquals.setParameter("propertyValue", generatedPropertyKey);
+
+            keyNestedCondition.setParameter("subCondition", keyEquals);
+
+            Condition mustNotExist = new 
Condition(definitionsService.getConditionType("notCondition"));
+            mustNotExist.setParameter("subCondition", keyNestedCondition);
+
+            // 2. Key present in profile but value equals to 0
+            Condition counterZero = new 
Condition(profilePropertyConditionType);
+            counterZero.setParameter("propertyName", 
"systemProperties.pastEvents.count");
             counterZero.setParameter("comparisonOperator", "equals");
             counterZero.setParameter("propertyValueInteger", 0);
 
+            Condition keyExistsAndCounterZero = new 
Condition(definitionsService.getConditionType("booleanCondition"));
+            keyExistsAndCounterZero.setParameter("operator", "and");
+            keyExistsAndCounterZero.setParameter("subConditions", 
Arrays.asList(subConditionKey, counterZero));
+
+            Condition nestedKeyExistsAndCounterZero = new Condition();
+            
nestedKeyExistsAndCounterZero.setConditionType(definitionsService.getConditionType("nestedCondition"));
+            nestedKeyExistsAndCounterZero.setParameter("path", 
"systemProperties.pastEvents");
+            nestedKeyExistsAndCounterZero.setParameter("subCondition", 
keyExistsAndCounterZero);
+
             Condition counterCondition = new Condition();
             
counterCondition.setConditionType(definitionsService.getConditionType("booleanCondition"));
             counterCondition.setParameter("operator", "or");
-            counterCondition.setParameter("subConditions", 
Arrays.asList(counterMissing, counterZero));
+            counterCondition.setParameter("subConditions", 
Arrays.asList(mustNotExist, nestedKeyExistsAndCounterZero));
+
             return counterCondition;
         }
     }
 
+    private Condition getKeyEqualsCondition(String generatedPropertyKey) {
+        Condition subConditionKey = new 
Condition(definitionsService.getConditionType("profilePropertyCondition"));
+        subConditionKey.setParameter("propertyName", 
"systemProperties.pastEvents.key");
+        subConditionKey.setParameter("comparisonOperator", "equals");
+        subConditionKey.setParameter("propertyValue", generatedPropertyKey);
+        return subConditionKey;
+    }
+

Review Comment:
   Updated!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@unomi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to