Search in sources :

Example 91 with Aggregation

use of org.springframework.data.mongodb.core.aggregation.Aggregation in project oc-explorer by devgateway.

the class AwardsWonLostController method buyersProcuringEntities.

@ApiOperation(value = "List of buyers with releases for the given procuring entities." + "procuringEntityId is mandatory")
@RequestMapping(value = "/api/buyersForProcuringEntities", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> buyersProcuringEntities(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
    Assert.notEmpty(filter.getProcuringEntityId(), "procuringEntityId must not be empty!");
    Aggregation agg = newAggregation(match(where(MongoConstants.FieldNames.BUYER_ID).exists(true).andOperator(getYearDefaultFilterCriteria(filter, TENDER_PERIOD_START_DATE))), group(Fields.from(Fields.field("buyerId", MongoConstants.FieldNames.BUYER_ID), Fields.field("buyerName", MongoConstants.FieldNames.BUYER_NAME), Fields.field("procuringEntityId", MongoConstants.FieldNames.TENDER_PROCURING_ENTITY_ID))));
    return releaseAgg(agg);
}
Also used : Aggregation.newAggregation(org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation) Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 92 with Aggregation

use of org.springframework.data.mongodb.core.aggregation.Aggregation in project oc-explorer by devgateway.

the class AwardsWonLostController method procurementsWonLostPerProcuringEntity.

@ApiOperation(value = "Counts the number of wins per supplierId per procuringEntityId, plus shows the flags" + " and the awarded total. You must provide supplierId parameter. Any other filter can be used as well.")
@RequestMapping(value = "/api/supplierWinsPerProcuringEntity", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> procurementsWonLostPerProcuringEntity(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
    Assert.notEmpty(filter.getSupplierId(), "supplierId must not be empty!");
    Aggregation agg = newAggregation(match(where(AWARDS_STATUS).is(Award.Status.active.toString()).andOperator(getYearDefaultFilterCriteria(filter, TENDER_PERIOD_START_DATE)).and(MongoConstants.FieldNames.TENDER_PROCURING_ENTITY_ID).exists(true).and(MongoConstants.FieldNames.TENDER_PROCURING_ENTITY_NAME).exists(true)), unwind("awards"), unwind("awards.suppliers"), match(where(AWARDS_STATUS).is(Award.Status.active.toString()).andOperator(getYearDefaultFilterCriteria(filter.awardFiltering(), TENDER_PERIOD_START_DATE))), group(Fields.from(field("supplierId", MongoConstants.FieldNames.AWARDS_SUPPLIERS_ID), field("procuringEntityName", MongoConstants.FieldNames.TENDER_PROCURING_ENTITY_NAME), field("procuringEntityId", MongoConstants.FieldNames.TENDER_PROCURING_ENTITY_ID))).count().as("count").sum(MongoConstants.FieldNames.AWARDS_VALUE_AMOUNT).as("totalAmountAwarded").sum(FLAGS_TOTAL_FLAGGED).as("countFlags"), sort(Sort.Direction.DESC, "count"));
    return releaseAgg(agg);
}
Also used : Aggregation.newAggregation(org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation) Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 93 with Aggregation

use of org.springframework.data.mongodb.core.aggregation.Aggregation in project oc-explorer by devgateway.

the class CorruptionRiskDashboardIndicatorsStatsController method totalProjectsByYear.

@ApiOperation(value = "Count total projects by year/month.")
@RequestMapping(value = "/api/totalProjectsByYear", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> totalProjectsByYear(final YearFilterPagingRequest filter) {
    DBObject project1 = new BasicDBObject();
    addYearlyMonthlyProjection(filter, project1, ref(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE));
    project1.put(Fields.UNDERSCORE_ID, 0);
    Aggregation agg = newAggregation(match(where(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).exists(true).andOperator(getYearDefaultFilterCriteria(filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))), new CustomProjectionOperation(project1), group(getYearlyMonthlyGroupingFields(filter)).count().as(Keys.PROJECT_COUNT), transformYearlyGrouping(filter).andInclude(Keys.PROJECT_COUNT), getSortByYearMonth(filter));
    return releaseAgg(agg);
}
Also used : Aggregation.newAggregation(org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation) Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) BasicDBObject(com.mongodb.BasicDBObject) CustomProjectionOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 94 with Aggregation

use of org.springframework.data.mongodb.core.aggregation.Aggregation in project oc-explorer by devgateway.

the class CorruptionRiskDashboardIndicatorsStatsController method totalProjectsByIndicatorTypeByYear.

private List<DBObject> totalProjectsByIndicatorTypeByYear(String statsProperty, final YearFilterPagingRequest filter) {
    DBObject project1 = new BasicDBObject();
    addYearlyMonthlyProjection(filter, project1, ref(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE));
    project1.put("stats", "$flags." + statsProperty);
    project1.put(Fields.UNDERSCORE_ID, 0);
    Aggregation agg = newAggregation(match(where(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).exists(true).and("flags." + statsProperty + ".0").exists(true).andOperator(getYearDefaultFilterCriteria(filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))), unwind("flags." + statsProperty), new CustomProjectionOperation(project1), group(getYearlyMonthlyGroupingFields(filter, "stats.type")).sum("stats.count").as(statsProperty.equals(ELIGIBLE_STATS) ? Keys.ELIGIBLE_COUNT : Keys.FLAGGED_COUNT).count().as(statsProperty.equals(ELIGIBLE_STATS) ? Keys.ELIGIBLE_PROJECT_COUNT : Keys.FLAGGED_PROJECT_COUNT), getSortByYearMonthWhenOtherGroups(filter, "_id.type"));
    return releaseAgg(agg);
}
Also used : Aggregation.newAggregation(org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation) Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) BasicDBObject(com.mongodb.BasicDBObject) CustomProjectionOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Example 95 with Aggregation

use of org.springframework.data.mongodb.core.aggregation.Aggregation in project oc-explorer by devgateway.

the class CostEffectivenessVisualsController method costEffectivenessAwardAmount.

@ApiOperation(value = "Cost effectiveness of Awards: Displays the total amount of active awards grouped by year." + "The tender entity, for each award, has to have amount value. The year is calculated from " + MongoConstants.FieldNames.TENDER_PERIOD_START_DATE)
@RequestMapping(value = "/api/costEffectivenessAwardAmount", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> costEffectivenessAwardAmount(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
    DBObject project = new BasicDBObject();
    addYearlyMonthlyProjection(filter, project, ref(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE));
    project.put(MongoConstants.FieldNames.AWARDS_VALUE_AMOUNT, 1);
    project.put("totalAwardsWithTender", new BasicDBObject("$cond", Arrays.asList(new BasicDBObject("$gt", Arrays.asList(ref(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE), null)), 1, 0)));
    project.put("awardsWithTenderValue", new BasicDBObject("$cond", Arrays.asList(new BasicDBObject("$gt", Arrays.asList(ref(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE), null)), "$awards.value.amount", 0)));
    Aggregation agg = Aggregation.newAggregation(match(where("awards").elemMatch(where("status").is(Award.Status.active.toString())).and(MongoConstants.FieldNames.AWARDS_DATE).exists(true).and(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).exists(true)), getMatchDefaultFilterOperation(filter), unwind("awards"), match(where(MongoConstants.FieldNames.AWARDS_STATUS).is(Award.Status.active.toString()).and("awards.value").exists(true).andOperator(getYearDefaultFilterCriteria(filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))), new CustomProjectionOperation(project), getYearlyMonthlyGroupingOperation(filter).sum("awardsWithTenderValue").as(Keys.TOTAL_AWARD_AMOUNT).count().as(Keys.TOTAL_AWARDS).sum("totalAwardsWithTender").as(Keys.TOTAL_AWARDS_WITH_TENDER), project(Fields.UNDERSCORE_ID, Keys.TOTAL_AWARD_AMOUNT, Keys.TOTAL_AWARDS, Keys.TOTAL_AWARDS_WITH_TENDER).and(Keys.TOTAL_AWARDS_WITH_TENDER).divide(Keys.TOTAL_AWARDS).as(Keys.FRACTION_AWARDS_WITH_TENDER), project(Fields.UNDERSCORE_ID, Keys.TOTAL_AWARD_AMOUNT, Keys.TOTAL_AWARDS, Keys.TOTAL_AWARDS_WITH_TENDER, Keys.FRACTION_AWARDS_WITH_TENDER).and(Keys.FRACTION_AWARDS_WITH_TENDER).multiply(100).as(Keys.PERCENTAGE_AWARDS_WITH_TENDER), transformYearlyGrouping(filter).andInclude(Keys.TOTAL_AWARD_AMOUNT, Keys.TOTAL_AWARDS, Keys.TOTAL_AWARDS_WITH_TENDER, Keys.PERCENTAGE_AWARDS_WITH_TENDER), getSortByYearMonth(filter), skip(filter.getSkip()), limit(filter.getPageSize()));
    return releaseAgg(agg);
}
Also used : Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) BasicDBObject(com.mongodb.BasicDBObject) CustomProjectionOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)113 DBObject (com.mongodb.DBObject)79 ApiOperation (io.swagger.annotations.ApiOperation)79 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)79 BasicDBObject (com.mongodb.BasicDBObject)73 Aggregation.newAggregation (org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation)73 CustomProjectionOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation)55 CustomGroupingOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomGroupingOperation)22 Test (org.junit.Test)20 CustomOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomOperation)16 Document (org.bson.Document)14 MongoVersion (org.springframework.data.mongodb.test.util.MongoVersion)5 JsonView (com.fasterxml.jackson.annotation.JsonView)4 Criteria (org.springframework.data.mongodb.core.query.Criteria)4 CustomUnwindOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomUnwindOperation)3 ChangeStreamDocument (com.mongodb.client.model.changestream.ChangeStreamDocument)2 FullDocument (com.mongodb.client.model.changestream.FullDocument)2 List (java.util.List)2 TypedAggregation (org.springframework.data.mongodb.core.aggregation.TypedAggregation)2 NearQuery (org.springframework.data.mongodb.core.query.NearQuery)2