CorruptionRiskDashboardIndicatorsStatsController extends GenericOCDSController { @ApiOperation(value = "Percent of total projects flagged (denominator is total projects)") @RequestMapping(value = "/api/percentTotalProjectsFlaggedByYear", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json") public List<Document> percentTotalProjectsFlaggedByYear(final YearFilterPagingRequest filter) { List<Document> totalFlaggedProjects = totalFlaggedProjectsByIndicatorTypeByYear(filter); List<Document> totalProjectsByYear = totalProjectsByYear(filter); totalFlaggedProjects.forEach(e -> { findByYearAndTypeAndMonth(totalProjectsByYear, (Integer) e.get(Keys.YEAR), null, (Integer) e.get(Keys.MONTH)) .forEach(f -> { e.put(Keys.PROJECT_COUNT, f.get(Keys.PROJECT_COUNT)); e.put(Keys.PERCENT, (BigDecimal.valueOf((Integer) e.get(Keys.FLAGGED_PROJECT_COUNT)) .setScale(BIGDECIMAL_SCALE) .divide(BigDecimal.valueOf((Integer) f.get(Keys.PROJECT_COUNT)), BigDecimal.ROUND_HALF_UP).multiply(ONE_HUNDRED))); }); }); return totalFlaggedProjects; }  @ApiOperation(value = "Counts the indicators flagged, and groups them by indicator type. " + "An indicator that has two types it will be counted twice, once in each group.") @RequestMapping(value = "/api/totalFlaggedIndicatorsByIndicatorType", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json") List<Document> totalFlaggedIndicatorsByIndicatorType(
            @ModelAttribute @Valid final YearFilterPagingRequest filter); @ApiOperation(value = "Counts the indicators eligible, and groups them by indicator type. " + "An indicator that has two types it will be counted twice, once in each group.") @RequestMapping(value = "/api/totalEligibleIndicatorsByIndicatorType", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json") List<Document> totalEligibleIndicatorsByIndicatorType(
            @ModelAttribute @Valid final YearFilterPagingRequest filter); @ApiOperation(value = "Counts the indicators that are flagged, across all releases. If one " + "indicator has multiple types is only counted once, so this is different from flaggedStats and " + "cannot be reproduced by just summing up all types in flaggedStats!") @RequestMapping(value = "/api/totalFlags", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json") List<Document> totalFlags(final YearFilterPagingRequest filter); @ApiOperation(value = "Counts the indicators flagged, and groups them by indicator type and by year/month. " + "An indicator that has two types it will be counted twice, once in each group.") @RequestMapping(value = "/api/totalFlaggedIndicatorsByIndicatorTypeByYear", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json") List<Document> totalFlaggedIndicatorsByIndicatorTypeByYear(
            @ModelAttribute @Valid final YearFilterPagingRequest filter); @ApiOperation(value = "Counts the indicators eligible, and groups them by indicator type and by year/month. " + "An indicator that has two types it will be counted twice, once in each group.") @RequestMapping(value = "/api/totalEligibleIndicatorsByIndicatorTypeByYear", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json") List<Document> totalEligibleIndicatorsByIndicatorTypeByYear(
            @ModelAttribute @Valid final YearFilterPagingRequest filter); @ApiOperation(value = "Counts the projects and the indicators flagged, grouped by indicator type. " + "The 'count' represents the number of indicators flagged, the 'projectCount' represents the number" + " of projects flagged.") @RequestMapping(value = "/api/totalFlaggedProjectsByIndicatorTypeByYear", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json") List<Document> totalFlaggedProjectsByIndicatorTypeByYear(
            @ModelAttribute @Valid final YearFilterPagingRequest filter); @ApiOperation(value = "Counts the projects and the indicators eligible, grouped by indicator type. " + "The 'count' represents the number of indicators eligible, the 'projectCount' represents the number" + " of projects eligible.") @RequestMapping(value = "/api/totalEligibleProjectsByIndicatorTypeByYear", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json") List<Document> totalEligibleProjectsByIndicatorTypeByYear(
            @ModelAttribute @Valid final YearFilterPagingRequest filter); @ApiOperation(value = "Count total projects by year/month.") @RequestMapping(value = "/api/totalProjectsByYear", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json") List<Document> totalProjectsByYear(final YearFilterPagingRequest filter); @ApiOperation(value = "Percent of total projects flagged (denominator is total projects)") @RequestMapping(value = "/api/percentTotalProjectsFlaggedByYear", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json") List<Document> percentTotalProjectsFlaggedByYear(final YearFilterPagingRequest filter); @ApiOperation(value = "Percent of total projects eligible (denominator is total projects)") @RequestMapping(value = "/api/percentTotalProjectsEligibleByYear", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json") List<Document> percentTotalProjectsEligibleByYear(final YearFilterPagingRequest filter); @ApiOperation(value = "Percent of eligible projects flagged (denominator is number of eligible projects)") @RequestMapping(value = "/api/percentOfEligibleProjectsFlaggedByYear", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json") List<Document> percentOfEligibleProjectsFlaggedByYear(final YearFilterPagingRequest filter);  }
@Test public void percentTotalProjectsFlaggedByYearTest() throws Exception { final List<Document> result = corruptionRiskDashboardIndicatorsStatsController .percentTotalProjectsFlaggedByYear(new YearFilterPagingRequest()); Assert.assertEquals(1, result.size()); Assert.assertEquals(2015, result.get(0).get(CorruptionRiskDashboardIndicatorsStatsController.Keys.YEAR)); Assert.assertEquals(FlagType.RIGGING.toString(), result.get(0).get(CorruptionRiskDashboardIndicatorsStatsController.Keys.TYPE)); Assert.assertEquals(2, result.get(0).get(CorruptionRiskDashboardIndicatorsStatsController.Keys.FLAGGED_COUNT)); Assert.assertEquals(1, result.get(0).get(CorruptionRiskDashboardIndicatorsStatsController.Keys.FLAGGED_PROJECT_COUNT)); Assert.assertEquals(2, result.get(0).get(CorruptionRiskDashboardIndicatorsStatsController.Keys.PROJECT_COUNT)); Assert.assertTrue(BigDecimal.valueOf(50). compareTo((BigDecimal)result.get(0).get(CorruptionRiskDashboardIndicatorsStatsController.Keys.PERCENT))==0); }