Search in sources :

Example 6 with CustomOperation

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

the class CountPlansTendersAwardsController method countTendersByYear.

/**
     * db.release.aggregate( [ {$match : { MongoConstants.FieldNames.TENDER_PERIOD_START_DATE: {
     * $exists: true } }}, {$project: { year: {$year :
     * MongoConstants.FieldNames.TENDER_PERIOD_START_DATE_REF} } }, {$group: {_id: "$year", count: {
     * $sum:1}}}, {$sort: { _id:1}} ])
     *
     * @return
     */
@ApiOperation(value = "Count the tenders and group the results by year. The year is calculated from " + "tender.tenderPeriod.startDate.")
@RequestMapping(value = "/api/countTendersByYear", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> countTendersByYear(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
    DBObject project = new BasicDBObject();
    addYearlyMonthlyProjection(filter, project, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE_REF);
    Aggregation agg = Aggregation.newAggregation(match(where(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).exists(true).andOperator(getYearDefaultFilterCriteria(filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))), new CustomOperation(new BasicDBObject("$project", project)), group(getYearlyMonthlyGroupingFields(filter)).count().as(Keys.COUNT), transformYearlyGrouping(filter).andInclude(Keys.COUNT), 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) CustomOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomOperation) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with CustomOperation

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

the class AverageTenderAndAwardPeriodsController method averageAwardPeriod.

@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")
public List<DBObject> averageAwardPeriod(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
    DBObject awardLengthDays = new BasicDBObject("$divide", Arrays.asList(new BasicDBObject("$subtract", Arrays.asList("$awards.date", MongoConstants.FieldNames.TENDER_PERIOD_END_DATE_REF)), MongoConstants.DAY_MS));
    DBObject project = new BasicDBObject();
    project.put(Fields.UNDERSCORE_ID, 0);
    addYearlyMonthlyProjection(filter, project, "$awards.date");
    project.put("awardLengthDays", awardLengthDays);
    project.put("awards.date", 1);
    project.put("awards.status", 1);
    project.put(MongoConstants.FieldNames.TENDER_PERIOD_END_DATE, 1);
    Aggregation agg = newAggregation(// unwind
    match(where(MongoConstants.FieldNames.TENDER_PERIOD_END_DATE).exists(true).and("awards.date").exists(true).and("awards.status").is("active")), unwind("$awards"), // we need to filter the awards again after unwind
    match(where("awards.date").exists(true).and("awards.status").is("active").andOperator(getYearDefaultFilterCriteria(filter, "awards.date"))), new CustomOperation(new BasicDBObject("$project", project)), group(getYearlyMonthlyGroupingFields(filter)).avg("$awardLengthDays").as(Keys.AVERAGE_AWARD_DAYS), transformYearlyGrouping(filter).andInclude(Keys.AVERAGE_AWARD_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) CustomOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomOperation) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with CustomOperation

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

the class ProcurementMethodSearchController method procurementMethods.

@ApiOperation(value = "Display the available procurement methods. " + "These are taken from tender.procurementMethod")
@RequestMapping(value = "/api/ocds/procurementMethod/all", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> procurementMethods() {
    DBObject project = new BasicDBObject("tender.procurementMethod", 1);
    Aggregation agg = newAggregation(new CustomOperation(new BasicDBObject("$project", project)), group("$tender.procurementMethod"));
    AggregationResults<DBObject> results = mongoTemplate.aggregate(agg, "release", DBObject.class);
    List<DBObject> mappedResults = results.getMappedResults();
    return mappedResults;
}
Also used : Aggregation.newAggregation(org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation) Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) BasicDBObject(com.mongodb.BasicDBObject) CustomOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomOperation) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with CustomOperation

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

the class TotalCancelledTendersByYearController method totalCancelledTendersByYear.

@ApiOperation(value = "Total Cancelled tenders by year. The tender amount is read from tender.value." + "The tender status has to be 'cancelled'. The year is retrieved from tender.tenderPeriod.startDate.")
@RequestMapping(value = "/api/totalCancelledTendersByYear", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> totalCancelledTendersByYear(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
    DBObject project = new BasicDBObject();
    addYearlyMonthlyProjection(filter, project, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE_REF);
    project.put("tender.value.amount", 1);
    Aggregation agg = newAggregation(match(where("tender.status").is("cancelled").and(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).exists(true).andOperator(getYearDefaultFilterCriteria(filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))), new CustomOperation(new BasicDBObject("$project", project)), getYearlyMonthlyGroupingOperation(filter).sum("$tender.value.amount").as(Keys.TOTAL_CANCELLED_TENDERS_AMOUNT), transformYearlyGrouping(filter).andInclude(Keys.TOTAL_CANCELLED_TENDERS_AMOUNT), getSortByYearMonth(filter));
    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) CustomOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomOperation) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

BasicDBObject (com.mongodb.BasicDBObject)9 DBObject (com.mongodb.DBObject)9 ApiOperation (io.swagger.annotations.ApiOperation)9 CustomOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomOperation)9 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)9 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)9 Aggregation.newAggregation (org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation)6