Esempio n. 1
0
  private void updateExistingProfilesForSegment(Segment segment) {
    long t = System.currentTimeMillis();
    Condition segmentCondition = new Condition();

    segmentCondition.setConditionType(
        definitionsService.getConditionType("profilePropertyCondition"));
    segmentCondition.setParameter("propertyName", "segments");
    segmentCondition.setParameter("comparisonOperator", "equals");
    segmentCondition.setParameter("propertyValue", segment.getItemId());

    if (segment.getMetadata().isEnabled()) {
      List<Profile> previousProfiles =
          persistenceService.query(segmentCondition, null, Profile.class);
      List<Profile> newProfiles =
          persistenceService.query(segment.getCondition(), null, Profile.class);

      List<Profile> add = new ArrayList<>(newProfiles);
      add.removeAll(previousProfiles);
      previousProfiles.removeAll(newProfiles);

      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);
        eventService.send(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);
        eventService.send(profileUpdated);
      }

    } else {
      List<Profile> previousProfiles =
          persistenceService.query(segmentCondition, null, Profile.class);
      for (Profile profileToRemove : previousProfiles) {
        profileToRemove.getSegments().remove(segment.getItemId());
        persistenceService.update(
            profileToRemove.getItemId(),
            null,
            Profile.class,
            "segments",
            profileToRemove.getSegments());
      }
    }
    logger.info("Profiles updated in {}", System.currentTimeMillis() - t);
  }
Esempio n. 2
0
  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);
            }
          }
        }
      }
    }
  }
Esempio n. 3
0
  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);
  }
Esempio n. 4
0
  public void createScoringDefinition(
      String scope, String scoringId, String name, String description) {
    Metadata metadata = new Metadata(scope, scoringId, name, description);
    Scoring scoring = new Scoring(metadata);
    Condition rootCondition = new Condition();
    rootCondition.setConditionType(definitionsService.getConditionType("booleanCondition"));
    rootCondition.setParameter("operator", "and");
    rootCondition.setParameter("subConditions", new ArrayList<Condition>());
    scoring.setElements(new ArrayList<ScoringElement>());

    setScoringDefinition(scoring);
  }
Esempio n. 5
0
  public long getMatchingIndividualsCount(String segmentID) {
    if (getSegmentDefinition(segmentID) == null) {
      return 0;
    }

    Condition segmentCondition =
        new Condition(definitionsService.getConditionType("profilePropertyCondition"));
    segmentCondition.setParameter("propertyName", "segments");
    segmentCondition.setParameter("comparisonOperator", "equals");
    segmentCondition.setParameter("propertyValue", segmentID);

    return persistenceService.queryCount(segmentCondition, Profile.ITEM_TYPE);
  }
Esempio n. 6
0
  public PartialList<Profile> getMatchingIndividuals(
      String segmentID, int offset, int size, String sortBy) {
    Segment segment = getSegmentDefinition(segmentID);
    if (segment == null) {
      return new PartialList<Profile>();
    }
    Condition segmentCondition =
        new Condition(definitionsService.getConditionType("profilePropertyCondition"));
    segmentCondition.setParameter("propertyName", "segments");
    segmentCondition.setParameter("comparisonOperator", "equals");
    segmentCondition.setParameter("propertyValue", segmentID);

    return persistenceService.query(segmentCondition, sortBy, Profile.class, offset, size);
  }
Esempio n. 7
0
 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());
 }
Esempio n. 8
0
  private void updateExistingProfilesForScoring(Scoring scoring) {
    long t = System.currentTimeMillis();
    Condition scoringCondition = new Condition();
    scoringCondition.setConditionType(
        definitionsService.getConditionType("profilePropertyCondition"));
    scoringCondition.setParameter("propertyName", "scores." + scoring.getItemId());
    scoringCondition.setParameter("comparisonOperator", "exists");
    List<Profile> previousProfiles =
        persistenceService.query(scoringCondition, null, Profile.class);

    HashMap<String, Object> scriptParams = new HashMap<>();
    scriptParams.put("scoringId", scoring.getItemId());

    for (Profile profileToRemove : previousProfiles) {
      persistenceService.updateWithScript(
          profileToRemove.getItemId(),
          null,
          Profile.class,
          "if (ctx._source.systemProperties.scoreModifiers == null) { ctx._source.systemProperties.scoreModifiers=[:] } ; if (ctx._source.systemProperties.scoreModifiers.containsKey(scoringId)) { ctx._source.scores[scoringId] = ctx._source.systemProperties.scoreModifiers[scoringId] } else { ctx._source.scores.remove(scoringId) }",
          scriptParams);
    }
    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);
          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);
  }
Esempio n. 9
0
  private void updateExistingProfilesForRemovedScoring(String scoringId) {
    long t = System.currentTimeMillis();
    Condition scoringCondition = new Condition();
    scoringCondition.setConditionType(
        definitionsService.getConditionType("profilePropertyCondition"));
    scoringCondition.setParameter("propertyName", "scores." + scoringId);
    scoringCondition.setParameter("comparisonOperator", "exists");
    List<Profile> previousProfiles =
        persistenceService.query(scoringCondition, null, Profile.class);

    HashMap<String, Object> scriptParams = new HashMap<>();
    scriptParams.put("scoringId", scoringId);

    for (Profile profileToRemove : previousProfiles) {
      persistenceService.updateWithScript(
          profileToRemove.getItemId(),
          null,
          Profile.class,
          "ctx._source.scores.remove(scoringId)",
          scriptParams);
    }
    logger.info("Profiles updated in {}", System.currentTimeMillis() - t);
  }
Esempio n. 10
0
  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);
  }