Search in sources :

Example 11 with CustomGroupingOperation

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

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

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

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

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

Aggregations

BasicDBObject (com.mongodb.BasicDBObject)22 CustomGroupingOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomGroupingOperation)22 CustomProjectionOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation)22 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)22 DBObject (com.mongodb.DBObject)20 ApiOperation (io.swagger.annotations.ApiOperation)18 Aggregation.newAggregation (org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation)18 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)18 CustomUnwindOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomUnwindOperation)4 CustomSortingOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomSortingOperation)2 Criteria (org.springframework.data.mongodb.core.query.Criteria)2