TenderPercentagesController extends GenericOCDSController { @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<Document> 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); }  @ApiOperation("Returns the percent of tenders that were cancelled, grouped by year." + " The year is taken from tender.tenderPeriod.startDate. The response also contains the" + " total number of tenders and total number of cancelled tenders for each year.") @RequestMapping(value = "/api/percentTendersCancelled", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json") List<Document> percentTendersCancelled(@ModelAttribute @Valid final YearFilterPagingRequest filter); @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") List<Document> percentTendersWithTwoOrMoreTenderers(@ModelAttribute
                                                               @Valid final YearFilterPagingRequest filter); @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") List<Document> percentTendersAwarded(@ModelAttribute @Valid final YearFilterPagingRequest filter); @ApiOperation("Returns the percent of tenders with active awards, " + "with tender.submissionMethod='electronicSubmission'." + "The endpoint also returns the total tenderds with active awards and the count of tenders with " + "tender.submissionMethod='electronicSubmission") @RequestMapping(value = "/api/percentTendersUsingEBid", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json") List<Document> percentTendersUsingEBid(@ModelAttribute @Valid final YearFilterPagingRequest filter); @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") List<Document> percentTendersWithLinkedProcurementPlan(@ModelAttribute
                                                                  @Valid final YearFilterPagingRequest filter);  }
@Test public void percentTendersWithLinkedProcurementPlan() throws Exception { final List<Document> percentTendersWithLinkedProcurementPlan = tenderPercentagesController .percentTendersWithLinkedProcurementPlan(new YearFilterPagingRequest()); final Document first = percentTendersWithLinkedProcurementPlan.get(0); int year = (int) first.get(TenderPercentagesController.Keys.YEAR); int totalTendersWithLinkedProcurementPlan = (int) first .get(TenderPercentagesController.Keys.TOTAL_TENDERS_WITH_LINKED_PROCUREMENT_PLAN); int totalTenders = (int) first .get(TenderPercentagesController.Keys.TOTAL_TENDERS); double percentTenders = (double) first.get(TenderPercentagesController.Keys.PERCENT_TENDERS); Assert.assertEquals(2014, year); Assert.assertEquals(1, totalTendersWithLinkedProcurementPlan); Assert.assertEquals(1, totalTenders); Assert.assertEquals(100.0, percentTenders, 0); final Document second = percentTendersWithLinkedProcurementPlan.get(1); year = (int) second.get(TenderPercentagesController.Keys.YEAR); totalTendersWithLinkedProcurementPlan = (int) second .get(TenderPercentagesController.Keys.TOTAL_TENDERS_WITH_LINKED_PROCUREMENT_PLAN); totalTenders = (int) second .get(TenderPercentagesController.Keys.TOTAL_TENDERS); percentTenders = (double) second.get(TenderPercentagesController.Keys.PERCENT_TENDERS); Assert.assertEquals(2015, year); Assert.assertEquals(2, totalTendersWithLinkedProcurementPlan); Assert.assertEquals(2, totalTenders); Assert.assertEquals(100.0, percentTenders, 0); }