CostEffectivenessVisualsController extends GenericOCDSController { @ApiOperation(value = "Cost effectiveness of Awards: Displays the total amount of active awards grouped by year." + "The tender entity, for each award, has to have amount value. The year is calculated from " + MongoConstants.FieldNames.TENDER_PERIOD_START_DATE) @RequestMapping(value = "/api/costEffectivenessAwardAmount", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json") public List<Document> costEffectivenessAwardAmount( @ModelAttribute @Valid final YearFilterPagingRequest filter) { DBObject project = new BasicDBObject(); addYearlyMonthlyProjection(filter, project, ref(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE)); project.put(MongoConstants.FieldNames.AWARDS_VALUE_AMOUNT, 1); project.put("totalAwardsWithTender", new BasicDBObject( "$cond", Arrays.asList(new BasicDBObject( "$gt", Arrays.asList(ref(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE), null) ), 1, 0) )); project.put( "awardsWithTenderValue", new BasicDBObject( "$cond", Arrays.asList(new BasicDBObject( "$gt", Arrays.asList(ref(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE), null) ), "$awards.value.amount", 0 ) ) ); Aggregation agg = Aggregation.newAggregation( match(where("awards").elemMatch(where("status").is(Award.Status.active.toString())) .and(MongoConstants.FieldNames.AWARDS_DATE) .exists(true) .and(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE) .exists(true)), getMatchDefaultFilterOperation(filter), unwind("awards"), match(where(MongoConstants.FieldNames.AWARDS_STATUS).is(Award.Status.active.toString()) .and("awards.value") .exists(true) . andOperator(getYearDefaultFilterCriteria( filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE ))), new CustomProjectionOperation(project), getYearlyMonthlyGroupingOperation(filter) .sum("awardsWithTenderValue").as(Keys.TOTAL_AWARD_AMOUNT).count().as(Keys.TOTAL_AWARDS) .sum("totalAwardsWithTender").as(Keys.TOTAL_AWARDS_WITH_TENDER), project(Fields.UNDERSCORE_ID, Keys.TOTAL_AWARD_AMOUNT, Keys.TOTAL_AWARDS, Keys.TOTAL_AWARDS_WITH_TENDER) .and(Keys.TOTAL_AWARDS_WITH_TENDER).divide(Keys.TOTAL_AWARDS) .as(Keys.FRACTION_AWARDS_WITH_TENDER), project( Fields.UNDERSCORE_ID, Keys.TOTAL_AWARD_AMOUNT, Keys.TOTAL_AWARDS, Keys.TOTAL_AWARDS_WITH_TENDER, Keys.FRACTION_AWARDS_WITH_TENDER ).and(Keys.FRACTION_AWARDS_WITH_TENDER).multiply(100) .as(Keys.PERCENTAGE_AWARDS_WITH_TENDER), transformYearlyGrouping(filter).andInclude(Keys.TOTAL_AWARD_AMOUNT, Keys.TOTAL_AWARDS, Keys.TOTAL_AWARDS_WITH_TENDER, Keys.PERCENTAGE_AWARDS_WITH_TENDER ), getSortByYearMonth(filter), skip(filter.getSkip()), limit(filter.getPageSize()) ); return releaseAgg(agg); }  @ApiOperation(value = "Cost effectiveness of Awards: Displays the total amount of active awards grouped by year." + "The tender entity, for each award, has to have amount value. The year is calculated from " + MongoConstants.FieldNames.TENDER_PERIOD_START_DATE) @RequestMapping(value = "/api/costEffectivenessAwardAmount", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json") List<Document> costEffectivenessAwardAmount(
            @ModelAttribute @Valid final YearFilterPagingRequest filter); @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") List<Document> costEffectivenessTenderAmount(
            @ModelAttribute @Valid final GroupingFilterPagingRequest filter); @ApiOperation(value = "Aggregated version of /api/costEffectivenessTenderAmount and " + "/api/costEffectivenessAwardAmount." + "This endpoint aggregates the responses from the specified endpoints, per year. " + "Responds to the same filters.") @RequestMapping(value = "/api/costEffectivenessTenderAwardAmount", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json") ArrayList<Document> costEffectivenessTenderAwardAmount(
            @ModelAttribute @Valid final GroupingFilterPagingRequest filter);  }
@Test public void costEffectivenessAwardAmount() throws Exception { final List<Document> costEffectivenessAwardAmount = costEffectivenessVisualsController .costEffectivenessAwardAmount(new YearFilterPagingRequest()); final Document first = costEffectivenessAwardAmount.get(0); int year = (int) first.get(CostEffectivenessVisualsController.Keys.YEAR); double totalAwardAmount = (double) first.get(CostEffectivenessVisualsController.Keys.TOTAL_AWARD_AMOUNT); int totalAwards = (int) first.get(CostEffectivenessVisualsController.Keys.TOTAL_AWARDS); int totalAwardsWithTender = (int) first.get(CostEffectivenessVisualsController.Keys.TOTAL_AWARDS_WITH_TENDER); double percentageAwardsWithTender = (double) first. get(CostEffectivenessVisualsController.Keys.PERCENTAGE_AWARDS_WITH_TENDER); Assert.assertEquals(2014, year); Assert.assertEquals(6000000.0, totalAwardAmount, 0); Assert.assertEquals(1, totalAwards); Assert.assertEquals(1, totalAwardsWithTender); Assert.assertEquals(100.0, percentageAwardsWithTender, 0); final Document second = costEffectivenessAwardAmount.get(1); year = (int) second.get(CostEffectivenessVisualsController.Keys.YEAR); totalAwardAmount = (double) second.get(CostEffectivenessVisualsController.Keys.TOTAL_AWARD_AMOUNT); totalAwards = (int) second.get(CostEffectivenessVisualsController.Keys.TOTAL_AWARDS); totalAwardsWithTender = (int) second.get(CostEffectivenessVisualsController.Keys.TOTAL_AWARDS_WITH_TENDER); percentageAwardsWithTender = (double) second. get(CostEffectivenessVisualsController.Keys.PERCENTAGE_AWARDS_WITH_TENDER); Assert.assertEquals(2015, year); Assert.assertEquals(40000.0, totalAwardAmount, 0); Assert.assertEquals(1, totalAwards); Assert.assertEquals(1, totalAwardsWithTender); Assert.assertEquals(100.0, percentageAwardsWithTender, 0); }