UNOMI-47 : Send event updatedProfile optimization, send it also when segments are updated
Project: http://git-wip-us.apache.org/repos/asf/incubator-unomi/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-unomi/commit/68469334 Tree: http://git-wip-us.apache.org/repos/asf/incubator-unomi/tree/68469334 Diff: http://git-wip-us.apache.org/repos/asf/incubator-unomi/diff/68469334 Branch: refs/heads/master Commit: 68469334ef90062b0a653bc1a4ad2171149affc5 Parents: fbffc99 Author: Abdelkader Midani <[email protected]> Authored: Mon Jul 25 18:36:13 2016 +0200 Committer: Quentin Lamerand <[email protected]> Committed: Thu Aug 4 19:01:49 2016 +0200 ---------------------------------------------------------------------- .../services/services/SegmentServiceImpl.java | 21 +++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/68469334/services/src/main/java/org/apache/unomi/services/services/SegmentServiceImpl.java ---------------------------------------------------------------------- diff --git a/services/src/main/java/org/apache/unomi/services/services/SegmentServiceImpl.java b/services/src/main/java/org/apache/unomi/services/services/SegmentServiceImpl.java index 1041365..744bd71 100644 --- a/services/src/main/java/org/apache/unomi/services/services/SegmentServiceImpl.java +++ b/services/src/main/java/org/apache/unomi/services/services/SegmentServiceImpl.java @@ -638,14 +638,28 @@ public class SegmentServiceImpl implements SegmentService, SynchronousBundleList add.removeAll(previousProfiles); previousProfiles.removeAll(newProfiles); + Map<String, Event> updatedProfiles = new HashMap<>(); + for (Profile profileToAdd : add) { profileToAdd.getSegments().add(segment.getItemId()); persistenceService.update(profileToAdd.getItemId(), null, Profile.class, "segments", profileToAdd.getSegments()); + Event profileUpdated = new Event("profileUpdated", null, profileToAdd, null, null, profileToAdd, new Date()); + profileUpdated.setPersistent(false); + updatedProfiles.put(profileToAdd.getItemId(), profileUpdated); } for (Profile profileToRemove : previousProfiles) { profileToRemove.getSegments().remove(segment.getItemId()); persistenceService.update(profileToRemove.getItemId(), null, Profile.class, "segments", profileToRemove.getSegments()); + Event profileUpdated = new Event("profileUpdated", null, profileToRemove, null, null, profileToRemove, new Date()); + profileUpdated.setPersistent(false); + updatedProfiles.put(profileToRemove.getItemId(), profileUpdated); + } + + Iterator<Map.Entry<String, Event>> entries = updatedProfiles.entrySet().iterator(); + while (entries.hasNext()) { + eventService.send(entries.next().getValue()); } + } else { List<Profile> previousProfiles = persistenceService.query(segmentCondition, null, Profile.class); for (Profile profileToRemove : previousProfiles) { @@ -672,15 +686,20 @@ public class SegmentServiceImpl implements SegmentService, SynchronousBundleList } if(scoring.getMetadata().isEnabled()) { String script = "if (ctx._source.scores == null) { ctx._source.scores=[:] } ; if (ctx._source.scores.containsKey(scoringId)) { ctx._source.scores[scoringId] += scoringValue } else { ctx._source.scores[scoringId] = scoringValue }"; + Map<String, Event> updatedProfiles = new HashMap<>(); for (ScoringElement element : scoring.getElements()) { scriptParams.put("scoringValue", element.getValue()); for (Profile p : persistenceService.query(element.getCondition(), null, Profile.class)) { persistenceService.updateWithScript(p.getItemId(), null, Profile.class, script, scriptParams); Event profileUpdated = new Event("profileUpdated", null, p, null, null, p, new Date()); profileUpdated.setPersistent(false); - eventService.send(profileUpdated); + updatedProfiles.put(p.getItemId(), profileUpdated); } } + Iterator<Map.Entry<String, Event>> entries = updatedProfiles.entrySet().iterator(); + while (entries.hasNext()) { + eventService.send(entries.next().getValue()); + } } logger.info("Profiles updated in {}", System.currentTimeMillis()-t); }
