Skip to content
This repository has been archived by the owner on Sep 23, 2020. It is now read-only.

Commit

Permalink
fix(operations): resolve historic operational metrics regex match exp…
Browse files Browse the repository at this point in the history
…ression
  • Loading branch information
kdaimiel committed Apr 16, 2018
1 parent cce2a47 commit e5847ca
Showing 1 changed file with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import com.bbva.arq.devops.ae.mirrorgate.model.HistoricUserMetric;
import com.bbva.arq.devops.ae.mirrorgate.model.HistoricUserMetricStats;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
Expand All @@ -41,13 +43,13 @@ public HistoricUserMetricRepositoryImpl(MongoTemplate mongoTemplate){
}

@Override
public List<HistoricUserMetricStats> getUserMetricTendencyForPeriod(List<String> views, ChronoUnit unit, long timestamp) {
public List<HistoricUserMetricStats> getUserMetricTendencyForPeriod(List<String> viewIds, ChronoUnit unit, long timestamp) {

Cond sampleSizeCondition = ConditionalOperators.when(Criteria.where("sampleSize").gt(0))
.thenValueOf("$sampleSize").otherwise(1l);

TypedAggregation<HistoricUserMetric> aggregation = newAggregation(HistoricUserMetric.class,
match(Criteria.where("viewId").in(views)
match(getCriteriaExpressionsForUserMetrics(viewIds)
.and("historicType").is(unit)
.and("timestamp").gte(timestamp)),
project("identifier", "viewId", "appVersion", "platform", "name", "value", "sampleSize", "collectorId")
Expand All @@ -69,4 +71,14 @@ public List<HistoricUserMetricStats> getUserMetricTendencyForPeriod(List<String>

return groupResults.getMappedResults();
}

private Criteria getCriteriaExpressionsForUserMetrics(List<String> viewIds) {
List<Criteria> regExs = new ArrayList<>();
viewIds.forEach((String id) -> {
regExs.add(Criteria.where("viewId")
.in(Pattern.compile("^" + id + ".*$")));
});
return new Criteria()
.orOperator(regExs.toArray(new Criteria[regExs.size()]));
}
}

0 comments on commit e5847ca

Please sign in to comment.