List of usage examples for org.springframework.data.mongodb.core.aggregation Fields Fields
private Fields(List<Field> fields)
From source file:com.epam.ta.reportportal.database.dao.TestItemRepositoryCustomImpl.java
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override/*w w w . j a v a2 s .c o m*/
public Map<String, String> getMostFailedTestCases(List<Launch> launches, String criteria) {
Map<String, String> output = new HashMap<>();
List<String> launchIds = launches.stream().map(Launch::getId).collect(toList());
GroupOperation operationTotal = new GroupOperation(Fields.fields("$name")).count().as("count");
Aggregation aggregationTotal = newAggregation(match(where(LAUNCH_REFERENCE).in(launchIds)),
match(where(HAS_CHILD).is(false)), operationTotal);
AggregationResults<Map> resultTotal = mongoTemplate.aggregate(aggregationTotal, TestItem.class, Map.class);
Map<String, String> values = resultTotal.getMappedResults().stream()
.collect(toMap(key -> key.get("_id").toString(), value -> value.get("count").toString()));
GroupOperation operation = new GroupOperation(Fields.fields("$name")).count().as("count").last("$startTime")
.as("last");
Aggregation aggregation = newAggregation(match(where(LAUNCH_REFERENCE).in(launchIds)),
match(where(criteria).is(1)), match(where(HAS_CHILD).is(false)), operation);
AggregationResults<Map> result = mongoTemplate.aggregate(aggregation, TestItem.class, Map.class);
for (Map<String, ?> entry : result.getMappedResults()) {
String itemName = String.valueOf(entry.get("_id"));
String count = String.valueOf(entry.get("count"));
Date date = (Date) entry.get("last");
String total = values.get(itemName);
// FIXME Update dirty # separator with common case
// And update after {@link MostFailedTestCasesFilterStrategy}
output.put(itemName, count + "#" + date.getTime() + "#" + total);
}
return output;
}