CorruptionRiskDashboardIndicatorsStatsController extends GenericOCDSController { @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") public List<DBObject> percentOfEligibleProjectsFlaggedByYear(final YearFilterPagingRequest filter) { List<DBObject> totalFlaggedProjects = totalFlaggedProjectsByIndicatorTypeByYear(filter); List<DBObject> totalEligibleProjectsByYear = totalEligibleProjectsByIndicatorTypeByYear(filter); totalEligibleProjectsByYear.forEach(e -> { e.put(Keys.PERCENT, BigDecimal.ZERO); e.put(Keys.FLAGGED_PROJECT_COUNT, 0); }); totalEligibleProjectsByYear.forEach(e -> { findByYearAndTypeAndMonth(totalFlaggedProjects, (Integer) e.get(Keys.YEAR), (String) e.get(Keys.TYPE), (Integer) e.get(Keys.MONTH)) .forEach(f -> { e.put(Keys.FLAGGED_PROJECT_COUNT, f.get(Keys.FLAGGED_PROJECT_COUNT)); e.put(Keys.PERCENT, (BigDecimal.valueOf((Integer) f.get(Keys.FLAGGED_PROJECT_COUNT)) .setScale(BIGDECIMAL_SCALE) .divide(BigDecimal.valueOf((Integer) e.get(Keys.ELIGIBLE_PROJECT_COUNT)), BigDecimal.ROUND_HALF_UP).multiply(ONE_HUNDRED))); }); }); return totalEligibleProjectsByYear; }  @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<DBObject> 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<DBObject> 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<DBObject> 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<DBObject> 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<DBObject> 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<DBObject> 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<DBObject> 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<DBObject> 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<DBObject> 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<DBObject> 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<DBObject> percentOfEligibleProjectsFlaggedByYear(final YearFilterPagingRequest filter);  }
@Test public void percentOfEligibleProjectsFlaggedByYearTest() throws Exception { final List<DBObject> result = corruptionRiskDashboardIndicatorsStatsController .percentOfEligibleProjectsFlaggedByYear(new YearFilterPagingRequest()); Assert.assertEquals(6, result.size()); Assert.assertEquals(2014, result.get(0).get(CorruptionRiskDashboardIndicatorsStatsController.Keys.YEAR)); Assert.assertEquals(FlagType.COLLUSION.toString(), result.get(0).get(CorruptionRiskDashboardIndicatorsStatsController .Keys.TYPE)); Assert.assertEquals(1, result.get(0).get(CorruptionRiskDashboardIndicatorsStatsController.Keys .ELIGIBLE_COUNT)); Assert.assertEquals(1, result.get(0).get(CorruptionRiskDashboardIndicatorsStatsController.Keys .ELIGIBLE_PROJECT_COUNT)); Assert.assertEquals(0, result.get(0).get(CorruptionRiskDashboardIndicatorsStatsController.Keys .FLAGGED_PROJECT_COUNT)); Assert.assertTrue(BigDecimal.valueOf(0). compareTo((BigDecimal)result.get(0).get(CorruptionRiskDashboardIndicatorsStatsController.Keys.PERCENT))==0); Assert.assertEquals(2014, result.get(1).get(CorruptionRiskDashboardIndicatorsStatsController.Keys.YEAR)); Assert.assertEquals(FlagType.FRAUD.toString(), result.get(1).get(CorruptionRiskDashboardIndicatorsStatsController .Keys.TYPE)); Assert.assertEquals(1, result.get(1).get(CorruptionRiskDashboardIndicatorsStatsController.Keys.ELIGIBLE_COUNT)); Assert.assertEquals(1, result.get(1).get(CorruptionRiskDashboardIndicatorsStatsController.Keys.ELIGIBLE_PROJECT_COUNT)); Assert.assertEquals(0, result.get(1).get(CorruptionRiskDashboardIndicatorsStatsController.Keys.FLAGGED_PROJECT_COUNT)); Assert.assertTrue(BigDecimal.ZERO. compareTo((BigDecimal)result.get(1).get(CorruptionRiskDashboardIndicatorsStatsController.Keys.PERCENT))==0); }