Search in sources :

Example 6 with Aggregation

use of org.springframework.data.mongodb.core.aggregation.Aggregation in project ocvn 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, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE_REF);
    project.put("awards.value.amount", 1);
    project.put("totalAwardsWithTender", new BasicDBObject("$cond", Arrays.asList(new BasicDBObject("$gt", Arrays.asList(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE_REF, null)), 1, 0)));
    project.put("awardsWithTenderValue", new BasicDBObject("$cond", Arrays.asList(new BasicDBObject("$gt", Arrays.asList(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE_REF, null)), "$awards.value.amount", 0)));
    Aggregation agg = Aggregation.newAggregation(match(where("awards").elemMatch(where("status").is(Award.Status.active.toString())).and("awards.date").exists(true).and(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).exists(true)), getMatchDefaultFilterOperation(filter), unwind("$awards"), match(where("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()));
    AggregationResults<DBObject> results = mongoTemplate.aggregate(agg, "release", DBObject.class);
    List<DBObject> tagCount = results.getMappedResults();
    return tagCount;
}
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)

Example 7 with Aggregation

use of org.springframework.data.mongodb.core.aggregation.Aggregation in project ocvn by devgateway.

the class TenderPercentagesController method percentTendersUsingEBid.

@ApiOperation("Returns the percent of tenders with active awards, " + "with tender.submissionMethod='electronicSubmission'." + "The endpoint also returns the total tenderds with active awards and the count of tenders with " + "tender.submissionMethod='electronicSubmission")
@RequestMapping(value = "/api/percentTendersUsingEBid", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> percentTendersUsingEBid(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
    DBObject project1 = new BasicDBObject();
    addYearlyMonthlyProjection(filter, project1, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE_REF);
    project1.put(Fields.UNDERSCORE_ID, "$tender._id");
    project1.put("electronicSubmission", new BasicDBObject("$cond", Arrays.asList(new BasicDBObject("$eq", Arrays.asList("$tender.submissionMethod", Tender.SubmissionMethod.electronicSubmission.toString())), 1, 0)));
    DBObject group1 = new BasicDBObject();
    group1.put(Fields.UNDERSCORE_ID, Fields.UNDERSCORE_ID_REF);
    addYearlyMonthlyGroupingOperationFirst(filter, group1);
    group1.put("electronicSubmission", new BasicDBObject("$max", "$electronicSubmission"));
    DBObject group2 = new BasicDBObject();
    addYearlyMonthlyReferenceToGroup(filter, group2);
    group2.put(Keys.TOTAL_TENDERS, new BasicDBObject("$sum", 1));
    group2.put(Keys.TOTAL_TENDERS_USING_EBID, new BasicDBObject("$sum", "$electronicSubmission"));
    DBObject project2 = new BasicDBObject();
    project2.put(Keys.TOTAL_TENDERS, 1);
    project2.put(Keys.TOTAL_TENDERS_USING_EBID, 1);
    project2.put(Keys.PERCENTAGE_TENDERS_USING_EBID, new BasicDBObject("$multiply", Arrays.asList(new BasicDBObject("$divide", Arrays.asList("$totalTendersUsingEbid", "$totalTenders")), 100)));
    Aggregation agg = newAggregation(match(where(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).exists(true).and("tender.submissionMethod.0").exists(true).and("awards.status").is("active").andOperator(getYearDefaultFilterCriteria(filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))), unwind("$tender.submissionMethod"), new CustomProjectionOperation(project1), new CustomGroupingOperation(group1), new CustomGroupingOperation(group2), new CustomProjectionOperation(project2), transformYearlyGrouping(filter).andInclude(Keys.TOTAL_TENDERS, Keys.TOTAL_TENDERS_USING_EBID, Keys.PERCENTAGE_TENDERS_USING_EBID), getSortByYearMonth(filter), skip(filter.getSkip()), limit(filter.getPageSize()));
    AggregationResults<DBObject> results = mongoTemplate.aggregate(agg, "release", DBObject.class);
    List<DBObject> list = results.getMappedResults();
    return list;
}
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) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with Aggregation

use of org.springframework.data.mongodb.core.aggregation.Aggregation in project ocvn by devgateway.

the class TenderPercentagesController method percentTendersUsingEgp.

@ApiOperation("Returns the percent of tenders that are using eProcurement." + " This is read from tender.publicationMethod='eGP'")
@RequestMapping(value = "/api/percentTendersUsingEgp", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> percentTendersUsingEgp(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
    DBObject project1 = new BasicDBObject();
    addYearlyMonthlyProjection(filter, project1, "$tender.tenderPeriod.startDate");
    project1.put("tender.publicationMethod", 1);
    DBObject group = new BasicDBObject();
    addYearlyMonthlyReferenceToGroup(filter, group);
    group.put("totalTenders", new BasicDBObject("$sum", 1));
    group.put(Keys.TOTAL_EGP, new BasicDBObject("$sum", new BasicDBObject("$cond", Arrays.asList(new BasicDBObject("$eq", Arrays.asList("$tender.publicationMethod", "eGP")), 1, 0))));
    DBObject project2 = new BasicDBObject();
    project2.put("totalTenders", 1);
    project2.put(Keys.TOTAL_EGP, 1);
    project2.put("year", 1);
    project2.put("month", 1);
    project2.put("percentEgp", new BasicDBObject("$multiply", Arrays.asList(new BasicDBObject("$divide", Arrays.asList("$totalEgp", "$totalTenders")), 100)));
    Aggregation agg = newAggregation(match(where("tender.tenderPeriod.startDate").exists(true).andOperator(getYearDefaultFilterCriteria(filter, "tender.tenderPeriod.startDate"))), new CustomProjectionOperation(project1), new CustomGroupingOperation(group), transformYearlyGrouping(filter).andInclude("totalTenders", "percentEgp", Keys.TOTAL_EGP), new CustomProjectionOperation(project2), getSortByYearMonth(filter), skip(filter.getSkip()), limit(filter.getPageSize()));
    AggregationResults<DBObject> results = mongoTemplate.aggregate(agg, "release", DBObject.class);
    List<DBObject> list = results.getMappedResults();
    return list;
}
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) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with Aggregation

use of org.springframework.data.mongodb.core.aggregation.Aggregation in project ocvn by devgateway.

the class TenderPercentagesController method percentTendersCancelled.

@ApiOperation("Returns the percent of tenders that were cancelled, grouped by year." + " The year is taken from tender.tenderPeriod.startDate. The response also contains the" + " total number of tenders and total number of cancelled tenders for each year.")
@RequestMapping(value = "/api/percentTendersCancelled", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> percentTendersCancelled(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
    DBObject project1 = new BasicDBObject();
    addYearlyMonthlyProjection(filter, project1, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE_REF);
    project1.put("tender.status", 1);
    DBObject group = new BasicDBObject();
    addYearlyMonthlyReferenceToGroup(filter, group);
    group.put(Keys.TOTAL_TENDERS, new BasicDBObject("$sum", 1));
    group.put(Keys.TOTAL_CANCELLED, new BasicDBObject("$sum", new BasicDBObject("$cond", Arrays.asList(new BasicDBObject("$eq", Arrays.asList("$tender.status", "cancelled")), 1, 0))));
    DBObject project2 = new BasicDBObject();
    project2.put(Keys.TOTAL_TENDERS, 1);
    project2.put(Keys.TOTAL_CANCELLED, 1);
    project2.put(Keys.PERCENT_CANCELLED, new BasicDBObject("$multiply", Arrays.asList(new BasicDBObject("$divide", Arrays.asList("$totalCancelled", "$totalTenders")), 100)));
    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), new CustomGroupingOperation(group), new CustomProjectionOperation(project2), transformYearlyGrouping(filter).andInclude(Keys.TOTAL_TENDERS, Keys.TOTAL_CANCELLED, Keys.PERCENT_TENDERS, Keys.PERCENT_CANCELLED), getSortByYearMonth(filter), skip(filter.getSkip()), limit(filter.getPageSize()));
    AggregationResults<DBObject> results = mongoTemplate.aggregate(agg, "release", DBObject.class);
    List<DBObject> list = results.getMappedResults();
    return list;
}
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) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 10 with Aggregation

use of org.springframework.data.mongodb.core.aggregation.Aggregation in project ocvn 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("awards.suppliers._id", 1);
    project.put("awards.value.amount", 1);
    project.put("tender.procuringEntity._id", 1);
    BasicDBObject group = new BasicDBObject();
    group.put(Fields.UNDERSCORE_ID, "$awards.suppliers._id");
    group.put(Keys.TOTAL_AWARD_AMOUNT, new BasicDBObject("$sum", "$awards.value.amount"));
    group.put(Keys.TOTAL_CONTRACTS, new BasicDBObject("$sum", 1));
    group.put(Keys.PROCURING_ENTITY_IDS, new BasicDBObject("$addToSet", "$tender.procuringEntity._id"));
    Aggregation agg = newAggregation(match(where("awards.value.amount").exists(true).and("awards.status").is("active").andOperator(getDefaultFilterCriteria(filter))), unwind("$awards"), match(where("awards.status").is("active")), unwind("$awards.suppliers"), match(getYearFilterCriteria(filter, "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));
    AggregationResults<DBObject> results = mongoTemplate.aggregate(agg, "release", DBObject.class);
    List<DBObject> tagCount = results.getMappedResults();
    return tagCount;
}
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) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

DBObject (com.mongodb.DBObject)51 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)51 ApiOperation (io.swagger.annotations.ApiOperation)44 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)44 BasicDBObject (com.mongodb.BasicDBObject)41 Aggregation.newAggregation (org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation)40 CustomProjectionOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation)30 CustomGroupingOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomGroupingOperation)12 CustomOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomOperation)9 JsonView (com.fasterxml.jackson.annotation.JsonView)4 CustomUnwindOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomUnwindOperation)2 Date (java.util.Date)1 CustomSortingOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomSortingOperation)1 Criteria (org.springframework.data.mongodb.core.query.Criteria)1