This is an automated email from the ASF dual-hosted git repository. dgriffon pushed a commit to branch fix-scoring-plan in repository https://gitbox.apache.org/repos/asf/unomi.git
commit 09232066372092e9580052a66d16321759838db7 Author: David Griffon <[email protected]> AuthorDate: Thu Nov 18 07:16:35 2021 +0100 UNOMI-530 : fix maximumEventCount in past event condition --- .../java/org/apache/unomi/itests/SegmentIT.java | 98 ++++++++++++++++++++++ .../PastEventConditionESQueryBuilder.java | 2 +- 2 files changed, 99 insertions(+), 1 deletion(-) diff --git a/itests/src/test/java/org/apache/unomi/itests/SegmentIT.java b/itests/src/test/java/org/apache/unomi/itests/SegmentIT.java index bbb0354..cba61d5 100644 --- a/itests/src/test/java/org/apache/unomi/itests/SegmentIT.java +++ b/itests/src/test/java/org/apache/unomi/itests/SegmentIT.java @@ -42,11 +42,13 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.inject.Inject; +import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.ZoneId; import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.Map; @RunWith(PaxExam.class) @ExamReactorStrategy(PerSuite.class) @@ -358,4 +360,100 @@ public class SegmentIT extends BaseIT { updatedProfile -> !updatedProfile.getScores().containsKey("past-event-scoring-test"), 1000, 20); } + + + @Test + public void testScoringRecalculation() throws Exception { + // create Profile + Profile profile = new Profile(); + profile.setItemId("test_profile_id"); + profileService.save(profile); + persistenceService.refreshIndex(Profile.class, null); // wait for profile to be full persisted and index + + Date timestampEventInRange = new SimpleDateFormat("yyyy-MM-dd").parse("2000-10-30"); + // create the past event condition + Condition pastEventCondition = new Condition(definitionsService.getConditionType("pastEventCondition")); + pastEventCondition.setParameter("minimumEventCount", 1); + pastEventCondition.setParameter("maximumEventCount", 2); + + pastEventCondition.setParameter("fromDate","2000-07-15T07:00:00Z"); + pastEventCondition.setParameter("toDate","2001-01-15T07:00:00Z");; + Condition pastEventEventCondition = new Condition(definitionsService.getConditionType("eventTypeCondition")); + pastEventEventCondition.setParameter("eventTypeId", "test-event-type"); + pastEventCondition.setParameter("eventCondition", pastEventEventCondition); + + // create the scoring + Metadata scoringMetadata = new Metadata("past-event-scoring-test"); + Scoring scoring = new Scoring(scoringMetadata); + List<ScoringElement> scoringElements = new ArrayList<>(); + ScoringElement scoringElement = new ScoringElement(); + scoringElement.setCondition(pastEventCondition); + scoringElement.setValue(50); + scoringElements.add(scoringElement); + scoring.setElements(scoringElements); + segmentService.setScoringDefinition(scoring); + Thread.sleep(5000); + + // Send 2 events that match the scoring plan. + Event testEvent = new Event("test-event-type", null, profile, null, null, profile, timestampEventInRange); + testEvent.setPersistent(true); + eventService.send(testEvent); + refreshPersistence(); + // 2nd event + testEvent = new Event("test-event-type", null, testEvent.getProfile(), null, null, testEvent.getProfile(), timestampEventInRange); + eventService.send(testEvent); + refreshPersistence(); + + // insure the profile is engaged; + try { + Assert.assertTrue("Profile should have 2 events in the scoring", (Long) ((Map) testEvent.getProfile().getSystemProperties().get("pastEvents")).get(pastEventCondition.getParameterValues().get("generatedPropertyKey")) == 2); + } catch (Exception e) { + Assert.fail("Unable to read past event because " + e.getMessage()); + } + profileService.save(testEvent.getProfile()); + refreshPersistence(); + // recalculate event conditions + segmentService.recalculatePastEventConditions(); + // insure the profile is still engaged after recalculate; + keepTrying("Profile should have 2 events in the scoring", + () -> profileService.load("test_profile_id"), + updatedProfile -> { + try { + return (Integer) ((Map) updatedProfile.getSystemProperties().get("pastEvents")).get(pastEventCondition.getParameterValues().get("generatedPropertyKey")) == 2; + } catch (Exception e) { + // Do nothing, unable to read value + }; + return false; + }, + 1000, 20); + + + // Add one more event + testEvent = new Event("test-event-type", null, testEvent.getProfile(), null, null, testEvent.getProfile(), timestampEventInRange); + eventService.send(testEvent); + + // As 3 events have match, the profile should not be part of the scoring plan. + try { + Assert.assertTrue("Profile should have no scoring", testEvent.getProfile().getScores().get("past-event-scoring-test") == 0); + } catch (Exception e) { + Assert.fail("Unable to read past event because " + e.getMessage()); + } + profileService.save(testEvent.getProfile()); + refreshPersistence(); + // now recalculate the past event conditions + segmentService.recalculatePastEventConditions(); + persistenceService.refreshIndex(Profile.class, null); + // As 3 events have match, the profile should not be part of the scoring plan. + keepTrying("Profile should not be part of the scoring anymore", + () -> profileService.load("test_profile_id"), + updatedProfile -> { + try { + return updatedProfile.getScores().get("past-event-scoring-test") == 0; + } catch (Exception e) { + // Do nothing, unable to read value + }; + return false; + }, + 1000, 20); + } } \ No newline at end of file diff --git a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/conditions/PastEventConditionESQueryBuilder.java b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/conditions/PastEventConditionESQueryBuilder.java index ca2ee7e..c76a750 100644 --- a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/conditions/PastEventConditionESQueryBuilder.java +++ b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/conditions/PastEventConditionESQueryBuilder.java @@ -83,7 +83,7 @@ public class PastEventConditionESQueryBuilder implements ConditionESQueryBuilder // A property is already set on profiles matching the past event condition, use it to check the numbers of occurrences RangeQueryBuilder builder = QueryBuilders.rangeQuery("systemProperties.pastEvents." + generatedPropertyKey); builder.gte(minimumEventCount); - builder.lte(minimumEventCount); + builder.lte(maximumEventCount); return builder; } else { // No property set - tries to build an idsQuery
