AverageTenderAndAwardPeriodsController extends GenericOCDSController { @ApiOperation(value = "Calculates the average tender period, per each year. The year is taken from " + "tender.tenderPeriod.startDate and the duration is taken by counting the days" + "between tender.tenderPeriod.endDate and tender.tenderPeriod.startDate") @RequestMapping(value = "/api/averageTenderPeriod", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json") public List<DBObject> averageTenderPeriod(@ModelAttribute @Valid final YearFilterPagingRequest filter) { DBObject tenderLengthDays = new BasicDBObject("$divide", Arrays.asList( new BasicDBObject("$subtract", Arrays.asList(MongoConstants.FieldNames.TENDER_PERIOD_END_DATE_REF, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE_REF)), MongoConstants.DAY_MS)); DBObject project = new BasicDBObject(); project.put(Fields.UNDERSCORE_ID, 0); addYearlyMonthlyProjection(filter, project, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE_REF); project.put("tenderLengthDays", tenderLengthDays); Aggregation agg = newAggregation( match(where(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE) .exists(true).and(MongoConstants.FieldNames.TENDER_PERIOD_END_DATE) .exists(true).andOperator(getYearDefaultFilterCriteria(filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))), new CustomProjectionOperation(project), getYearlyMonthlyGroupingOperation(filter).avg("$tenderLengthDays").as(Keys.AVERAGE_TENDER_DAYS), transformYearlyGrouping(filter).andInclude(Keys.AVERAGE_TENDER_DAYS), 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(value = "Calculates the average tender period, per each year. The year is taken from " + "tender.tenderPeriod.startDate and the duration is taken by counting the days" + "between tender.tenderPeriod.endDate and tender.tenderPeriod.startDate") @RequestMapping(value = "/api/averageTenderPeriod", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json") List<DBObject> averageTenderPeriod(@ModelAttribute @Valid final YearFilterPagingRequest filter); @ApiOperation(value = "Quality indicator for averageTenderPeriod endpoint, " + "showing the percentage of tenders that have start and end dates vs the total tenders in the system") @RequestMapping(value = "/api/qualityAverageTenderPeriod", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json") List<DBObject> qualityAverageTenderPeriod(@ModelAttribute @Valid final DefaultFilterPagingRequest filter); @ApiOperation(value = "Calculates the average award period, per each year. The year is taken from " + "awards.date and the duration is taken by counting the days" + "between tender.tenderPeriod.endDate and tender.tenderPeriod.startDate. The award has to be active.") @RequestMapping(value = "/api/averageAwardPeriod", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json") List<DBObject> averageAwardPeriod(@ModelAttribute @Valid final YearFilterPagingRequest filter); @ApiOperation(value = "Quality indicator for averageAwardPeriod endpoint, " + "showing the percentage of awards that have start and end dates vs the total tenders in the system") @RequestMapping(value = "/api/qualityAverageAwardPeriod", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json") List<DBObject> qualityAverageAwardPeriod(@ModelAttribute @Valid final DefaultFilterPagingRequest filter);  }
@Test public void averageTenderPeriod() throws Exception { final List<DBObject> averageTenderPeriod = averageTenderAndAwardPeriodsController .averageTenderPeriod(new YearFilterPagingRequest()); final DBObject first = averageTenderPeriod.get(0); int year = (int) first.get(AverageTenderAndAwardPeriodsController.Keys.YEAR); double averageTenderDays = (double) first.get(AverageTenderAndAwardPeriodsController.Keys.AVERAGE_TENDER_DAYS); Assert.assertEquals(2014, year); Assert.assertEquals(15.0, averageTenderDays, 0); final DBObject second = averageTenderPeriod.get(1); year = (int) second.get(AverageTenderAndAwardPeriodsController.Keys.YEAR); averageTenderDays = (double) second.get(AverageTenderAndAwardPeriodsController.Keys.AVERAGE_TENDER_DAYS); Assert.assertEquals(2015, year); Assert.assertEquals(46.0, averageTenderDays, 0); }