Search in sources :

Example 16 with CustomProjectionOperation

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

the class FundingByLocationController method qualityPlannedFundingByLocation.

@ApiOperation("Calculates percentage of releases with planning with at least one specified location," + " that is the array planning.budget.projectLocation has to be initialzied." + "Filters out stub planning, therefore planning.budget.amount has to exist." + "Responds only to the procuring entity id filter: tender.procuringEntity._id")
@RequestMapping(value = "/api/qualityPlannedFundingByLocation", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> qualityPlannedFundingByLocation(@ModelAttribute @Valid final DefaultFilterPagingRequest filter) {
    DBObject project = new BasicDBObject();
    project.putAll(filterProjectMap);
    project.put("planning.budget.projectLocation", 1);
    project.put("planning.budget.amount", 1);
    project.put(Fields.UNDERSCORE_ID, 0);
    DBObject group = new BasicDBObject();
    group.put(Fields.UNDERSCORE_ID, null);
    group.put(Keys.TOTAL_PLANS_WITH_AMOUNTS, new BasicDBObject("$sum", 1));
    group.put(Keys.TOTAL_PLANS_WITH_AMOUNTS_AND_LOCATION, new BasicDBObject("$sum", new BasicDBObject("$cond", Arrays.asList(new BasicDBObject("$gt", Arrays.asList(new BasicDBObject("$size", new BasicDBObject("$ifNull", Arrays.asList("$planning.budget.projectLocation", ArrayUtils.toArray()))), 0)), 1, 0))));
    DBObject project2 = new BasicDBObject();
    project2.put(Fields.UNDERSCORE_ID, 0);
    project2.put(Keys.TOTAL_PLANS_WITH_AMOUNTS, 1);
    project2.put(Keys.TOTAL_PLANS_WITH_AMOUNTS_AND_LOCATION, 1);
    project2.put(Keys.PERCENT_PLANS_WITH_AMOUNTS_AND_LOCATION, new BasicDBObject("$multiply", Arrays.asList(new BasicDBObject("$divide", Arrays.asList("$totalPlansWithAmountsAndLocation", "$totalPlansWithAmounts")), 100)));
    Aggregation agg = newAggregation(new CustomProjectionOperation(project), match(where("planning.budget.amount").exists(true).andOperator(getProcuringEntityIdCriteria(filter))), new CustomGroupingOperation(group), new CustomProjectionOperation(project2));
    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 17 with CustomProjectionOperation

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

the class CorruptionRiskDashboardIndicatorsStatsController method totalProjectsByYear.

@ApiOperation(value = "Count total projects by year/month.")
@RequestMapping(value = "/api/totalProjectsByYear", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> totalProjectsByYear(final YearFilterPagingRequest filter) {
    DBObject project1 = new BasicDBObject();
    addYearlyMonthlyProjection(filter, project1, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE_REF);
    project1.put(Fields.UNDERSCORE_ID, 0);
    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), group(getYearlyMonthlyGroupingFields(filter)).count().as(Keys.PROJECT_COUNT), transformYearlyGrouping(filter).andInclude(Keys.PROJECT_COUNT), getSortByYearMonth(filter));
    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 18 with CustomProjectionOperation

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

the class CorruptionRiskDashboardIndicatorsStatsController method totalProjectsByIndicatorTypeByYear.

private List<DBObject> totalProjectsByIndicatorTypeByYear(String statsProperty, final YearFilterPagingRequest filter) {
    DBObject project1 = new BasicDBObject();
    addYearlyMonthlyProjection(filter, project1, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE_REF);
    project1.put("stats", "$flags." + statsProperty);
    project1.put(Fields.UNDERSCORE_ID, 0);
    Aggregation agg = newAggregation(match(where(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).exists(true).and("flags." + statsProperty + ".0").exists(true).andOperator(getYearDefaultFilterCriteria(filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))), unwind("flags." + statsProperty), new CustomProjectionOperation(project1), group(getYearlyMonthlyGroupingFields(filter, "stats.type")).sum("stats.count").as(statsProperty.equals(ELIGIBLE_STATS) ? Keys.ELIGIBLE_COUNT : Keys.FLAGGED_COUNT).count().as(statsProperty.equals(ELIGIBLE_STATS) ? Keys.ELIGIBLE_PROJECT_COUNT : Keys.FLAGGED_PROJECT_COUNT), getSortByYearMonthWhenOtherGroups(filter, "_id.type"));
    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)

Example 19 with CustomProjectionOperation

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

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

the class FundingByLocationController method qualityFundingByTenderDeliveryLocation.

@ApiOperation("Calculates percentage of releases with tender with at least one specified delivery location," + " that is the array tender.items.deliveryLocation has to have items." + "Filters out stub tenders, therefore tender.tenderPeriod.startDate has to exist.")
@RequestMapping(value = "/api/qualityFundingByTenderDeliveryLocation", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> qualityFundingByTenderDeliveryLocation(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
    DBObject project = new BasicDBObject();
    project.putAll(filterProjectMap);
    project.put(Fields.UNDERSCORE_ID, "$tender._id");
    project.put("tenderItemsDeliveryLocation", new BasicDBObject("$cond", Arrays.asList(new BasicDBObject("$gt", Arrays.asList("$tender.items.deliveryLocation", 0)), 1, 0)));
    DBObject project1 = new BasicDBObject();
    project1.put(Fields.UNDERSCORE_ID, 0);
    project1.put(Keys.TOTAL_TENDERS_WITH_START_DATE, 1);
    project1.put(Keys.TOTAL_TENDERS_WITH_START_DATE_AND_LOCATION, 1);
    project1.put(Keys.PERCENT_TENDERS_WITH_START_DATE_AND_LOCATION, new BasicDBObject("$multiply", Arrays.asList(new BasicDBObject("$divide", Arrays.asList("$totalTendersWithStartDateAndLocation", "$totalTendersWithStartDate")), 100)));
    Aggregation agg = newAggregation(match(where(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).exists(true).andOperator(getYearDefaultFilterCriteria(filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))), unwind("$tender.items"), new CustomProjectionOperation(project), group(Fields.UNDERSCORE_ID_REF).max("tenderItemsDeliveryLocation").as("hasTenderItemsDeliverLocation"), group().count().as("totalTendersWithStartDate").sum("hasTenderItemsDeliverLocation").as(Keys.TOTAL_TENDERS_WITH_START_DATE_AND_LOCATION), new CustomProjectionOperation(project1));
    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)

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