This is an automated email from the ASF dual-hosted git repository. jsinovassinnaik pushed a commit to branch UNOMI-660-fix-origineventids-property in repository https://gitbox.apache.org/repos/asf/unomi.git
commit 24185d066f41d929c510d5d1efb8e68883e97ee6 Author: jsinovassin <[email protected]> AuthorDate: Fri Sep 2 16:32:58 2022 +0200 UNOMI-660 : store correctly the ids of the events leading to the session creation --- api/src/main/java/org/apache/unomi/api/Session.java | 5 +++-- .../java/org/apache/unomi/itests/ContextServletIT.java | 10 ++++++---- .../apache/unomi/rest/endpoints/ContextJsonEndpoint.java | 2 +- .../unomi/rest/endpoints/EventsCollectorEndpoint.java | 2 +- .../org/apache/unomi/rest/service/RestServiceUtils.java | 3 +-- .../unomi/rest/service/impl/RestServiceUtilsImpl.java | 15 +++++++++++---- 6 files changed, 23 insertions(+), 14 deletions(-) diff --git a/api/src/main/java/org/apache/unomi/api/Session.java b/api/src/main/java/org/apache/unomi/api/Session.java index fc622ab7c..986323c12 100644 --- a/api/src/main/java/org/apache/unomi/api/Session.java +++ b/api/src/main/java/org/apache/unomi/api/Session.java @@ -17,6 +17,7 @@ package org.apache.unomi.api; +import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; @@ -53,8 +54,8 @@ public class Session extends Item implements TimestampedItem { private int duration = 0; - private List<String> originEventTypes; - private List<String> originEventIds; + private List<String> originEventTypes = new ArrayList<>(); + private List<String> originEventIds = new ArrayList<>(); /** * Instantiates a new Session. 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 5717fbc83..6559c4660 100644 --- a/itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java +++ b/itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java @@ -66,6 +66,7 @@ import java.util.Objects; import static org.hamcrest.core.IsCollectionContaining.hasItem; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThat; @@ -201,7 +202,7 @@ public class ContextServletIT extends BaseIT { Objects::nonNull, DEFAULT_TRYING_TIMEOUT, DEFAULT_TRYING_TRIES); //Act - Event event = new Event(eventId, TEST_EVENT_TYPE, null, profile, TEST_SCOPE, null, null, new Date()); + Event event = new Event(TEST_EVENT_TYPE, null, profile, TEST_SCOPE, null, null, new Date()); ContextRequest contextRequest = new ContextRequest(); contextRequest.setSessionId(sessionId); @@ -211,12 +212,13 @@ public class ContextServletIT extends BaseIT { request.setEntity(new StringEntity(objectMapper.writeValueAsString(contextRequest), ContentType.APPLICATION_JSON)); TestUtils.executeContextJSONRequest(request, sessionId); - keepTrying("Event " + eventId + " not saved in the required time", () -> eventService.getEvent(eventId), Objects::nonNull, DEFAULT_TRYING_TIMEOUT, + Session session = keepTrying("Session with the id " + sessionId + " not saved in the required time", + () -> profileService.loadSession(sessionId, + null), Objects::nonNull, DEFAULT_TRYING_TIMEOUT, DEFAULT_TRYING_TRIES); - Session session = profileService.loadSession(sessionId, null); assertEquals(TEST_EVENT_TYPE, session.getOriginEventTypes().get(0)); - assertEquals(eventId, session.getOriginEventIds().get(0)); + assertFalse(session.getOriginEventIds().isEmpty()); } @Test diff --git a/rest/src/main/java/org/apache/unomi/rest/endpoints/ContextJsonEndpoint.java b/rest/src/main/java/org/apache/unomi/rest/endpoints/ContextJsonEndpoint.java index 546ce5a35..9d2a3c661 100644 --- a/rest/src/main/java/org/apache/unomi/rest/endpoints/ContextJsonEndpoint.java +++ b/rest/src/main/java/org/apache/unomi/rest/endpoints/ContextJsonEndpoint.java @@ -167,7 +167,7 @@ public class ContextJsonEndpoint { // build public context, profile + session creation/anonymous etc ... EventsRequestContext eventsRequestContext = restServiceUtils.initEventsRequest(scope, sessionId, profileId, - personaId, invalidateProfile, invalidateSession, request, response, timestamp, events != null ? events: Collections.emptyList()); + personaId, invalidateProfile, invalidateSession, request, response, timestamp); // Build response ContextResponse contextResponse = new ContextResponse(); diff --git a/rest/src/main/java/org/apache/unomi/rest/endpoints/EventsCollectorEndpoint.java b/rest/src/main/java/org/apache/unomi/rest/endpoints/EventsCollectorEndpoint.java index 81007d812..c9b3109dd 100644 --- a/rest/src/main/java/org/apache/unomi/rest/endpoints/EventsCollectorEndpoint.java +++ b/rest/src/main/java/org/apache/unomi/rest/endpoints/EventsCollectorEndpoint.java @@ -114,7 +114,7 @@ public class EventsCollectorEndpoint { // build public context, profile + session creation/anonymous etc ... EventsRequestContext eventsRequestContext = restServiceUtils.initEventsRequest(scope, sessionId, profileId, null, false, false, - request, response, timestamp, events); + request, response, timestamp); // process events eventsRequestContext = restServiceUtils.performEventsRequest(eventsCollectorRequest.getEvents(), eventsRequestContext); diff --git a/rest/src/main/java/org/apache/unomi/rest/service/RestServiceUtils.java b/rest/src/main/java/org/apache/unomi/rest/service/RestServiceUtils.java index 6024a36b8..153b6ad11 100644 --- a/rest/src/main/java/org/apache/unomi/rest/service/RestServiceUtils.java +++ b/rest/src/main/java/org/apache/unomi/rest/service/RestServiceUtils.java @@ -47,13 +47,12 @@ public interface RestServiceUtils { * @param request the current request * @param response the current request response * @param timestamp the current date, for timestamp the current visitor data - * @param events list of events sent with the request * @return the built EventsRequestContext */ EventsRequestContext initEventsRequest(String scope, String sessionId, String profileId, String personaId, boolean invalidateProfile, boolean invalidateSession, HttpServletRequest request, HttpServletResponse response, - Date timestamp, List<Event> events); + Date timestamp); /** * Execute the list of events using the dedicated eventsRequestContext diff --git a/rest/src/main/java/org/apache/unomi/rest/service/impl/RestServiceUtilsImpl.java b/rest/src/main/java/org/apache/unomi/rest/service/impl/RestServiceUtilsImpl.java index c18fad5e3..c7f2e24a2 100644 --- a/rest/src/main/java/org/apache/unomi/rest/service/impl/RestServiceUtilsImpl.java +++ b/rest/src/main/java/org/apache/unomi/rest/service/impl/RestServiceUtilsImpl.java @@ -88,8 +88,7 @@ public class RestServiceUtilsImpl implements RestServiceUtils { @Override public EventsRequestContext initEventsRequest(String scope, String sessionId, String profileId, String personaId, boolean invalidateProfile, boolean invalidateSession, - HttpServletRequest request, HttpServletResponse response, Date timestamp, - List<Event> events) { + HttpServletRequest request, HttpServletResponse response, Date timestamp) { // Build context EventsRequestContext eventsRequestContext = new EventsRequestContext(timestamp, null, null, request, response); @@ -186,8 +185,6 @@ public class RestServiceUtilsImpl implements RestServiceUtils { // Only save session and send event if a session id was provided, otherwise keep transient session Session session = new Session(sessionId, sessionProfile, timestamp, scope); - session.setOriginEventTypes(events.stream().map(Event::getEventType).collect(Collectors.toList())); - session.setOriginEventIds(events.stream().map(Item::getItemId).collect(Collectors.toList())); eventsRequestContext.setSession(session); eventsRequestContext.addChanges(EventService.SESSION_UPDATED); Event event = new Event("sessionCreated", eventsRequestContext.getSession(), eventsRequestContext.getProfile(), @@ -241,6 +238,11 @@ public class RestServiceUtilsImpl implements RestServiceUtils { // set Total items on context eventsRequestContext.setTotalItems(events.size()); + boolean toStoreEventIdsInSession = false; + if (eventsRequestContext.getSession() != null + && profileService.loadSession(eventsRequestContext.getSession().getItemId(), null) == null) { + toStoreEventIdsInSession= true; + } for (Event event : events) { eventsRequestContext.setProcessedItems(eventsRequestContext.getProcessedItems() + 1); @@ -276,6 +278,10 @@ public class RestServiceUtilsImpl implements RestServiceUtils { if ((eventsRequestContext.getChanges() & EventService.PROFILE_UPDATED) == EventService.PROFILE_UPDATED) { eventsRequestContext.setProfile(eventToSend.getProfile()); } + if(toStoreEventIdsInSession){ + eventsRequestContext.getSession().getOriginEventIds().add(eventToSend.getItemId()); + eventsRequestContext.getSession().getOriginEventTypes().add(eventToSend.getEventType()); + } if ((eventsRequestContext.getChanges() & EventService.ERROR) == EventService.ERROR) { //Don't count the event that failed eventsRequestContext.setProcessedItems(eventsRequestContext.getProcessedItems() - 1); @@ -284,6 +290,7 @@ public class RestServiceUtilsImpl implements RestServiceUtils { } } } + } return eventsRequestContext;
