class AverageTenderAndAwardPeriodsController extends GenericOCDSController {

    @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")
    public List<Document> qualityAverageTenderPeriod(@ModelAttribute @Valid final DefaultFilterPagingRequest filter) {

        DBObject project = new BasicDBObject();
        project.put(Fields.UNDERSCORE_ID, 0);
        project.put(
                "tenderWithStartEndDates",
                new BasicDBObject(
                        "$cond",
                        Arrays.asList(
                                new BasicDBObject("$and", Arrays.asList(
                                        new BasicDBObject(
                                                "$gt",
                                                Arrays.asList(
                                                        ref(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE),
                                                        null
                                                )
                                        ),
                                        new BasicDBObject(
                                                "$gt",
                                                Arrays.asList(
                                                        ref(MongoConstants.FieldNames.TENDER_PERIOD_END_DATE),
                                                        null
                                                )
                                        )
                                )),
                                1, 0
                        )
                )
        );

        DBObject project1 = new BasicDBObject();
        project1.put(Keys.TOTAL_TENDER_WITH_START_END_DATES, 1);
        project1.put(Keys.TOTAL_TENDERS, 1);
        project1.put(
                Keys.PERCENTAGE_TENDER_WITH_START_END_DATES,
                new BasicDBObject("$multiply", Arrays.asList(
                        new BasicDBObject("$divide", Arrays.asList("$totalTenderWithStartEndDates", "$totalTenders")),
                        100
                ))
        );

        Aggregation agg = newAggregation(
                match(getDefaultFilterCriteria(filter)),
                new CustomProjectionOperation(project),
                group().sum("tenderWithStartEndDates").as(Keys.TOTAL_TENDER_WITH_START_END_DATES).count().
                        as(Keys.TOTAL_TENDERS),
                new CustomProjectionOperation(project1)
        );

        return releaseAgg(agg);
    }

}