Search in sources :

Example 41 with Aggregation

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

the class TopTenController method topTenLargestTenders.

/**
 * db.release.aggregate( [ {$match: {MongoConstants.FieldNames.TENDER_VALUE_AMOUNT: {$exists:
 * true}}}, {$project:{_id:0,"tender.value":1,
 * "tender.tenderPeriod":1,"tender.procuringEntity.name":1}}, {$sort:
 * {MongoConstants.FieldNames.TENDER_VALUE_AMOUNT:-1}}, {$limit:10} ] )
 *
 * @return
 */
@ApiOperation(value = "Returns the top ten largest active tenders." + " The amount is taken from the tender.value.amount field." + " The returned data will contain" + "the following fields: " + "tender.date, tender.value.amount, tender.tenderPeriod, " + "tender.procuringEntity.name")
@RequestMapping(value = "/api/topTenLargestTenders", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> topTenLargestTenders(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
    BasicDBObject project = new BasicDBObject();
    project.put(Fields.UNDERSCORE_ID, 0);
    project.put(MongoConstants.FieldNames.TENDER_VALUE_AMOUNT, 1);
    project.put("tender.tenderPeriod", 1);
    project.put("tender.procuringEntity.name", 1);
    Aggregation agg = newAggregation(match(where(MongoConstants.FieldNames.TENDER_VALUE_AMOUNT).exists(true).andOperator(getYearDefaultFilterCriteria(filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))), new CustomOperation(new BasicDBObject("$project", project)), sort(Direction.DESC, MongoConstants.FieldNames.TENDER_VALUE_AMOUNT), limit(10));
    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) CustomOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomOperation) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 42 with Aggregation

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

the class TopTenController method topTenLargestSuppliers.

@ApiOperation(value = "This endpoint should return the following data for the Top 10 suppliers (by award value)." + "Returns supplier id, total awarded amount of all awarded contracts, count of awarded contracts," + "Ids of the procuring entities from which they have received an award, and their count. " + "All filters ally here, the year filter uses the awards.date field.")
@RequestMapping(value = "/api/topTenSuppliers", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> topTenLargestSuppliers(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
    BasicDBObject project = new BasicDBObject();
    project.put(Fields.UNDERSCORE_ID, 0);
    project.put(MongoConstants.FieldNames.AWARDS_SUPPLIERS_ID, 1);
    project.put(MongoConstants.FieldNames.AWARDS_VALUE_AMOUNT, 1);
    project.put(MongoConstants.FieldNames.TENDER_PROCURING_ENTITY_ID, 1);
    BasicDBObject group = new BasicDBObject();
    group.put(Fields.UNDERSCORE_ID, ref(MongoConstants.FieldNames.AWARDS_SUPPLIERS_ID));
    group.put(Keys.TOTAL_AWARD_AMOUNT, new BasicDBObject("$sum", ref(MongoConstants.FieldNames.AWARDS_VALUE_AMOUNT)));
    group.put(Keys.TOTAL_CONTRACTS, new BasicDBObject("$sum", 1));
    group.put(Keys.PROCURING_ENTITY_IDS, new BasicDBObject("$addToSet", ref(MongoConstants.FieldNames.TENDER_PROCURING_ENTITY_ID)));
    Aggregation agg = newAggregation(match(where(MongoConstants.FieldNames.AWARDS_VALUE_AMOUNT).exists(true).and(MongoConstants.FieldNames.AWARDS_STATUS).is(Award.Status.active.toString()).andOperator(getDefaultFilterCriteria(filter))), unwind("awards"), match(where(MongoConstants.FieldNames.AWARDS_STATUS).is(Award.Status.active.toString())), unwind("awards.suppliers"), match(getYearFilterCriteria(filter.awardFiltering(), MongoConstants.FieldNames.AWARDS_DATE)), new CustomProjectionOperation(project), new CustomGroupingOperation(group), sort(Direction.DESC, Keys.TOTAL_AWARD_AMOUNT), limit(10), project().and(Fields.UNDERSCORE_ID).as(Keys.SUPPLIER_ID).andInclude(Keys.TOTAL_AWARD_AMOUNT, Keys.TOTAL_CONTRACTS, Keys.PROCURING_ENTITY_IDS).andExclude(Fields.UNDERSCORE_ID).and(Keys.PROCURING_ENTITY_IDS).size().as(Keys.PROCURING_ENTITY_IDS_COUNT));
    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) CustomGroupingOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomGroupingOperation) CustomProjectionOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 43 with Aggregation

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

the class TopTenController method topTenLargestAwards.

/**
 * db.release.aggregate( [ {$match: {MongoConstants.FieldNames.AWARDS_VALUE_AMOUNT: {$exists:
 * true}}}, {$unwind:"$awards"},
 * {$project:{_id:0,MongoConstants.FieldNames.AWARDS_DATE:1,
 * "awards.suppliers.name":1,"awards.value":1, "planning.budget":1}},
 * {$sort: {MongoConstants.FieldNames.AWARDS_VALUE_AMOUNT:-1}}, {$limit:10} ] )
 *
 * @return
 */
@ApiOperation(value = "Returns the top ten largest active awards." + " The amount is taken from the award.value field. The returned data will contain" + "the following fields: " + "awards.date, awards.suppliers.name, " + "awards.value.amount, awards.suppliers.name, planning.budget (if any)")
@RequestMapping(value = "/api/topTenLargestAwards", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> topTenLargestAwards(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
    BasicDBObject project = new BasicDBObject();
    project.put(Fields.UNDERSCORE_ID, 0);
    project.put(MongoConstants.FieldNames.AWARDS_DATE, 1);
    project.put("awards.suppliers.name", 1);
    project.put(MongoConstants.FieldNames.AWARDS_VALUE_AMOUNT, 1);
    project.put("planning.budget", 1);
    Aggregation agg = newAggregation(match(where(MongoConstants.FieldNames.AWARDS_VALUE_AMOUNT).exists(true).and(MongoConstants.FieldNames.AWARDS_STATUS).is(Award.Status.active.toString()).andOperator(getDefaultFilterCriteria(filter))), unwind("awards"), match(getYearFilterCriteria(filter.awardFiltering(), MongoConstants.FieldNames.AWARDS_DATE)), new CustomOperation(new BasicDBObject("$project", project)), sort(Direction.DESC, MongoConstants.FieldNames.AWARDS_VALUE_AMOUNT), limit(10));
    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) CustomOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomOperation) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 44 with Aggregation

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

the class AbstractFlagCrosstabController method flagStats.

protected List<DBObject> flagStats(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
    DBObject projectPrepare = getProjectPrepare(filter);
    DBObject group = getGroup(filter);
    DBObject projectPercentage = getProjectPercentage(filter);
    Aggregation agg = newAggregation(match(getYearDefaultFilterCriteria(filter, getYearProperty()).and(getYearProperty()).exists(true).and(getFlagProperty()).is(true)), new CustomProjectionOperation(projectPrepare), new CustomGroupingOperation(group), new CustomProjectionOperation(projectPercentage));
    return releaseAgg(agg);
}
Also used : Aggregation.newAggregation(org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation) Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) CustomGroupingOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomGroupingOperation) CustomProjectionOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject)

Example 45 with Aggregation

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

the class AbstractFlagStatsController method flagStats.

public List<DBObject> flagStats(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
    DBObject projectPrepare = getProjectPrepare(filter);
    DBObject group = getGroup(filter);
    DBObject projectPercentage = getProjectPercentage(filter);
    Aggregation agg = newAggregation(match(getYearDefaultFilterCriteria(filter, getYearProperty()).and(getYearProperty()).exists(true)), new CustomProjectionOperation(projectPrepare), new CustomGroupingOperation(group), new CustomProjectionOperation(projectPercentage), transformYearlyGrouping(filter).andInclude(GenericKeys.TOTAL, GenericKeys.TOTAL_TRUE, GenericKeys.TOTAL_FALSE, GenericKeys.TOTAL_PRECOND_MET, GenericKeys.PERCENT_TRUE_PRECOND_MET, GenericKeys.PERCENT_FALSE_PRECOND_MET, GenericKeys.PERCENT_PRECOND_MET));
    return releaseAgg(agg);
}
Also used : Aggregation.newAggregation(org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation) Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) CustomGroupingOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomGroupingOperation) CustomProjectionOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject)

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