Search in sources :

Example 21 with CustomProjectionOperation

use of org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation in project ocvn by devgateway.

the class FundingByLocationController method plannedFundingByLocation.

@ApiOperation(value = "Planned funding by location by year. Returns the total amount of planning.budget" + " available per planning.budget.projectLocation, grouped by year. " + "This will return full location information, including geocoding data." + "Responds only to the procuring entity id filter: tender.procuringEntity._id")
@RequestMapping(value = "/api/plannedFundingByLocation", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> plannedFundingByLocation(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
    DBObject vars = new BasicDBObject();
    vars.put("numberOfLocations", new BasicDBObject("$size", "$planning.budget.projectLocation"));
    vars.put("planningBudget", "$planning.budget.amount.amount");
    DBObject in = new BasicDBObject("$divide", Arrays.asList("$$planningBudget", "$$numberOfLocations"));
    DBObject let = new BasicDBObject();
    let.put("vars", vars);
    let.put("in", in);
    DBObject dividedTotal = new BasicDBObject("$let", let);
    DBObject project = new BasicDBObject();
    project.put("planning.budget.projectLocation", 1);
    project.put("cntprj", new BasicDBObject("$literal", 1));
    project.put("planning.budget.amount.amount", 1);
    project.put("dividedTotal", dividedTotal);
    addYearlyMonthlyProjection(filter, project, "$planning.bidPlanProjectDateApprove");
    Aggregation agg = newAggregation(match(where("planning").exists(true).and("planning.budget.projectLocation.0").exists(true).andOperator(getProcuringEntityIdCriteria(filter), getYearFilterCriteria(filter, "planning.bidPlanProjectDateApprove"))), new CustomProjectionOperation(project), unwind("$planning.budget.projectLocation"), match(where("planning.budget.projectLocation.geometry.coordinates.0").exists(true)), project(getYearlyMonthlyGroupingFields(filter)).and("dividedTotal").as("dividedTotal").and("cntprj").as("cntprj").and("planning.budget.projectLocation").as("projectLocation"), group(getYearlyMonthlyGroupingFields(filter, Keys.PLANNING_BUDGET_PROJECTLOCATION)).sum("dividedTotal").as(Keys.TOTAL_PLANNED_AMOUNT).sum("cntprj").as(Keys.RECORDS_COUNT), getSortByYearMonth(filter));
    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) 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 22 with CustomProjectionOperation

use of org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation in project ocvn by devgateway.

the class FundingByLocationController method fundingByTenderDeliveryLocation.

@ApiOperation(value = "Total estimated funding (tender.value) grouped by " + "tender.items.deliveryLocation and also grouped by year." + " The endpoint also returns the count of tenders for each location. " + "It responds to all filters. The year is calculated based on tender.tenderPeriod.startDate")
@RequestMapping(value = "/api/fundingByTenderDeliveryLocation", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> fundingByTenderDeliveryLocation(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
    DBObject project = new BasicDBObject();
    project.put("tender.items.deliveryLocation", 1);
    project.put("tender.value.amount", 1);
    addYearlyMonthlyProjection(filter, project, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE_REF);
    Aggregation agg = newAggregation(match(where("tender").exists(true).and(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).exists(true).andOperator(getYearDefaultFilterCriteria(filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))), new CustomProjectionOperation(project), unwind("$tender.items"), unwind("$tender.items.deliveryLocation"), project(getYearlyMonthlyGroupingFields(filter)).and("tender.value.amount").as("tenderAmount").and("tender.items.deliveryLocation").as("deliveryLocation"), match(where("deliveryLocation.geometry.coordinates.0").exists(true)), group(getYearlyMonthlyGroupingFields(filter, Keys.ITEMS_DELIVERY_LOCATION)).sum("tenderAmount").as(Keys.TOTAL_TENDERS_AMOUNT).count().as(Keys.TENDERS_COUNT), getSortByYearMonth(filter));
    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) 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 23 with CustomProjectionOperation

use of org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation in project ocvn by devgateway.

the class AverageNumberOfTenderersController method averageNumberOfTenderers.

@ApiOperation(value = "Calculate average number of tenderers, by year. The endpoint can be filtered" + "by year read from tender.tenderPeriod.startDate. " + "The number of tenderers are read from tender.numberOfTenderers")
@RequestMapping(value = "/api/averageNumberOfTenderers", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> averageNumberOfTenderers(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
    DBObject project = new BasicDBObject();
    addYearlyMonthlyProjection(filter, project, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE_REF);
    project.put("tender.numberOfTenderers", 1);
    Aggregation agg = newAggregation(match(where("tender.numberOfTenderers").gt(0).and(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).exists(true).andOperator(getYearDefaultFilterCriteria(filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))), new CustomProjectionOperation(project), group(getYearlyMonthlyGroupingFields(filter)).avg("tender.numberOfTenderers").as(Keys.AVERAGE_NO_OF_TENDERERS), transformYearlyGrouping(filter).andInclude(Keys.AVERAGE_NO_OF_TENDERERS), 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) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 24 with CustomProjectionOperation

use of org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation in project ocvn by devgateway.

the class AverageTenderAndAwardPeriodsController method qualityAverageAwardPeriod.

@ApiOperation(value = "Quality indicator for averageAwardPeriod endpoint, " + "showing the percentage of awards that have start and end dates vs the total tenders in the system")
@RequestMapping(value = "/api/qualityAverageAwardPeriod", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> qualityAverageAwardPeriod(@ModelAttribute @Valid final DefaultFilterPagingRequest filter) {
    DBObject project = new BasicDBObject();
    project.put(Fields.UNDERSCORE_ID, 0);
    project.put("awardWithStartEndDates", new BasicDBObject("$cond", Arrays.asList(new BasicDBObject("$and", Arrays.asList(new BasicDBObject("$gt", Arrays.asList("$awards.date", null)), new BasicDBObject("$gt", Arrays.asList(MongoConstants.FieldNames.TENDER_PERIOD_END_DATE_REF, null)))), 1, 0)));
    DBObject project1 = new BasicDBObject();
    project1.put(Fields.UNDERSCORE_ID, 0);
    project1.put(Keys.TOTAL_AWARD_WITH_START_END_DATES, 1);
    project1.put(Keys.TOTAL_AWARDS, 1);
    project1.put(Keys.PERCENTAGE_AWARD_WITH_START_END_DATES, new BasicDBObject("$multiply", Arrays.asList(new BasicDBObject("$divide", Arrays.asList("$totalAwardWithStartEndDates", "$totalAwards")), 100)));
    Aggregation agg = newAggregation(match(where("awards.0").exists(true).andOperator(getDefaultFilterCriteria(filter))), unwind("$awards"), new CustomProjectionOperation(project), group().sum("awardWithStartEndDates").as(Keys.TOTAL_AWARD_WITH_START_END_DATES).count().as(Keys.TOTAL_AWARDS), new CustomProjectionOperation(project1));
    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 25 with CustomProjectionOperation

use of org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation in project ocvn by devgateway.

the class PercentageAwardsNarrowPublicationDates method percentageAwardsNarrowPublicationDates.

@ApiOperation(value = "Percentage of awards where award publication date - award.date is less than 7 days." + " Percentage should be by year. The denominator for the percentage is the " + "number of awards that have both awards.date and awards.publishedDate")
@RequestMapping(value = "/api/percentageAwardsNarrowPublicationDates", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> percentageAwardsNarrowPublicationDates(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
    BasicDBObject project = new BasicDBObject();
    project.put("year", new BasicDBObject("$year", "$awards.date"));
    project.put("narrowAwardPublicationDates", new BasicDBObject("$cond", Arrays.asList(new BasicDBObject("$and", Arrays.asList(new BasicDBObject("$gt", Arrays.asList("$awards.date", null)), new BasicDBObject("$gt", Arrays.asList("$awards.publishedDate", null)), new BasicDBObject("$lt", Arrays.asList(new BasicDBObject("$divide", Arrays.asList(new BasicDBObject("$subtract", Arrays.asList("$awards.publishedDate", "$awards.date")), MongoConstants.DAY_MS)), 7)))), 1, 0)));
    DBObject project2 = new BasicDBObject();
    project2.put(Keys.YEAR, Fields.UNDERSCORE_ID_REF);
    project2.put(Fields.UNDERSCORE_ID, 0);
    project2.put(Keys.TOTAL_AWARDS, 1);
    project2.put(Keys.TOTAL_AWARDS_NARROW_PUBLICATION_DATES, 1);
    project2.put(Keys.PERCENT_NARROW_AWARD_PUBLICATION_DATES, new BasicDBObject("$multiply", Arrays.asList(new BasicDBObject("$divide", Arrays.asList("$totalAwardsNarrowPublicationDates", "$totalAwards")), 100)));
    Aggregation agg = Aggregation.newAggregation(match(where("awards.0").exists(true)), unwind("$awards"), match(where("awards.date").exists(true).and("awards.publishedDate").exists(true).andOperator(getYearDefaultFilterCriteria(filter, "awards.date"))), new CustomProjectionOperation(project), group("$year").count().as(Keys.TOTAL_AWARDS).sum("narrowAwardPublicationDates").as(Keys.TOTAL_AWARDS_NARROW_PUBLICATION_DATES), new CustomProjectionOperation(project2), sort(Direction.ASC, Fields.UNDERSCORE_ID), 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)

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