TenderPercentagesController extends GenericOCDSController { @ApiOperation("Returns the percent of tenders that are using eProcurement." + " This is read from tender.publicationMethod='eGP'") @RequestMapping(value = "/api/percentTendersUsingEgp", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json") public List<DBObject> percentTendersUsingEgp(@ModelAttribute @Valid final YearFilterPagingRequest filter) { DBObject project1 = new BasicDBObject(); addYearlyMonthlyProjection(filter, project1, "$tender.tenderPeriod.startDate"); project1.put("tender.publicationMethod", 1); DBObject group = new BasicDBObject(); addYearlyMonthlyReferenceToGroup(filter, group); group.put("totalTenders", new BasicDBObject("$sum", 1)); group.put(Keys.TOTAL_EGP, new BasicDBObject("$sum", new BasicDBObject("$cond", Arrays.asList(new BasicDBObject("$eq", Arrays.asList("$tender.publicationMethod", "eGP")), 1, 0)))); DBObject project2 = new BasicDBObject(); project2.put("totalTenders", 1); project2.put(Keys.TOTAL_EGP, 1); project2.put("year", 1); project2.put("month", 1); project2.put("percentEgp", new BasicDBObject("$multiply", Arrays.asList(new BasicDBObject("$divide", Arrays.asList("$totalEgp", "$totalTenders")), 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("totalTenders", "percentEgp", Keys.TOTAL_EGP), new CustomProjectionOperation(project2), getSortByYearMonth(filter), skip(filter.getSkip()), limit(filter.getPageSize())); AggregationResults<DBObject> results = mongoTemplate.aggregate(agg, "release", DBObject.class); List<DBObject> list = results.getMappedResults(); return list; }  @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<DBObject> 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<DBObject> 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<DBObject> 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<DBObject> percentTendersUsingEBid(@ModelAttribute @Valid final YearFilterPagingRequest filter); @ApiOperation("Returns the percent of tenders that are using eProcurement." + " This is read from tender.publicationMethod='eGP'") @RequestMapping(value = "/api/percentTendersUsingEgp", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json") List<DBObject> percentTendersUsingEgp(@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<DBObject> percentTendersWithLinkedProcurementPlan(@ModelAttribute
                                                                  @Valid final YearFilterPagingRequest filter); @ApiOperation("For all tenders that have both tender.tenderPeriod.startDate and planning.bidPlanProjectDateApprove" + "calculates the number o days from planning.bidPlanProjectDateApprove to tender.tenderPeriod.startDate" + "and creates the average. Groups results by tender year, calculatedfrom tender.tenderPeriod.startDate") @RequestMapping(value = "/api/avgTimeFromPlanToTenderPhase", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json") List<DBObject> avgTimeFromPlanToTenderPhase(@ModelAttribute @Valid final YearFilterPagingRequest filter);  }
@Test public void percentTendersUsingEgp() throws Exception { final List<DBObject> percentTendersUsingEgp = tenderPercentagesController .percentTendersUsingEgp(new YearFilterPagingRequest()); final DBObject first = percentTendersUsingEgp.get(0); int year = (int) first.get(TenderPercentagesController.Keys.YEAR); int totalTenders = (int) first.get(TenderPercentagesController.Keys.TOTAL_TENDERS); int totalEgp = (int) first.get(TenderPercentagesController.Keys.TOTAL_EGP); double percentEgp = (double) first.get(TenderPercentagesController.Keys.PERCENTAGE_EGP); Assert.assertEquals(2014, year); Assert.assertEquals(1, totalTenders); Assert.assertEquals(0, totalEgp); Assert.assertEquals(0.0, percentEgp, 0); final DBObject second = percentTendersUsingEgp.get(1); year = (int) second.get(TenderPercentagesController.Keys.YEAR); totalTenders = (int) second.get(TenderPercentagesController.Keys.TOTAL_TENDERS); totalEgp = (int) second.get(TenderPercentagesController.Keys.TOTAL_EGP); percentEgp = (double) second.get(TenderPercentagesController.Keys.PERCENTAGE_EGP); Assert.assertEquals(2015, year); Assert.assertEquals(2, totalTenders); Assert.assertEquals(1, totalEgp); Assert.assertEquals(50.0, percentEgp, 0); }