Search in sources :

Example 6 with CustomProjectionOperation

use of org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation 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 7 with CustomProjectionOperation

use of org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation 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 8 with CustomProjectionOperation

use of org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation 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 9 with CustomProjectionOperation

use of org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation 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)

Example 10 with CustomProjectionOperation

use of org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation 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)

Aggregations

BasicDBObject (com.mongodb.BasicDBObject)30 DBObject (com.mongodb.DBObject)30 CustomProjectionOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation)30 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)30 ApiOperation (io.swagger.annotations.ApiOperation)26 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)26 Aggregation.newAggregation (org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation)25 CustomGroupingOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomGroupingOperation)12 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