Search in sources :

Example 36 with Aggregation

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

the class TenderPercentagesController method percentTendersWithLinkedProcurementPlan.

@ApiOperation("Percentage of tenders that are associated in releases that " + "have the planning.budget.amount non empty," + "meaning there really is a planning entity correlated with the tender entity." + "This endpoint uses tender.tenderPeriod.startDate to calculate the tender year.")
@RequestMapping(value = "/api/percentTendersWithLinkedProcurementPlan", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> percentTendersWithLinkedProcurementPlan(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
    DBObject project1 = new BasicDBObject();
    addYearlyMonthlyProjection(filter, project1, "$tender.tenderPeriod.startDate");
    project1.put("tender._id", 1);
    project1.put("planning.budget.amount", 1);
    DBObject group = new BasicDBObject();
    addYearlyMonthlyReferenceToGroup(filter, group);
    group.put(Keys.TOTAL_TENDERS, new BasicDBObject("$sum", 1));
    group.put(Keys.TOTAL_TENDERS_WITH_LINKED_PROCUREMENT_PLAN, new BasicDBObject("$sum", new BasicDBObject("$cond", Arrays.asList(new BasicDBObject("$gt", Arrays.asList("$planning.budget.amount", null)), 1, 0))));
    DBObject project2 = new BasicDBObject();
    project2.put(Keys.YEAR, 1);
    project2.put("month", 1);
    project2.put(Keys.TOTAL_TENDERS, 1);
    project2.put(Keys.TOTAL_TENDERS_WITH_LINKED_PROCUREMENT_PLAN, 1);
    project2.put(Keys.PERCENT_TENDERS, new BasicDBObject("$multiply", Arrays.asList(new BasicDBObject("$divide", Arrays.asList("$" + Keys.TOTAL_TENDERS_WITH_LINKED_PROCUREMENT_PLAN, "$" + Keys.TOTAL_TENDERS)), 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(Keys.TOTAL_TENDERS, Keys.TOTAL_TENDERS_WITH_LINKED_PROCUREMENT_PLAN, Keys.PERCENT_TENDERS), new CustomProjectionOperation(project2), getSortByYearMonth(filter), skip(filter.getSkip()), limit(filter.getPageSize()));
    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) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 37 with Aggregation

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

the class TenderPercentagesController method percentTendersWithTwoOrMoreTenderers.

@ApiOperation("Percentage of tenders with >1 tenderer/bidder): " + "Count of tenders with numberOfTenderers >1 divided by total count of tenders." + "This endpoint uses tender.tenderPeriod.startDate to calculate the tender year.")
@RequestMapping(value = "/api/percentTendersWithTwoOrMoreTenderers", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> percentTendersWithTwoOrMoreTenderers(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
    DBObject project1 = new BasicDBObject();
    addYearlyMonthlyProjection(filter, project1, ref(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE));
    project1.put(MongoConstants.FieldNames.TENDER_NO_TENDERERS, 1);
    DBObject group = new BasicDBObject();
    addYearlyMonthlyReferenceToGroup(filter, group);
    group.put(Keys.TOTAL_TENDERS, new BasicDBObject("$sum", 1));
    group.put("totalTendersWithTwoOrMoreTenderers", new BasicDBObject("$sum", new BasicDBObject("$cond", Arrays.asList(new BasicDBObject("$gt", Arrays.asList(ref(MongoConstants.FieldNames.TENDER_NO_TENDERERS), 1)), 1, 0))));
    DBObject project2 = new BasicDBObject();
    project2.put(Keys.TOTAL_TENDERS, 1);
    project2.put(Keys.TOTAL_TENDERS_WITH_TWO_OR_MORE_TENDERERS, 1);
    project2.put(Keys.PERCENT_TENDERS, new BasicDBObject("$multiply", Arrays.asList(new BasicDBObject("$divide", Arrays.asList("$totalTendersWithTwoOrMoreTenderers", "$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_TENDERS_WITH_TWO_OR_MORE_TENDERERS, Keys.PERCENT_TENDERS), getSortByYearMonth(filter), skip(filter.getSkip()), limit(filter.getPageSize()));
    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) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 38 with Aggregation

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

the class TenderPriceByTypeYearController method tenderPriceByProcurementMethod.

@ApiOperation(value = "Returns the tender price by OCDS type (procurementMethod), by year. " + "The OCDS type is read from tender.procurementMethod. The tender price is read from " + MongoConstants.FieldNames.TENDER_VALUE_AMOUNT)
@RequestMapping(value = "/api/tenderPriceByProcurementMethod", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> tenderPriceByProcurementMethod(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
    DBObject project = new BasicDBObject();
    project.put("tender." + Keys.PROCUREMENT_METHOD, 1);
    project.put("tender.value", 1);
    Aggregation agg = newAggregation(match(where("awards").elemMatch(where("status").is(Award.Status.active.toString())).and("tender.value").exists(true).andOperator(getYearDefaultFilterCriteria(filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))), new CustomProjectionOperation(project), group("tender." + Keys.PROCUREMENT_METHOD).sum(ref(MongoConstants.FieldNames.TENDER_VALUE_AMOUNT)).as(Keys.TOTAL_TENDER_AMOUNT), project().and(Fields.UNDERSCORE_ID).as(Keys.PROCUREMENT_METHOD).andInclude(Keys.TOTAL_TENDER_AMOUNT).andExclude(Fields.UNDERSCORE_ID), sort(Direction.DESC, Keys.TOTAL_TENDER_AMOUNT));
    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) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 39 with Aggregation

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

the class TendersAwardsYears method tendersAwardsYears.

@ApiOperation(value = "Computes all available years from awards.date, tender.tenderPeriod.startDate")
@RequestMapping(value = "/api/tendersAwardsYears", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> tendersAwardsYears() {
    BasicDBObject project1 = new BasicDBObject();
    project1.put("tenderYear", new BasicDBObject("$cond", Arrays.asList(new BasicDBObject("$gt", Arrays.asList(ref(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE), null)), new BasicDBObject("$year", ref(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE)), null)));
    project1.put("awardYear", new BasicDBObject("$cond", Arrays.asList(new BasicDBObject("$gt", Arrays.asList(ref(MongoConstants.FieldNames.AWARDS_DATE), null)), new BasicDBObject("$year", ref(MongoConstants.FieldNames.AWARDS_DATE)), null)));
    project1.put(Fields.UNDERSCORE_ID, 0);
    BasicDBObject project2 = new BasicDBObject();
    project2.put("year", Arrays.asList("$tenderYear", "$awardYear"));
    Aggregation agg = Aggregation.newAggregation(project().and(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).as(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).and(MongoConstants.FieldNames.AWARDS_DATE).as(MongoConstants.FieldNames.AWARDS_DATE), match(new Criteria().orOperator(where(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).exists(true), where(MongoConstants.FieldNames.AWARDS_DATE).exists(true))), new CustomUnwindOperation("$awards", true), new CustomProjectionOperation(project1), new CustomProjectionOperation(project2), new CustomUnwindOperation("$year"), match(where("year").ne(null)), new CustomGroupingOperation(new BasicDBObject(Fields.UNDERSCORE_ID, "$year")), new CustomSortingOperation(new BasicDBObject(Fields.UNDERSCORE_ID, 1)));
    return releaseAgg(agg);
}
Also used : Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) BasicDBObject(com.mongodb.BasicDBObject) CustomUnwindOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomUnwindOperation) CustomGroupingOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomGroupingOperation) CustomSortingOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomSortingOperation) CustomProjectionOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation) Criteria(org.springframework.data.mongodb.core.query.Criteria) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 40 with Aggregation

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

the class TendersByItemClassification method tendersByItemClassification.

@ApiOperation(value = "This should show the number of tenders per tender.items.classification." + "The tender date is taken from tender.tenderPeriod.startDate.")
@RequestMapping(value = "/api/tendersByItemClassification", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> tendersByItemClassification(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
    DBObject project = new BasicDBObject();
    project.put(Fields.UNDERSCORE_ID, 0);
    project.put("tender." + Keys.ITEMS_CLASSIFICATION, 1);
    project.put(MongoConstants.FieldNames.TENDER_VALUE_AMOUNT, 1);
    Aggregation agg = newAggregation(match(where(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).exists(true).andOperator(getYearDefaultFilterCriteria(filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))), new CustomProjectionOperation(project), unwind("tender.items"), group("$tender." + Keys.ITEMS_CLASSIFICATION).count().as(Keys.TOTAL_TENDERS).sum(MongoConstants.FieldNames.TENDER_VALUE_AMOUNT).as(Keys.TOTAL_TENDER_AMOUNT), sort(Direction.ASC, Fields.UNDERSCORE_ID));
    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) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) 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