public DependentMetadata removeScoringDefinition(String scoringId, boolean validate) { Set<Segment> impactedSegments = getScoringDependentSegments(scoringId); Set<Scoring> impactedScorings = getScoringDependentScorings(scoringId); if (!validate || (impactedSegments.isEmpty() && impactedScorings.isEmpty())) { // update profiles updateExistingProfilesForRemovedScoring(scoringId); // update impacted segments for (Segment segment : impactedSegments) { Condition updatedCondition = updateScoringDependentCondition(segment.getCondition(), scoringId); segment.setCondition(updatedCondition); if (updatedCondition == null) { clearAutoGeneratedRules( persistenceService.query( "linkedItems", segment.getMetadata().getId(), null, Rule.class), segment.getMetadata().getId()); segment.getMetadata().setEnabled(false); } setSegmentDefinition(segment); } // update impacted scorings for (Scoring scoring : impactedScorings) { List<ScoringElement> updatedScoringElements = new ArrayList<>(); for (ScoringElement scoringElement : scoring.getElements()) { Condition updatedCondition = updateScoringDependentCondition(scoringElement.getCondition(), scoringId); if (updatedCondition != null) { scoringElement.setCondition(updatedCondition); updatedScoringElements.add(scoringElement); } } scoring.setElements(updatedScoringElements); if (updatedScoringElements.isEmpty()) { clearAutoGeneratedRules( persistenceService.query( "linkedItems", scoring.getMetadata().getId(), null, Rule.class), scoring.getMetadata().getId()); scoring.getMetadata().setEnabled(false); } setScoringDefinition(scoring); } persistenceService.remove(scoringId, Scoring.class); List<Rule> previousRules = persistenceService.query("linkedItems", scoringId, null, Rule.class); clearAutoGeneratedRules(previousRules, scoringId); } List<Metadata> segments = new LinkedList<>(); List<Metadata> scorings = new LinkedList<>(); for (Segment definition : impactedSegments) { segments.add(definition.getMetadata()); } for (Scoring definition : impactedScorings) { scorings.add(definition.getMetadata()); } return new DependentMetadata(segments, scorings); }
private void getAutoGeneratedRules( Metadata metadata, Condition condition, Condition parentCondition, List<Rule> rules) { Set<String> tags = condition.getConditionType().getMetadata().getTags(); if (tags.contains("eventCondition") && !tags.contains("profileCondition")) { try { Map<String, Object> m = new HashMap<>(3); m.put("scope", metadata.getScope()); m.put("condition", condition); m.put("numberOfDays", parentCondition.getParameter("numberOfDays")); String key = CustomObjectMapper.getObjectMapper().writeValueAsString(m); key = "eventTriggered" + getMD5(key); parentCondition.setParameter("generatedPropertyKey", key); Rule rule = rulesService.getRule(key); if (rule == null) { rule = new Rule( new Metadata( metadata.getScope(), key, "Auto generated rule for " + metadata.getName(), "")); rule.setCondition(condition); rule.getMetadata().setHidden(true); final Action action = new Action(); action.setActionType(definitionsService.getActionType("setEventOccurenceCountAction")); action.setParameter("pastEventCondition", parentCondition); rule.setActions(Arrays.asList(action)); rule.setLinkedItems(Arrays.asList(metadata.getId())); rules.add(rule); updateExistingProfilesForPastEventCondition(condition, parentCondition); } else { rule.getLinkedItems().add(metadata.getId()); rules.add(rule); } } catch (JsonProcessingException e) { logger.error(e.getMessage(), e); } } else { Collection<Object> values = new ArrayList<>(condition.getParameterValues().values()); for (Object parameterValue : values) { if (parameterValue instanceof Condition) { getAutoGeneratedRules(metadata, (Condition) parameterValue, condition, rules); } else if (parameterValue instanceof Collection) { for (Object subCondition : (Collection<?>) parameterValue) { if (subCondition instanceof Condition) { getAutoGeneratedRules(metadata, (Condition) subCondition, condition, rules); } } } } } }
public DependentMetadata getScoringDependentMetadata(String scoringId) { List<Metadata> segments = new LinkedList<>(); List<Metadata> scorings = new LinkedList<>(); for (Segment definition : getScoringDependentSegments(scoringId)) { segments.add(definition.getMetadata()); } for (Scoring definition : getScoringDependentScorings(scoringId)) { scorings.add(definition.getMetadata()); } return new DependentMetadata(segments, scorings); }
/** * Return an updated condition that do not contain a condition on the scoringId anymore it's * remove the unnecessary boolean condition (if a condition is the only one of a boolean the * boolean will be remove and the subcondition returned) it's return null when there is no more * condition after (if the condition passed was only a scoring condition on the scoringId) * * @param condition the condition to update * @param scoringId the scoring id to remove in the condition * @return updated condition */ private Condition updateScoringDependentCondition(Condition condition, String scoringId) { if ("booleanCondition".equals(condition.getConditionTypeId())) { @SuppressWarnings("unchecked") final List<Condition> subConditions = (List<Condition>) condition.getParameter("subConditions"); List<Condition> updatedSubConditions = new LinkedList<>(); for (Condition subCondition : subConditions) { Condition updatedCondition = updateScoringDependentCondition(subCondition, scoringId); if (updatedCondition != null) { updatedSubConditions.add(updatedCondition); } } if (!updatedSubConditions.isEmpty()) { if (updatedSubConditions.size() == 1) { return updatedSubConditions.get(0); } else { condition.setParameter("subConditions", updatedSubConditions); return condition; } } else { return null; } } else if ("scoringCondition".equals(condition.getConditionTypeId()) && scoringId.equals(condition.getParameter("scoringPlanId"))) { return null; } return condition; }
private <T extends MetadataItem> PartialList<Metadata> getMetadatas( int offset, int size, String sortBy, Class<T> clazz) { PartialList<T> items = persistenceService.getAllItems(clazz, offset, size, sortBy); List<Metadata> details = new LinkedList<>(); for (T definition : items.getList()) { details.add(definition.getMetadata()); } return new PartialList<>(details, items.getOffset(), items.getPageSize(), items.getTotalSize()); }
public PartialList<Metadata> getSegmentMetadatas( String scope, int offset, int size, String sortBy) { PartialList<Segment> segments = persistenceService.query("metadata.scope", scope, sortBy, Segment.class, offset, size); List<Metadata> details = new LinkedList<>(); for (Segment definition : segments.getList()) { details.add(definition.getMetadata()); } return new PartialList<>( details, segments.getOffset(), segments.getPageSize(), segments.getTotalSize()); }
private void updateExistingProfilesForPastEventCondition( Condition eventCondition, Condition parentCondition) { long t = System.currentTimeMillis(); List<Condition> l = new ArrayList<Condition>(); Condition andCondition = new Condition(); andCondition.setConditionType(definitionsService.getConditionType("booleanCondition")); andCondition.setParameter("operator", "and"); andCondition.setParameter("subConditions", l); l.add(eventCondition); Integer numberOfDays = (Integer) parentCondition.getParameter("numberOfDays"); if (numberOfDays != null) { Condition numberOfDaysCondition = new Condition(); numberOfDaysCondition.setConditionType( definitionsService.getConditionType("sessionPropertyCondition")); numberOfDaysCondition.setParameter("propertyName", "timeStamp"); numberOfDaysCondition.setParameter("comparisonOperator", "greaterThan"); numberOfDaysCondition.setParameter("propertyValue", "now-" + numberOfDays + "d"); l.add(numberOfDaysCondition); } String propertyKey = (String) parentCondition.getParameter("generatedPropertyKey"); Map<String, Long> res = persistenceService.aggregateQuery( andCondition, new TermsAggregate("profileId"), Event.ITEM_TYPE); for (Map.Entry<String, Long> entry : res.entrySet()) { if (!entry.getKey().startsWith("_")) { Map<String, Object> p = new HashMap<>(); p.put(propertyKey, entry.getValue()); Map<String, Object> p2 = new HashMap<>(); p2.put("pastEvents", p); try { persistenceService.update(entry.getKey(), null, Profile.class, "systemProperties", p2); } catch (Exception e) { logger.error(e.getMessage(), e); } } } logger.info("Profiles past condition updated in {}", System.currentTimeMillis() - t); }
public List<Metadata> getSegmentMetadatasForProfile(Profile profile) { List<Metadata> metadatas = new ArrayList<>(); List<Segment> allSegments = this.allSegments; for (Segment segment : allSegments) { if (persistenceService.testMatch(segment.getCondition(), profile)) { metadatas.add(segment.getMetadata()); } } return metadatas; }
private <T extends MetadataItem> PartialList<Metadata> getMetadatas(Query query, Class<T> clazz) { if (query.isForceRefresh()) { persistenceService.refresh(); } definitionsService.resolveConditionType(query.getCondition()); PartialList<T> items = persistenceService.query( query.getCondition(), query.getSortby(), clazz, query.getOffset(), query.getLimit()); List<Metadata> details = new LinkedList<>(); for (T definition : items.getList()) { details.add(definition.getMetadata()); } return new PartialList<>(details, items.getOffset(), items.getPageSize(), items.getTotalSize()); }
/** * Return an updated condition that do not contain a condition on the segmentId anymore it's * remove the unnecessary boolean condition (if a condition is the only one of a boolean the * boolean will be remove and the subcondition returned) it's return null when there is no more * condition after (if the condition passed was only a segment condition on the segmentId) * * @param condition the condition to update * @param segmentId the segment id to remove in the condition * @return updated condition */ private Condition updateSegmentDependentCondition(Condition condition, String segmentId) { if ("booleanCondition".equals(condition.getConditionTypeId())) { @SuppressWarnings("unchecked") final List<Condition> subConditions = (List<Condition>) condition.getParameter("subConditions"); List<Condition> updatedSubConditions = new LinkedList<>(); for (Condition subCondition : subConditions) { Condition updatedCondition = updateSegmentDependentCondition(subCondition, segmentId); if (updatedCondition != null) { updatedSubConditions.add(updatedCondition); } } if (!updatedSubConditions.isEmpty()) { if (updatedSubConditions.size() == 1) { return updatedSubConditions.get(0); } else { condition.setParameter("subConditions", updatedSubConditions); return condition; } } else { return null; } } else if ("profileSegmentCondition".equals(condition.getConditionTypeId())) { @SuppressWarnings("unchecked") final List<String> referencedSegmentIds = (List<String>) condition.getParameter("segments"); if (referencedSegmentIds.indexOf(segmentId) >= 0) { referencedSegmentIds.remove(segmentId); if (referencedSegmentIds.isEmpty()) { return null; } else { condition.setParameter("segments", referencedSegmentIds); } } } return condition; }
public DependentMetadata removeSegmentDefinition(String segmentId, boolean validate) { Set<Segment> impactedSegments = getSegmentDependentSegments(segmentId); Set<Scoring> impactedScorings = getSegmentDependentScorings(segmentId); if (!validate || (impactedSegments.isEmpty() && impactedScorings.isEmpty())) { // update profiles Condition segmentCondition = new Condition(); segmentCondition.setConditionType( definitionsService.getConditionType("profilePropertyCondition")); segmentCondition.setParameter("propertyName", "segments"); segmentCondition.setParameter("comparisonOperator", "equals"); segmentCondition.setParameter("propertyValue", segmentId); List<Profile> previousProfiles = persistenceService.query(segmentCondition, null, Profile.class); for (Profile profileToRemove : previousProfiles) { profileToRemove.getSegments().remove(segmentId); persistenceService.update( profileToRemove.getItemId(), null, Profile.class, "segments", profileToRemove.getSegments()); } // update impacted segments for (Segment segment : impactedSegments) { Condition updatedCondition = updateSegmentDependentCondition(segment.getCondition(), segmentId); segment.setCondition(updatedCondition); if (updatedCondition == null) { clearAutoGeneratedRules( persistenceService.query( "linkedItems", segment.getMetadata().getId(), null, Rule.class), segment.getMetadata().getId()); segment.getMetadata().setEnabled(false); } setSegmentDefinition(segment); } // update impacted scorings for (Scoring scoring : impactedScorings) { List<ScoringElement> updatedScoringElements = new ArrayList<>(); for (ScoringElement scoringElement : scoring.getElements()) { Condition updatedCondition = updateSegmentDependentCondition(scoringElement.getCondition(), segmentId); if (updatedCondition != null) { scoringElement.setCondition(updatedCondition); updatedScoringElements.add(scoringElement); } } scoring.setElements(updatedScoringElements); if (updatedScoringElements.isEmpty()) { clearAutoGeneratedRules( persistenceService.query( "linkedItems", scoring.getMetadata().getId(), null, Rule.class), scoring.getMetadata().getId()); scoring.getMetadata().setEnabled(false); } setScoringDefinition(scoring); } persistenceService.remove(segmentId, Segment.class); List<Rule> previousRules = persistenceService.query("linkedItems", segmentId, null, Rule.class); clearAutoGeneratedRules(previousRules, segmentId); } List<Metadata> segments = new LinkedList<>(); List<Metadata> scorings = new LinkedList<>(); for (Segment definition : impactedSegments) { segments.add(definition.getMetadata()); } for (Scoring definition : impactedScorings) { scorings.add(definition.getMetadata()); } return new DependentMetadata(segments, scorings); }