Search in sources :

Example 1 with CustomUnwindOperation

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

the class CostEffectivenessVisualsController method costEffectivenessTenderAmount.

@ApiOperation(value = "Cost effectiveness of Tenders:" + " Displays the total amount of the active tenders that have active awards, " + "grouped by year. Only tenders.status=active" + "are taken into account. The year is calculated from tenderPeriod.startDate")
@RequestMapping(value = "/api/costEffectivenessTenderAmount", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> costEffectivenessTenderAmount(@ModelAttribute @Valid final GroupingFilterPagingRequest filter) {
    DBObject project = new BasicDBObject();
    project.put("year", new BasicDBObject("$year", MongoConstants.FieldNames.TENDER_PERIOD_START_DATE_REF));
    addYearlyMonthlyProjection(filter, project, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE_REF);
    project.put("tender.value.amount", 1);
    project.put(Fields.UNDERSCORE_ID, "$tender._id");
    project.put("tenderWithAwards", new BasicDBObject("$cond", Arrays.asList(new BasicDBObject("$eq", Arrays.asList("$awards.status", Award.Status.active.toString())), 1, 0)));
    project.put("tenderWithAwardsValue", new BasicDBObject("$cond", Arrays.asList(new BasicDBObject("$eq", Arrays.asList("$awards.status", Award.Status.active.toString())), "$tender.value.amount", 0)));
    project.putAll(filterProjectMap);
    DBObject group1 = new BasicDBObject();
    group1.put(Fields.UNDERSCORE_ID, Fields.UNDERSCORE_ID_REF);
    addYearlyMonthlyGroupingOperationFirst(filter, group1);
    group1.put("tenderWithAwards", new BasicDBObject("$max", "$tenderWithAwards"));
    group1.put("tenderWithAwardsValue", new BasicDBObject("$max", "$tenderWithAwardsValue"));
    group1.put("tenderAmount", new BasicDBObject("$first", "$tender.value.amount"));
    filterProjectMap.forEach((k, v) -> group1.put(k.replace(".", ""), k.equals("tender.items.classification._id") ? new BasicDBObject("$first", new BasicDBObject("$arrayElemAt", Arrays.asList("$" + k, 0))) : new BasicDBObject("$first", "$" + k)));
    Aggregation agg = Aggregation.newAggregation(match(where("tender.status").is(Tender.Status.active.toString()).and(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).exists(true).andOperator(getYearDefaultFilterCriteria(filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))), getMatchDefaultFilterOperation(filter), new CustomUnwindOperation("$awards", true), new CustomProjectionOperation(project), new CustomGroupingOperation(group1), getTopXFilterOperation(filter, getYearlyMonthlyGroupingFields(filter)).sum("tenderWithAwardsValue").as(Keys.TOTAL_TENDER_AMOUNT).count().as(Keys.TOTAL_TENDERS).sum("tenderWithAwards").as(Keys.TOTAL_TENDER_WITH_AWARDS), project(Keys.TOTAL_TENDER_AMOUNT, Keys.TOTAL_TENDERS, Keys.TOTAL_TENDER_WITH_AWARDS).andInclude(Fields.from(Fields.field(Fields.UNDERSCORE_ID, Fields.UNDERSCORE_ID_REF))).and(Keys.TOTAL_TENDER_WITH_AWARDS).divide(Keys.TOTAL_TENDERS).as(Keys.FRACTION_TENDERS_WITH_AWARDS), project(Keys.TOTAL_TENDER_AMOUNT, Keys.TOTAL_TENDERS, Keys.TOTAL_TENDER_WITH_AWARDS, Fields.UNDERSCORE_ID).and(Keys.FRACTION_TENDERS_WITH_AWARDS).multiply(100).as(Keys.PERCENTAGE_TENDERS_WITH_AWARDS), (filter.getGroupByCategory() == null ? transformYearlyGrouping(filter) : project()).andInclude(Keys.TOTAL_TENDER_AMOUNT, Keys.TOTAL_TENDERS, Keys.TOTAL_TENDER_WITH_AWARDS, Keys.PERCENTAGE_TENDERS_WITH_AWARDS), filter.getGroupByCategory() == null ? getSortByYearMonth(filter) : sort(Sort.Direction.DESC, Keys.TOTAL_TENDER_AMOUNT), skip(filter.getSkip()), limit(filter.getPageSize())).withOptions(Aggregation.newAggregationOptions().allowDiskUse(true).build());
    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) CustomUnwindOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomUnwindOperation) 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 2 with CustomUnwindOperation

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

the class TendersAwardsYears method tendersAwardsYears.

@ApiOperation(value = "Computes all available years from awards.date, tender.tenderPeriod.startDate" + "and planning.bidPlanProjectDateApprove")
@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(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE_REF, null)), new BasicDBObject("$year", MongoConstants.FieldNames.TENDER_PERIOD_START_DATE_REF), null)));
    project1.put("bidPlanYear", new BasicDBObject("$cond", Arrays.asList(new BasicDBObject("$gt", Arrays.asList("$planning.bidPlanProjectDateApprove", null)), new BasicDBObject("$year", "$planning.bidPlanProjectDateApprove"), null)));
    project1.put("awardYear", new BasicDBObject("$cond", Arrays.asList(new BasicDBObject("$gt", Arrays.asList("$awards.date", null)), new BasicDBObject("$year", "$awards.date"), null)));
    project1.put(Fields.UNDERSCORE_ID, 0);
    BasicDBObject project2 = new BasicDBObject();
    project2.put("year", Arrays.asList("$tenderYear", "$awardYear", "$bidPlanYear"));
    Aggregation agg = Aggregation.newAggregation(project().and(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).as(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).and("awards.date").as("awards.date").and("planning.bidPlanProjectDateApprove").as("planning.bidPlanProjectDateApprove"), match(new Criteria().orOperator(where(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).exists(true), where("awards.date").exists(true), where("planning.bidPlanProjectDateApprove").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)));
    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) 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) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

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