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/27 17:59:21 UTC

[unomi] branch avoidEventCountTwice created (now f1db5eb)

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

jkevan pushed a change to branch avoidEventCountTwice
in repository https://gitbox.apache.org/repos/asf/unomi.git.


      at f1db5eb  UNOMI-539: avoid event count twice in SetEventOccurenceCountAction and fix profile updated return statement

This branch includes the following new commits:

     new f1db5eb  UNOMI-539: avoid event count twice in SetEventOccurenceCountAction and fix profile updated return statement

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


[unomi] 01/01: UNOMI-539: avoid event count twice in SetEventOccurenceCountAction and fix profile updated return statement

Posted by jk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit f1db5ebbaf27c7461ab1050f9e20b6e665e22deb
Author: Kevan <ke...@jahia.com>
AuthorDate: Mon Dec 27 18:58:19 2021 +0100

    UNOMI-539: avoid event count twice in SetEventOccurenceCountAction and fix profile updated return statement
---
 .../org/apache/unomi/itests/ContextServletIT.java  |  7 +++---
 .../unomi/persistence/spi/PropertyHelper.java      | 15 ++++++++++++
 .../actions/SetEventOccurenceCountAction.java      | 27 ++++++++++++----------
 3 files changed, 34 insertions(+), 15 deletions(-)

diff --git a/itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java b/itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java
index c3e2b3d..cdedf34 100644
--- a/itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java
+++ b/itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java
@@ -345,16 +345,17 @@ public class ContextServletIT extends BaseIT {
         HttpPost request = new HttpPost(regularURI);
         request.setEntity(new StringEntity(objectMapper.writeValueAsString(contextRequest), ContentType.create("application/json")));
         //The first event is with a default timestamp (now)
-        String cookieHeaderValue = TestUtils.executeContextJSONRequest(request, sessionId).getCookieHeaderValue();
+        TestUtils.RequestResponse response = TestUtils.executeContextJSONRequest(request, sessionId);
+        String cookieHeaderValue = response.getCookieHeaderValue();
         refreshPersistence();
         //The second event is with a customized timestamp
         request.setURI(URI.create(customTimestampURI));
         request.addHeader("Cookie", cookieHeaderValue);
-        ContextResponse response = (TestUtils.executeContextJSONRequest(request, sessionId)).getContextResponse(); //second event
+        response = (TestUtils.executeContextJSONRequest(request, sessionId)); //second event
         refreshPersistence();
 
         //Assert
-        assertEquals(0, response.getProfileSegments().size());
+        assertEquals(0, response.getContextResponse().getProfileSegments().size());
     }
 
     @Test
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) {