This is an automated email from the ASF dual-hosted git repository. jsinovassinnaik pushed a commit to branch UNOMI-623-store-event-creation-ids in repository https://gitbox.apache.org/repos/asf/unomi.git
commit f5feb8f0ef496bbad868bff179c567368d40e079 Author: jsinovassin <[email protected]> AuthorDate: Thu Aug 25 16:47:15 2022 +0200 UNOMI-623 : store ids and types of events in session when it's created --- .../main/java/org/apache/unomi/api/Session.java | 40 ++++++++++++++++++++++ .../org/apache/unomi/itests/ContextServletIT.java | 31 +++++++++++++++++ .../resources/META-INF/cxs/mappings/session.json | 2 +- .../unomi/rest/endpoints/ContextJsonEndpoint.java | 4 ++- .../rest/endpoints/EventsCollectorEndpoint.java | 20 ++++++----- .../unomi/rest/service/RestServiceUtils.java | 4 +-- .../rest/service/impl/RestServiceUtilsImpl.java | 11 ++++-- 7 files changed, 96 insertions(+), 16 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 7260e9ec3..fc622ab7c 100644 --- a/api/src/main/java/org/apache/unomi/api/Session.java +++ b/api/src/main/java/org/apache/unomi/api/Session.java @@ -19,6 +19,7 @@ package org.apache.unomi.api; import java.util.Date; import java.util.HashMap; +import java.util.List; import java.util.Map; /** @@ -52,6 +53,9 @@ public class Session extends Item implements TimestampedItem { private int duration = 0; + private List<String> originEventTypes; + private List<String> originEventIds; + /** * Instantiates a new Session. */ @@ -224,4 +228,40 @@ public class Session extends Item implements TimestampedItem { public void setScope(String scope) { this.scope = scope; } + + /** + * Get the events types which causes the session creation + * + * @return List of event types + */ + public List<String> getOriginEventTypes() { + return originEventTypes; + } + + /** + * Set the events types which causes the session creation + * + * @param originEventTypes List of event types + */ + public void setOriginEventTypes(List<String> originEventTypes) { + this.originEventTypes = originEventTypes; + } + + /** + * Get the events ids which causes the session creation + * + * @return event ids + */ + public List<String> getOriginEventIds() { + return originEventIds; + } + + /** + * Set the events ids which causes the session creation + * + * @param originEventIds List of event ids + */ + public void setOriginEventIds(List<String> originEventIds) { + this.originEventIds = originEventIds; + } } 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 75931756b..5717fbc83 100644 --- a/itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java +++ b/itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java @@ -57,6 +57,7 @@ import java.time.ZoneId; import java.time.ZoneOffset; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.List; @@ -188,6 +189,36 @@ public class ContextServletIT extends BaseIT { assertEquals(2, event.getVersion().longValue()); } + @Test + public void testCallingContextWithSessionCreation() throws Exception { + //Arrange + String eventId = "test-event-id-" + System.currentTimeMillis(); + String sessionId = "test-session-id"; + Profile profile = new Profile(TEST_PROFILE_ID); + profileService.save(profile); + + keepTrying("Profile " + TEST_PROFILE_ID + " not found in the required time", () -> profileService.load(TEST_PROFILE_ID), + 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()); + + ContextRequest contextRequest = new ContextRequest(); + contextRequest.setSessionId(sessionId); + contextRequest.setEvents(Collections.singletonList(event)); + HttpPost request = new HttpPost(getFullUrl(CONTEXT_URL)); + request.addHeader(THIRD_PARTY_HEADER_NAME, UNOMI_KEY); + 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, + DEFAULT_TRYING_TRIES); + + Session session = profileService.loadSession(sessionId, null); + assertEquals(TEST_EVENT_TYPE, session.getOriginEventTypes().get(0)); + assertEquals(eventId, session.getOriginEventIds().get(0)); + } + @Test public void testUpdateEventFromContextUnAuthorizedThirdParty_Fail() throws Exception { //Arrange diff --git a/persistence-elasticsearch/core/src/main/resources/META-INF/cxs/mappings/session.json b/persistence-elasticsearch/core/src/main/resources/META-INF/cxs/mappings/session.json index 1a0fd6eae..e28657c67 100644 --- a/persistence-elasticsearch/core/src/main/resources/META-INF/cxs/mappings/session.json +++ b/persistence-elasticsearch/core/src/main/resources/META-INF/cxs/mappings/session.json @@ -70,4 +70,4 @@ "type": "date" } } -} \ No newline at end of file +} 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 6207845f8..42e64183a 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 @@ -157,15 +157,17 @@ public class ContextJsonEndpoint { // init ids String profileId = null; String scope = null; + List<Event> events = Collections.emptyList(); if (contextRequest != null) { scope = contextRequest.getSource() != null ? contextRequest.getSource().getScope() : scope; sessionId = contextRequest.getSessionId() != null ? contextRequest.getSessionId() : sessionId; profileId = contextRequest.getProfileId(); + events = contextRequest.getEvents(); } // build public context, profile + session creation/anonymous etc ... EventsRequestContext eventsRequestContext = restServiceUtils.initEventsRequest(scope, sessionId, profileId, - personaId, invalidateProfile, invalidateSession, request, response, timestamp); + personaId, invalidateProfile, invalidateSession, request, response, timestamp, events); // 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 f727b352f..81007d812 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 @@ -42,6 +42,7 @@ import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import java.util.Date; +import java.util.List; @WebService @Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8") @@ -51,6 +52,7 @@ import java.util.Date; @Component(service = EventsCollectorEndpoint.class, property = "osgi.jaxrs.resource=true") public class EventsCollectorEndpoint { + public static final String SYSTEMSCOPE = "systemscope"; @Reference private RestServiceUtils restServiceUtils; @@ -68,7 +70,7 @@ public class EventsCollectorEndpoint { @GET @Path("/eventcollector") public EventCollectorResponse collectAsGet(@QueryParam("payload") EventsCollectorRequest eventsCollectorRequest, - @QueryParam("timestamp") Long timestampAsString) { + @QueryParam("timestamp") Long timestampAsString) { return doEvent(eventsCollectorRequest, timestampAsString); } @@ -95,15 +97,15 @@ public class EventsCollectorEndpoint { String profileId = eventsCollectorRequest.getProfileId(); // Get the first available scope that is not equal to systemscope otherwise systemscope will be used - String scope = "systemscope"; - for (Event event : eventsCollectorRequest.getEvents()) { + String scope = SYSTEMSCOPE; + List<Event> events = eventsCollectorRequest.getEvents(); + for (Event event : events) { if (StringUtils.isNotBlank(event.getEventType())) { - if (StringUtils.isNotBlank(event.getScope()) && !event.getScope().equals("systemscope")) { + if (StringUtils.isNotBlank(event.getScope()) && !event.getScope().equals(SYSTEMSCOPE)) { scope = event.getScope(); break; - } else if (event.getSource() != null && - StringUtils.isNotBlank(event.getSource().getScope()) && - !event.getSource().getScope().equals("systemscope")) { + } else if (event.getSource() != null && StringUtils.isNotBlank(event.getSource().getScope()) && !event.getSource() + .getScope().equals(SYSTEMSCOPE)) { scope = event.getSource().getScope(); break; } @@ -111,8 +113,8 @@ public class EventsCollectorEndpoint { } // build public context, profile + session creation/anonymous etc ... - EventsRequestContext eventsRequestContext = restServiceUtils.initEventsRequest(scope, sessionId, profileId, - null, false, false, request, response, timestamp); + EventsRequestContext eventsRequestContext = restServiceUtils.initEventsRequest(scope, sessionId, profileId, null, false, false, + request, response, timestamp, events); // 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 0e6a9ba11..6024a36b8 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,13 @@ 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); + Date timestamp, List<Event> events); /** * 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 75a98d1e1..c18fad5e3 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 @@ -40,6 +40,7 @@ import javax.ws.rs.BadRequestException; import java.util.Date; import java.util.List; import java.util.UUID; +import java.util.stream.Collectors; @Component(service = RestServiceUtils.class) public class RestServiceUtilsImpl implements RestServiceUtils { @@ -87,7 +88,8 @@ 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) { + HttpServletRequest request, HttpServletResponse response, Date timestamp, + List<Event> events) { // Build context EventsRequestContext eventsRequestContext = new EventsRequestContext(timestamp, null, null, request, response); @@ -182,9 +184,12 @@ public class RestServiceUtilsImpl implements RestServiceUtils { if (StringUtils.isNotBlank(sessionId)) { // Only save session and send event if a session id was provided, otherwise keep transient session - eventsRequestContext.setSession(new Session(sessionId, sessionProfile, timestamp, scope)); - eventsRequestContext.addChanges(EventService.SESSION_UPDATED); + 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(), scope, null, eventsRequestContext.getSession(), null, timestamp, false); if (sessionProfile.isAnonymousProfile()) {
