Search in sources :

Example 1 with CustomProjectionOperation

use of org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation in project ocvn by devgateway.

the class AverageTenderAndAwardPeriodsController method averageTenderPeriod.

@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;
}
Also used : Aggregation.newAggregation(org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation) Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) BasicDBObject(com.mongodb.BasicDBObject) CustomProjectionOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with CustomProjectionOperation

use of org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation in project ocvn by devgateway.

the class AverageTenderAndAwardPeriodsController method qualityAverageTenderPeriod.

@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<DBObject> 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(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE_REF, null)), new BasicDBObject("$gt", Arrays.asList(MongoConstants.FieldNames.TENDER_PERIOD_END_DATE_REF, null)))), 1, 0)));
    DBObject project1 = new BasicDBObject();
    project1.put(Fields.UNDERSCORE_ID, 0);
    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));
    AggregationResults<DBObject> results = mongoTemplate.aggregate(agg, "release", DBObject.class);
    List<DBObject> list = results.getMappedResults();
    return list;
}
Also used : Aggregation.newAggregation(org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation) Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) BasicDBObject(com.mongodb.BasicDBObject) CustomProjectionOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with CustomProjectionOperation

use of org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation in project ocvn by devgateway.

the class AbstractFlagCrosstabController method flagStats.

protected List<DBObject> flagStats(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
    DBObject projectPrepare = getProjectPrepare(filter);
    DBObject group = getGroup(filter);
    DBObject projectPercentage = getProjectPercentage(filter);
    Aggregation agg = newAggregation(match(getYearDefaultFilterCriteria(filter, getYearProperty()).and(getYearProperty()).exists(true).and(getFlagProperty()).is(true)), new CustomProjectionOperation(projectPrepare), new CustomGroupingOperation(group), new CustomProjectionOperation(projectPercentage));
    AggregationResults<DBObject> results = mongoTemplate.aggregate(agg, "release", DBObject.class);
    List<DBObject> list = results.getMappedResults();
    return list;
}
Also used : Aggregation.newAggregation(org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation) Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) CustomGroupingOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomGroupingOperation) CustomProjectionOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject)

Example 4 with CustomProjectionOperation

use of org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation in project ocvn by devgateway.

the class CorruptionRiskDashboardIndicatorsStatsController method totalIndicatorsByIndicatorTypeByYear.

private List<DBObject> totalIndicatorsByIndicatorTypeByYear(String statsProperty, final YearFilterPagingRequest filter) {
    DBObject project1 = new BasicDBObject();
    addYearlyMonthlyProjection(filter, project1, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE_REF);
    project1.put("stats", "$flags." + statsProperty);
    project1.put(Fields.UNDERSCORE_ID, 0);
    Aggregation agg = newAggregation(match(where(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).exists(true).and("flags." + statsProperty + ".0").exists(true).andOperator(getYearDefaultFilterCriteria(filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))), unwind("flags." + statsProperty), new CustomProjectionOperation(project1), group(getYearlyMonthlyGroupingFields(filter, "stats.type")).sum("stats.count").as(Keys.INDICATOR_COUNT), getSortByYearMonthWhenOtherGroups(filter, "_id.type"));
    AggregationResults<DBObject> results = mongoTemplate.aggregate(agg, "release", DBObject.class);
    List<DBObject> list = results.getMappedResults();
    return list;
}
Also used : Aggregation.newAggregation(org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation) Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) BasicDBObject(com.mongodb.BasicDBObject) CustomProjectionOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Example 5 with CustomProjectionOperation

use of org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation in project ocvn by devgateway.

the class CostEffectivenessVisualsController method costEffectivenessAwardAmount.

@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<DBObject> costEffectivenessAwardAmount(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
    DBObject project = new BasicDBObject();
    addYearlyMonthlyProjection(filter, project, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE_REF);
    project.put("awards.value.amount", 1);
    project.put("totalAwardsWithTender", new BasicDBObject("$cond", Arrays.asList(new BasicDBObject("$gt", Arrays.asList(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE_REF, null)), 1, 0)));
    project.put("awardsWithTenderValue", new BasicDBObject("$cond", Arrays.asList(new BasicDBObject("$gt", Arrays.asList(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE_REF, null)), "$awards.value.amount", 0)));
    Aggregation agg = Aggregation.newAggregation(match(where("awards").elemMatch(where("status").is(Award.Status.active.toString())).and("awards.date").exists(true).and(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).exists(true)), getMatchDefaultFilterOperation(filter), unwind("$awards"), match(where("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()));
    AggregationResults<DBObject> results = mongoTemplate.aggregate(agg, "release", DBObject.class);
    List<DBObject> tagCount = results.getMappedResults();
    return tagCount;
}
Also used : Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) BasicDBObject(com.mongodb.BasicDBObject) CustomProjectionOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

BasicDBObject (com.mongodb.BasicDBObject)30 DBObject (com.mongodb.DBObject)30 CustomProjectionOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation)30 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)30 ApiOperation (io.swagger.annotations.ApiOperation)26 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)26 Aggregation.newAggregation (org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation)25 CustomGroupingOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomGroupingOperation)12 CustomUnwindOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomUnwindOperation)2 Date (java.util.Date)1 CustomSortingOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomSortingOperation)1 Criteria (org.springframework.data.mongodb.core.query.Criteria)1