You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@unomi.apache.org by jk...@apache.org on 2021/12/28 13:34:33 UTC

[unomi] branch unomi-1.6.x updated: UNOMI-539: avoid event count twice in SetEventOccurenceCountAction and fix profile updated return statement (#370)

This is an automated email from the ASF dual-hosted git repository.

jkevan pushed a commit to branch unomi-1.6.x
in repository https://gitbox.apache.org/repos/asf/unomi.git


The following commit(s) were added to refs/heads/unomi-1.6.x by this push:
     new c361dd9  UNOMI-539: avoid event count twice in SetEventOccurenceCountAction and fix profile updated return statement (#370)
c361dd9 is described below

commit c361dd9fbb30a82bf1c423034ed4543013d27f3c
Author: kevan Jahanshahi <ke...@jahia.com>
AuthorDate: Tue Dec 28 14:28:55 2021 +0100

    UNOMI-539: avoid event count twice in SetEventOccurenceCountAction and fix profile updated return statement (#370)
---
 .../unomi/persistence/spi/PropertyHelper.java      | 15 ++++++++++++
 .../actions/SetEventOccurenceCountAction.java      | 27 ++++++++++++----------
 2 files changed, 30 insertions(+), 12 deletions(-)

diff --git a/persistence-spi/src/main/java/org/apache/unomi/persistence/spi/PropertyHelper.java b/persistence-spi/src/main/java/org/apache/unomi/persistence/spi/PropertyHelper.java
index 33735ac..538f71e 100644
--- a/persistence-spi/src/main/java/org/apache/unomi/persistence/spi/PropertyHelper.java
+++ b/persistence-spi/src/main/java/org/apache/unomi/persistence/spi/PropertyHelper.java
@@ -137,6 +137,19 @@ public class PropertyHelper {
         return null;
     }
 
+    public static Long getLong(Object value) {
+        if (value instanceof Number) {
+            return ((Number) value).longValue();
+        } else {
+            try {
+                return Long.parseLong(value.toString());
+            } catch (NumberFormatException e) {
+                // Not a number
+            }
+        }
+        return null;
+    }
+
     public static Double getDouble(Object value) {
         if (value instanceof Number) {
             return ((Number) value).doubleValue();
@@ -189,6 +202,8 @@ public class PropertyHelper {
         }
         if (propertyValue instanceof Integer) {
             return propertyValue.equals(getInteger(beanPropertyValue));
+        } if (propertyValue instanceof Long) {
+            return propertyValue.equals(getLong(beanPropertyValue));
         } else if (propertyValue instanceof Boolean) {
             return propertyValue.equals(getBooleanValue(beanPropertyValue));
         } else {
diff --git a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/SetEventOccurenceCountAction.java b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/SetEventOccurenceCountAction.java
index 19b1afa..aa26976 100644
--- a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/SetEventOccurenceCountAction.java
+++ b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/SetEventOccurenceCountAction.java
@@ -24,6 +24,7 @@ import org.apache.unomi.api.conditions.Condition;
 import org.apache.unomi.api.services.DefinitionsService;
 import org.apache.unomi.api.services.EventService;
 import org.apache.unomi.persistence.spi.PersistenceService;
+import org.apache.unomi.persistence.spi.PropertyHelper;
 
 import java.time.Duration;
 import java.time.LocalDateTime;
@@ -66,6 +67,13 @@ public class SetEventOccurenceCountAction implements ActionExecutor {
         c.setParameter("propertyValue", event.getProfileId());
         conditions.add(c);
 
+        // may be current event is already persisted and indexed, in that case we filter it from the count to increment it manually at the end
+        Condition eventIdFilter = new Condition(definitionsService.getConditionType("eventPropertyCondition"));
+        eventIdFilter.setParameter("propertyName", "itemId");
+        eventIdFilter.setParameter("comparisonOperator", "notEquals");
+        eventIdFilter.setParameter("propertyValue", event.getItemId());
+        conditions.add(eventIdFilter);
+
         Integer numberOfDays = (Integer) pastEventCondition.getParameter("numberOfDays");
         String fromDate = (String) pastEventCondition.getParameter("fromDate");
         String toDate = (String) pastEventCondition.getParameter("toDate");
@@ -98,12 +106,6 @@ public class SetEventOccurenceCountAction implements ActionExecutor {
 
         long count = persistenceService.queryCount(andCondition, Event.ITEM_TYPE);
 
-        Map<String, Object> pastEvents = (Map<String, Object>) event.getProfile().getSystemProperties().get("pastEvents");
-        if (pastEvents == null) {
-            pastEvents = new LinkedHashMap<>();
-            event.getProfile().getSystemProperties().put("pastEvents", pastEvents);
-        }
-
         LocalDateTime fromDateTime = null;
         if (fromDate != null) {
             Calendar fromDateCalendar = DatatypeConverter.parseDateTime(fromDate);
@@ -117,15 +119,16 @@ public class SetEventOccurenceCountAction implements ActionExecutor {
 
         LocalDateTime eventTime = LocalDateTime.ofInstant(event.getTimeStamp().toInstant(),ZoneId.of("UTC"));
 
-        if (!persistenceService.isConsistent(event)) {
-            if (inTimeRange(eventTime, numberOfDays, fromDateTime, toDateTime)) {
-                count++;
-            }
+        if (inTimeRange(eventTime, numberOfDays, fromDateTime, toDateTime)) {
+            count++;
         }
 
-        pastEvents.put((String) pastEventCondition.getParameter("generatedPropertyKey"), count);
+        String generatedPropertyKey = (String) pastEventCondition.getParameter("generatedPropertyKey");
+        if (PropertyHelper.setProperty(event.getProfile(), "systemProperties.pastEvents." + generatedPropertyKey, count, "alwaysSet")) {
+            return EventService.PROFILE_UPDATED;
+        }
 
-        return EventService.PROFILE_UPDATED;
+        return EventService.NO_CHANGE;
     }
 
     private boolean inTimeRange(LocalDateTime eventTime, Integer numberOfDays, LocalDateTime fromDate, LocalDateTime toDate) {