Example usage for org.springframework.data.mongodb.core.aggregation GroupOperation GroupOperation

List of usage examples for org.springframework.data.mongodb.core.aggregation GroupOperation GroupOperation

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.core.aggregation GroupOperation GroupOperation.

Prototype

protected GroupOperation(GroupOperation groupOperation) 

Source Link

Document

Creates a new GroupOperation from the given GroupOperation .

Usage

From source file:com.epam.ta.reportportal.database.dao.TestItemRepositoryCustomImpl.java

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override/*from  www.  ja  v a  2 s. c  om*/
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;
}