Search in sources :

Example 11 with Aggregation

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

the class CountPlansTendersAwardsController method countAwardsByYear.

/**
     * db.release.aggregate( [ {$match : { "awards.0": { $exists: true } }},
     * {$project: {awards:1}}, {$unwind: "$awards"}, {$match: {"awards.date":
     * {$exists:true}}}, {$project: { year: {$year : "$awards.date"} } },
     * {$group: {_id: "$year", count: { $sum:1}}}, {$sort: { _id:1}} ])
     *
     * @return
     */
@ApiOperation(value = "Count the awards and group the results by year. " + "The year is calculated from the awards.date field.")
@RequestMapping(value = "/api/countAwardsByYear", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> countAwardsByYear(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
    DBObject project0 = new BasicDBObject();
    project0.put("awards", 1);
    DBObject project = new BasicDBObject();
    addYearlyMonthlyProjection(filter, project, "$awards.date");
    Aggregation agg = Aggregation.newAggregation(match(where("awards.0").exists(true).andOperator(getDefaultFilterCriteria(filter))), new CustomOperation(new BasicDBObject("$project", project0)), unwind("$awards"), match(where("awards.date").exists(true).andOperator(getYearFilterCriteria(filter, "awards.date"))), new CustomOperation(new BasicDBObject("$project", project)), group(getYearlyMonthlyGroupingFields(filter)).count().as(Keys.COUNT), transformYearlyGrouping(filter).andInclude(Keys.COUNT), 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) CustomOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomOperation) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 12 with Aggregation

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

the class FrequentSuppliersTimeIntervalController method frequentSuppliersTimeInterval.

@ApiOperation(value = "Returns the frequent suppliers of a procuringEntity split by a time interval. " + "The time interval is " + "a parameter and represents the number of days to take as interval, starting with today and going back " + "till the last award date. The awards are grouped by procuringEntity, supplier and the time interval. " + "maxAwards parameter is used to designate what is the maximum number of awards granted to one supplier " + "by the same procuringEntity inside one timeInterval. The default value for maxAwards is 3 (days) and the" + " default value for intervalDays is 365.")
@RequestMapping(value = "/api/frequentSuppliersTimeInterval", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<FrequentSuppliersResponse> frequentSuppliersTimeInterval(@RequestParam(defaultValue = "365", required = false) Integer intervalDays, @RequestParam(defaultValue = "3", required = false) Integer maxAwards, @RequestParam(required = false) Date now) {
    if (now == null) {
        now = new Date();
    }
    DBObject project = new BasicDBObject();
    project.put("tender.procuringEntity._id", 1);
    project.put("awards.suppliers._id", 1);
    project.put("awards.date", 1);
    project.put(Fields.UNDERSCORE_ID, 0);
    project.put("timeInterval", new BasicDBObject("$ceil", new BasicDBObject("$divide", Arrays.asList(new BasicDBObject("$divide", Arrays.asList(new BasicDBObject("$subtract", Arrays.asList(now, "$awards.date")), DAY_MS)), intervalDays))));
    Aggregation agg = Aggregation.newAggregation(match(where("tender.procuringEntity").exists(true).and("awards.suppliers.0").exists(true).and("awards.date").exists(true)), unwind("awards"), unwind("awards.suppliers"), new CustomProjectionOperation(project), group(Fields.from(Fields.field("procuringEntityId", "tender.procuringEntity._id"), Fields.field("supplierId", "awards.suppliers._id"), Fields.field("timeInterval", "timeInterval"))).count().as("count"), project("count").and("identifier").previousOperation(), match(where("count").gt(maxAwards)), sort(Sort.Direction.DESC, "count"));
    AggregationResults<FrequentSuppliersResponse> results = mongoTemplate.aggregate(agg, "release", FrequentSuppliersResponse.class);
    List<FrequentSuppliersResponse> list = results.getMappedResults();
    return list;
}
Also used : 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) Date(java.util.Date) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 13 with Aggregation

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

the class FrequentTenderersController method frequentTenderers.

@ApiOperation(value = "Detect frequent pairs of tenderers that apply together to bids." + "We are only showing pairs if they applied to more than one bid together." + "We are sorting the results after the number of occurences, descending." + "You can use all the filters that are available along with pagination options.")
@RequestMapping(value = "/api/frequentTenderers", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> frequentTenderers(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
    Aggregation agg = newAggregation(match(where("tender.tenderers.1").exists(true).and("awards.suppliers.0").exists(true).and("awards.status").is("active").andOperator(getYearDefaultFilterCriteria(filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))), unwind("tender.tenderers"), unwind("awards"), unwind("awards.suppliers"), match(where("awards.status").is("active")), project().and("awards.suppliers._id").as("supplierId").and("tender.tenderers._id").as("tendererId").andExclude(Fields.UNDERSCORE_ID).and(ComparisonOperators.valueOf("awards.suppliers._id").compareTo("tender.tenderers._id")).as("cmp"), match((where("cmp").ne(0))), project("supplierId", "tendererId", "cmp").and(ConditionalOperators.when(Criteria.where("cmp").is(1)).thenValueOf("$supplierId").otherwiseValueOf("$tendererId")).as("tendererId1").and(ConditionalOperators.when(Criteria.where("cmp").is(1)).thenValueOf("$tendererId").otherwiseValueOf("$supplierId")).as("tendererId2"), group("tendererId1", "tendererId2").count().as("pairCount"), sort(Sort.Direction.DESC, "pairCount"), 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.newAggregation(org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation) Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) DBObject(com.mongodb.DBObject) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 14 with Aggregation

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

the class TenderPercentagesController method avgTimeFromPlanToTenderPhase.

@ApiOperation("For all tenders that have both tender.tenderPeriod.startDate and planning.bidPlanProjectDateApprove" + "calculates the number o days from planning.bidPlanProjectDateApprove to tender.tenderPeriod.startDate" + "and creates the average. Groups results by tender year, calculatedfrom tender.tenderPeriod.startDate")
@RequestMapping(value = "/api/avgTimeFromPlanToTenderPhase", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> avgTimeFromPlanToTenderPhase(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
    DBObject timeFromPlanToTenderPhase = new BasicDBObject("$divide", Arrays.asList(new BasicDBObject("$subtract", Arrays.asList("$tender.tenderPeriod.startDate", "$planning.bidPlanProjectDateApprove")), MongoConstants.DAY_MS));
    DBObject project1 = new BasicDBObject();
    addYearlyMonthlyProjection(filter, project1, "$tender.tenderPeriod.startDate");
    project1.put("timeFromPlanToTenderPhase", timeFromPlanToTenderPhase);
    Aggregation agg = newAggregation(match(where("tender.tenderPeriod.startDate").exists(true).and("planning.budget.amount").exists(true).and("planning.bidPlanProjectDateApprove").exists(true).andOperator(getYearDefaultFilterCriteria(filter, "tender.tenderPeriod.startDate"))), new CustomProjectionOperation(project1), getYearlyMonthlyGroupingOperation(filter).avg("timeFromPlanToTenderPhase").as(Keys.AVG_TIME_FROM_PLAN_TO_TENDER_PHASE), transformYearlyGrouping(filter).andInclude(Keys.AVG_TIME_FROM_PLAN_TO_TENDER_PHASE), 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) 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 15 with Aggregation

use of org.springframework.data.mongodb.core.aggregation.Aggregation in project ocvn 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, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE_REF);
    project1.put("tender.numberOfTenderers", 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("$tender.numberOfTenderers", 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()));
    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)

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