use of org.devgateway.toolkit.persistence.mongo.aggregate.CustomGroupingOperation in project ocvn 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, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE_REF);
project1.put("tender.numberOfTenderers", 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("$tender.numberOfTenderers", 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()));
AggregationResults<DBObject> results = mongoTemplate.aggregate(agg, "release", DBObject.class);
List<DBObject> list = results.getMappedResults();
return list;
}
use of org.devgateway.toolkit.persistence.mongo.aggregate.CustomGroupingOperation in project ocvn by devgateway.
the class AbstractFlagStatsController method flagStats.
public 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)), new CustomProjectionOperation(projectPrepare), new CustomGroupingOperation(group), new CustomProjectionOperation(projectPercentage), transformYearlyGrouping(filter).andInclude(GenericKeys.TOTAL, GenericKeys.TOTAL_TRUE, GenericKeys.TOTAL_FALSE, GenericKeys.TOTAL_PRECOND_MET, GenericKeys.PERCENT_TRUE_PRECOND_MET, GenericKeys.PERCENT_FALSE_PRECOND_MET, GenericKeys.PERCENT_PRECOND_MET));
AggregationResults<DBObject> results = mongoTemplate.aggregate(agg, "release", DBObject.class);
List<DBObject> list = results.getMappedResults();
return list;
}
use of org.devgateway.toolkit.persistence.mongo.aggregate.CustomGroupingOperation 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;
}
use of org.devgateway.toolkit.persistence.mongo.aggregate.CustomGroupingOperation 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;
}
use of org.devgateway.toolkit.persistence.mongo.aggregate.CustomGroupingOperation in project ocvn by devgateway.
the class TenderPercentagesController method percentTendersAwarded.
@ApiOperation("Percent of awarded tenders with >1 tenderer/bidder" + "Count of tenders with numberOfTenderers >1 divided by total count of tenders with numberOfTenderers >0" + "This endpoint uses tender.tenderPeriod.startDate to calculate the tender year.")
@RequestMapping(value = "/api/percentTendersAwardedWithTwoOrMoreTenderers", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> percentTendersAwarded(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
DBObject project1 = new BasicDBObject();
addYearlyMonthlyProjection(filter, project1, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE_REF);
project1.put("tender.numberOfTenderers", 1);
DBObject group = new BasicDBObject();
addYearlyMonthlyReferenceToGroup(filter, group);
group.put(Keys.TOTAL_TENDERS_WITH_ONE_OR_MORE_TENDERERS, new BasicDBObject("$sum", new BasicDBObject("$cond", Arrays.asList(new BasicDBObject("$gt", Arrays.asList("$tender.numberOfTenderers", 0)), 1, 0))));
group.put(Keys.TOTAL_TENDERS_WITH_TWO_OR_MORE_TENDERERS, new BasicDBObject("$sum", new BasicDBObject("$cond", Arrays.asList(new BasicDBObject("$gt", Arrays.asList("$tender.numberOfTenderers", 1)), 1, 0))));
DBObject project2 = new BasicDBObject();
project2.put(Keys.TOTAL_TENDERS_WITH_ONE_OR_MORE_TENDERERS, 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", "$totalTendersWithOneOrMoreTenderers")), 100)));
Aggregation agg = newAggregation(match(where(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).exists(true).and("tender.numberOfTenderers").gt(0).andOperator(getYearDefaultFilterCriteria(filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))), new CustomProjectionOperation(project1), new CustomGroupingOperation(group), new CustomProjectionOperation(project2), transformYearlyGrouping(filter).andInclude(Keys.TOTAL_TENDERS_WITH_ONE_OR_MORE_TENDERERS, Keys.TOTAL_TENDERS_WITH_TWO_OR_MORE_TENDERERS, Keys.PERCENT_TENDERS), getSortByYearMonth(filter), skip(filter.getSkip()), limit(filter.getPageSize()));
AggregationResults<DBObject> results = mongoTemplate.aggregate(agg, "release", DBObject.class);
List<DBObject> list = results.getMappedResults();
return list;
}
Aggregations